bsdialog4/bsdialog4.min.js

5 lines
5.6 KiB
JavaScript
Raw Normal View History

2023-08-03 10:29:17 +00:00
/**
* BSDialog4
2023-08-21 00:43:47 +00:00
* @version v0.1.2.043 (2023/08/20 00:39)
2023-08-03 10:29:17 +00:00
*/
2023-08-21 00:43:47 +00:00
var BSDialog={Create:async function(t,e,o,l){var a=this;a.id=t,a.pfx="bsdia4_",a.body=document.getElementsByTagName("body")[0],a.addBackdrop(),a.addModal(t,e,l),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 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},ShowToast:function(t,e,o,l){this.Create(t,e,null,l),this.UpdateBody(t,o)},ShowPrompt:async function(t){let e=this,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+='<button type="button" class="btn btn-'+t.Colour+'" data-prompt-value="'+t.Value+'">'+t.Label+"</button>"})),e.UpdateFooter(o,s);const d=e.Find(o);d.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)}))})),d.Close.forEach((function(l){l.addEventListener("click",(function(l){l.stopPropagation(),l.preventDefault(),e.Close(o),t("")}))}))}))},DefaultPromptOptions:{Title:"",Message:"",Size:"md",Buttons:[{Label:"Yes",Value:"Yes",Colour:"primary"},{Label:"No",Value:"No",Colour:"secondary"},{Label:"Cancel",Value:"Cancel",Colour:"secondary"}]},addBackdrop:function(){let t=this;t.body.querySelectorAll(".modal-backdrop").length>0||(t.appendHtml(t.body,'<div class="modal-backdrop fade show"></div>'),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){var l=this;let a=l.Find(t);if(null!==a)return;let s="";s+='<div class="modal fade show d-block" id="'+l.pfx+t+'" tabindex="-1">',s+=' <div class="modal-dialog modal-'+o+'">',s+=' <div class="modal-content">',s+=' <div class="modal-header">',s+=' <span class="modal-title font-weight-bold">'+e+"</span>",s+=' <button type="button" class="close" data-dismiss="modal" aria-label="Close">',s+=' <span aria-hidden="true">&times;</span>',s+=" </button>",s+=" </div>",s+=' <div class="modal-body">',s+='<div class="row mt-3 pb-3">',s+=' <div class="col-12 text-center">',s+=' <div class="spinner-border" role="status">',s+='\t <span class="sr-only">Loading...</span>',s+=" </div>",s+=" </div>",s+="</div>",s+=" </div>",s+=' <div class="modal-footer">',s+=' <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>',s+=" </di