UNPKG

@jaymago/simple-display-alert

Version:

A lightweight, customizable alert system for web applications with TypeScript support

2 lines (1 loc) 4.78 kB
"use strict";var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var T=(e,t)=>{for(var i in t)y(e,i,{get:t[i],enumerable:!0})},S=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of P(t))!C.call(e,o)&&o!==i&&y(e,o,{get:()=>t[o],enumerable:!(r=D(t,o))||r.enumerable});return e};var L=e=>S(y({},"__esModule",{value:!0}),e);var X={};T(X,{ALERT_POSITIONS:()=>U,createAlertElement:()=>w,displayAlert:()=>p,displayAlertError:()=>k,displayAlertInfo:()=>H,displayAlertLoader:()=>I,displayAlertSuccess:()=>O,displayAlertWarning:()=>q,displayErrors:()=>B,getErrorMessage:()=>A,getStatusCode:()=>R,hideAlertLoader:()=>z,removeDisplayAlert:()=>$,removeResponseAlert:()=>a});module.exports=L(X);var u=e=>typeof e=="object"&&e!==null&&typeof e.message=="string";function n(e,t){return setTimeout(e,t)}function x(...e){return e.join(" ")}window.alertTimer=window.alertTimer||{};var m={parentId:"simple-display-alert",icons:{loader:'<span class="sda-icon sda-loader-icon"></span> ',success:'<span class="sda-icon sda-success-icon"></span> ',danger:'<span class="sda-icon sda-danger-icon"></span> ',warning:'<span class="sda-icon sda-warning-icon"></span> ',info:'<span class="sda-icon sda-info-icon"></span> '}};function b(e){return e.position==="top-left"||e.position==="top-right"}function w(e,t,i,r={}){let o=document.createElement("div"),d=r.position??"bottom-right";return o.classList=x(`fade alert alert-response-all alert-${e} `,`${i||""} `,`${i?"has-identifier":""} ${r.icon?"has-icon":""} `,`fade-${r.fadeDirection??"none"}`),o.id="simple-display-alert-items",o.innerHTML=x("<div>",r.icon?m.icons[r.icon]:"",t,r.closeBtn===!1?"":'<a class="close" title="close">&times;</a>',"</div>"),r.closeBtn!==!1&&o.querySelector(".close").addEventListener("click",()=>{a(i,!0,d),v(i)}),o}function p(e="primary",t,i={}){u(e)&&(i=e),u(t)&&(i=t,i.variant=e),typeof i=="number"&&(i={variant:e,message:t,timeout:i});let r=i.position??"bottom-right",o=`#${m.parentId}.${r}`;if(!document.querySelector(o)){let s=document.createElement("div");s.id=m.parentId,s.classList.add(i.position??"bottom-right"),document.body.append(s)}let l=i.className||"";i.multiple===!0&&!i.className&&(l=`alert-${Math.floor(Math.random()*1e3)}-${Math.floor(Math.random()*1e3)}`);let c=(i.variant||e).toString(),E=(i.message||t||"Undefined").toString();i.icon!==null&&m.icons[c]&&(i.icon=c);let f=document.querySelector(o),g=w(c,E,l,i);a(l,!1,r),v(l);let h=i.timeout??5e3;if(b(i)&&f.append(g),!b(i)&&f.prepend(g),n(()=>g.classList+=" show ",150),i.backdrop===!0&&(f.classList+=" has-backdrop"),h>0){let s=l||0;window.alertTimer[s]=n(()=>{a(l,!0,r)},h)}return{remove:()=>{a(l,!0,r)}}}function v(e){e&&e.length?clearTimeout(window.alertTimer[e]):Object.keys(window.alertTimer).length&&Object.keys(window.alertTimer).forEach(t=>{(t===0||t==="0")&&(clearTimeout(window.alertTimer[t]),delete window.alertTimer[t])})}function a(e,t,i){let r=document.querySelector(`#${m.parentId}${i?`.${i}`:""}`),o=r?.querySelectorAll(".alert-response-all:not(.has-identifier)");e&&e.length&&(o=r.querySelectorAll("."+e));function d(){r.querySelectorAll(".alert-response-all").length||(window.alertTimer={})}o&&o.length&&(r.classList.remove("has-backdrop"),o.forEach(l=>{if(t){l.classList.remove("show"),n(()=>{l.remove(),n(d,500)},150);return}l.remove(),n(d,500)}))}function $(e,t){a(e,t)}function M(e,t){let i=t||"Oops! Something went wrong.";return e.message&&(i=e.message),e.response&&e.response.status!==200&&(i=e.response.status===404?"Page not found.":e.response.data.UserError??e.response.data.userError??e.message),typeof e=="string"&&!t&&(i=e.toString()),i}function j(e){let t=500;return e.response&&e.response.status&&(t=e.response.status),t}function A(e,t){return console.error(e),M(e,t)}function R(e){return j(e)}function z(){a()}function B(e){if(e&&e.length)for(let t of e)k({message:A(t.error)+t.append,className:`alert-${Math.floor(Math.random()*1e3)}-${Math.floor(Math.random()*1e3)}`,timeout:7e3})}function I(e,t={}){return p(t.variant||"light",{message:e?`${e}<span class="dot-load" />`:'Loading information. Please wait<span class="dot-load"></span>',...t,icon:"loader",timeout:0,closeBtn:t.closeBtn??!1,backdrop:t.backdrop??!0})}function k(e,t={}){return p("danger",e,t)}function O(e,t={}){return p("success",e,t)}function q(e,t={}){return p("warning",e,t)}function H(e,t={}){return p("info",e,t)}var U=["top-left","top-right","bottom-left","bottom-right"];0&&(module.exports={ALERT_POSITIONS,createAlertElement,displayAlert,displayAlertError,displayAlertInfo,displayAlertLoader,displayAlertSuccess,displayAlertWarning,displayErrors,getErrorMessage,getStatusCode,hideAlertLoader,removeDisplayAlert,removeResponseAlert});