5 lines
8.0 KiB
JavaScript
5 lines
8.0 KiB
JavaScript
/**
|
|
* BSDialog4
|
|
* @version v0.1.3.017 (2023/08/30 00:17)
|
|
*/
|
|
var BSDialog={Show:async function(t){const e=this,o=Object.assign(e.Default().ShowOptions,t);e.id=o.ID,e.pfx="bsdia4_",e.body=document.getElementsByTagName("body")[0],e.addBackdrop(),e.addModal(o.ID,o.Title,o.Size,!0,o.Colour),null==o.URL?await e.UpdateBody(o.ID,o.Message):e.isNullOrWhitespace(o.URL)?await e.UpdateBody(o.ID,o.URL):o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")?await e.UpdateBodyRemote(o.ID,o.URL):await e.UpdateBody(o.ID,o.URL)},Prompt:async function(t){const e=this;let o=Math.floor(1e4*Math.random())+1e3;const l=Object.assign(e.Default().PromptOptions,t);return"textbox"===l.Type?await e.showTextboxPrompt(o,l):await e.showButtonPrompt(o,l)},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())},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.forEach((function(t){t.innerHTML=o.Title})),this.isNullOrWhitespace(o.Body)||l.Body.forEach((function(t){e.html(t,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)||l.Footer.forEach((function(t){e.html(t,o.Footer)})),this.isNullOrWhitespace(o.Size)||l.Modal[0].querySelectorAll(".modal-dialog").forEach((function(t){t.classList.forEach((function(e){"modal-dialog"!=e&&e.startsWith("modal-")&&t.classList.remove(e)})),t.classList.add("modal-"+o.Size)})))},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)}))},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},Default:function(){return{ShowOptions:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0},PromptOptions:{Type:"button",Title:"",Message:"",Size:"md",Buttons:[{Label:"Yes",Value:"Yes",Colour:"primary"},{Label:"No",Value:"No",Colour:"secondary"},{Label:"Cancel",Value:"Cancel",Colour:"secondary"}],Label:"",Placeholder:""},ToastOptions:{ID:null,Title:"",Message:"",Size:"md"},UpdateOptions:{ID:null,Title:null,Body:null,BodyURL:null,Footer:null,Size:null}}},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,l,a){var i=this;let n=i.Find(t);if(null!==n)return;let s="";s+='<div class="modal fade show d-block" id="'+i.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">×</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>",!0===l&&(s+=' <div class="modal-footer">',s+=' <button type="button" class="btn btn-'+a+'" data-dismiss="modal">Close</button>',s+=" </div>"),s+=" </div>",s+=" </div>",s+="</div>",i.appendHtml(i.body,s),n=i.Find(t),null!==n&&n.Close.forEach((function(e){e.addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),i.Close(t)})),e.addEventListener("auxclick",(function(t){t.stopPropagation(),t.preventDefault(),1===t.button&&i.toggleSize()}))}))},showButtonPrompt:async function(t,e){const o=this;return await new Promise((async(l,a)=>{await o.Show(t,e.Title,e.Message,e.Size);let i="";e.Buttons.forEach((function(t){i+='<button type="button" class="btn btn-'+t.Colour+'" data-prompt-value="'+t.Value+'">'+t.Label+"</button>"})),o.UpdateFooter(t,i);const n=o.Find(t);n.Footer[0].querySelectorAll("button").forEach((function(e){e.addEventListener("click",(function(a){a.stopPropagation(),a.preventDefault();const i=e.getAttribute("data-prompt-value");o.Close(t),l(i)}))})),n.Close.forEach((function(e){e.addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),o.Close(t),l("")}))}))}))},showTextboxPrompt:async function(t,e){const o=this;return await new Promise((async(l,a)=>{e.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await o.Show({ID:t,Title:e.Title,Message:e.Message,Size:e.Size});let i="";o.isNullOrWhitespace(e.Message)||(i+="<p>"+e.Message+"</p>"),i+='<div class="form-group row">',o.isNullOrWhitespace(e.Label)?(i+='<div class="col-sm-12">',i+='<input type="text" class="form-control" id="textbox'+t+'" placeholder="'+e.Placeholder+'">',i+="</div>"):(i+='<label for="textbox'+t+'" class="col-sm-4 col-form-label">'+e.Label+"</label>",i+='<div class="col-sm-8">',i+='<input type="text" class="form-control" id="textbox'+t+'" placeholder="'+e.Placeholder+'">',i+="</div>"),i+="</div>";let n="";e.Buttons.forEach((function(t){n+='<button type="button" class="btn btn-'+t.Colour+'" data-prompt-value="'+t.Value+'">'+t.Label+"</button>"})),o.Update({ID:t,Body:i,Footer:n});const s=o.Find(t),d=s.Footer[0].querySelectorAll("button");d[0].addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault();const a=s.Body[0].querySelectorAll("input")[0].value;o.Close(t),l(a)})),d[1].addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),o.Close(t),l("")})),s.Close.forEach((function(e){e.addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault(),o.Close(t),l("")}))}))}))},appendHtml:function(t,e){let o=document.createElement("template");o.innerHTML=e,o=o.content.firstChild,t.appendChild(o)},html:function(t,e){jQuery?jQuery(t).html(e):t.innerHTML=e},isNullOrWhitespace:function(t){return void 0===t||(null==t||(0==t||t.trim().length<=0))},removeBackdrop:function(){if(this.body.querySelectorAll(".modal-backdrop").length<=0)return;if(this.body.querySelectorAll(".modal").length>0)return;let t=this.body.querySelectorAll(".modal-backdrop")[0];t.parentNode.removeChild(t),this.body.classList.remove("modal-open"),this.body.style.overflow=null},toggleSize:function(){var t=this;let e=t.Find(t.id);if(null===e)return;let o=e.Modal[0].querySelectorAll(".modal-dialog")[0];o.classList.contains("modal-sm")?t.Update({ID:t.id,Size:"md"}):o.classList.contains("modal-md")?t.Update({ID:t.id,Size:"lg"}):o.classList.contains("modal-lg")?t.Update({ID:t.id,Size:"xl"}):o.classList.contains("modal-xl")?t.Update({ID:t.id,Size:"sm"}):t.Update({ID:t.id,Size:"md"})},Create:async function(t,e,o,l){await this.Show({ID:t,Title:e,Message:o,URL:o,Size:l,Colour:"secondary"})},UpdateTitle:function(t,e){this.Update({ID:t,Title:e})},UpdateSize:function(t,e){this.Update({ID:t,Size:e})},UpdateBody:function(t,e){this.Update({ID:t,Body:e})},UpdateFooter:function(t,e){this.Update({ID:t,Footer:e})},Toast:async function(t){const e=Object.assign(this.Default().ToastOptions,t);await this.Show({ID:e.ID,Title:e.Title,Message:e.Message,URL:null,Size:e.Size,Colour:"secondary",ShowFooter:!1})},ShowToast:function(t,e,o,l){this.Toast({ID:t,Title:e,Message:o,Size:l})}}; |