diff --git a/bs4-test.html b/bs4-test.html index 0b6bf85..5f1e02e 100644 --- a/bs4-test.html +++ b/bs4-test.html @@ -16,89 +16,231 @@ + + + -
-
-
-

Example One

-

Launch an empty modal

-
BSDialog.Create('abc123', 'My Modal Box 123', null, 'xl');
-

+
+
-
-
-
-
+
+

Example. Simple Text Modal

+

Launch an empty modal

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

+
-

- -
-
-
-
- -

Example Four

-

Launch a multiple modals

-
- BSDialog.ShowToast('abc123', 'My Modal Box 123', 'Help! I\'m toast.', 'sm');
- BSDialog.ShowToast('abc123456', 'My Modal Box 123.567', 'Help! I\'m a second toast.', 'md');
-
-

- -
-
-
- - + $("#buttonL2").on('click', async function(){ + let response = await BSDialog.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); + }); + + }); + +
+ + +
+

Example. Multiple Modals

+

Launch multiple modals

+
+
+BSDialog.Show({
+  ID: "modalL3a",
+  Title: "Modal A Title",
+  Message: "First!",
+  URL: null,
+  Size: "md",
+  Colour: "secondary"
+});
+
+BSDialog.Show({
+  ID: "modalL3b",
+  Title: "Modal B Title",
+  Message: "Second!",
+  URL: null,
+  Size: "md",
+  Colour: "secondary"
+});
+            
+
+

+ +
+ + +
+
+ +
+

Example. Simple Text Modal Deprecated

+

Launch an empty modal

+
+
+BSDialog.Create('abc123', 'My Modal Box 123', 'Hello momo!', 'xl');
+            
+
+

+ +
+ +
+

Example. Empty Modal and Updates. Deprecated

+

Launch an empty modal then update its title, body, footer and size.

+
+
+BSDialog.Create('abc123', 'My Modal Box 123', null, 'sm');
+BSDialog.UpdateTitle('abc123', 'My Modal Box 567');
+BSDialog.UpdateBody('abc123', 'Help, I\'m toast!');
+BSDialog.UpdateFooter('abc123', '');
+BSDialog.UpdateSize('abc123', 'lg');
+            
+
+

+ +
+ + +
+

Example. Toast Modal. Deprecated

+

Show a toast-style modal

+
+
+BSDialog.ShowToast('abc123', 'My Modal Box 123', 'Help! I\'m toast.', 'sm');
+            
+
+

+ +
+ + +
+ + + \ No newline at end of file diff --git a/bsdialog4.js b/bsdialog4.js index 66d087c..bf78007 100644 --- a/bsdialog4.js +++ b/bsdialog4.js @@ -1,25 +1,17 @@ /** * BSDialog4 - * @version v0.1.1.026 (2023/08/19 12:58) + * @version v0.1.2.046 (2023/08/20 00:39) */ var BSDialog = { Create: async function (id, title, url, size) { - var a = this; - - a.id = id; - a.pfx = "bsdia4_"; - a.body = document.getElementsByTagName("body")[0]; - - a.addBackdrop(); - a.addModal(id, title, size); - - if (a.isNullOrWhitespace(url)) { - a.UpdateBody(id, ""); - } else if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")) { - a.UpdateBodyRemote(id, url); - } else { - await a.UpdateBody(id, url); - } + await this.Show({ + ID: id, + Title: title, + Message: url, + URL: url, + Size: size, + Colour: "secondary" + }); }, Clear: function () { this.body.querySelectorAll(".modal").forEach(function(e) { @@ -132,9 +124,122 @@ var BSDialog = { Modal: modal }; }, + Show: async function (options) { + const a = this; + const _options = Object.assign(a.DefaultShowOptions, options); + + a.id = _options.ID; + a.pfx = "bsdia4_"; + a.body = document.getElementsByTagName("body")[0]; + + a.addBackdrop(); + a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour); + + if (_options.URL == null) { + await a.UpdateBody(_options.ID, _options.Message); + } else { + if (a.isNullOrWhitespace(_options.URL)) { + await a.UpdateBody(_options.ID, _options.URL); + } else { + if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) { + await a.UpdateBodyRemote(_options.ID, _options.URL); + } else { + await a.UpdateBody(_options.ID, _options.URL); + } + } + } + }, + Toast: async function (options) { + const a = this; + const _options = Object.assign(a.DefaultToastOptions, options); + + await this.Show({ + ID: _options.ID, + Title: _options.Title, + Message: _options.Message, + URL: null, + Size: _options.Size, + Colour: "secondary", + ShowFooter: false + }); + }, + Prompt: async function (options) { + const a = this; + let id = "prompt" + Math.floor(Math.random() * 10000) + 1000; + + const _options = Object.assign(a.DefaultPromptOptions, options); + + return await new Promise((resolve, reject) => { + + a.Create(id, _options.Title, _options.Message, _options.Size); + + let html = ''; + _options.Buttons.forEach(function(e) { + html += ''; + }); + + a.UpdateFooter(id, 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(""); + }); + }); + + }); + }, + DefaultShowOptions: { + ID: null, + Title: "", + Message: "", + URL: null, + Size: "md", + Colour: "secondary", + ShowFooter: true + }, + DefaultToastOptions: { + ID: null, + Title: "", + Message: "", + Size: "md" + }, + DefaultPromptOptions: { + Title: "", + Message: "", + Size: "md", + Buttons: [ + { Label: "Yes", Value: "Yes", Colour: "primary" }, + { Label: "No", Value: "No", Colour: "secondary" }, + { Label: "Cancel", Value: "Cancel", Colour: "secondary" } + ] + }, ShowToast: function (id, title, message, size) { - this.Create(id, title, null, size); - this.UpdateBody(id, message); + this.Toast({ + ID: id, + Title: title, + Message: message, + Size: size + }); }, addBackdrop: function () { let a = this; @@ -159,7 +264,7 @@ var BSDialog = { a.Clear(); }); }, - addModal: function (id, title, size) { + addModal: function (id, title, size, showFooter, closeColour) { var a = this; // don't allow duplicates @@ -189,9 +294,13 @@ var BSDialog = { html += ''; html += ' '; - html += ' '; + + if (showFooter === true) { + html += ' '; + } + html += ' '; html += ' '; html += ''; diff --git a/bsdialog4.min.js b/bsdialog4.min.js index 649a0d5..2f8ab3c 100644 --- a/bsdialog4.min.js +++ b/bsdialog4.min.js @@ -1,5 +1,5 @@ /** * BSDialog4 - * @version v0.1.1.026 (2023/08/19 12:58) + * @version v0.1.2.046 (2023/08/20 00:39) */ -var BSDialog={Create:async function(t,l,o,e){var a=this;a.id=t,a.pfx="bsdia4_",a.body=document.getElementsByTagName("body")[0],a.addBackdrop(),a.addModal(t,l,e),a.isNullOrWhitespace(o)?a.UpdateBody(t,""):o.startsWith("http://")||o.startsWith("https://")||o.startsWith("/")?a.UpdateBodyRemote(t,o):await a.UpdateBody(t,o)},Clear:function(){this.body.querySelectorAll(".modal").forEach((function(t){t.parentNode.removeChild(t)})),this.removeBackdrop()},Close:function(t){let l=this.Find(t);null!==l&&(l.Modal.forEach((function(t){t.parentNode.removeChild(t)})),l=this.Find(t),null===l&&this.removeBackdrop())},UpdateTitle:function(t,l){let o=this.Find(t);null!==o&&o.Title.forEach((function(t){t.innerHTML=l}))},UpdateSize:function(t,l){let o=this.Find(t);null!==o&&o.Modal.forEach((function(t){let o=t.querySelectorAll(".modal-dialog")[0];o.classList.remove("modal-sm"),o.classList.remove("modal-md"),o.classList.remove("modal-lg"),o.classList.remove("modal-xl"),o.classList.add("modal-"+l)}))},UpdateBody:function(t,l){var o=this;let e=o.Find(t);null!==e&&e.Body.forEach((function(t){o.html(t,l)}))},UpdateBodyRemote:async function(t,l){var o=this;o.Exists(t)&&await fetch(l,{cache:"no-cache",credentials:"same-origin"}).then((t=>t.text())).then((l=>{o.UpdateBody(t,l)})).catch((l=>{o.UpdateBody(t,"Error: "+l)}))},UpdateFooter:function(t,l){var o=this;let e=o.Find(t);null!==e&&e.Footer.forEach((function(t){o.html(t,l)}))},Exists:function(t){return null!==this.Find(t)},Find:function(t){let l=this.body.querySelectorAll("#"+this.pfx+t+".modal");return l?l.length<=0?null:{Title:l[0].querySelectorAll(".modal-title"),Body:l[0].querySelectorAll(".modal-body"),Footer:l[0].querySelectorAll(".modal-footer"),Close:l[0].querySelectorAll("[data-dismiss='modal']"),Modal:l}:null},ShowToast:function(t,l,o,e){this.Create(t,l,null,e),this.UpdateBody(t,o)},addBackdrop:function(){let t=this;t.body.querySelectorAll(".modal-backdrop").length>0||(t.appendHtml(t.body,''),t.body.classList.add("modal-open"),t.body.style.overflow="hidden",t.body.querySelectorAll(".modal-backdrop")[0].addEventListener("click",(function(l){l.stopPropagation(),l.preventDefault(),t.Clear()})))},addModal:function(t,l,o){var e=this;let a=e.Find(t);if(null!==a)return;let d="";d+='",e.appendHtml(e.body,d),a=e.Find(t),null!==a&&a.Close.forEach((function(l){l.addEventListener("click",(function(l){l.stopPropagation(),l.preventDefault(),e.Close(t)})),l.addEventListener("auxclick",(function(t){t.stopPropagation(),t.preventDefault(),1===t.button&&e.toggleSize()}))}))},appendHtml:function(t,l){let o=document.createElement("template");o.innerHTML=l,o=o.content.firstChild,t.appendChild(o)},html:function(t,l){jQuery?jQuery(t).html(l):t.innerHTML=l},isNullOrWhitespace:function(t){return void 0===t||(null==t||(0==t||t.trim().length<=0))},removeBackdrop:function(){if(this.body.querySelectorAll(".modal-backdrop").length<=0)return;if(this.body.querySelectorAll(".modal").length>0)return;let t=this.body.querySelectorAll(".modal-backdrop")[0];t.parentNode.removeChild(t),this.body.classList.remove("modal-open"),this.body.style.overflow=null},toggleSize:function(){let t=this.Find(this.id);if(null===t)return;let l=t.Modal[0].querySelectorAll(".modal-dialog")[0];l.classList.contains("modal-sm")?(l.classList.remove("modal-sm"),l.classList.add("modal-md")):l.classList.contains("modal-md")?(l.classList.remove("modal-md"),l.classList.add("modal-lg")):l.classList.contains("modal-lg")?(l.classList.remove("modal-lg"),l.classList.add("modal-xl")):l.classList.contains("modal-xl")?(l.classList.remove("modal-xl"),l.classList.add("modal-sm")):(l.classList.remove("modal-sm"),l.classList.remove("modal-md"),l.classList.remove("modal-lg"),l.classList.remove("modal-xl"),l.classList.add("modal-md"))}}; \ No newline at end of file +var BSDialog={Create:async function(t,e,o,l){await this.Show({ID:t,Title:e,Message:o,URL:o,Size:l,Colour:"secondary"})},Clear:function(){this.body.querySelectorAll(".modal").forEach((function(t){t.parentNode.removeChild(t)})),this.removeBackdrop()},Close:function(t){let e=this.Find(t);null!==e&&(e.Modal.forEach((function(t){t.parentNode.removeChild(t)})),e=this.Find(t),null===e&&this.removeBackdrop())},UpdateTitle:function(t,e){let o=this.Find(t);null!==o&&o.Title.forEach((function(t){t.innerHTML=e}))},UpdateSize:function(t,e){let o=this.Find(t);null!==o&&o.Modal.forEach((function(t){let o=t.querySelectorAll(".modal-dialog")[0];o.classList.remove("modal-sm"),o.classList.remove("modal-md"),o.classList.remove("modal-lg"),o.classList.remove("modal-xl"),o.classList.add("modal-"+e)}))},UpdateBody:function(t,e){var o=this;let l=o.Find(t);null!==l&&l.Body.forEach((function(t){o.html(t,e)}))},UpdateBodyRemote:async function(t,e){var o=this;o.Exists(t)&&await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((t=>t.text())).then((e=>{o.UpdateBody(t,e)})).catch((e=>{o.UpdateBody(t,"Error: "+e)}))},UpdateFooter:function(t,e){var o=this;let l=o.Find(t);null!==l&&l.Footer.forEach((function(t){o.html(t,e)}))},Exists:function(t){return null!==this.Find(t)},Find:function(t){let e=this.body.querySelectorAll("#"+this.pfx+t+".modal");return e?e.length<=0?null:{Title:e[0].querySelectorAll(".modal-title"),Body:e[0].querySelectorAll(".modal-body"),Footer:e[0].querySelectorAll(".modal-footer"),Close:e[0].querySelectorAll("[data-dismiss='modal']"),Modal:e}:null},Show:async function(t){const e=this,o=Object.assign(e.DefaultShowOptions,t);e.id=o.ID,e.pfx="bsdia4_",e.body=document.getElementsByTagName("body")[0],e.addBackdrop(),e.addModal(o.ID,o.Title,o.Size,!0,o.Colour),null==o.URL?await e.UpdateBody(o.ID,o.Message):e.isNullOrWhitespace(o.URL)?await e.UpdateBody(o.ID,o.URL):o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")?await e.UpdateBodyRemote(o.ID,o.URL):await e.UpdateBody(o.ID,o.URL)},Toast:async function(t){const e=Object.assign(this.DefaultToastOptions,t);await this.Show({ID:e.ID,Title:e.Title,Message:e.Message,URL:null,Size:e.Size,Colour:"secondary",ShowFooter:!1})},Prompt:async function(t){const e=this;let o="prompt"+Math.floor(1e4*Math.random())+1e3;const l=Object.assign(e.DefaultPromptOptions,t);return await new Promise(((t,a)=>{e.Create(o,l.Title,l.Message,l.Size);let s="";l.Buttons.forEach((function(t){s+='"})),e.UpdateFooter(o,s);const i=e.Find(o);i.Footer[0].querySelectorAll("button").forEach((function(l){l.addEventListener("click",(function(a){a.stopPropagation(),a.preventDefault();const s=l.getAttribute("data-prompt-value");e.Close(o),t(s)}))})),i.Close.forEach((function(l){l.addEventListener("click",(function(l){l.stopPropagation(),l.preventDefault(),e.Close(o),t("")}))}))}))},DefaultShowOptions:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0},DefaultToastOptions:{ID:null,Title:"",Message:"",Size:"md"},DefaultPromptOptions:{Title:"",Message:"",Size:"md",Buttons:[{Label:"Yes",Value:"Yes",Colour:"primary"},{Label:"No",Value:"No",Colour:"secondary"},{Label:"Cancel",Value:"Cancel",Colour:"secondary"}]},ShowToast:function(t,e,o,l){this.Toast({ID:t,Title:e,Message:o,Size:l})},addBackdrop:function(){let t=this;t.body.querySelectorAll(".modal-backdrop").length>0||(t.appendHtml(t.body,''),t.body.classList.add("modal-open"),t.body.style.overflow="hidden",t.body.querySelectorAll(".modal-backdrop")[0].addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),t.Clear()})))},addModal:function(t,e,o,l,a){var s=this;let i=s.Find(t);if(null!==i)return;let n="";n+='",s.appendHtml(s.body,n),i=s.Find(t),null!==i&&i.Close.forEach((function(e){e.addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),s.Close(t)})),e.addEventListener("auxclick",(function(t){t.stopPropagation(),t.preventDefault(),1===t.button&&s.toggleSize()}))}))},appendHtml:function(t,e){let o=document.createElement("template");o.innerHTML=e,o=o.content.firstChild,t.appendChild(o)},html:function(t,e){jQuery?jQuery(t).html(e):t.innerHTML=e},isNullOrWhitespace:function(t){return void 0===t||(null==t||(0==t||t.trim().length<=0))},removeBackdrop:function(){if(this.body.querySelectorAll(".modal-backdrop").length<=0)return;if(this.body.querySelectorAll(".modal").length>0)return;let t=this.body.querySelectorAll(".modal-backdrop")[0];t.parentNode.removeChild(t),this.body.classList.remove("modal-open"),this.body.style.overflow=null},toggleSize:function(){let t=this.Find(this.id);if(null===t)return;let e=t.Modal[0].querySelectorAll(".modal-dialog")[0];e.classList.contains("modal-sm")?(e.classList.remove("modal-sm"),e.classList.add("modal-md")):e.classList.contains("modal-md")?(e.classList.remove("modal-md"),e.classList.add("modal-lg")):e.classList.contains("modal-lg")?(e.classList.remove("modal-lg"),e.classList.add("modal-xl")):e.classList.contains("modal-xl")?(e.classList.remove("modal-xl"),e.classList.add("modal-sm")):(e.classList.remove("modal-sm"),e.classList.remove("modal-md"),e.classList.remove("modal-lg"),e.classList.remove("modal-xl"),e.classList.add("modal-md"))}}; \ No newline at end of file