bsdialog5/bsdialog5.min.js

5 lines
8.3 KiB
JavaScript
Raw Normal View History

2023-07-30 13:21:02 +00:00
/**
* BSDialog5
* @version v0.2.0.055 (2023/11/12 1428)
2023-11-12 14:52:18 +00:00
*/
var BSDialog5={Default:function(){return{ShowOptions:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0,EasyClose:!0},PromptOptions:{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}},UpdateOptions:{ID:null,Title:null,Body:null,BodyURL:null,Footer:null,Size:null}}},Show:async function(t){const e=this,o=Object.assign(e.Default().ShowOptions,t);e.id=o.ID,e.pfx="bsdia5_",e.addBackdrop(),e.addModal(o.ID,o.Title,o.Size,o.ShowFooter,o.EasyClose,o.Colour),null==o.URL?await e.Update({ID:o.ID,Body:o.Message}):e.isNullOrWhitespace(o.URL)?await e.Update({ID:o.ID,Body:URL}):o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")?await e.UpdateBodyRemote(o.ID,o.URL):await e.Update({ID:o.ID,Body:URL})},Prompt:async function(t){const e=this,o="prompt"+Math.floor(1e4*Math.random())+1e3,l=Object.assign(e.Default().PromptOptions,t);return"textbox"===l.Type?await e.showTextboxPrompt(o,l):await e.showButtonPrompt(o,l)},Clear:function(){this.GetBody().querySelectorAll(".modal").forEach((function(t){t.parentNode.removeChild(t)})),this.removeBackdrop()},Close:function(t){let e=this.Find(t);null!==e&&(e.Modal.parentNode.removeChild(e.Modal),e=this.Find(t),null===e&&this.removeBackdrop())},Update:async function(t){const e=this;let o=Object.assign(e.Default().UpdateOptions,t);const l=e.Find(o.ID);null!==l&&(this.isNullOrWhitespace(o.Title)||(l.Title.innerHTML=o.Title),this.isNullOrWhitespace(o.Body)||e.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)||await e.UpdateBodyRemote(o.ID,o.BodyURL),this.isNullOrWhitespace(o.Footer)||e.html(l.Footer,o.Footer),this.isNullOrWhitespace(o.Size)||(l.Modal.classList.remove("modal-sm"),l.Modal.classList.remove("modal-md"),l.Modal.classList.remove("modal-lg"),l.Modal.classList.remove("modal-xl"),l.Modal.classList.add("modal-"+o.Size)))},UpdateBodyRemote:async function(t,e){const o=this;o.Exists(t)&&await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((t=>t.text())).then((e=>{o.Update({ID:t,Body:e})})).catch((e=>{o.Update({ID:t,Body:"Error: "+e})}))},Exists:function(t){return null!==this.Find(t)},Find:function(t){const e=this.GetBody().querySelectorAll("#"+this.pfx+t+".modal");return e?e.length<=0?null:{Title:e[0].querySelectorAll(".modal-title")[0],Header:e[0].querySelectorAll(".modal-header")[0],Body:e[0].querySelectorAll(".modal-body")[0],Footer:e[0].querySelectorAll(".modal-footer")[0],Close:e[0].querySelectorAll("[data-bs-dismiss='modal']"),Modal:e[0]}:null},GetBody:function(){return document.getElementsByTagName("body")[0]},addBackdrop:function(){const t=this;t.GetBody().querySelectorAll(".modal-backdrop").length>0||(t.appendHtml(t.GetBody(),'<div class="modal-backdrop fade show"></div>'),t.GetBody().classList.add("modal-open"),t.GetBody().style.overflow="hidden")},addModal:function(t,e,o,l,a,s){const d=this;let i=d.Find(t);if(null!==i)return;let n="";if(n+='<div class="modal modal-'+o+' fade show d-block" id="'+d.pfx+t+'" tabindex="-1" aria-modal="true" role="dialog">',n+=' <div class="modal-dialog">',n+=' <div class="modal-content">',n+=' <div class="modal-header">',n+=' <span class="modal-title fs-5" style="cursor: default; user-select: none;">'+e+"</span>",n+=' <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" style="user-select: none;"></button>',n+=" </div>",n+=' <div class="modal-body">',n+='<div class="row mt-3 pb-3">',n+=' <div class="col-12 text-center">',n+=' <div class="spinner-border">',n+='\t <span class="visually-hidden">Loading...</span>',n+=" </div>",n+=" </div>",n+="</div>",n+=" </div>",!0===l&&(n+=' <div class="modal-footer">',n+=' <button type="button" class="btn