-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtoastify-es.js
8 lines (8 loc) · 4.78 KB
/
toastify-es.js
1
2
3
4
5
6
7
8
/*!
* Toastify js 1.12.0
* https://github.com/apvarun/toastify-js
* @license MIT licensed
*
* Copyright (C) 2018 Varun A P
*/
class t{defaults={oldestFirst:!0,text:"Toastify is awesome!",node:void 0,duration:3e3,selector:void 0,callback:function(){},destination:void 0,newWindow:!1,close:!1,gravity:"toastify-top",positionLeft:!1,position:"",backgroundColor:"",avatar:"",className:"",stopOnFocus:!0,onClick:function(){},offset:{x:0,y:0},escapeMarkup:!0,ariaLive:"polite",style:{background:""}};constructor(t){this.version="1.12.0",this.options={},this.toastElement=null,this._rootElement=document.body,this._init(t)}showToast(){if(this.toastElement=this._buildToast(),"string"==typeof this.options.selector?this._rootElement=document.getElementById(this.options.selector):this.options.selector instanceof HTMLElement||this.options.selector instanceof ShadowRoot?this._rootElement=this.options.selector:this._rootElement=document.body,!this._rootElement)throw"Root element is not defined";return this._rootElement.insertBefore(this.toastElement,this._rootElement.firstChild),this._reposition(),this.options.duration>0&&(this.toastElement.timeOutValue=window.setTimeout((()=>{this._removeElement(this.toastElement)}),this.options.duration)),this}hideToast(){this.toastElement.timeOutValue&&clearTimeout(this.toastElement.timeOutValue),this._removeElement(this.toastElement)}_init(t){this.options=Object.assign(this.defaults,t),this.options.backgroundColor&&console.warn('DEPRECATION NOTICE: "backgroundColor" is being deprecated. Please use the "style.background" property.'),this.toastElement=null,this.options.gravity="bottom"===t.gravity?"toastify-bottom":"toastify-top",this.options.stopOnFocus=void 0===t.stopOnFocus||t.stopOnFocus,t.backgroundColor&&(this.options.style.background=t.backgroundColor)}_buildToast(){if(!this.options)throw"Toastify is not initialized";let t=document.createElement("div");t.className=`toastify on ${this.options.className}`,t.className+=` toastify-${this.options.position}`,t.className+=` ${this.options.gravity}`;for(const o in this.options.style)t.style[o]=this.options.style[o];if(this.options.ariaLive&&t.setAttribute("aria-live",this.options.ariaLive),this.options.node&&this.options.node.nodeType===Node.ELEMENT_NODE)t.appendChild(this.options.node);else if(this.options.escapeMarkup?t.innerText=this.options.text:t.innerHTML=this.options.text,""!==this.options.avatar){let o=document.createElement("img");o.src=this.options.avatar,o.className="toastify-avatar","left"==this.options.position?t.appendChild(o):t.insertAdjacentElement("afterbegin",o)}if(!0===this.options.close){let o=document.createElement("button");o.type="button",o.setAttribute("aria-label","Close"),o.className="toast-close",o.innerHTML="✖",o.addEventListener("click",(t=>{t.stopPropagation(),this._removeElement(this.toastElement),window.clearTimeout(this.toastElement.timeOutValue)}));const e=window.innerWidth>0?window.innerWidth:screen.width;"left"==this.options.position&&e>360?t.insertAdjacentElement("afterbegin",o):t.appendChild(o)}if(this.options.stopOnFocus&&this.options.duration>0&&(t.addEventListener("mouseover",(o=>{window.clearTimeout(t.timeOutValue)})),t.addEventListener("mouseleave",(()=>{t.timeOutValue=window.setTimeout((()=>{this._removeElement(t)}),this.options.duration)}))),void 0!==this.options.destination&&t.addEventListener("click",(t=>{t.stopPropagation(),!0===this.options.newWindow?window.open(this.options.destination,"_blank"):window.location=this.options.destination})),"function"==typeof this.options.onClick&&void 0===this.options.destination&&t.addEventListener("click",(t=>{t.stopPropagation(),this.options.onClick()})),"object"==typeof this.options.offset){this._getAxisOffsetAValue("x",this.options);const o=this._getAxisOffsetAValue("y",this.options),e="var(--offset-x)",i="toastify-top"==this.options.gravity?o:`-${o}`;t.style.transform=`translate(${e},${i})`}return t}_removeElement(t){t.className=t.className.replace(" on",""),window.setTimeout((()=>{this.options.node&&this.options.node.parentNode&&this.options.node.parentNode.removeChild(this.options.node),t.parentNode&&t.parentNode.removeChild(t),this.options.callback.call(t),this._reposition()}),400)}_reposition(){let t,o={top:15,bottom:15},e={top:15,bottom:15},i={top:15,bottom:15},s=this._rootElement.querySelectorAll(".toastify");for(let n=0;n<s.length;n++){t=!0===s[n].classList.contains("toastify-top")?"toastify-top":"toastify-bottom";let a=s[n].offsetHeight;t=t.substr(9,t.length-1);let l=15;(window.innerWidth>0?window.innerWidth:screen.width)<=360?(s[n].style[t]=`${i[t]}px`,i[t]+=a+l):!0===s[n].classList.contains("toastify-left")?(s[n].style[t]=`${o[t]}px`,o[t]+=a+l):(s[n].style[t]=`${e[t]}px`,e[t]+=a+l)}}_getAxisOffsetAValue(t,o){return o.offset[t]?isNaN(o.offset[t])?o.offset[t]:`${o.offset[t]}px`:"0px"}}function o(o){return new t(o)}export default o;