diff --git a/bs5-test.html b/bs5-test.html index a1f9721..26a999e 100644 --- a/bs5-test.html +++ b/bs5-test.html @@ -29,93 +29,133 @@
-
-
+
+
+

Example. Simple Text Modal

+

Launch a modal with a text body.

-
-

Example. Simple Text Modal

-

Launch an empty modal

-
-
-BSDialog5.Show({
+        

+ + + +
+
+ +
+
BSDialog5.Show({
   ID: "modalL1",
   Title: "Modal Title",
   Message: "Hello Momo!",
   URL: null,
   Size: "md",
   Colour: "secondary"
-});
-            
-
-

- +});
+
+
-
-

Example. Prompt Modal

-

Launch a prompt modal and wait for a response

-
-
-let response = await BSDialog5.Prompt({
-  Title: "Modal Title",
-  Message: "Are you sure want to wait for a response?",
-  Size: "md",
-  Buttons: [
-    { Label: "Yes", Value: "Yes", Colour: "primary" },
-    { Label: "No", Value: "No", Colour: "secondary" },
-    { Label: "Cancel", Value: "Cancel", Colour: "secondary" }
-  ]
-});
+    
+
-alert(response); -
-
-

- + + }); + +
+
+ +
+
BSDialog5.Update({
+  ID: "modalL1",
+  Title: "Modal Changed Title",
+  Body: "Hello momo again!",
+  URL: null,
+  Size: "lg",
+  Footer: null
+});
+
+
-
-

Example. Multiple Modals

-

Launch multiple modals

-
-
-BSDialog5.Show({
+    
+
+ +

Example. Multiple Modals (Stack)

+

Launch multiple modals, stacked on top of each other.

+ +

+ + +
+
+
+
BSDialog5.Show({
   ID: "modalL3a",
   Title: "Modal A Title",
   Message: "First!",
@@ -131,47 +171,86 @@ BSDialog5.Show({
   URL: null,
   Size: "md",
   Colour: "secondary"
-});
-            
-
-

- -
- + }); +
-
+
+
+
let response = await BSDialog5.Prompt({
+  Title: "Modal Title",
+  Message: "Are you sure want to wait for a response?",
+  Size: "md",
+  Buttons: [
+    { Label: "Yes", Value: "Yes", Colour: "primary" },
+    { Label: "No", Value: "No", Colour: "secondary" },
+    { Label: "Cancel", Value: "Cancel", Colour: "secondary" }
+  ]
+});
 
-        
-

Example. Prompt Modal (Textbox)

-

Launch a prompt modal and wait for a textbox response

-
-
-let response = await BSDialog5.Prompt({
+alert(response);
+
+
+
+ +
+
+ +

Example. Text Prompt (Modal)

+

Launch a modal with a text prompt. Modal waits for a response from the textbox.

+ +

+ +
+
+
+
let response = await BSDialog5.Prompt({
   Type: "textbox",
   Title: "Modal Title",
   Message: "Are you sure want to wait for a response?",
@@ -180,125 +259,56 @@ let response = await BSDialog5.Prompt({
   Placeholder: ""
 });
 
-alert(response);
-            
-
-

- +alert(response);
+
+
-
-

Example. Text Modal with Updates

-

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

-
-
-BSDialog5.Show({
-  ID: "modalR1",
-  Title: "Modal Title",
-  Message: "Hello Momo!",
-  URL: null,
-  Size: "md",
-  Colour: "secondary"
-});
+    
+
-BSDialog5.Update({ - ID: "modalR1", - Title: "Modal Changed Title", - Body: "Hello momo again!", - BodyURL: null, - Size: "lg", - Footer: null -}); -
-
-

- + + }); + + +
+
+
+
BSDialog5.Close("modalR1");
+
+
+
+
-
-

Example. Close Modal

-

Close a modal using its identifier

-
-
-BSDialog5.Close("modalR1");
-            
-
-

- + + }); + + +
+
+
+
BSDialog5.Clear();
- - -
-

Example. Clear Modal

-

Close all modals

-
-
-BSDialog5.Clear();
-            
-
-

- -
- -
diff --git a/bsdialog5.js b/bsdialog5.js index b639458..c03d5af 100644 --- a/bsdialog5.js +++ b/bsdialog5.js @@ -1,11 +1,16 @@ /** * BSDialog5 - * @version v0.2.1.005 (2023/11/14 2028) + * @version v0.2.1.068 (2023/11/14 2028) */ -var BSDialog5 = { - Default: function() { +class BSDialog05 { + + constructor() { + } + + + get Options() { return { - ShowOptions: { + ShowModal: { ID: null, Title: "", Message: "", @@ -15,7 +20,7 @@ var BSDialog5 = { ShowFooter: true, EasyClose: true }, - PromptOptions: { + ShowPrompt: { Type: "button", Title: "", Message: "", @@ -34,115 +39,130 @@ var BSDialog5 = { BoxSize: 8 } }, - UpdateOptions: { + UpdateModal: { ID: null, Title: null, Body: null, - BodyURL: null, + URL: null, Footer: null, Size: null } }; - }, - Show: async function (options) { + } + + get #prefix() { + return "bsdia5_"; + } + + get #body() { + return document.getElementsByTagName("body")[0]; + } + + get #sizeList() { + return [ + "modal-sm", + "modal-md", + "modal-lg", + "modal-xl", + "modal-xxl" + ]; + } + + + async Show(options) { const a = this; - const _options = Object.assign(a.Default().ShowOptions, options); + const _options = Object.assign(a.Options.ShowModal, options); - a.id = _options.ID; - a.pfx = "bsdia5_"; + a.#addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.Colour); - a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.Colour); - - (new bootstrap.Modal(document.getElementById(a.pfx + _options.ID), { + (new bootstrap.Modal(document.getElementById(a.#prefix + _options.ID), { backdrop: _options.EasyClose })).show(); - if (_options.URL == null) { - await a.Update({ ID: _options.ID, Body: _options.Message }); + if (a.#isURL(_options.URL)) { + await a.UpdateBodyRemote(_options.ID, _options.URL); } else { - if (a.isNullOrWhitespace(_options.URL)) { - await a.Update({ ID: _options.ID, Body: URL }); - } else { - if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) { - await a.UpdateBodyRemote(_options.ID, _options.URL); - } else { - await a.Update({ ID: _options.ID, Body: URL }); - } - } + await a.Update({ ID: _options.ID, Body: _options.Message }); } - }, - Prompt: async function (options) { + } + + async Prompt(options) { const a = this; const id = "prompt" + Math.floor(Math.random() * 10000) + 1000; - const _options = Object.assign(a.Default().PromptOptions, options); + const _options = Object.assign(a.Options.ShowPrompt, options); switch (_options.Type) { case "textbox": - return await a.showTextboxPrompt(id, _options); + return await a.#showTextboxPrompt(id, _options); case "button": default: - return await a.showButtonPrompt(id, _options); + return await a.#showButtonPrompt(id, _options); } - }, - Clear: function () { - this.GetBody().querySelectorAll(".modal").forEach(function(e) { - e.parentNode.removeChild(e); - }); + } - this.removeBackdrop(); - }, - Close: function (id) { - let modal = this.Find(id); - if (modal === null) { - return; - } - - modal.Modal.parentNode.removeChild(modal.Modal); - - modal = this.Find(id); - if (modal !== null) { - return; - } - - this.removeBackdrop(); - }, - Update: async function (options) { + async Clear() { const a = this; - let _options = Object.assign(a.Default().UpdateOptions, options); + + document.querySelectorAll('.modal').forEach(function(e) { + const modal = bootstrap.Modal.getInstance(e); + + if (modal) { + modal.hide(); + } + }); + } + + async Close(id) { + const a = this; + + if (id.startsWith(a.#prefix)) { + id = id.substr(a.#prefix.length); + } + + const node = document.getElementById(a.#prefix + id); + const modal = bootstrap.Modal.getInstance(node); + + if (modal){ + modal.hide(); + } + + if (node) { + node.parentNode.removeChild(node); + } + } + + async Update(options) { + const a = this; + let _options = Object.assign(a.Options.UpdateModal, options); const modal = a.Find(_options.ID); if (modal === null) { return; } - if (!this.isNullOrWhitespace(_options.Title)) modal.Title.innerHTML = _options.Title; + if (!this.#isNullOrWhitespace(_options.Title)) { + modal.Title.innerHTML = _options.Title; + } - 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("/")) { - // ok - } else { - _options.BodyURL = null; + if (!this.#isNullOrWhitespace(_options.Body)) { + a.#html(modal.Body, _options.Body); + } else { + if (this.#isURL(_options.URL)) { + await a.UpdateBodyRemote(_options.ID, _options.URL); } } - if (!this.isNullOrWhitespace(_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)) a.html(modal.Footer, _options.Footer); - - if (!this.isNullOrWhitespace(_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); + if (!this.#isNullOrWhitespace(_options.Size)) { + a.#setSize(_options.ID, "modal-" + _options.Size); } - }, - UpdateBodyRemote: async function (id, url) { + } + + async UpdateBodyRemote(id, url) { const a = this; if (!a.Exists(id)) { @@ -157,12 +177,16 @@ var BSDialog5 = { }).catch((error) => { a.Update({ ID: id, Body: "Error: " + error }); }); - }, - Exists: function (id) { + } + + Exists(id) { return (this.Find(id) !== null); - }, - Find: function (id) { - const modal = this.GetBody().querySelectorAll("#" + this.pfx + id + ".modal"); + } + + Find(id) { + const a = this; + + const modal = a.#body.querySelectorAll("#" + a.#prefix + id + ".modal"); if (!modal) { return null; } @@ -179,11 +203,10 @@ var BSDialog5 = { Close: modal[0].querySelectorAll("[data-bs-dismiss='modal']"), Modal: modal[0] }; - }, - GetBody: function() { - return document.getElementsByTagName("body")[0]; - }, - addModal: function (id, title, size, showFooter, closeColour) { + } + + + #addModal(id, title, size, showFooter, closeColour) { const a = this; // don't allow duplicates @@ -193,7 +216,7 @@ var BSDialog5 = { } let html = ""; - html += '