UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 3.25 kB
(()=>{"use strict";var e={d:(t,i)=>{for(var n in i)e.o(i,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:i[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyNotifications:()=>i});const i=class{#e=!1;#t=!1;#i=null;#n=100;#o=null;#r;constructor({audio:e=null,defaultIcon:t=null,bodyLimit:i=100,defaultOnClick:n=function(e){e.preventDefault(),window.focus&&window.focus(),this.close()}}={}){if(!(e instanceof HTMLAudioElement)&&"string"!=typeof e&&null!==e)throw new TypeError("audio must be an instance of HTMLAudioElement or null.");if(null!==t&&"string"!=typeof t)throw new TypeError("defaultIcon must be a string or null.");if(!Number.isFinite(i)||i<0)throw new TypeError("bodyLimit must be a non-negative number.");if("function"!=typeof n)throw new TypeError("defaultOnClick must be a function.");this.#i="string"!=typeof e?e:new Audio(e),this.#o=t,this.#n=i,this.#r=n}requestPerm(){const e=this;return new Promise((t,i)=>{e.isCompatible()?"default"===Notification.permission?Notification.requestPermission().then(i=>{this.#t=!0,e.#e="granted"===i,t(e.#e)}).catch(i):(this.#t=!0,e.#e="granted"===Notification.permission,t(e.#e)):(this.#t=!0,e.#e=!1,t(!1))})}isCompatible(){return"Notification"in window}send(e,t={}){if(!this.#t)throw new Error("You must call requestPerm() before sending a notification.");if("string"!=typeof e)throw new TypeError("title must be a string.");if("object"!=typeof t||null===t)throw new TypeError("config must be a non-null object.");if(!this.#e)return null;const{icon:i=this.#o||void 0,vibrate:n=[200,100,200]}=t,o={...t};"string"==typeof i&&(o.icon=i),Array.isArray(n)&&(o.vibrate=n),"string"==typeof o.body&&(o.body=function(e,t,i=.6){if("string"!=typeof e)throw new TypeError("Expected a string for 'text', but received "+typeof e);if(!Number.isInteger(t)||t<=0)throw new TypeError(`Expected 'limit' to be a positive integer, but received ${t}`);if("number"!=typeof i||i<0||i>1)throw new TypeError(`Expected 'safeCutZone' to be a number between 0 and 1, but received ${i}`);let n=e.trim();if(n.length>t){const e=n.lastIndexOf(" ",t);return e>0&&e>=t*i?`${n.substring(0,e).trim()}...`:`${n.substring(0,t).trim()}...`}return n}(o.body,this.#n));const r=new Notification(e,o);return r.addEventListener("show",()=>{this.#i instanceof HTMLAudioElement&&(this.#i.currentTime=0,this.#i.play().catch(e=>console.error(e)))}),"function"==typeof this.#r&&r.addEventListener("click",this.#r),r}wasPermissionRequested(){return this.#t}isAllowed(){return this.#e}getAudio(){return this.#i}setAudio(e){if(!(e instanceof HTMLAudioElement)&&"string"!=typeof e&&null!==e)throw new TypeError("sound must be an instance of HTMLAudioElement or null.");this.#i="string"!=typeof e?e:new Audio(e)}getBodyLimit(){return this.#n}setBodyLimit(e){if(!Number.isFinite(e)||e<0)throw new TypeError("bodyLimit must be a non-negative number.");this.#n=e}getDefaultAvatar(){return this.#o}setDefaultAvatar(e){if("string"!=typeof e&&null!==e)throw new TypeError("defaultIcon must be a string or null.");this.#o=e}getDefaultOnClick(){return this.#r}setDefaultOnClick(e){if("function"!=typeof e)throw new TypeError("defaultOnClick must be a function.");this.#r=e}};window.TinyNotifications=t.TinyNotifications})();