From c366a2a9d20467caf8063d28916be1b688b06313 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 12 Nov 2023 14:51:35 +0000 Subject: [PATCH 1/2] Added support for easy close Added support for textbox prompts --- bs5-test.html | 38 ++++++ bsdialog5.js | 331 ++++++++++++++++++++++++++++++----------------- bsdialog5.min.js | 5 +- 3 files changed, 253 insertions(+), 121 deletions(-) diff --git a/bs5-test.html b/bs5-test.html index c84d425..a1f9721 100644 --- a/bs5-test.html +++ b/bs5-test.html @@ -166,6 +166,44 @@ BSDialog5.Show({
+
+

Example. Prompt Modal (Textbox)

+

Launch a prompt modal and wait for a textbox response

+
+
+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);
+            
+
+

+ +
+

Example. Text Modal with Updates

Launch a basic modal, make updates to the title, body, size and footer.

diff --git a/bsdialog5.js b/bsdialog5.js index 0f7ccd4..15750f9 100644 --- a/bsdialog5.js +++ b/bsdialog5.js @@ -1,6 +1,6 @@ /** * BSDialog5 - * @version v0.2.0.013 (2023/08/21 1945) + * @version v0.2.0.055 (2023/11/12 1428) */ var BSDialog5 = { Default: function() { @@ -12,17 +12,27 @@ var BSDialog5 = { URL: null, Size: "md", Colour: "secondary", - ShowFooter: true + ShowFooter: true, + EasyClose: true }, PromptOptions: { + Type: "button", Title: "", Message: "", Size: "md", + EasyClose: true, Buttons: [ { Label: "Yes", Value: "Yes", Colour: "primary" }, { Label: "No", Value: "No", Colour: "secondary" }, { Label: "Cancel", Value: "Cancel", Colour: "secondary" } - ] + ], + Textbox: { + Label: "", + LabelSize: 4, + Placeholder: "", + Value: "", + BoxSize: 8 + } }, UpdateOptions: { ID: null, @@ -42,7 +52,7 @@ var BSDialog5 = { a.pfx = "bsdia5_"; a.addBackdrop(); - a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour); + a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.EasyClose, _options.Colour); if (_options.URL == null) { await a.Update({ ID: _options.ID, Body: _options.Message }); @@ -61,52 +71,15 @@ var BSDialog5 = { Prompt: async function (options) { const a = this; const id = "prompt" + Math.floor(Math.random() * 10000) + 1000; - const _options = Object.assign(a.Default().PromptOptions, options); - return await new Promise(async (resolve) => { - - await a.Show({ - ID: id, - Title: _options.Title, - Message: _options.Message, - Size: _options.Size - }); - - let html = ''; - _options.Buttons.forEach(function(e) { - html += ''; - }); - - 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(""); - }); - }); - - }); + switch (_options.Type) { + case "textbox": + return await a.showTextboxPrompt(id, _options); + case "button": + default: + return await a.showButtonPrompt(id, _options); + } }, Clear: function () { this.GetBody().querySelectorAll(".modal").forEach(function(e) { @@ -121,9 +94,7 @@ var BSDialog5 = { return; } - modal.Modal.forEach(function(e) { - e.parentNode.removeChild(e); - }); + modal.Modal.parentNode.removeChild(modal.Modal); modal = this.Find(id); if (modal !== null) { @@ -141,17 +112,9 @@ var BSDialog5 = { return; } - if (!this.isNullOrWhitespace(_options.Title)) { - modal.Title.forEach(function(e) { - e.innerHTML = _options.Title; - }); - } + if (!this.isNullOrWhitespace(_options.Title)) modal.Title.innerHTML = _options.Title; - if (!this.isNullOrWhitespace(_options.Body)) { - modal.Body.forEach(function(e) { - a.html(e, _options.Body); - }); - } + if (!this.isNullOrWhitespace(_options.Body)) a.html(modal.Body, _options.Body); if (!this.isNullOrWhitespace(_options.BodyURL)) { if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) { @@ -165,20 +128,14 @@ var BSDialog5 = { await a.UpdateBodyRemote(_options.ID, _options.BodyURL); } - if (!this.isNullOrWhitespace(_options.Footer)) { - modal.Footer.forEach(function(e) { - a.html(e, _options.Footer); - }); - } + if (!this.isNullOrWhitespace(_options.Footer)) a.html(modal.Footer, _options.Footer); if (!this.isNullOrWhitespace(_options.Size)) { - modal.Modal.forEach(function(e) { - e.classList.remove("modal-sm"); - e.classList.remove("modal-md"); - e.classList.remove("modal-lg"); - e.classList.remove("modal-xl"); - e.classList.add("modal-" + _options.Size); - }); + modal.Modal.classList.remove("modal-sm"); + modal.Modal.classList.remove("modal-md"); + modal.Modal.classList.remove("modal-lg"); + modal.Modal.classList.remove("modal-xl"); + modal.Modal.classList.add("modal-" + _options.Size); } }, @@ -212,11 +169,12 @@ var BSDialog5 = { } return { - Title: modal[0].querySelectorAll(".modal-title"), - Body: modal[0].querySelectorAll(".modal-body"), - Footer: modal[0].querySelectorAll(".modal-footer"), + Title: modal[0].querySelectorAll(".modal-title")[0], + Header: modal[0].querySelectorAll(".modal-header")[0], + Body: modal[0].querySelectorAll(".modal-body")[0], + Footer: modal[0].querySelectorAll(".modal-footer")[0], Close: modal[0].querySelectorAll("[data-bs-dismiss='modal']"), - Modal: modal + Modal: modal[0] }; }, GetBody: function() { @@ -235,17 +193,8 @@ var BSDialog5 = { // lock background a.GetBody().classList.add("modal-open"); 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, closeColour) { + addModal: function (id, title, size, showFooter, easyClose, closeColour) { const a = this; // don't allow duplicates @@ -259,8 +208,8 @@ var BSDialog5 = { html += '