diff --git a/src/toastify-es.js b/src/toastify-es.js index e036994..3b0fd93 100644 --- a/src/toastify-es.js +++ b/src/toastify-es.js @@ -32,426 +32,426 @@ class Toastify { - 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, - style: { background: "" }, - }; - - constructor(options) { - /** - * The version of Toastify - * @type {string} - * @public - */ - this.version = "1.11.2"; - - /** - * The configuration object to configure Toastify - * @type {ToastifyConfigurationObject} - * @public - */ - this.options = {}; - - /** - * The element that is the Toast - * @type {Element} - * @public - */ - this.toastElement = null; - - /** - * The root element that contains all the toasts - * @type {Element} - * @private - */ - this._rootElement = document.body; - - this._init(options); - } - + 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, + style: { background: "" }, + }; + + constructor(options) { /** - * Display the toast + * The version of Toastify + * @type {string} * @public */ - showToast() { - // Creating the DOM object for the toast - this.toastElement = this._buildToast(); - - // Getting the root element to with the toast needs to be added - if (typeof this.options.selector === "string") { - this._rootElement = document.getElementById(this.options.selector); - } else if (this.options.selector instanceof HTMLElement || this.options.selector instanceof ShadowRoot) { - this._rootElement = this.options.selector; - } else { - this._rootElement = document.body; - } + this.version = "1.11.2"; - // Validating if root element is present in DOM - if (!this._rootElement) { - throw "Root element is not defined"; - } - - // Adding the DOM element - this._rootElement.insertBefore(this.toastElement, this._rootElement.firstChild); - - // Repositioning the toasts in case multiple toasts are present - this._reposition(); - - if (this.options.duration > 0) { - this.toastElement.timeOutValue = window.setTimeout( - () => { - // Remove the toast from DOM - this._removeElement(this.toastElement); - }, - this.options.duration - ); // Binding `this` for function invocation - } - - // Supporting function chaining - return this; - } + /** + * The configuration object to configure Toastify + * @type {ToastifyConfigurationObject} + * @public + */ + this.options = {}; /** - * Hide the toast + * The element that is the Toast + * @type {Element} * @public */ - hideToast() { - if (this.toastElement.timeOutValue) { - clearTimeout(this.toastElement.timeOutValue); - } - this._removeElement(this.toastElement); - } + this.toastElement = null; /** - * Init the Toastify class - * @param {ToastifyConfigurationObject} options - The configuration object to configure Toastify - * @param {string} [options.text=Hi there!] - Message to be displayed in the toast - * @param {Element} [options.node] - Provide a node to be mounted inside the toast. node takes higher precedence over text - * @param {number} [options.duration=3000] - Duration for which the toast should be displayed. -1 for permanent toast - * @param {string} [options.selector] - CSS Selector on which the toast should be added - * @param {url} [options.destination] - URL to which the browser should be navigated on click of the toast - * @param {boolean} [options.newWindow=false] - Decides whether the destination should be opened in a new window or not - * @param {boolean} [options.close=false] - To show the close icon or not - * @param {string} [options.gravity=toastify-top] - To show the toast from top or bottom - * @param {string} [options.position=right] - To show the toast on left or right - * @param {string} [options.backgroundColor] - Sets the background color of the toast (To be deprecated) - * @param {url} [options.avatar] - Image/icon to be shown before text - * @param {string} [options.className] - Ability to provide custom class name for further customization - * @param {boolean} [options.stopOnFocus] - To stop timer when hovered over the toast (Only if duration is set) - * @param {Function} [options.callback] - Invoked when the toast is dismissed - * @param {Function} [options.onClick] - Invoked when the toast is clicked - * @param {Object} [options.offset] - Ability to add some offset to axis - * @param {boolean} [options.escapeMarkup=true] - Toggle the default behavior of escaping HTML markup - * @param {Object} [options.style] - Use the HTML DOM style property to add styles to toast + * The root element that contains all the toasts + * @type {Element} * @private */ - _init(options) { + this._rootElement = document.body; - // Setting defaults - this.options = Object.assign(this.defaults, options); + this._init(options); + } - if (this.options.backgroundColor) { - // This is being deprecated in favor of using the style HTML DOM property - console.warn('DEPRECATION NOTICE: "backgroundColor" is being deprecated. Please use the "style.background" property.'); - } + /** + * Display the toast + * @public + */ + showToast() { + /* Creating the DOM object for the toast */ + this.toastElement = this._buildToast(); + + /* Getting the root element to with the toast needs to be added */ + if (typeof this.options.selector === "string") { + this._rootElement = document.getElementById(this.options.selector); + } else if (this.options.selector instanceof HTMLElement || this.options.selector instanceof ShadowRoot) { + this._rootElement = this.options.selector; + } else { + this._rootElement = document.body; + } - this.toastElement = null; + /* Validating if root element is present in DOM */ + if (!this._rootElement) { + throw "Root element is not defined"; + } - this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : "toastify-top"; // toast position - top or bottom - this.options.stopOnFocus = options.stopOnFocus === undefined ? true : options.stopOnFocus; // stop timeout on focus - if(options.backgroundColor) { - this.options.style.background = options.backgroundColor; - } + /* Adding the DOM element */ + this._rootElement.insertBefore(this.toastElement, this._rootElement.firstChild); + + /* Repositioning the toasts in case multiple toasts are present */ + this._reposition(); + + if (this.options.duration > 0) { + this.toastElement.timeOutValue = window.setTimeout( + () => { + /* Remove the toast from DOM */ + this._removeElement(this.toastElement); + }, + this.options.duration + ); /* Binding `this` for function invocation */ } - /** - * Build the Toastify element - * @returns {Element} - * @private - */ - _buildToast() { - // Validating if the options are defined - if (!this.options) { - throw "Toastify is not initialized"; - } + /* Supporting function chaining */ + return this; + } - // Creating the DOM object - let divElement = document.createElement("div"); - divElement.className = `toastify on ${this.options.className}`; + /** + * Hide the toast + * @public + */ + hideToast() { + if (this.toastElement.timeOutValue) { + clearTimeout(this.toastElement.timeOutValue); + } + this._removeElement(this.toastElement); + } - // Positioning toast to left or right or center (default right) - divElement.className += ` toastify-${this.options.position}`; + /** + * Init the Toastify class + * @param {ToastifyConfigurationObject} options - The configuration object to configure Toastify + * @param {string} [options.text=Hi there!] - Message to be displayed in the toast + * @param {Element} [options.node] - Provide a node to be mounted inside the toast. node takes higher precedence over text + * @param {number} [options.duration=3000] - Duration for which the toast should be displayed. -1 for permanent toast + * @param {string} [options.selector] - CSS Selector on which the toast should be added + * @param {url} [options.destination] - URL to which the browser should be navigated on click of the toast + * @param {boolean} [options.newWindow=false] - Decides whether the destination should be opened in a new window or not + * @param {boolean} [options.close=false] - To show the close icon or not + * @param {string} [options.gravity=toastify-top] - To show the toast from top or bottom + * @param {string} [options.position=right] - To show the toast on left or right + * @param {string} [options.backgroundColor] - Sets the background color of the toast (To be deprecated) + * @param {url} [options.avatar] - Image/icon to be shown before text + * @param {string} [options.className] - Ability to provide custom class name for further customization + * @param {boolean} [options.stopOnFocus] - To stop timer when hovered over the toast (Only if duration is set) + * @param {Function} [options.callback] - Invoked when the toast is dismissed + * @param {Function} [options.onClick] - Invoked when the toast is clicked + * @param {Object} [options.offset] - Ability to add some offset to axis + * @param {boolean} [options.escapeMarkup=true] - Toggle the default behavior of escaping HTML markup + * @param {Object} [options.style] - Use the HTML DOM style property to add styles to toast + * @private + */ + _init(options) { + + /* Setting defaults */ + this.options = Object.assign(this.defaults, options); + + if (this.options.backgroundColor) { + /* This is being deprecated in favor of using the style HTML DOM property */ + console.warn('DEPRECATION NOTICE: "backgroundColor" is being deprecated. Please use the "style.background" property.'); + } - // Assigning gravity of element - divElement.className += ` ${this.options.gravity}`; + this.toastElement = null; - // Loop through our style object and apply styles to divElement - for (const property in this.options.style) { - divElement.style[property] = this.options.style[property]; - } + this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : "toastify-top"; /* toast position - top or bottom */ + this.options.stopOnFocus = options.stopOnFocus === undefined ? true : options.stopOnFocus; /* stop timeout on focus */ + if (options.backgroundColor) { + this.options.style.background = options.backgroundColor; + } + } - // Adding the toast message/node - if (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) { - // If we have a valid node, we insert it - divElement.appendChild(this.options.node) - } else { - if (this.options.escapeMarkup) { - divElement.innerText = this.options.text; - } else { - divElement.innerHTML = this.options.text; - } + /** + * Build the Toastify element + * @returns {Element} + * @private + */ + _buildToast() { + /* Validating if the options are defined */ + if (!this.options) { + throw "Toastify is not initialized"; + } - if (this.options.avatar !== "") { - let avatarElement = document.createElement("img"); - avatarElement.src = this.options.avatar; + /* Creating the DOM object */ + let divElement = document.createElement("div"); + divElement.className = `toastify on ${this.options.className}`; - avatarElement.className = "toastify-avatar"; + /* Positioning toast to left or right or center (default right) */ + divElement.className += ` toastify-${this.options.position}`; - if (this.options.position == "left") { - // Adding close icon on the left of content - divElement.appendChild(avatarElement); - } else { - // Adding close icon on the right of content - divElement.insertAdjacentElement("afterbegin", avatarElement); - } - } + /* Assigning gravity of element */ + divElement.className += ` ${this.options.gravity}`; + + /* Loop through our style object and apply styles to divElement */ + for (const property in this.options.style) { + divElement.style[property] = this.options.style[property]; + } + + /* Adding the toast message/node */ + if (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) { + /* If we have a valid node, we insert it */ + divElement.appendChild(this.options.node) + } else { + if (this.options.escapeMarkup) { + divElement.innerText = this.options.text; + } else { + divElement.innerHTML = this.options.text; } - // Adding a close icon to the toast - if (this.options.close === true) { - // Create a span for close element - let closeElement = document.createElement("span"); - closeElement.innerHTML = "✖"; - - closeElement.className = "toast-close"; - - // Triggering the removal of toast from DOM on close click - closeElement.addEventListener( - "click", - (event) => { - event.stopPropagation(); - this._removeElement(this.toastElement); - window.clearTimeout(this.toastElement.timeOutValue); - } - ); + if (this.options.avatar !== "") { + let avatarElement = document.createElement("img"); + avatarElement.src = this.options.avatar; - //Calculating screen width - const width = window.innerWidth > 0 ? window.innerWidth : screen.width; + avatarElement.className = "toastify-avatar"; - // Adding the close icon to the toast element - // Display on the right if screen width is less than or equal to 360px - if ((this.options.position == "left") && width > 360) { - // Adding close icon on the left of content - divElement.insertAdjacentElement("afterbegin", closeElement); + if (this.options.position == "left") { + /* Adding close icon on the left of content */ + divElement.appendChild(avatarElement); } else { - // Adding close icon on the right of content - divElement.appendChild(closeElement); + /* Adding close icon on the right of content */ + divElement.insertAdjacentElement("afterbegin", avatarElement); } } + } - // Clear timeout while toast is focused - if (this.options.stopOnFocus && this.options.duration > 0) { - // stop countdown - divElement.addEventListener( - "mouseover", - (event) => { - window.clearTimeout(divElement.timeOutValue); - } - ) - // add back the timeout - divElement.addEventListener( - "mouseleave", - () => { - divElement.timeOutValue = window.setTimeout( - () => { - // Remove the toast from DOM - this._removeElement(divElement); - }, - this.options.duration - ) - } - ) - } + /* Adding a close icon to the toast */ + if (this.options.close === true) { + /* Create a span for close element */ + let closeElement = document.createElement("span"); + closeElement.innerHTML = "✖"; + + closeElement.className = "toast-close"; + + /* Triggering the removal of toast from DOM on close click */ + closeElement.addEventListener( + "click", + (event) => { + event.stopPropagation(); + this._removeElement(this.toastElement); + window.clearTimeout(this.toastElement.timeOutValue); + } + ); - // Adding an on-click destination path - if (typeof this.options.destination !== "undefined") { - divElement.addEventListener( - "click", - (event) => { - event.stopPropagation(); - if (this.options.newWindow === true) { - window.open(this.options.destination, "_blank"); - } else { - window.location = this.options.destination; - } - } - ); + /* Calculating screen width */ + const 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 360px */ + if ((this.options.position == "left") && width > 360) { + /* Adding close icon on the left of content */ + divElement.insertAdjacentElement("afterbegin", closeElement); + } else { + /* Adding close icon on the right of content */ + divElement.appendChild(closeElement); } + } + + /* Clear timeout while toast is focused */ + if (this.options.stopOnFocus && this.options.duration > 0) { + /* stop countdown */ + divElement.addEventListener( + "mouseover", + (event) => { + window.clearTimeout(divElement.timeOutValue); + } + ); + /* add back the timeout */ + divElement.addEventListener( + "mouseleave", + () => { + divElement.timeOutValue = window.setTimeout( + () => { + /* Remove the toast from DOM */ + this._removeElement(divElement); + }, + this.options.duration + ) + } + ); + } - if (typeof this.options.onClick === "function" && typeof this.options.destination === "undefined") { - divElement.addEventListener( - "click", - (event) => { - event.stopPropagation(); - this.options.onClick(); + /* Adding an on-click destination path */ + if (typeof this.options.destination !== "undefined") { + divElement.addEventListener( + "click", + (event) => { + event.stopPropagation(); + if (this.options.newWindow === true) { + window.open(this.options.destination, "_blank"); + } else { + window.location = this.options.destination; } - ); - } + } + ); + } - // Adding offset - if (typeof this.options.offset === "object") { + if (typeof this.options.onClick === "function" && typeof this.options.destination === "undefined") { + divElement.addEventListener( + "click", + (event) => { + event.stopPropagation(); + this.options.onClick(); + } + ); + } - const x = this._getAxisOffsetAValue("x", this.options); - const y = this._getAxisOffsetAValue("y", this.options); + /* Adding offset */ + if (typeof this.options.offset === "object") { - const xOffset = this.options.position == "left" ? x : `-${x}`; - const yOffset = this.options.gravity == "toastify-top" ? y : `-${y}`; + const x = this._getAxisOffsetAValue("x", this.options); + const y = this._getAxisOffsetAValue("y", this.options); - divElement.style.transform = `translate(${xOffset},${yOffset})`; + const xOffset = this.options.position == "left" ? x : `-${x}`; + const yOffset = this.options.gravity == "toastify-top" ? y : `-${y}`; - } + divElement.style.transform = `translate(${xOffset},${yOffset})`; - // Returning the generated element - return divElement; } - /** - * Remove the toast from the DOM - * @param {Element} toastElement - */ - _removeElement(toastElement) { - // Hiding the element - toastElement.className = toastElement.className.replace(" on", ""); + /* Returning the generated element */ + return divElement; + } - // Removing the element from DOM after transition end - window.setTimeout( - () => { - // remove options node if any - if (this.options.node && this.options.node.parentNode) { - this.options.node.parentNode.removeChild(this.options.node); - } + /** + * Remove the toast from the DOM + * @param {Element} toastElement + */ + _removeElement(toastElement) { + /* Hiding the element */ + toastElement.className = toastElement.className.replace(" on", ""); + + /* Removing the element from DOM after transition end */ + window.setTimeout( + () => { + /* remove options node if any */ + if (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); - } + /* 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 function - this.options.callback.call(toastElement); + /* Calling the callback function */ + this.options.callback.call(toastElement); - // Repositioning the toasts again - this._reposition(); - }, - 400 - ); // Binding `this` for function invocation - } + /* Repositioning the toasts again */ + this._reposition(); + }, + 400 + ); /* Binding `this` for function invocation */ + } - /** - * Position the toast on the DOM - * @private - */ - _reposition() { - - // Top margins with gravity - let topLeftOffsetSize = { - top: 15, - bottom: 15, - }; - let topRightOffsetSize = { - top: 15, - bottom: 15, - }; - let offsetSize = { - top: 15, - bottom: 15, - }; - - // Get all toast messages that have been added to the container (selector) - let allToasts = this._rootElement.querySelectorAll(".toastify"); - - let classUsed; - - // Modifying the position of each toast element - for (let i = 0; i < allToasts.length; i++) { - // Getting the applied gravity - if (allToasts[i].classList.contains("toastify-top") === true) { - classUsed = "toastify-top"; - } else { - classUsed = "toastify-bottom"; - } + /** + * Position the toast on the DOM + * @private + */ + _reposition() { - let height = allToasts[i].offsetHeight; - classUsed = classUsed.substr(9, classUsed.length - 1) - // Spacing between toasts - let offset = 15; + /* Top margins with gravity */ + let topLeftOffsetSize = { + top: 15, + bottom: 15, + }; + let topRightOffsetSize = { + top: 15, + bottom: 15, + }; + let offsetSize = { + top: 15, + bottom: 15, + }; - let width = window.innerWidth > 0 ? window.innerWidth : screen.width; + /* Get all toast messages that have been added to the container (selector) */ + let allToasts = this._rootElement.querySelectorAll(".toastify"); - // Show toast in center if screen with less than or equal to 360px - if (width <= 360) { - // Setting the position - allToasts[i].style[classUsed] = `${offsetSize[classUsed]}px`; + let classUsed; - offsetSize[classUsed] += height + offset; - } else { - if (allToasts[i].classList.contains("toastify-left") === true) { - // Setting the position - allToasts[i].style[classUsed] = `${topLeftOffsetSize[classUsed]}px`; + /* Modifying the position of each toast element */ + for (let i = 0; i < allToasts.length; i++) { + /* Getting the applied gravity */ + if (allToasts[i].classList.contains("toastify-top") === true) { + classUsed = "toastify-top"; + } else { + classUsed = "toastify-bottom"; + } - topLeftOffsetSize[classUsed] += height + offset; - } else { - // Setting the position - allToasts[i].style[classUsed] = `${topRightOffsetSize[classUsed]}px`; + let height = allToasts[i].offsetHeight; + classUsed = classUsed.substr(9, classUsed.length - 1); + /* Spacing between toasts */ + let offset = 15; - topRightOffsetSize[classUsed] += height + offset; - } - } - } - } + let width = window.innerWidth > 0 ? window.innerWidth : screen.width; - /** - * Helper function to get offset - * @param {string} axis - 'x' or 'y' - * @param {ToastifyConfigurationObject} options - The options object containing the offset object - */ - _getAxisOffsetAValue(axis, options) { + /* Show toast in center if screen with less than or equal to 360px */ + if (width <= 360) { + /* Setting the position */ + allToasts[i].style[classUsed] = `${offsetSize[classUsed]}px`; - if (options.offset[axis]) { - if (isNaN(options.offset[axis])) { - return options.offset[axis]; + offsetSize[classUsed] += height + offset; + } else { + if (allToasts[i].classList.contains("toastify-left") === true) { + /* Setting the position */ + allToasts[i].style[classUsed] = `${topLeftOffsetSize[classUsed]}px`; + + topLeftOffsetSize[classUsed] += height + offset; } else { - return `${options.offset[axis]}px`; + /* Setting the position */ + allToasts[i].style[classUsed] = `${topRightOffsetSize[classUsed]}px`; + + topRightOffsetSize[classUsed] += height + offset; } } + } + } - return '0px'; + /** + * Helper function to get offset + * @param {string} axis - 'x' or 'y' + * @param {ToastifyConfigurationObject} options - The options object containing the offset object + */ + _getAxisOffsetAValue(axis, options) { + if (options.offset[axis]) { + if (isNaN(options.offset[axis])) { + return options.offset[axis]; + } else { + return `${options.offset[axis]}px`; + } } + return '0px'; + } +} - // Returning the Toastify function to be assigned to the window object/module - function StartToastifyInstance(options) { - return new Toastify(options); - } - export default StartToastifyInstance; +/* Returning the Toastify function to be assigned to the window object/module */ +function StartToastifyInstance(options) { + return new Toastify(options); +} + +export default StartToastifyInstance; diff --git a/src/toastify.css b/src/toastify.css index 5fe826a..94e0310 100644 --- a/src/toastify.css +++ b/src/toastify.css @@ -29,7 +29,11 @@ .toast-close { opacity: 0.4; - padding: 0 5px; + padding: 0 15px; +} + +.toast-close:hover { + opacity: 1; } .toastify-right { diff --git a/src/toastify.js b/src/toastify.js index a2c02b8..ce237a1 100644 --- a/src/toastify.js +++ b/src/toastify.js @@ -5,22 +5,23 @@ * * Copyright (C) 2018 Varun A P */ -(function(root, factory) { + +(function (root, factory) { if (typeof module === "object" && module.exports) { module.exports = factory(); } else { root.Toastify = factory(); } -})(this, function(global) { - // Object initialization - var Toastify = function(options) { - // Returning a new init object - return new Toastify.lib.init(options); - }, - // Library version +})(this, function (global) { + /* Object initialization */ + var Toastify = function (options) { + /* Returning a new init object */ + return new Toastify.lib.init(options); + }, + /* Library version */ version = "1.11.2"; - // Set the default global options + /* Set the default global options */ Toastify.defaults = { oldestFirst: true, text: "Toastify is awesome!", @@ -41,98 +42,98 @@ stopOnFocus: true, onClick: function () { }, - offset: {x: 0, y: 0}, + offset: { x: 0, y: 0 }, escapeMarkup: true, - style: {background: ''} + style: { background: '' } }; - // Defining the prototype of the object + /* Defining the prototype of the object */ Toastify.lib = Toastify.prototype = { toastify: version, constructor: Toastify, - // Initializing the object with required parameters - init: function(options) { - // Verifying and validating the input object + /* Initializing the object with required parameters */ + init: function (options) { + /* Verifying and validating the input object */ if (!options) { options = {}; } - // Creating the options object + /* Creating the options object */ this.options = {}; this.toastElement = null; - // Validating the options - this.options.text = options.text || Toastify.defaults.text; // Display message - this.options.node = options.node || Toastify.defaults.node; // Display content as node - this.options.duration = options.duration === 0 ? 0 : options.duration || Toastify.defaults.duration; // Display duration - this.options.selector = options.selector || Toastify.defaults.selector; // Parent selector - this.options.callback = options.callback || Toastify.defaults.callback; // Callback after display - this.options.destination = options.destination || Toastify.defaults.destination; // On-click destination - this.options.newWindow = options.newWindow || Toastify.defaults.newWindow; // Open destination in new window - this.options.close = options.close || Toastify.defaults.close; // Show toast close icon - this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : Toastify.defaults.gravity; // toast position - top or bottom - this.options.positionLeft = options.positionLeft || Toastify.defaults.positionLeft; // toast position - left or right - this.options.position = options.position || Toastify.defaults.position; // toast position - left or right - this.options.backgroundColor = options.backgroundColor || Toastify.defaults.backgroundColor; // toast background color - this.options.avatar = options.avatar || Toastify.defaults.avatar; // img element src - url or a path - this.options.className = options.className || Toastify.defaults.className; // additional class names for the toast - this.options.stopOnFocus = options.stopOnFocus === undefined ? Toastify.defaults.stopOnFocus : options.stopOnFocus; // stop timeout on focus - this.options.onClick = options.onClick || Toastify.defaults.onClick; // Callback after click - this.options.offset = options.offset || Toastify.defaults.offset; // toast offset + /* Validating the options */ + this.options.text = options.text || Toastify.defaults.text; /* Display message */ + this.options.node = options.node || Toastify.defaults.node; /* Display content as node */ + this.options.duration = options.duration === 0 ? 0 : options.duration || Toastify.defaults.duration; /* Display duration */ + this.options.selector = options.selector || Toastify.defaults.selector; /* Parent selector */ + this.options.callback = options.callback || Toastify.defaults.callback; /* Callback after display */ + this.options.destination = options.destination || Toastify.defaults.destination; /* On-click destination */ + this.options.newWindow = options.newWindow || Toastify.defaults.newWindow; /* Open destination in new window */ + this.options.close = options.close || Toastify.defaults.close; /* Show toast close icon */ + this.options.gravity = options.gravity === "bottom" ? "toastify-bottom" : Toastify.defaults.gravity; /* toast position - top or bottom */ + this.options.positionLeft = options.positionLeft || Toastify.defaults.positionLeft; /* toast position - left or right */ + this.options.position = options.position || Toastify.defaults.position; /* toast position - left or right */ + this.options.backgroundColor = options.backgroundColor || Toastify.defaults.backgroundColor; /* toast background color */ + this.options.avatar = options.avatar || Toastify.defaults.avatar; /* img element src - url or a path */ + this.options.className = options.className || Toastify.defaults.className; /* additional class names for the toast */ + this.options.stopOnFocus = options.stopOnFocus === undefined ? Toastify.defaults.stopOnFocus : options.stopOnFocus; /* stop timeout on focus */ + this.options.onClick = options.onClick || Toastify.defaults.onClick; /* Callback after click */ + this.options.offset = options.offset || Toastify.defaults.offset; /* toast offset */ this.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : Toastify.defaults.escapeMarkup; this.options.style = options.style || Toastify.defaults.style; - if(options.backgroundColor) { + if (options.backgroundColor) { this.options.style.background = options.backgroundColor; } - // Returning the current object for chaining functions + /* Returning the current object for chaining functions */ return this; }, - // Building the DOM element - buildToast: function() { - // Validating if the options are defined + /* Building the DOM element */ + buildToast: function () { + /* Validating if the options are defined */ if (!this.options) { throw "Toastify is not initialized"; } - // Creating the DOM object + /* Creating the DOM object */ var divElement = document.createElement("div"); divElement.className = "toastify on " + this.options.className; - // Positioning toast to left or right or center + /* Positioning toast to left or right or center */ if (!!this.options.position) { divElement.className += " toastify-" + this.options.position; } else { - // To be depreciated in further versions + /* To be depreciated in further versions */ if (this.options.positionLeft === true) { divElement.className += " toastify-left"; console.warn('Property `positionLeft` will be depreciated in further versions. Please use `position` instead.') } else { - // Default position + /* Default position */ divElement.className += " toastify-right"; } } - // Assigning gravity of element + /* Assigning gravity of element */ divElement.className += " " + this.options.gravity; if (this.options.backgroundColor) { - // This is being deprecated in favor of using the style HTML DOM property + /* This is being deprecated in favor of using the style HTML DOM property */ console.warn('DEPRECATION NOTICE: "backgroundColor" is being deprecated. Please use the "style.background" property.'); } - // Loop through our style object and apply styles to divElement + /* Loop through our style object and apply styles to divElement */ for (var property in this.options.style) { divElement.style[property] = this.options.style[property]; } - // Adding the toast message/node + /* Adding the toast message/node */ if (this.options.node && this.options.node.nodeType === Node.ELEMENT_NODE) { - // If we have a valid node, we insert it + /* If we have a valid node, we insert it */ divElement.appendChild(this.options.node) } else { if (this.options.escapeMarkup) { @@ -148,77 +149,77 @@ avatarElement.className = "toastify-avatar"; if (this.options.position == "left" || this.options.positionLeft === true) { - // Adding close icon on the left of content + /* Adding close icon on the left of content */ divElement.appendChild(avatarElement); } else { - // Adding close icon on the right of content + /* Adding close icon on the right of content */ divElement.insertAdjacentElement("afterbegin", avatarElement); } } } - // Adding a close icon to the toast + /* Adding a close icon to the toast */ if (this.options.close === true) { - // Create a span for close element + /* Create a span for close element */ var closeElement = document.createElement("span"); closeElement.innerHTML = "✖"; closeElement.className = "toast-close"; - // Triggering the removal of toast from DOM on close click + /* Triggering the removal of toast from DOM on close click */ closeElement.addEventListener( "click", - function(event) { + function (event) { event.stopPropagation(); this.removeElement(this.toastElement); window.clearTimeout(this.toastElement.timeOutValue); }.bind(this) ); - //Calculating screen width + /* Calculating screen width */ var 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 360px + /* Adding the close icon to the toast element */ + /* Display on the right if screen width is less than or equal to 360px */ if ((this.options.position == "left" || this.options.positionLeft === true) && width > 360) { - // Adding close icon on the left of content + /* Adding close icon on the left of content */ divElement.insertAdjacentElement("afterbegin", closeElement); } else { - // Adding close icon on the right of content + /* Adding close icon on the right of content */ divElement.appendChild(closeElement); } } - // Clear timeout while toast is focused + /* Clear timeout while toast is focused */ if (this.options.stopOnFocus && this.options.duration > 0) { var self = this; - // stop countdown + /* stop countdown */ divElement.addEventListener( "mouseover", - function(event) { + function (event) { window.clearTimeout(divElement.timeOutValue); } - ) - // add back the timeout + ); + /* add back the timeout */ divElement.addEventListener( "mouseleave", - function() { + function () { divElement.timeOutValue = window.setTimeout( - function() { - // Remove the toast from DOM + function () { + /* Remove the toast from DOM */ self.removeElement(divElement); }, self.options.duration ) } - ) + ); } - // Adding an on-click destination path + /* Adding an on-click destination path */ if (typeof this.options.destination !== "undefined") { divElement.addEventListener( "click", - function(event) { + function (event) { event.stopPropagation(); if (this.options.newWindow === true) { window.open(this.options.destination, "_blank"); @@ -232,15 +233,15 @@ if (typeof this.options.onClick === "function" && typeof this.options.destination === "undefined") { divElement.addEventListener( "click", - function(event) { + function (event) { event.stopPropagation(); this.options.onClick(); }.bind(this) ); } - // Adding offset - if(typeof this.options.offset === "object") { + /* Adding offset */ + if (typeof this.options.offset === "object") { var x = getAxisOffsetAValue("x", this.options); var y = getAxisOffsetAValue("y", this.options); @@ -252,16 +253,16 @@ } - // Returning the generated element + /* Returning the generated element */ return divElement; }, - // Displaying the toast - showToast: function() { - // Creating the DOM object for the toast + /* Displaying the toast */ + showToast: function () { + /* Creating the DOM object for the toast */ this.toastElement = this.buildToast(); - // Getting the root element to with the toast needs to be added + /* Getting the root element to with the toast needs to be added */ var rootElement; if (typeof this.options.selector === "string") { rootElement = document.getElementById(this.options.selector); @@ -271,73 +272,73 @@ rootElement = document.body; } - // Validating if root element is present in DOM + /* Validating if root element is present in DOM */ if (!rootElement) { throw "Root element is not defined"; } - // Adding the DOM element + /* Adding the DOM element */ var elementToInsert = Toastify.defaults.oldestFirst ? rootElement.firstChild : rootElement.lastChild; rootElement.insertBefore(this.toastElement, elementToInsert); - // Repositioning the toasts in case multiple toasts are present + /* Repositioning the toasts in case multiple toasts are present */ Toastify.reposition(); if (this.options.duration > 0) { this.toastElement.timeOutValue = window.setTimeout( - function() { - // Remove the toast from DOM + function () { + /* Remove the toast from DOM */ this.removeElement(this.toastElement); }.bind(this), this.options.duration - ); // Binding `this` for function invocation + ); /* Binding `this` for function invocation */ } - // Supporting function chaining + /* Supporting function chaining */ return this; }, - hideToast: function() { + hideToast: function () { if (this.toastElement.timeOutValue) { clearTimeout(this.toastElement.timeOutValue); } this.removeElement(this.toastElement); }, - // Removing the element from the DOM - removeElement: function(toastElement) { - // Hiding the element - // toastElement.classList.remove("on"); + /* Removing the element from the DOM */ + removeElement: function (toastElement) { + /* Hiding the element */ + /* toastElement.classList.remove("on"); */ toastElement.className = toastElement.className.replace(" on", ""); - // Removing the element from DOM after transition end + /* Removing the element from DOM after transition end */ window.setTimeout( - function() { - // remove options node if any + function () { + /* remove options node if any */ if (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. + /* 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 function + /* Calling the callback function */ this.options.callback.call(toastElement); - // Repositioning the toasts again + /* Repositioning the toasts again */ Toastify.reposition(); }.bind(this), 400 - ); // Binding `this` for function invocation + ); /* Binding `this` for function invocation */ }, }; - // Positioning the toasts on the DOM - Toastify.reposition = function() { + /* Positioning the toasts on the DOM */ + Toastify.reposition = function () { - // Top margins with gravity + /* Top margins with gravity */ var topLeftOffsetSize = { top: 15, bottom: 15, @@ -351,14 +352,14 @@ bottom: 15, }; - // Get all toast messages on the DOM + /* Get all toast messages on the DOM */ var allToasts = document.getElementsByClassName("toastify"); var classUsed; - // Modifying the position of each toast element + /* Modifying the position of each toast element */ for (var i = 0; i < allToasts.length; i++) { - // Getting the applied gravity + /* Getting the applied gravity */ if (containsClass(allToasts[i], "toastify-top") === true) { classUsed = "toastify-top"; } else { @@ -366,26 +367,26 @@ } var height = allToasts[i].offsetHeight; - classUsed = classUsed.substr(9, classUsed.length-1) - // Spacing between toasts + classUsed = classUsed.substr(9, classUsed.length - 1); + /* Spacing between toasts */ var offset = 15; var width = window.innerWidth > 0 ? window.innerWidth : screen.width; - // Show toast in center if screen with less than or equal to 360px + /* Show toast in center if screen with less than or equal to 360px */ if (width <= 360) { - // Setting the position + /* Setting the position */ allToasts[i].style[classUsed] = offsetSize[classUsed] + "px"; offsetSize[classUsed] += height + offset; } else { if (containsClass(allToasts[i], "toastify-left") === true) { - // Setting the position + /* Setting the position */ allToasts[i].style[classUsed] = topLeftOffsetSize[classUsed] + "px"; topLeftOffsetSize[classUsed] += height + offset; } else { - // Setting the position + /* Setting the position */ allToasts[i].style[classUsed] = topRightOffsetSize[classUsed] + "px"; topRightOffsetSize[classUsed] += height + offset; @@ -393,15 +394,15 @@ } } - // Supporting function chaining + /* Supporting function chaining */ return this; }; - // Helper function to get offset. + /* Helper function to get offset. */ function getAxisOffsetAValue(axis, options) { - if(options.offset[axis]) { - if(isNaN(options.offset[axis])) { + if (options.offset[axis]) { + if (isNaN(options.offset[axis])) { return options.offset[axis]; } else { @@ -429,9 +430,9 @@ } } - // Setting up the prototype for the init object + /* Setting up the prototype for the init object */ Toastify.lib.init.prototype = Toastify.lib; - // Returning the Toastify function to be assigned to the window object/module + /* Returning the Toastify function to be assigned to the window object/module */ return Toastify; });