commit 8a8b886be1d36a9f5ad948142555b8737febc917 Author: Ray Date: Wed Aug 16 00:26:52 2023 +0100 Initial commit diff --git a/bs5-test.html b/bs5-test.html new file mode 100644 index 0000000..5ab98bb --- /dev/null +++ b/bs5-test.html @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + +
+
+
+ + + \ No newline at end of file diff --git a/bstoast5.js b/bstoast5.js new file mode 100644 index 0000000..027d250 --- /dev/null +++ b/bstoast5.js @@ -0,0 +1,147 @@ +/** + * BSToast5 + * @version v0.1.0.001 (2023/08/16 0022) + */ +var BSToast5 = { + Create: async function (options) { + var a = this; + + a.Options = Object.assign(a.DefaultOptions, options); + + a.pfx = "bstoa5_"; + a.id = Math.floor(Math.random() * 100000) + 10000; + a.body = document.getElementsByTagName("body")[0]; + + a.addBackdrop(); + a.addToast(a.Options); + }, + DefaultOptions: { + Icon: null, + Title: "", + Time: "", + Message: "", + Animation: true, + AutoHide: true, + Delay: 2500, + Position: "top-0 end-0 p-3" + }, + Clear: function () { + this.body.querySelectorAll(".toast").forEach(function(e) { + e.parentNode.removeChild(e); + }); + + this.removeBackdrop(); + }, + Close: function (id) { + var a = this; + + // Close this toast + a.body.querySelectorAll("#" + a.pfx + id + ".toast").forEach(function(e) { + if (typeof(e.parentNode) == "undefined") { + return; + } + + e.parentNode.removeChild(e); + }); + + // Clean-up all closed toasts + a.body.querySelectorAll(".toast.hide").forEach(function(e) { + if (typeof(e.parentNode) == "undefined") { + return; + } + + e.parentNode.removeChild(e); + }); + + if (a.body.querySelectorAll(".toast").length <= 0) { + let backdrop = a.body.querySelectorAll(".toast-backdrop")[0]; + + if (typeof(backdrop) == "undefined") { + return; + } + + if (typeof(backdrop.parentNode) == "undefined") { + return; + } + + backdrop.parentNode.removeChild(backdrop); + } + }, + addBackdrop: async function () { + let a = this; + + // don't allow duplicates + if (a.body.querySelectorAll(".toast-backdrop").length > 0) { + return; + } + + a.appendHtml(a.body, '
'); + }, + addToast: function (options) { + var a = this; + + if (a.body.querySelectorAll(".toast-container").length <= 0) { + return; + } + + let html = ""; + html += ''; + + a.appendHtml(a.body.querySelectorAll(".toast-container")[0], html); + + const toastEl = document.querySelectorAll("#" + a.pfx + a.id + ".toast"); + const toast = new bootstrap.Toast(toastEl[0], { + animation: options.Animation, + autohide: options.AutoHide, + delay: options.Delay + }); + toast.show(); + + toastEl[0].addEventListener('hidden.bs.toast', () => { + a.Close(a.id); + }); + }, + appendHtml: function (el, html) { + let node = document.createElement('template'); + node.innerHTML = html.trim(); + + node = node.content.firstChild; + + el.appendChild(node); + }, + isNullOrWhitespace: function(e) { + if (typeof (e) == "undefined") { + return true; + } + + if (e == null) { + return true; + } + + if (e == false) { + return true; + } + + return (e.trim().length <= 0); + }, + removeBackdrop: function () { + if (this.body.querySelectorAll(".toast-backdrop").length <= 0) { + return; + } + + let backdrop = this.body.querySelectorAll(".toast-backdrop")[0]; + backdrop.parentNode.removeChild(backdrop); + } +}; \ No newline at end of file diff --git a/bstoast5.min.js b/bstoast5.min.js new file mode 100644 index 0000000..25c59bb --- /dev/null +++ b/bstoast5.min.js @@ -0,0 +1,5 @@ +/** + * 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,'
')},addToast:function(t){var e=this;if(e.body.querySelectorAll(".toast-container").length<=0)return;let o="";o+='",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)}}; \ No newline at end of file