bsdialog4/bsdialog4.min.js
Ray ecd6dd156d Added Pop() to close top-most modal
Fixed stacking modal causes scrolling and focus failure
2023-12-08 22:19:39 +00:00

5 lines
6.9 KiB
JavaScript

/**
* BSDialog4
* @version v0.1.6.015 (2023/12/08 22:04)
*/
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#l(){return["modal-sm","modal-md","modal-lg","modal-xl","modal-xxl"]}async Show(e){const t=this,l=Object.assign(t.Options.ShowModal,e);if(t.addModal(l.ID,l.Title,l.Size,l.ShowFooter,l.Colour),$("#"+t.#e+l.ID).modal({backdrop:l.EasyClose,show:!0}),t.#o(l.URL)){const e=await t.#a(l.URL);await t.Update({ID:l.ID,Body:e})}else await t.Update({ID:l.ID,Body:l.Message})}async Prompt(e){const t=this,l=Math.floor(1e4*Math.random())+1e3,o=Object.assign(t.Options.ShowPrompt,e);return"textbox"===o.Type?await t.#s(l,o):await t.#i(l,o)}async Clear(){const e=this;document.querySelectorAll(".modal").forEach((async function(t){await e.Close(t.getAttribute("id"))}))}async Close(e){const t=this;e.toString().startsWith(t.#e)&&(e=e.toString().substr(t.#e.length));const l=document.getElementById(t.#e+e);l&&(await $(l).modal("hide"),l.parentNode.removeChild(l)),document.querySelectorAll(".modal").length>0&&(t.#t.classList.contains("modal-open")||(t.#t.classList.add("modal-open"),t.#t.style.paddingRight="17px"))}async Pop(){const e=this;if(document.querySelectorAll(".modal").length>0){const t=document.querySelectorAll(".modal")[0].getAttribute("id");await e.Close(t)}}async Update(e){const t=this;let l=Object.assign(t.Options.UpdateModal,e);const o=t.Find(l.ID);if(null!==o){if(t.#n(l.Title)||(o.Title.innerHTML=l.Title),t.#n(l.Body)||t.#r(o.Body,l.Body),this.#n(l.Body)){if(this.#o(l.URL)){const e=await t.#a(l.URL);await t.Update({ID:l.ID,Body:e})}}else t.#r(o.Body,l.Body);t.#n(l.Footer)||t.#r(o.Footer,l.Footer),t.#n(l.Size)||t.#d(l.ID,"modal-"+l.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,l,o,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-'+l+'">',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">&times;</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===o&&(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 l=this;return await new Promise((async(o,a)=>{await l.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>"})),l.Update({ID:e,Footer:s});const i=l.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");l.Close(e),o(s)}))})),i.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}async#s(e,t){const l=this;return await new Promise((async(o,a)=>{t.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await l.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";l.#n(t.Message)||(s+="<p>"+t.Message+"</p>"),s+='<div class="form-group row">',l.#n(t.Textbox.Label)||t.Textbox.LabelSize<=0?(s+='<div class="col-sm-12">',s+='<input type="text" class="form-control" id="textbox'+e+'" placeholder="'+t.Textbox.Placeholder+'" value="'+t.Textbox.Value+'">',s+="</div>"):(s+='<label for="textbox'+e+'" class="col-sm-'+t.Textbox.LabelSize+' col-form-label">'+t.Textbox.Label+"</label>",s+='<div class="col-sm-'+t.Textbox.BoxSize+'">',s+='<input type="text" class="form-control" id="textbox'+e+'" placeholder="'+t.Textbox.Placeholder+'" value="'+t.Textbox.Value+'">',s+="</div>"),s+="</div>";let i="";t.Buttons.forEach((function(e){i+='<button type="button" class="btn btn-'+e.Colour+'" data-prompt-value="'+e.Value+'">'+e.Label+"</button>"})),l.Update({ID:e,Body:s,Footer:i});const n=l.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;l.Close(e),o(a)})),r[1].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")})),n.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}#c(e,t){let l=document.createElement("template");l.innerHTML=t,l=l.content.firstChild,e.appendChild(l)}#r(e,t){jQuery?jQuery(e).html(t):e.innerHTML=t}#n(e){return void 0===e||(null==e||(0==e||e.trim().length<=0))}async#a(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)}))}))}#o(e){return!this.#n(e)&&!!(e.startsWith("http://")||e.startsWith("https://")||e.startsWith("/"))}#d(e,t){const l=this,o=l.Find(e);if(null===o)return;let a=o.Modal.querySelectorAll(".modal-dialog")[0];for(let e=0;e<l.#l.length;e++)a.classList.remove(l.#l[e]);a.classList.add(t)}#u(e){const t=this,l=t.Find(e);if(null===l)return;let o=l.Modal.querySelectorAll(".modal-dialog")[0],a=0;for(let e=0;e<t.#l.length;e++)if(o.classList.contains(t.#l[e])){o.classList.remove(t.#l[e]),a=e;break}a++,a>t.#l.length-1&&(a=0),o.classList.add(t.#l[a])}}var BSDialog=new BSDialog4;