Compare commits

..

No commits in common. "a6f9eb561bf2847b6835817cfd415b3bed8b451d" and "94bbd618f5b15db453bcb475b7f11098e008380b" have entirely different histories.

3 changed files with 120 additions and 253 deletions

View File

@ -166,44 +166,6 @@ BSDialog5.Show({
</div> </div>
<div class="col-6"> <div class="col-6">
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Prompt Modal (Textbox)</b></p>
<p>Launch a prompt modal and wait for a textbox response</p>
<div class="alert alert-secondary text-sm">
<pre>
let response = await BSDialog5.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Label: "",
Placeholder: ""
});
alert(response);
</pre>
</div>
<p><button id="buttonL2a" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL2a").on('click', async function(){
let response = await BSDialog5.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Label: "",
Placeholder: ""
});
alert(response);
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom"> <div class="pb-3 mb-3 border-bottom">
<p><b>Example. Text Modal with Updates</b></p> <p><b>Example. Text Modal with Updates</b></p>
<p>Launch a basic modal, make updates to the title, body, size and footer.</p> <p>Launch a basic modal, make updates to the title, body, size and footer.</p>

View File

@ -1,6 +1,6 @@
/** /**
* BSDialog5 * BSDialog5
* @version v0.2.0.055 (2023/11/12 1428) * @version v0.2.0.013 (2023/08/21 1945)
*/ */
var BSDialog5 = { var BSDialog5 = {
Default: function() { Default: function() {
@ -12,27 +12,17 @@ var BSDialog5 = {
URL: null, URL: null,
Size: "md", Size: "md",
Colour: "secondary", Colour: "secondary",
ShowFooter: true, ShowFooter: true
EasyClose: true
}, },
PromptOptions: { PromptOptions: {
Type: "button",
Title: "", Title: "",
Message: "", Message: "",
Size: "md", Size: "md",
EasyClose: true,
Buttons: [ Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" }, { Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" }, { Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" } { Label: "Cancel", Value: "Cancel", Colour: "secondary" }
], ]
Textbox: {
Label: "",
LabelSize: 4,
Placeholder: "",
Value: "",
BoxSize: 8
}
}, },
UpdateOptions: { UpdateOptions: {
ID: null, ID: null,
@ -52,7 +42,7 @@ var BSDialog5 = {
a.pfx = "bsdia5_"; a.pfx = "bsdia5_";
a.addBackdrop(); a.addBackdrop();
a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.EasyClose, _options.Colour); a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour);
if (_options.URL == null) { if (_options.URL == null) {
await a.Update({ ID: _options.ID, Body: _options.Message }); await a.Update({ ID: _options.ID, Body: _options.Message });
@ -71,15 +61,52 @@ var BSDialog5 = {
Prompt: async function (options) { Prompt: async function (options) {
const a = this; const a = this;
const id = "prompt" + Math.floor(Math.random() * 10000) + 1000; const id = "prompt" + Math.floor(Math.random() * 10000) + 1000;
const _options = Object.assign(a.Default().PromptOptions, options); const _options = Object.assign(a.Default().PromptOptions, options);
switch (_options.Type) { return await new Promise(async (resolve) => {
case "textbox":
return await a.showTextboxPrompt(id, _options); await a.Show({
case "button": ID: id,
default: Title: _options.Title,
return await a.showButtonPrompt(id, _options); Message: _options.Message,
} Size: _options.Size
});
let html = '';
_options.Buttons.forEach(function(e) {
html += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
});
a.Update({ ID: id, Footer: html });
const modal = a.Find(id);
modal.Footer[0].querySelectorAll("button").forEach(function(button) {
button.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
const value = button.getAttribute("data-prompt-value");
a.Close(id);
resolve(value);
});
});
modal.Close.forEach(function(sender) {
sender.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
});
});
}, },
Clear: function () { Clear: function () {
this.GetBody().querySelectorAll(".modal").forEach(function(e) { this.GetBody().querySelectorAll(".modal").forEach(function(e) {
@ -94,7 +121,9 @@ var BSDialog5 = {
return; return;
} }
modal.Modal.parentNode.removeChild(modal.Modal); modal.Modal.forEach(function(e) {
e.parentNode.removeChild(e);
});
modal = this.Find(id); modal = this.Find(id);
if (modal !== null) { if (modal !== null) {
@ -112,9 +141,17 @@ var BSDialog5 = {
return; return;
} }
if (!this.isNullOrWhitespace(_options.Title)) modal.Title.innerHTML = _options.Title; if (!this.isNullOrWhitespace(_options.Title)) {
modal.Title.forEach(function(e) {
e.innerHTML = _options.Title;
});
}
if (!this.isNullOrWhitespace(_options.Body)) a.html(modal.Body, _options.Body); if (!this.isNullOrWhitespace(_options.Body)) {
modal.Body.forEach(function(e) {
a.html(e, _options.Body);
});
}
if (!this.isNullOrWhitespace(_options.BodyURL)) { if (!this.isNullOrWhitespace(_options.BodyURL)) {
if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) { if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) {
@ -128,14 +165,20 @@ var BSDialog5 = {
await a.UpdateBodyRemote(_options.ID, _options.BodyURL); await a.UpdateBodyRemote(_options.ID, _options.BodyURL);
} }
if (!this.isNullOrWhitespace(_options.Footer)) a.html(modal.Footer, _options.Footer); if (!this.isNullOrWhitespace(_options.Footer)) {
modal.Footer.forEach(function(e) {
a.html(e, _options.Footer);
});
}
if (!this.isNullOrWhitespace(_options.Size)) { if (!this.isNullOrWhitespace(_options.Size)) {
modal.Modal.classList.remove("modal-sm"); modal.Modal.forEach(function(e) {
modal.Modal.classList.remove("modal-md"); e.classList.remove("modal-sm");
modal.Modal.classList.remove("modal-lg"); e.classList.remove("modal-md");
modal.Modal.classList.remove("modal-xl"); e.classList.remove("modal-lg");
modal.Modal.classList.add("modal-" + _options.Size); e.classList.remove("modal-xl");
e.classList.add("modal-" + _options.Size);
});
} }
}, },
@ -169,12 +212,11 @@ var BSDialog5 = {
} }
return { return {
Title: modal[0].querySelectorAll(".modal-title")[0], Title: modal[0].querySelectorAll(".modal-title"),
Header: modal[0].querySelectorAll(".modal-header")[0], Body: modal[0].querySelectorAll(".modal-body"),
Body: modal[0].querySelectorAll(".modal-body")[0], Footer: modal[0].querySelectorAll(".modal-footer"),
Footer: modal[0].querySelectorAll(".modal-footer")[0],
Close: modal[0].querySelectorAll("[data-bs-dismiss='modal']"), Close: modal[0].querySelectorAll("[data-bs-dismiss='modal']"),
Modal: modal[0] Modal: modal
}; };
}, },
GetBody: function() { GetBody: function() {
@ -193,8 +235,17 @@ var BSDialog5 = {
// lock background // lock background
a.GetBody().classList.add("modal-open"); a.GetBody().classList.add("modal-open");
a.GetBody().style.overflow = "hidden"; a.GetBody().style.overflow = "hidden";
// close
let backdrop = a.GetBody().querySelectorAll(".modal-backdrop")[0];
backdrop.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Clear();
});
}, },
addModal: function (id, title, size, showFooter, easyClose, closeColour) { addModal: function (id, title, size, showFooter, closeColour) {
const a = this; const a = this;
// don't allow duplicates // don't allow duplicates
@ -208,8 +259,8 @@ var BSDialog5 = {
html += ' <div class="modal-dialog">'; html += ' <div class="modal-dialog">';
html += ' <div class="modal-content">'; html += ' <div class="modal-content">';
html += ' <div class="modal-header">'; html += ' <div class="modal-header">';
html += ' <span class="modal-title fs-5" style="cursor: default; user-select: none;">' + title + '</span>'; html += ' <span class="modal-title fs-5">' + title + '</span>';
html += ' <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" style="user-select: none;"></button>'; html += ' <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>';
html += ' </div>'; html += ' </div>';
html += ' <div class="modal-body">'; html += ' <div class="modal-body">';
@ -240,175 +291,32 @@ var BSDialog5 = {
return; return;
} }
// Close on backdrp // modal.addEventListener("click", function(e){
if (easyClose === true) { // e.stopPropagation();
modal.Modal.addEventListener("click", function(e){ // e.preventDefault();
e.stopPropagation();
e.preventDefault();
a.Close(id); // a.Close(id);
}); // });
const modalContent = modal.Modal.querySelectorAll(".modal-content")[0];
modalContent.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
});
}
modal.Close.forEach(function(e){ modal.Close.forEach(function(e){
e.addEventListener("click", function(e){ e.addEventListener("click", function(e){
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
a.Close(id); a.Close(id);
}); });
});
modal.Title.addEventListener("dblclick", function(e){ e.addEventListener("auxclick", function(e){
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
a.toggleSize(); if (e.button === 1) {
}); a.toggleSize();
},
showButtonPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
await a.Show({
ID: id,
Title: options.Title,
Message: options.Message,
Size: options.Size,
EasyClose: options.EasyClose
});
let html = '';
options.Buttons.forEach(function(e) {
html += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
});
a.Update({ ID: id, Footer: html });
const modal = a.Find(id);
modal.Footer.querySelectorAll("button").forEach(function(button) {
button.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
const value = button.getAttribute("data-prompt-value");
a.Close(id);
resolve(value);
});
});
modal.Close.forEach(function(sender) {
sender.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
});
});
},
showTextboxPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
options.Buttons = [
{ Label: "OK", Value: "", Colour: "primary" },
{ Label: "Cancel", Value: "", Colour: "secondary" }
];
await a.Show({
ID: id,
Title: options.Title,
Message: options.Message,
Size: options.Size,
EasyClose: options.EasyClose
});
let body = '';
if (!a.isNullOrWhitespace(options.Message)) {
body += '<p>' + options.Message + '</p>';
} }
});
body += '<div class="form-group row">';
if (a.isNullOrWhitespace(options.Textbox.Label) || (options.Textbox.LabelSize <= 0)) {
body += '<div class="col-sm-12">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>';
} else {
body += '<label for="textbox' + id + '" class="col-sm-' + options.Textbox.LabelSize + ' col-form-label">' + options.Textbox.Label + '</label>';
body += '<div class="col-sm-' + options.Textbox.BoxSize + '">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>';
}
body += '</div>';
let footer = '';
options.Buttons.forEach(function(e) {
footer += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
});
a.Update({
ID: id,
Body: body,
Footer: footer
});
const modal = a.Find(id);
const buttons = modal.Footer.querySelectorAll("button");
buttons[0].addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
const value = modal.Body.querySelectorAll("input")[0].value;
a.Close(id);
resolve(value);
});
buttons[1].addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
modal.Close.forEach(function(sender) {
sender.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
});
}); });
}, },
appendHtml: function (el, html) { appendHtml: function (el, html) {
let node = document.createElement('template'); let node = document.createElement('template');
node.innerHTML = html; node.innerHTML = html;
@ -466,28 +374,25 @@ var BSDialog5 = {
return; return;
} }
if (modal.Modal.classList.contains('modal-sm')) { let modalDialog = modal.Modal[0];
modal.Modal.classList.remove("modal-sm"); if (modalDialog.classList.contains('modal-sm')) {
modal.Modal.classList.add("modal-md"); modalDialog.classList.remove("modal-sm");
} else if (modal.Modal.classList.contains('modal-md')) { modalDialog.classList.add("modal-md");
modal.Modal.classList.remove("modal-md"); } else if (modalDialog.classList.contains('modal-md')) {
modal.Modal.classList.add("modal-lg"); modalDialog.classList.remove("modal-md");
} else if (modal.Modal.classList.contains('modal-lg')) { modalDialog.classList.add("modal-lg");
modal.Modal.classList.remove("modal-lg"); } else if (modalDialog.classList.contains('modal-lg')) {
modal.Modal.classList.add("modal-xl"); modalDialog.classList.remove("modal-lg");
} else if (modal.Modal.classList.contains('modal-xl')) { modalDialog.classList.add("modal-xl");
modal.Modal.classList.remove("modal-xl"); } else if (modalDialog.classList.contains('modal-xl')) {
modal.Modal.classList.add("modal-xxl"); modalDialog.classList.remove("modal-xl");
} else if (modal.Modal.classList.contains('modal-xxl')) { modalDialog.classList.add("modal-sm");
modal.Modal.classList.remove("modal-xxl");
modal.Modal.classList.add("modal-sm");
} else { } else {
modal.Modal.classList.remove("modal-sm"); modalDialog.classList.remove("modal-sm");
modal.Modal.classList.remove("modal-md"); modalDialog.classList.remove("modal-md");
modal.Modal.classList.remove("modal-lg"); modalDialog.classList.remove("modal-lg");
modal.Modal.classList.remove("modal-xl"); modalDialog.classList.remove("modal-xl");
modal.Modal.classList.remove("modal-xxl"); modalDialog.classList.add("modal-md");
modal.Modal.classList.add("modal-md");
} }
} }
}; };

4
bsdialog5.min.js vendored

File diff suppressed because one or more lines are too long