/** * 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);if(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.Update({ID:o.ID,Body:o.Message});else if(e.isNullOrWhitespace(o.URL))await e.Update({ID:o.ID,Body:o.URL});else if(o.URL.startsWith("http://")||o.URL.startsWith("https://")||o.URL.startsWith("/")){let t=await e.retrieveURL(o.URL);await e.Update({ID:o.ID,Body:t})}else await e.Update({ID:o.ID,Body: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);if(null!==l){if(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)){let t=await e.retrieveURL(o.BodyURL);await e.Update({ID:o.ID,Body:t})}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)}))}},Exists:function(t){return null!==this.Find(t)},Find:function(t){const 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"}],Textbox:{Label:"",LabelSize:4,Placeholder:"",Value:"",BoxSize:8}},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,''),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+='",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({ID:t,Title:e.Title,Message:e.Message,Size:e.Size});let i="";e.Buttons.forEach((function(t){i+='"})),o.Update({ID:t,Footer: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+="

"+e.Message+"

"),i+='
',o.isNullOrWhitespace(e.Textbox.Label)||e.Textbox.LabelSize<=0?(i+='
',i+='',i+="
"):(i+='",i+='
',i+='',i+="
"),i+="
";let n="";e.Buttons.forEach((function(t){n+='"})),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},retrieveURL:async function(t){return await new Promise((async e=>{await fetch(t,{cache:"no-cache",credentials:"same-origin"}).then((t=>t.text())).then((t=>{e(t)})).catch((t=>{e("Error: "+t)}))}))},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})}};