bstoast5/bstoast5.min.js
2023-08-16 00:26:52 +01:00

5 lines
2.5 KiB
JavaScript

/**
* BSToast5
* @version v0.1.0.001 (2023/08/16 0022)
*/
var BSToast5={Create:async function(t){var e=this;e.Options=Object.assign(e.DefaultOptions,t),e.pfx="bstoa5_",e.id=Math.floor(1e5*Math.random())+1e4,e.body=document.getElementsByTagName("body")[0],e.addBackdrop(),e.addToast(e.Options)},DefaultOptions:{Icon:null,Title:"",Time:"",Message:"",Animation:!0,AutoHide:!0,Delay:2500,Position:"top-0 end-0 p-3"},Clear:function(){this.body.querySelectorAll(".toast").forEach((function(t){t.parentNode.removeChild(t)})),this.removeBackdrop()},Close:function(t){var e=this;if(e.body.querySelectorAll("#"+e.pfx+t+".toast").forEach((function(t){void 0!==t.parentNode&&t.parentNode.removeChild(t)})),e.body.querySelectorAll(".toast.hide").forEach((function(t){void 0!==t.parentNode&&t.parentNode.removeChild(t)})),e.body.querySelectorAll(".toast").length<=0){let t=e.body.querySelectorAll(".toast-backdrop")[0];if(void 0===t)return;if(void 0===t.parentNode)return;t.parentNode.removeChild(t)}},addBackdrop:async function(){let t=this;t.body.querySelectorAll(".toast-backdrop").length>0||t.appendHtml(t.body,'<div class="toast-backdrop" aria-live="polite" aria-atomic="true" style="position:static; min-height:200px; z-index:8;"><div class="toast-container '+t.Options.Position+'"></div></div>')},addToast:function(t){var e=this;if(e.body.querySelectorAll(".toast-container").length<=0)return;let o="";o+='<div id="'+e.pfx+e.id+'" class="toast" role="alert" aria-live="assertive" aria-atomic="true" style="min-width:200px;">',o+=' <div class="toast-header">',e.isNullOrWhitespace(t.Icon)||(o+=t.Icon),o+=' <strong class="me-auto">'+t.Title+"</strong>",o+=" <small>"+t.Time+"</small>",o+=' <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>',o+=" </div>",o+=' <div class="toast-body">'+t.Message+"</div>",o+="</div>",e.appendHtml(e.body.querySelectorAll(".toast-container")[0],o);const a=document.querySelectorAll("#"+e.pfx+e.id+".toast");new bootstrap.Toast(a[0],{animation:t.Animation,autohide:t.AutoHide,delay:t.Delay}).show(),a[0].addEventListener("hidden.bs.toast",(()=>{e.Close(e.id)}))},appendHtml:function(t,e){let o=document.createElement("template");o.innerHTML=e.trim(),o=o.content.firstChild,t.appendChild(o)},isNullOrWhitespace:function(t){return void 0===t||(null==t||(0==t||t.trim().length<=0))},removeBackdrop:function(){if(this.body.querySelectorAll(".toast-backdrop").length<=0)return;let t=this.body.querySelectorAll(".toast-backdrop")[0];t.parentNode.removeChild(t)}};