-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathnotyf.min.js
16 lines (14 loc) · 9.09 KB
/
notyf.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign=function(){return __assign=Object.assign||function(t){for(var i,e=1,n=arguments.length;e<n;e++)for(var o in i=arguments[e])Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o]);return t},__assign.apply(this,arguments)},NotyfNotification=function(){function t(t){this.options=t,this.listeners={}}return t.prototype.on=function(t,i){var e=this.listeners[t]||[];this.listeners[t]=e.concat([i])},t.prototype.triggerEvent=function(t,i){var e=this;(this.listeners[t]||[]).forEach((function(t){return t({target:e,event:i})}))},t}();!function(t){t[t.Add=0]="Add",t[t.Remove=1]="Remove"}(exports.NotyfArrayEvent||(exports.NotyfArrayEvent={}));var NotyfArray=function(){function t(){this.notifications=[]}return t.prototype.push=function(t){this.notifications.push(t),this.updateFn(t,exports.NotyfArrayEvent.Add,this.notifications)},t.prototype.splice=function(t,i){var e=this.notifications.splice(t,i)[0];return this.updateFn(e,exports.NotyfArrayEvent.Remove,this.notifications),e},t.prototype.indexOf=function(t){return this.notifications.indexOf(t)},t.prototype.onUpdate=function(t){this.updateFn=t},t}();!function(t){t.Dismiss="dismiss",t.Click="click"}(exports.NotyfEvent||(exports.NotyfEvent={}));var DEFAULT_OPTIONS={types:[{type:"success",className:"notyf__toast--success",backgroundColor:"#3dc763",icon:{className:"notyf__icon--success",tagName:"i"}},{type:"error",className:"notyf__toast--error",backgroundColor:"#ed3d3d",icon:{className:"notyf__icon--error",tagName:"i"}}],duration:2e3,ripple:!0,position:{x:"right",y:"bottom"},dismissible:!1},NotyfView=function(){function t(){this.notifications=[],this.events={},this.X_POSITION_FLEX_MAP={left:"flex-start",center:"center",right:"flex-end"},this.Y_POSITION_FLEX_MAP={top:"flex-start",center:"center",bottom:"flex-end"};var t=document.createDocumentFragment(),i=this._createHTMLElement({tagName:"div",className:"notyf"});t.appendChild(i),document.body.appendChild(t),this.container=i,this.animationEndEventName=this._getAnimationEndEventName(),this._createA11yContainer()}return t.prototype.on=function(t,i){var e;this.events=__assign(__assign({},this.events),((e={})[t]=i,e))},t.prototype.update=function(t,i){i===exports.NotyfArrayEvent.Add?this.addNotification(t):i===exports.NotyfArrayEvent.Remove&&this.removeNotification(t)},t.prototype.removeNotification=function(t){var i,e,n=this,o=this._popRenderedNotification(t);o&&((i=o.node).classList.add("notyf__toast--disappear"),i.addEventListener(this.animationEndEventName,e=function(t){t.target===i&&(i.removeEventListener(n.animationEndEventName,e),n.container.removeChild(i))}))},t.prototype.addNotification=function(t){var i=this._renderNotification(t);this.notifications.push({notification:t,node:i}),this._announce(t.options.message||"Notification")},t.prototype._renderNotification=function(t){var i,e=this._buildNotificationCard(t),n=t.options.className;return n&&(i=e.classList).add.apply(i,n.split(" ")),this.container.appendChild(e),e},t.prototype._popRenderedNotification=function(t){for(var i=-1,e=0;e<this.notifications.length&&i<0;e++)this.notifications[e].notification===t&&(i=e);if(-1!==i)return this.notifications.splice(i,1)[0]},t.prototype.getXPosition=function(t){var i;return(null===(i=null==t?void 0:t.position)||void 0===i?void 0:i.x)||"right"},t.prototype.getYPosition=function(t){var i;return(null===(i=null==t?void 0:t.position)||void 0===i?void 0:i.y)||"bottom"},t.prototype.adjustContainerAlignment=function(t){var i=this.X_POSITION_FLEX_MAP[this.getXPosition(t)],e=this.Y_POSITION_FLEX_MAP[this.getYPosition(t)],n=this.container.style;n.setProperty("justify-content",e),n.setProperty("align-items",i)},t.prototype._buildNotificationCard=function(t){var i=this,e=t.options,n=e.icon;this.adjustContainerAlignment(e);var o=this._createHTMLElement({tagName:"div",className:"notyf__toast"}),s=this._createHTMLElement({tagName:"div",className:"notyf__ripple"}),r=this._createHTMLElement({tagName:"div",className:"notyf__wrapper"}),a=this._createHTMLElement({tagName:"div",className:"notyf__message"});a.innerHTML=e.message||"";var c=e.background||e.backgroundColor;if(n){var p=this._createHTMLElement({tagName:"div",className:"notyf__icon"});if(("string"==typeof n||n instanceof String)&&(p.innerHTML=new String(n).valueOf()),"object"==typeof n){var f=n.tagName,d=void 0===f?"i":f,l=n.className,u=n.text,y=n.color,v=void 0===y?c:y,h=this._createHTMLElement({tagName:d,className:l,text:u});v&&(h.style.color=v),p.appendChild(h)}r.appendChild(p)}if(r.appendChild(a),o.appendChild(r),c&&(e.ripple?(s.style.background=c,o.appendChild(s)):o.style.background=c),e.dismissible){var m=this._createHTMLElement({tagName:"div",className:"notyf__dismiss"}),_=this._createHTMLElement({tagName:"button",className:"notyf__dismiss-btn"});m.appendChild(_),r.appendChild(m),o.classList.add("notyf__toast--dismissible"),_.addEventListener("click",(function(e){var n,o;null===(o=(n=i.events)[exports.NotyfEvent.Dismiss])||void 0===o||o.call(n,{target:t,event:e}),e.stopPropagation()}))}o.addEventListener("click",(function(e){var n,o;return null===(o=(n=i.events)[exports.NotyfEvent.Click])||void 0===o?void 0:o.call(n,{target:t,event:e})}));var N="top"===this.getYPosition(e)?"upper":"lower";return o.classList.add("notyf__toast--"+N),o},t.prototype._createHTMLElement=function(t){var i=t.tagName,e=t.className,n=t.text,o=document.createElement(i);return e&&(o.className=e),o.textContent=n||null,o},t.prototype._createA11yContainer=function(){var t=this._createHTMLElement({tagName:"div",className:"notyf-announcer"});t.setAttribute("aria-atomic","true"),t.setAttribute("aria-live","polite"),t.style.border="0",t.style.clip="rect(0 0 0 0)",t.style.height="1px",t.style.margin="-1px",t.style.overflow="hidden",t.style.padding="0",t.style.position="absolute",t.style.width="1px",t.style.outline="0",document.body.appendChild(t),this.a11yContainer=t},t.prototype._announce=function(t){var i=this;this.a11yContainer.textContent="",setTimeout((function(){i.a11yContainer.textContent=t}),100)},t.prototype._getAnimationEndEventName=function(){var t,i=document.createElement("_fake"),e={MozTransition:"animationend",OTransition:"oAnimationEnd",WebkitTransition:"webkitAnimationEnd",transition:"animationend"};for(t in e)if(void 0!==i.style[t])return e[t];return"animationend"},t}(),Notyf=function(){function t(t){var i=this;this.dismiss=this._removeNotification,this.notifications=new NotyfArray,this.view=new NotyfView;var e=this.registerTypes(t);this.options=__assign(__assign({},DEFAULT_OPTIONS),t),this.options.types=e,this.notifications.onUpdate((function(t,e){return i.view.update(t,e)})),this.view.on(exports.NotyfEvent.Dismiss,(function(t){var e=t.target,n=t.event;i._removeNotification(e),e.triggerEvent(exports.NotyfEvent.Dismiss,n)})),this.view.on(exports.NotyfEvent.Click,(function(t){var i=t.target,e=t.event;return i.triggerEvent(exports.NotyfEvent.Click,e)}))}return t.prototype.error=function(t){var i=this.normalizeOptions("error",t);return this.open(i)},t.prototype.success=function(t){var i=this.normalizeOptions("success",t);return this.open(i)},t.prototype.open=function(t){var i=this.options.types.find((function(i){return i.type===t.type}))||{},e=__assign(__assign({},i),t);this.assignProps(["ripple","position","dismissible"],e);var n=new NotyfNotification(e);return this._pushNotification(n),n},t.prototype.dismissAll=function(){for(;this.notifications.splice(0,1););},t.prototype.assignProps=function(t,i){var e=this;t.forEach((function(t){i[t]=null==i[t]?e.options[t]:i[t]}))},t.prototype._pushNotification=function(t){var i=this;this.notifications.push(t);var e=void 0!==t.options.duration?t.options.duration:this.options.duration;e&&setTimeout((function(){return i._removeNotification(t)}),e)},t.prototype._removeNotification=function(t){var i=this.notifications.indexOf(t);-1!==i&&this.notifications.splice(i,1)},t.prototype.normalizeOptions=function(t,i){var e={type:t};return"string"==typeof i?e.message=i:"object"==typeof i&&(e=__assign(__assign({},e),i)),e},t.prototype.registerTypes=function(t){var i=(t&&t.types||[]).slice();return DEFAULT_OPTIONS.types.map((function(t){var e=-1;i.forEach((function(i,n){i.type===t.type&&(e=n)}));var n=-1!==e?i.splice(e,1)[0]:{};return __assign(__assign({},t),n)})).concat(i)},t}();exports.DEFAULT_OPTIONS=DEFAULT_OPTIONS,exports.Notyf=Notyf,exports.NotyfArray=NotyfArray,exports.NotyfNotification=NotyfNotification,exports.NotyfView=NotyfView;