';
- a.appendHtml(a.body, html);
+ a.#appendHtml(a.#body, html);
modal = a.Find(id);
if (modal === null) {
return;
}
- $("#" + a.pfx + id).on('hidden.bs.modal', function (event) {
+ $("#" + a.#prefix + id).on('hidden.bs.modal', function (event) {
a.Close(id);
})
@@ -241,11 +263,12 @@ var BSDialog = {
e.stopPropagation();
e.preventDefault();
- a.toggleSize();
+ a.#toggleSize(id);
});
- },
- showButtonPrompt: async function (id, options) {
+ }
+
+ async #showButtonPrompt(id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
@@ -290,8 +313,9 @@ var BSDialog = {
});
});
- },
- showTextboxPrompt: async function (id, options) {
+ }
+
+ async #showTextboxPrompt(id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
@@ -310,13 +334,13 @@ var BSDialog = {
let body = '';
- if (!a.isNullOrWhitespace(options.Message)) {
+ if (!a.#isNullOrWhitespace(options.Message)) {
body += '
';
- if (a.isNullOrWhitespace(options.Textbox.Label) || (options.Textbox.LabelSize <= 0)) {
+ if (a.#isNullOrWhitespace(options.Textbox.Label) || (options.Textbox.LabelSize <= 0)) {
body += '
';
body += '';
body += '
';
@@ -375,24 +399,26 @@ var BSDialog = {
});
});
- },
- appendHtml: function (el, html) {
+ }
+
+ #appendHtml(el, html) {
let node = document.createElement('template');
node.innerHTML = html;
node = node.content.firstChild;
el.appendChild(node);
- },
- html: function (el, newHtml) {
- /// todo: replace with pure JS
+ }
+
+ #html(el, newHtml) {
if (jQuery) {
jQuery(el).html(newHtml);
} else {
el.innerHTML = newHtml;
}
- },
- isNullOrWhitespace: function(e) {
+ }
+
+ #isNullOrWhitespace(e) {
if (typeof (e) == "undefined") {
return true;
}
@@ -406,24 +432,29 @@ var BSDialog = {
}
return (e.trim().length <= 0);
- },
- removeBackdrop: function () {
- if (this.body.querySelectorAll(".modal-backdrop").length <= 0) {
+ }
+
+ #removeBackdrop() {
+ const a = this;
+
+ if (a.#body.querySelectorAll(".modal-backdrop").length <= 0) {
return;
}
- if (this.body.querySelectorAll(".modal").length > 0) {
+ if (a.#body.querySelectorAll(".modal").length > 0) {
return;
}
- let backdrop = this.body.querySelectorAll(".modal-backdrop")[0];
- backdrop.parentNode.removeChild(backdrop);
+ a.#body.querySelectorAll(".modal-backdrop").forEach(function(e){
+ e.parentNode.removeChild(e);
+ });
// unlock background
- this.body.classList.remove("modal-open");
- this.body.style.overflow = null;
- },
- retrieveURL: async function (url) {
+ a.#body.classList.remove("modal-open");
+ a.#body.style.overflow = null;
+ }
+
+ async #retrieveURL(url) {
return await new Promise(async (resolve) => {
await fetch(url, {
cache: 'no-cache',
@@ -434,28 +465,33 @@ var BSDialog = {
resolve("Error: " + error);
});
});
- },
- toggleSize: function () {
+ }
+
+ #toggleSize(id) {
const a = this;
- let modal = a.Find(a.id);
+ let modal = a.Find(id);
if (modal === null) {
return;
}
let modalDialog = modal.Modal.querySelectorAll(".modal-dialog")[0];
if (modalDialog.classList.contains('modal-sm')) {
- a.Update({ ID: a.id, Size: "md" });
+ a.Update({ ID: id, Size: "md" });
} else if (modalDialog.classList.contains('modal-md')) {
- a.Update({ ID: a.id, Size: "lg" });
+ a.Update({ ID: id, Size: "lg" });
} else if (modalDialog.classList.contains('modal-lg')) {
- a.Update({ ID: a.id, Size: "xl" });
+ a.Update({ ID: id, Size: "xl" });
} else if (modalDialog.classList.contains('modal-xl')) {
- a.Update({ ID: a.id, Size: "xxl" });
+ a.Update({ ID: id, Size: "xxl" });
} else if (modalDialog.classList.contains('modal-xxl')) {
- a.Update({ ID: a.id, Size: "sm" });
+ a.Update({ ID: id, Size: "sm" });
} else {
- a.Update({ ID: a.id, Size: "md" });
+ a.Update({ ID: id, Size: "md" });
}
}
-};
\ No newline at end of file
+
+}
+
+
+var BSDialog = new BSDialog4();
\ No newline at end of file
diff --git a/bsdialog4.min.js b/bsdialog4.min.js
index b33d478..535c550 100644
--- a/bsdialog4.min.js
+++ b/bsdialog4.min.js
@@ -1,5 +1,5 @@
/**
* BSDialog4
- * @version v0.1.5.006 (2023/11/14 17:40)
+ * @version v0.1.5.017 (2023/11/16 17:47)
*/
-var BSDialog={Show:async function(e){const t=this,o=Object.assign(t.Options.ShowModal,e);if(t.id=o.ID,t.pfx="bsdia4_",t.body=document.getElementsByTagName("body")[0],t.addModal(o.ID,o.Title,o.Size,o.ShowFooter,o.Colour),$("#"+t.pfx+o.ID).modal({backdrop:o.EasyClose,show:!0}),null==o.URL)await t.Update({ID:o.ID,Body:o.Message});else if(t.isNullOrWhitespace(o.URL))await t.Update({ID:o.ID,Body:o.URL});else if(o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")){let e=await t.retrieveURL(o.URL);await t.Update({ID:o.ID,Body:e})}else await t.Update({ID:o.ID,Body:o.URL})},Prompt:async function(e){const t=this;let o=Math.floor(1e4*Math.random())+1e3;const l=Object.assign(t.Options.ShowPrompt,e);return"textbox"===l.Type?await t.showTextboxPrompt(o,l):await t.showButtonPrompt(o,l)},Clear:function(){this.body.querySelectorAll(".modal").forEach((function(e){e.parentNode.removeChild(e)})),this.removeBackdrop()},Close:function(e){let t=this.Find(e);null!==t&&(t.Modal.parentNode.removeChild(t.Modal),t=this.Find(e),null===t&&this.removeBackdrop())},Update:async function(e){const t=this;let o=Object.assign(t.Options.UpdateModal,e);const l=t.Find(o.ID);if(null!==l){if(this.isNullOrWhitespace(o.Title)||(l.Title.innerHTML=o.Title),this.isNullOrWhitespace(o.Body)||t.html(l.Body,o.Body),this.isNullOrWhitespace(o.BodyURL)||o.BodyURL.startsWith("http://")||o.BodyURL.startsWith("https://")||o.BodyURL.startsWith("/")||(o.BodyURL=null),!this.isNullOrWhitespace(o.BodyURL)){let e=await t.retrieveURL(o.BodyURL);await t.Update({ID:o.ID,Body:e})}this.isNullOrWhitespace(o.Footer)||t.html(l.Footer,o.Footer),this.isNullOrWhitespace(o.Size)||l.Modal.querySelectorAll(".modal-dialog").forEach((function(e){e.classList.forEach((function(t){"modal-dialog"!=t&&t.startsWith("modal-")&&e.classList.remove(t)})),e.classList.add("modal-"+o.Size)}))}},Exists:function(e){return null!==this.Find(e)},Find:function(e){const t=this.body.querySelectorAll("#"+this.pfx+e+".modal");return t?t.length<=0?null:{Title:t[0].querySelectorAll(".modal-title")[0],Header:t[0].querySelectorAll(".modal-header")[0],Body:t[0].querySelectorAll(".modal-body")[0],Footer:t[0].querySelectorAll(".modal-footer")[0],Close:t[0].querySelectorAll("[data-dismiss='modal']"),Modal:t[0]}:null},Options:{ShowModal:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0,EasyClose:!0},ShowPrompt:{Type:"button",Title:"",Message:"",Size:"md",EasyClose:!0,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}},UpdateModal:{ID:null,Title:null,Body:null,BodyURL:null,Footer:null,Size:null}},addModal:function(e,t,o,l,a){const s=this;let i=s.Find(e);if(null!==i)return;let n="";n+='
',n+='
',n+='
',n+=' ",n+='
',n+='
',n+='
',n+='
',n+='\t Loading...',n+="
",n+="
",n+="
",n+="
",!0===l&&(n+=' "),n+="
",n+="
",n+="
",s.appendHtml(s.body,n),i=s.Find(e),null!==i&&($("#"+s.pfx+e).on("hidden.bs.modal",(function(t){s.Close(e)})),i.Title.addEventListener("dblclick",(function(e){e.stopPropagation(),e.preventDefault(),s.toggleSize()})))},showButtonPrompt:async function(e,t){const o=this;return await new Promise((async(l,a)=>{await o.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";t.Buttons.forEach((function(e){s+='
"})),o.Update({ID:e,Footer:s});const i=o.Find(e);i.Footer.querySelectorAll("button").forEach((function(t){t.addEventListener("click",(function(a){a.stopPropagation(),a.preventDefault();const s=t.getAttribute("data-prompt-value");o.Close(e),l(s)}))})),i.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")}))}))}))},showTextboxPrompt:async function(e,t){const o=this;return await new Promise((async(l,a)=>{t.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await o.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";o.isNullOrWhitespace(t.Message)||(s+="
"+t.Message+"
"),s+='
";let i="";t.Buttons.forEach((function(e){i+='
"})),o.Update({ID:e,Body:s,Footer:i});const n=o.Find(e),d=n.Footer.querySelectorAll("button");d[0].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault();const a=n.Body.querySelectorAll("input")[0].value;o.Close(e),l(a)})),d[1].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")})),n.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")}))}))}))},appendHtml:function(e,t){let o=document.createElement("template");o.innerHTML=t,o=o.content.firstChild,e.appendChild(o)},html:function(e,t){jQuery?jQuery(e).html(t):e.innerHTML=t},isNullOrWhitespace:function(e){return void 0===e||(null==e||(0==e||e.trim().length<=0))},removeBackdrop:function(){if(this.body.querySelectorAll(".modal-backdrop").length<=0)return;if(this.body.querySelectorAll(".modal").length>0)return;let e=this.body.querySelectorAll(".modal-backdrop")[0];e.parentNode.removeChild(e),this.body.classList.remove("modal-open"),this.body.style.overflow=null},retrieveURL:async function(e){return await new Promise((async t=>{await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((e=>e.text())).then((e=>{t(e)})).catch((e=>{t("Error: "+e)}))}))},toggleSize:function(){const e=this;let t=e.Find(e.id);if(null===t)return;let o=t.Modal.querySelectorAll(".modal-dialog")[0];o.classList.contains("modal-sm")?e.Update({ID:e.id,Size:"md"}):o.classList.contains("modal-md")?e.Update({ID:e.id,Size:"lg"}):o.classList.contains("modal-lg")?e.Update({ID:e.id,Size:"xl"}):o.classList.contains("modal-xl")?e.Update({ID:e.id,Size:"xxl"}):o.classList.contains("modal-xxl")?e.Update({ID:e.id,Size:"sm"}):e.Update({ID:e.id,Size:"md"})}};
\ No newline at end of file
+class BSDialog4{constructor(){}get Options(){return{ShowModal:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0,EasyClose:!0},ShowPrompt:{Type:"button",Title:"",Message:"",Size:"md",EasyClose:!0,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}},UpdateModal:{ID:null,Title:null,Body:null,BodyURL:null,Footer:null,Size:null}}}get#e(){return"bsdia4_"}get#t(){return document.getElementsByTagName("body")[0]}async Show(e){const t=this,o=Object.assign(t.Options.ShowModal,e);if(t.addModal(o.ID,o.Title,o.Size,o.ShowFooter,o.Colour),$("#"+t.#e+o.ID).modal({backdrop:o.EasyClose,show:!0}),t.#o(o.URL))await t.Update({ID:o.ID,Body:o.Message});else if(o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")){const e=await t.#l(o.URL);await t.Update({ID:o.ID,Body:e})}else await t.Update({ID:o.ID,Body:o.URL})}async Prompt(e){const t=this,o=Math.floor(1e4*Math.random())+1e3,l=Object.assign(t.Options.ShowPrompt,e);return"textbox"===l.Type?await t.#a(o,l):await t.#s(o,l)}Clear(){this.#t.querySelectorAll(".modal").forEach((function(e){e.parentNode.removeChild(e)})),this.#i()}Close(e){const t=this;let o=t.Find(e);null!==o&&(o.Modal.parentNode.removeChild(o.Modal),o=t.Find(e),null===o&&t.#i())}async Update(e){const t=this;let o=Object.assign(t.Options.UpdateModal,e);const l=t.Find(o.ID);if(null!==l){if(t.#o(o.Title)||(l.Title.innerHTML=o.Title),t.#o(o.Body)||t.#n(l.Body,o.Body),t.#o(o.BodyURL)||o.BodyURL.startsWith("http://")||o.BodyURL.startsWith("https://")||o.BodyURL.startsWith("/")||(o.BodyURL=null),!t.#o(o.BodyURL)){const e=await t.#l(o.BodyURL);await t.Update({ID:o.ID,Body:e})}t.#o(o.Footer)||t.#n(l.Footer,o.Footer),t.#o(o.Size)||l.Modal.querySelectorAll(".modal-dialog").forEach((function(e){e.classList.forEach((function(t){"modal-dialog"!=t&&t.startsWith("modal-")&&e.classList.remove(t)})),e.classList.add("modal-"+o.Size)}))}}Exists(e){return null!==this.Find(e)}Find(e){const t=this.#t.querySelectorAll("#"+this.#e+e+".modal");return t?t.length<=0?null:{Title:t[0].querySelectorAll(".modal-title")[0],Header:t[0].querySelectorAll(".modal-header")[0],Body:t[0].querySelectorAll(".modal-body")[0],Footer:t[0].querySelectorAll(".modal-footer")[0],Close:t[0].querySelectorAll("[data-dismiss='modal']"),Modal:t[0]}:null}addModal(e,t,o,l,a){const s=this;let i=s.Find(e);if(null!==i)return;let n="";n+='
',n+='
',n+='
',n+=' ",n+='
',n+='
',n+='
',n+='
',n+='\t Loading...',n+="
",n+="
",n+="
",n+="
",!0===l&&(n+=' "),n+="
",n+="
",n+="
",s.#r(s.#t,n),i=s.Find(e),null!==i&&($("#"+s.#e+e).on("hidden.bs.modal",(function(t){s.Close(e)})),i.Title.addEventListener("dblclick",(function(t){t.stopPropagation(),t.preventDefault(),s.#d(e)})))}async#s(e,t){const o=this;return await new Promise((async(l,a)=>{await o.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";t.Buttons.forEach((function(e){s+='
"})),o.Update({ID:e,Footer:s});const i=o.Find(e);i.Footer.querySelectorAll("button").forEach((function(t){t.addEventListener("click",(function(a){a.stopPropagation(),a.preventDefault();const s=t.getAttribute("data-prompt-value");o.Close(e),l(s)}))})),i.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")}))}))}))}async#a(e,t){const o=this;return await new Promise((async(l,a)=>{t.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await o.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";o.#o(t.Message)||(s+="
"+t.Message+"
"),s+='
";let i="";t.Buttons.forEach((function(e){i+='
"})),o.Update({ID:e,Body:s,Footer:i});const n=o.Find(e),r=n.Footer.querySelectorAll("button");r[0].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault();const a=n.Body.querySelectorAll("input")[0].value;o.Close(e),l(a)})),r[1].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")})),n.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),o.Close(e),l("")}))}))}))}#r(e,t){let o=document.createElement("template");o.innerHTML=t,o=o.content.firstChild,e.appendChild(o)}#n(e,t){jQuery?jQuery(e).html(t):e.innerHTML=t}#o(e){return void 0===e||(null==e||(0==e||e.trim().length<=0))}#i(){const e=this;e.#t.querySelectorAll(".modal-backdrop").length<=0||e.#t.querySelectorAll(".modal").length>0||(e.#t.querySelectorAll(".modal-backdrop").forEach((function(e){e.parentNode.removeChild(e)})),e.#t.classList.remove("modal-open"),e.#t.style.overflow=null)}async#l(e){return await new Promise((async t=>{await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((e=>e.text())).then((e=>{t(e)})).catch((e=>{t("Error: "+e)}))}))}#d(e){const t=this;let o=t.Find(e);if(null===o)return;let l=o.Modal.querySelectorAll(".modal-dialog")[0];l.classList.contains("modal-sm")?t.Update({ID:e,Size:"md"}):l.classList.contains("modal-md")?t.Update({ID:e,Size:"lg"}):l.classList.contains("modal-lg")?t.Update({ID:e,Size:"xl"}):l.classList.contains("modal-xl")?t.Update({ID:e,Size:"xxl"}):l.classList.contains("modal-xxl")?t.Update({ID:e,Size:"sm"}):t.Update({ID:e,Size:"md"})}}var BSDialog=new BSDialog4;
\ No newline at end of file