2023-11-18 20:08:55 +00:00
|
|
|
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,URL:null,Footer:null,Size:null}}}get#e(){return"bsdia4_"}get#t(){return document.getElementsByTagName("body")[0]}get#o(){return["modal-sm","modal-md","modal-lg","modal-xl","modal-xxl"]}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.#l(o.URL)){const e=await t.#a(o.URL);await t.Update({ID:o.ID,Body:e})}else await t.Update({ID:o.ID,Body:o.Message})}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.#s(o,l):await t.#i(o,l)}Clear(){document.querySelectorAll(".modal").forEach((function(e){$(e).modal("hide")}))}Close(e){const t=this;e.toString().startsWith(t.#e)&&(e=e.toString().substr(t.#e.length));const o=document.getElementById(t.#e+e);o&&($(o).modal("hide"),o.parentNode.removeChild(o))}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.#n(o.Title)||(l.Title.innerHTML=o.Title),t.#n(o.Body)||t.#r(l.Body,o.Body),this.#n(o.Body)){if(this.#l(o.URL)){const e=await t.#a(o.URL);await t.Update({ID:o.ID,Body:e})}}else t.#r(l.Body,o.Body);t.#n(o.Footer)||t.#r(l.Footer,o.Footer),t.#n(o.Size)||t.#d(o.ID,"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+='<div class="modal fade" style="display: none;" id="'+s.#e+e+'" tabindex="-1" aria-labelledby="exampleModalSmLabel" aria-hidden="true">',n+=' <div class="modal-dialog modal-'+o+'">',n+=' <div class="modal-content">',n+=' <div class="modal-header">',n+=' <span class="modal-title font-weight-bold" style="cursor: default; user-select: none;" aria-label="Title">'+t+"</span>",n+=' <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="user-select: none;">',n+=' <span aria-hidden="true">×</span>',n+=" </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" role="status">',n+='\t <span class="sr-only">Loading...</span>',n+=" </div>",n+=" </div>",n+="</div>",n+=" </div>",!0===l&&(n+=' <div class="modal-footer">',n+=' <button type="button" class="btn btn-'+a+'" data-dismiss="modal">Close</button>',n+=" </div>"),n+=" </div>",n+=" </div>",n+="</div>",s.#c(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.#u(e)})))}async#i(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+='<button type="button" class="btn btn-'+e.Colour+'" data-prompt-value="'+e.Value+'">'+e.Label+"</button>"})),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(
|