Blame | Last modification | View Log | RSS feed
/*!* Toastify js 1.12.0* https://github.com/apvarun/toastify-js* @license MIT licensed** Copyright (C) 2018 Varun A P*/(function (root, factory) {if (typeof module === "object" && module.exports) {module.exports = factory();} else {root.Toastify = factory();}})(this, function (global) {// Object initializationvar Toastify = function (options) {// Returning a new init objectreturn new Toastify.lib.init(options);},// Library versionversion = "1.12.0";// Set the default global optionsToastify.defaults = {oldestFirst: true,text: "Toastify is awesome!",node: undefined,duration: 3000,selector: undefined,callback: function () {},destination: undefined,newWindow: false,close: false,gravity: "toastify-top",positionLeft: false,position: '',backgroundColor: '',avatar: "",className: "",stopOnFocus: true,onClick: function () {},offset: {x: 0, y: 0},escapeMarkup: true,ariaLive: 'polite',style: {background: ''}};// Defining the prototype of the objectToastify.lib = Toastify.prototype = {toastify: version,constructor: Toastify,// Initializing the object with required parametersinit: function (options) {// Verifying and validating the input objectif (!options) {options = {};}// Creating the options objectthis.options = {};this.toastElement = null;// Validating the optionsthis.options.text = options.text || Toastify.defaults.text; // Display messagethis.options.node = options.node || Toastify.defaults.node; // Display content as nodethis.options.duration = options.duration === 0 ? 0 : options.duration || Toastify.defaults.duration; // Display durationthis.options.selector = options.selector || Toastify.defaults.selector; // Parent selectorthis.options.callback = options.callback || Toastify.defaults.callback; // Callback after displaythis.options.destination = options.destination || Toastify.defaults.destination; // On-click destinationthis.options.newWindow = options.newWindow || Toastify.defaults.newWindow; // Open destination in new windowthis.options.close = options.close || Toastify.defaults.close; // Show toast close iconthis.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : Toastify.defaults.gravity; // toast position - top or bottomthis.options.positionLeft = options.positionLeft || Toastify.defaults.positionLeft; // toast position - left or rightthis.options.position = options.position || Toastify.defaults.position; // toast position - left or rightthis.options.backgroundColor = options.backgroundColor || Toastify.defaults.backgroundColor; // toast background colorthis.options.avatar = options.avatar || Toastify.defaults.avatar; // img element src - url or a paththis.options.className = options.className || Toastify.defaults.className; // additional class names for the toastthis.options.stopOnFocus = options.stopOnFocus === undefined ? Toastify.defaults.stopOnFocus : options.stopOnFocus; // stop timeout on focusthis.options.onClick = options.onClick || Toastify.defaults.onClick; // Callback after clickthis.options.offset = options.offset || Toastify.defaults.offset; // toast offsetthis.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : Toastify.defaults.escapeMarkup;this.options.ariaLive = options.ariaLive || Toastify.defaults.ariaLive;this.options.style = options.style || Toastify.defaults.style;if (options.backgroundColor) {this.options.style.background = options.backgroundColor;}// Returning the current object for chaining functionsreturn this;},// Building the DOM elementbuildToast: function () {// Validating if the options are definedif (!this.options) {throw "Toastify is not initialized";}// Creating the DOM objectvar divElement = document.createElement("div");divElement.className = "toastify on " + this.options.className;// Positioning toast to left or right or centerif (!!this.options.position) {divElement.className += " toastify-" + this.options.position;} else {// To be depreciated in further versionsif (this.options.positionLeft === true) {divElement.className += " toastify-left";console.warn('Property `positionLeft` will be depreciated in further versions. Please use `position` instead.')} else {// Default positiondivElement.className += " toastify-right";}}// Assigning gravity of elementdivElement.className += " " + this.options.gravity;if (this.options.backgroundColor) {// This is being deprecated in favor of using the style HTML DOM propertyconsole.warn('DEPRECATION NOTICE: "backgroundColor" is being deprecated. Please use the "style.background" property.');}// Loop through our style object and apply styles to divElementfor (var property in this.options.style) {divElement.style[property] = this.options.style[property];}// Announce the toast to screen readersif (this.options.ariaLive) {divElement.setAttribute('aria-live', this.options.ariaLive)}// Adding the toast message/nodeif (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) {// If we have a valid node, we insert itdivElement.appendChild(this.options.node)} else {if (this.options.escapeMarkup) {divElement.innerText = this.options.text;} else {divElement.innerHTML = this.options.text;}if (this.options.avatar !== "") {var avatarElement = document.createElement("img");avatarElement.src = this.options.avatar;avatarElement.className = "toastify-avatar";if (this.options.position == "left" || this.options.positionLeft === true) {// Adding close icon on the left of contentdivElement.appendChild(avatarElement);} else {// Adding close icon on the right of contentdivElement.insertAdjacentElement("afterbegin", avatarElement);}}}// Adding a close icon to the toastif (this.options.close === true) {// Create a span for close elementvar closeElement = document.createElement("button");closeElement.type = "button";closeElement.setAttribute("aria-label", "Close");closeElement.className = "toast-close";closeElement.innerHTML = "✖";// Triggering the removal of toast from DOM on close clickcloseElement.addEventListener("click",function (event) {event.stopPropagation();this.removeElement(this.toastElement);window.clearTimeout(this.toastElement.timeOutValue);}.bind(this));//Calculating screen widthvar width = window.innerWidth > 0 ? window.innerWidth : screen.width;// Adding the close icon to the toast element// Display on the right if screen width is less than or equal to 360pxif ((this.options.position == "left" || this.options.positionLeft === true) && width > 360) {// Adding close icon on the left of contentdivElement.insertAdjacentElement("afterbegin", closeElement);} else {// Adding close icon on the right of contentdivElement.appendChild(closeElement);}}// Clear timeout while toast is focusedif (this.options.stopOnFocus && this.options.duration > 0) {var self = this;// stop countdowndivElement.addEventListener("mouseover",function (event) {window.clearTimeout(divElement.timeOutValue);})// add back the timeoutdivElement.addEventListener("mouseleave",function () {divElement.timeOutValue = window.setTimeout(function () {// Remove the toast from DOMself.removeElement(divElement);},self.options.duration)})}// Adding an on-click destination pathif (typeof this.options.destination !== "undefined") {divElement.addEventListener("click",function (event) {event.stopPropagation();if (this.options.newWindow === true) {window.open(this.options.destination, "_blank");} else {window.location = this.options.destination;}}.bind(this));}if (typeof this.options.onClick === "function" && typeof this.options.destination === "undefined") {divElement.addEventListener("click",function (event) {event.stopPropagation();this.options.onClick();}.bind(this));}// Adding offsetif (typeof this.options.offset === "object") {var x = getAxisOffsetAValue("x", this.options);var y = getAxisOffsetAValue("y", this.options);var xOffset = this.options.position == "left" ? x : "-" + x;var yOffset = this.options.gravity == "toastify-top" ? y : "-" + y;divElement.style.transform = "translate(" + xOffset + "," + yOffset + ")";}// Returning the generated elementreturn divElement;},// Displaying the toastshowToast: function () {// Creating the DOM object for the toastthis.toastElement = this.buildToast();// Getting the root element to with the toast needs to be addedvar rootElement;if (typeof this.options.selector === "string") {rootElement = document.getElementById(this.options.selector);} else if (this.options.selector instanceof HTMLElement || (typeof ShadowRoot !== 'undefined' && this.options.selector instanceof ShadowRoot)) {rootElement = this.options.selector;} else {rootElement = document.body;}// Validating if root element is present in DOMif (!rootElement) {throw "Root element is not defined";}// Adding the DOM elementvar elementToInsert = Toastify.defaults.oldestFirst ? rootElement.firstChild : rootElement.lastChild;rootElement.insertBefore(this.toastElement, elementToInsert);// Repositioning the toasts in case multiple toasts are presentToastify.reposition();if (this.options.duration > 0) {this.toastElement.timeOutValue = window.setTimeout(function () {// Remove the toast from DOMthis.removeElement(this.toastElement);}.bind(this),this.options.duration); // Binding `this` for function invocation}// Supporting function chainingreturn this;},hideToast: function () {if (this.toastElement.timeOutValue) {clearTimeout(this.toastElement.timeOutValue);}this.removeElement(this.toastElement);},// Removing the element from the DOMremoveElement: function (toastElement) {// Hiding the element// toastElement.classList.remove("on");toastElement.className = toastElement.className.replace(" on", "");// Removing the element from DOM after transition endwindow.setTimeout(function () {// remove options node if anyif (this.options.node && this.options.node.parentNode) {this.options.node.parentNode.removeChild(this.options.node);}// Remove the element from the DOM, only when the parent node was not removed before.if (toastElement.parentNode) {toastElement.parentNode.removeChild(toastElement);}// Calling the callback functionthis.options.callback.call(toastElement);// Repositioning the toasts againToastify.reposition();}.bind(this),400); // Binding `this` for function invocation},};// Positioning the toasts on the DOMToastify.reposition = function () {// Top margins with gravityvar topLeftOffsetSize = {top: 15,bottom: 15,};var topRightOffsetSize = {top: 15,bottom: 15,};var offsetSize = {top: 15,bottom: 15,};// Get all toast messages on the DOMvar allToasts = document.getElementsByClassName("toastify");var classUsed;// Modifying the position of each toast elementfor (var i = 0; i < allToasts.length; i++) {// Getting the applied gravityif (containsClass(allToasts[i], "toastify-top") === true) {classUsed = "toastify-top";} else {classUsed = "toastify-bottom";}var height = allToasts[i].offsetHeight;classUsed = classUsed.substr(9, classUsed.length - 1)// Spacing between toastsvar offset = 15;var width = window.innerWidth > 0 ? window.innerWidth : screen.width;// Show toast in center if screen with less than or equal to 360pxif (width <= 360) {// Setting the positionallToasts[i].style[classUsed] = offsetSize[classUsed] + "px";offsetSize[classUsed] += height + offset;} else {if (containsClass(allToasts[i], "toastify-left") === true) {// Setting the positionallToasts[i].style[classUsed] = topLeftOffsetSize[classUsed] + "px";topLeftOffsetSize[classUsed] += height + offset;} else {// Setting the positionallToasts[i].style[classUsed] = topRightOffsetSize[classUsed] + "px";topRightOffsetSize[classUsed] += height + offset;}}}// Supporting function chainingreturn this;};// Helper function to get offset.function getAxisOffsetAValue(axis, options) {if (options.offset[axis]) {if (isNaN(options.offset[axis])) {return options.offset[axis];} else {return options.offset[axis] + 'px';}}return '0px';}function containsClass(elem, yourClass) {if (!elem || typeof yourClass !== "string") {return false;} else if (elem.className &&elem.className.trim().split(/\s+/gi).indexOf(yourClass) > -1) {return true;} else {return false;}}// Setting up the prototype for the init objectToastify.lib.init.prototype = Toastify.lib;// Returning the Toastify function to be assigned to the window object/modulereturn Toastify;});