app/assets/javascript/toastr.js in toastr_rails-2.1.1 vs app/assets/javascript/toastr.js in toastr_rails-2.1.3
- old
+ new
@@ -9,11 +9,11 @@
* ARIA Support: Greta Krafsig
*
* Project: https://github.com/CodeSeven/toastr
*/
/* global define */
-; (function (define) {
+(function (define) {
define(['jquery'], function ($) {
return (function () {
var $container;
var listener;
var toastId = 0;
@@ -31,11 +31,11 @@
getContainer: getContainer,
info: info,
options: {},
subscribe: subscribe,
success: success,
- version: '2.1.1',
+ version: '2.1.3',
warning: warning
};
var previousToast;
@@ -142,13 +142,11 @@
}
function createContainer(options) {
$container = $('<div/>')
.attr('id', options.containerId)
- .addClass(options.positionClass)
- .attr('aria-live', 'polite')
- .attr('role', 'alert');
+ .addClass(options.positionClass);
$container.appendTo($(options.target));
return $container;
}
@@ -165,10 +163,14 @@
onShown: undefined,
hideMethod: 'fadeOut',
hideDuration: 1000,
hideEasing: 'swing',
onHidden: undefined,
+ closeMethod: false,
+ closeDuration: false,
+ closeEasing: false,
+ closeOnHover: true,
extendedTimeOut: 1000,
iconClasses: {
error: 'toast-error',
info: 'toast-info',
@@ -178,15 +180,19 @@
iconClass: 'toast-info',
positionClass: 'toast-top-right',
timeOut: 5000, // Set timeOut and extendedTimeOut to 0 to make it sticky
titleClass: 'toast-title',
messageClass: 'toast-message',
+ escapeHtml: false,
target: 'body',
closeHtml: '<button type="button">×</button>',
+ closeClass: 'toast-close-button',
newestOnTop: true,
preventDuplicates: false,
- progressBar: false
+ progressBar: false,
+ progressClass: 'toast-progress',
+ rtl: false
};
}
function publish(args) {
if (!listener) { return; }
@@ -239,21 +245,52 @@
console.log(response);
}
return $toastElement;
+ function escapeHtml(source) {
+ if (source == null) {
+ source = '';
+ }
+
+ return source
+ .replace(/&/g, '&')
+ .replace(/"/g, '"')
+ .replace(/'/g, ''')
+ .replace(/</g, '<')
+ .replace(/>/g, '>');
+ }
+
function personalizeToast() {
setIcon();
setTitle();
setMessage();
setCloseButton();
setProgressBar();
+ setRTL();
setSequence();
+ setAria();
}
+ function setAria() {
+ var ariaValue = '';
+ switch (map.iconClass) {
+ case 'toast-success':
+ case 'toast-info':
+ ariaValue = 'polite';
+ break;
+ default:
+ ariaValue = 'assertive';
+ }
+ $toastElement.attr('aria-live', ariaValue);
+ }
+
function handleEvents() {
- $toastElement.hover(stickAround, delayedHideToast);
+ if (options.closeOnHover) {
+ $toastElement.hover(stickAround, delayedHideToast);
+ }
+
if (!options.onclick && options.tapToDismiss) {
$toastElement.click(hideToast);
}
if (options.closeButton && $closeElement) {
@@ -261,17 +298,22 @@
if (event.stopPropagation) {
event.stopPropagation();
} else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
event.cancelBubble = true;
}
+
+ if (options.onCloseClick) {
+ options.onCloseClick(event);
+ }
+
hideToast(true);
});
}
if (options.onclick) {
- $toastElement.click(function () {
- options.onclick();
+ $toastElement.click(function (event) {
+ options.onclick(event);
hideToast();
});
}
}
@@ -306,36 +348,50 @@
}
}
function setTitle() {
if (map.title) {
- $titleElement.append(map.title).addClass(options.titleClass);
+ var suffix = map.title;
+ if (options.escapeHtml) {
+ suffix = escapeHtml(map.title);
+ }
+ $titleElement.append(suffix).addClass(options.titleClass);
$toastElement.append($titleElement);
}
}
function setMessage() {
if (map.message) {
- $messageElement.append(map.message).addClass(options.messageClass);
+ var suffix = map.message;
+ if (options.escapeHtml) {
+ suffix = escapeHtml(map.message);
+ }
+ $messageElement.append(suffix).addClass(options.messageClass);
$toastElement.append($messageElement);
}
}
function setCloseButton() {
if (options.closeButton) {
- $closeElement.addClass('toast-close-button').attr('role', 'button');
+ $closeElement.addClass(options.closeClass).attr('role', 'button');
$toastElement.prepend($closeElement);
}
}
function setProgressBar() {
if (options.progressBar) {
- $progressElement.addClass('toast-progress');
+ $progressElement.addClass(options.progressClass);
$toastElement.prepend($progressElement);
}
}
+ function setRTL() {
+ if (options.rtl) {
+ $toastElement.addClass('rtl');
+ }
+ }
+
function shouldExit(options, map) {
if (options.preventDuplicates) {
if (map.message === previousToast) {
return true;
} else {
@@ -344,19 +400,24 @@
}
return false;
}
function hideToast(override) {
+ var method = override && options.closeMethod !== false ? options.closeMethod : options.hideMethod;
+ var duration = override && options.closeDuration !== false ?
+ options.closeDuration : options.hideDuration;
+ var easing = override && options.closeEasing !== false ? options.closeEasing : options.hideEasing;
if ($(':focus', $toastElement).length && !override) {
return;
}
clearTimeout(progressBar.intervalId);
- return $toastElement[options.hideMethod]({
- duration: options.hideDuration,
- easing: options.hideEasing,
+ return $toastElement[method]({
+ duration: duration,
+ easing: easing,
complete: function () {
removeToast($toastElement);
+ clearTimeout(intervalId);
if (options.onHidden && response.state !== 'hidden') {
options.onHidden();
}
response.state = 'hidden';
response.endTime = new Date();
@@ -408,8 +469,8 @@
});
}(typeof define === 'function' && define.amd ? define : function (deps, factory) {
if (typeof module !== 'undefined' && module.exports) { //Node
module.exports = factory(require('jquery'));
} else {
- window['toastr'] = factory(window['jQuery']);
+ window.toastr = factory(window.jQuery);
}
}));