app/assets/javascript/toastr.js in toastr_rails-2.1.0.2 vs app/assets/javascript/toastr.js in toastr_rails-2.1.1
- old
+ new
@@ -1,17 +1,18 @@
/*
* Toastr
- * Copyright 2012-2014
+ * Copyright 2012-2015
* Authors: John Papa, Hans FjÀllemark, and Tim Ferrell.
* All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
*
* ARIA Support: Greta Krafsig
*
* Project: https://github.com/CodeSeven/toastr
*/
+/* global define */
; (function (define) {
define(['jquery'], function ($) {
return (function () {
var $container;
var listener;
@@ -30,19 +31,20 @@
getContainer: getContainer,
info: info,
options: {},
subscribe: subscribe,
success: success,
- version: '2.1.0',
+ version: '2.1.1',
warning: warning
};
var previousToast;
return toastr;
- //#region Accessible Methods
+ ////////////////
+
function error(message, title, optionsOverride) {
return notify({
type: toastType.error,
iconClass: getOptions().iconClasses.error,
message: message,
@@ -95,14 +97,14 @@
optionsOverride: optionsOverride,
title: title
});
}
- function clear($toastElement) {
+ function clear($toastElement, clearOptions) {
var options = getOptions();
if (!$container) { getContainer(options); }
- if (!clearToast($toastElement, options)) {
+ if (!clearToast($toastElement, options, clearOptions)) {
clearContainer(options);
}
}
function remove($toastElement) {
@@ -114,23 +116,23 @@
}
if ($container.children().length) {
$container.remove();
}
}
- //#endregion
- //#region Internal Methods
+ // internal functions
function clearContainer (options) {
var toastsToClear = $container.children();
for (var i = toastsToClear.length - 1; i >= 0; i--) {
clearToast($(toastsToClear[i]), options);
}
}
- function clearToast ($toastElement, options) {
- if ($toastElement && $(':focus', $toastElement).length === 0) {
+ function clearToast ($toastElement, options, clearOptions) {
+ var force = clearOptions && clearOptions.force ? clearOptions.force : false;
+ if ($toastElement && (force || $(':focus', $toastElement).length === 0)) {
$toastElement[options.hideMethod]({
duration: options.hideDuration,
easing: options.hideEasing,
complete: function () { removeToast($toastElement); }
});
@@ -190,122 +192,161 @@
if (!listener) { return; }
listener(args);
}
function notify(map) {
- var options = getOptions(),
- iconClass = map.iconClass || options.iconClass;
+ var options = getOptions();
+ var iconClass = map.iconClass || options.iconClass;
if (typeof (map.optionsOverride) !== 'undefined') {
options = $.extend(options, map.optionsOverride);
iconClass = map.optionsOverride.iconClass || iconClass;
}
-
- if (options.preventDuplicates) {
- if (map.message === previousToast) {
- return;
- } else {
- previousToast = map.message;
- }
- }
+ if (shouldExit(options, map)) { return; }
+
toastId++;
$container = getContainer(options, true);
- var intervalId = null,
- $toastElement = $('<div/>'),
- $titleElement = $('<div/>'),
- $messageElement = $('<div/>'),
- $progressElement = $('<div/>'),
- $closeElement = $(options.closeHtml),
- progressBar = {
- intervalId: null,
- hideEta: null,
- maxHideTime: null
- },
- response = {
- toastId: toastId,
- state: 'visible',
- startTime: new Date(),
- options: options,
- map: map
- };
- if (map.iconClass) {
- $toastElement.addClass(options.toastClass).addClass(iconClass);
+ var intervalId = null;
+ var $toastElement = $('<div/>');
+ var $titleElement = $('<div/>');
+ var $messageElement = $('<div/>');
+ var $progressElement = $('<div/>');
+ var $closeElement = $(options.closeHtml);
+ var progressBar = {
+ intervalId: null,
+ hideEta: null,
+ maxHideTime: null
+ };
+ var response = {
+ toastId: toastId,
+ state: 'visible',
+ startTime: new Date(),
+ options: options,
+ map: map
+ };
+
+ personalizeToast();
+
+ displayToast();
+
+ handleEvents();
+
+ publish(response);
+
+ if (options.debug && console) {
+ console.log(response);
}
- if (map.title) {
- $titleElement.append(map.title).addClass(options.titleClass);
- $toastElement.append($titleElement);
+ return $toastElement;
+
+ function personalizeToast() {
+ setIcon();
+ setTitle();
+ setMessage();
+ setCloseButton();
+ setProgressBar();
+ setSequence();
}
- if (map.message) {
- $messageElement.append(map.message).addClass(options.messageClass);
- $toastElement.append($messageElement);
+ function handleEvents() {
+ $toastElement.hover(stickAround, delayedHideToast);
+ if (!options.onclick && options.tapToDismiss) {
+ $toastElement.click(hideToast);
+ }
+
+ if (options.closeButton && $closeElement) {
+ $closeElement.click(function (event) {
+ if (event.stopPropagation) {
+ event.stopPropagation();
+ } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
+ event.cancelBubble = true;
+ }
+ hideToast(true);
+ });
+ }
+
+ if (options.onclick) {
+ $toastElement.click(function () {
+ options.onclick();
+ hideToast();
+ });
+ }
}
- if (options.closeButton) {
- $closeElement.addClass('toast-close-button').attr('role', 'button');
- $toastElement.prepend($closeElement);
+ function displayToast() {
+ $toastElement.hide();
+
+ $toastElement[options.showMethod](
+ {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
+ );
+
+ if (options.timeOut > 0) {
+ intervalId = setTimeout(hideToast, options.timeOut);
+ progressBar.maxHideTime = parseFloat(options.timeOut);
+ progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
+ if (options.progressBar) {
+ progressBar.intervalId = setInterval(updateProgress, 10);
+ }
+ }
}
- if (options.progressBar) {
- $progressElement.addClass('toast-progress');
- $toastElement.prepend($progressElement);
+ function setIcon() {
+ if (map.iconClass) {
+ $toastElement.addClass(options.toastClass).addClass(iconClass);
+ }
}
- $toastElement.hide();
- if (options.newestOnTop) {
- $container.prepend($toastElement);
- } else {
- $container.append($toastElement);
+ function setSequence() {
+ if (options.newestOnTop) {
+ $container.prepend($toastElement);
+ } else {
+ $container.append($toastElement);
+ }
}
- $toastElement[options.showMethod](
- {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
- );
- if (options.timeOut > 0) {
- intervalId = setTimeout(hideToast, options.timeOut);
- progressBar.maxHideTime = parseFloat(options.timeOut);
- progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
- if (options.progressBar) {
- progressBar.intervalId = setInterval(updateProgress, 10);
+ function setTitle() {
+ if (map.title) {
+ $titleElement.append(map.title).addClass(options.titleClass);
+ $toastElement.append($titleElement);
}
}
- $toastElement.hover(stickAround, delayedHideToast);
- if (!options.onclick && options.tapToDismiss) {
- $toastElement.click(hideToast);
+ function setMessage() {
+ if (map.message) {
+ $messageElement.append(map.message).addClass(options.messageClass);
+ $toastElement.append($messageElement);
+ }
}
- if (options.closeButton && $closeElement) {
- $closeElement.click(function (event) {
- if (event.stopPropagation) {
- event.stopPropagation();
- } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
- event.cancelBubble = true;
- }
- hideToast(true);
- });
+ function setCloseButton() {
+ if (options.closeButton) {
+ $closeElement.addClass('toast-close-button').attr('role', 'button');
+ $toastElement.prepend($closeElement);
+ }
}
- if (options.onclick) {
- $toastElement.click(function () {
- options.onclick();
- hideToast();
- });
+ function setProgressBar() {
+ if (options.progressBar) {
+ $progressElement.addClass('toast-progress');
+ $toastElement.prepend($progressElement);
+ }
}
- publish(response);
-
- if (options.debug && console) {
- console.log(response);
+ function shouldExit(options, map) {
+ if (options.preventDuplicates) {
+ if (map.message === previousToast) {
+ return true;
+ } else {
+ previousToast = map.message;
+ }
+ }
+ return false;
}
- return $toastElement;
-
function hideToast(override) {
if ($(':focus', $toastElement).length && !override) {
return;
}
clearTimeout(progressBar.intervalId);
@@ -360,10 +401,9 @@
if ($container.children().length === 0) {
$container.remove();
previousToast = undefined;
}
}
- //#endregion
})();
});
}(typeof define === 'function' && define.amd ? define : function (deps, factory) {
if (typeof module !== 'undefined' && module.exports) { //Node