bstoast4/bstoast4.min.js

5 lines
2.6 KiB
JavaScript

/**
* BSToast4
* @version v0.1.0.012 (2023/08/15 0056)
*/
var BSToast4={Create:async function(t){var e=this;e.Options=Object.assign(e.DefaultOptions,t),e.pfx="bstoa4_",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:2e3,Position:"top:0; right:0; margin:20px;"},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 style="position:absolute; '+t.Options.Position+'" class="toast-backdrop-body"></div></div>')},addToast:function(t){var e=this;if(e.body.querySelectorAll(".toast-backdrop-body").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="mr-auto">'+t.Title+"</strong>",o+=" <small>"+t.Time+"</small>",o+=' <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">',o+=' <span aria-hidden="true">&times;</span>',o+=" </button>",o+=" </div>",o+=' <div class="toast-body">'+t.Message+"</div>",o+="</div>",e.appendHtml(e.body.querySelectorAll(".toast-backdrop-body")[0],o),jQuery("#"+e.pfx+e.id+".toast").toast({animation:t.Animation,autohide:t.AutoHide,delay:t.Delay}).toast("show"),jQuery("#"+e.pfx+e.id+".toast").on("hidden.bs.toast",(function(){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)}};