2 changed files with 332 additions and 304 deletions
@ -1,310 +1,338 @@ |
|||||||
/* |
/* |
||||||
* Toastr |
* Toastr |
||||||
* Version 2.0.1 |
* Copyright 2012-2014 John Papa and Hans Fjällemark. |
||||||
* Copyright 2012 John Papa and Hans Fjällemark. |
* All Rights Reserved. |
||||||
* All Rights Reserved. |
* Use, reproduction, distribution, and modification of this code is subject to the terms and |
||||||
* 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
|
* conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
|
||||||
* |
* |
||||||
* Author: John Papa and Hans Fjällemark |
* Author: John Papa and Hans Fjällemark |
||||||
|
* ARIA Support: Greta Krafsig |
||||||
* Project: https://github.com/CodeSeven/toastr
|
* Project: https://github.com/CodeSeven/toastr
|
||||||
*/ |
*/ |
||||||
; (function (define) { |
; (function (define) { |
||||||
define(['jquery'], function ($) { |
define(['jquery'], function ($) { |
||||||
return (function () { |
return (function () { |
||||||
var version = '2.0.1'; |
var $container; |
||||||
var $container; |
var listener; |
||||||
var listener; |
var toastId = 0; |
||||||
var toastId = 0; |
var toastType = { |
||||||
var toastType = { |
error: 'error', |
||||||
error: 'error', |
info: 'info', |
||||||
info: 'info', |
success: 'success', |
||||||
success: 'success', |
warning: 'warning' |
||||||
warning: 'warning' |
}; |
||||||
}; |
|
||||||
|
var toastr = { |
||||||
var toastr = { |
clear: clear, |
||||||
clear: clear, |
remove: remove, |
||||||
error: error, |
error: error, |
||||||
getContainer: getContainer, |
getContainer: getContainer, |
||||||
info: info, |
info: info, |
||||||
options: {}, |
options: {}, |
||||||
subscribe: subscribe, |
subscribe: subscribe, |
||||||
success: success, |
success: success, |
||||||
version: version, |
version: '2.0.3', |
||||||
warning: warning |
warning: warning |
||||||
}; |
}; |
||||||
|
|
||||||
return toastr; |
return toastr; |
||||||
|
|
||||||
//#region Accessible Methods
|
//#region Accessible Methods
|
||||||
function error(message, title, optionsOverride) { |
function error(message, title, optionsOverride) { |
||||||
return notify({ |
return notify({ |
||||||
type: toastType.error, |
type: toastType.error, |
||||||
iconClass: getOptions().iconClasses.error, |
iconClass: getOptions().iconClasses.error, |
||||||
message: message, |
message: message, |
||||||
optionsOverride: optionsOverride, |
optionsOverride: optionsOverride, |
||||||
title: title |
title: title |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
|
||||||
function info(message, title, optionsOverride) { |
function getContainer(options, create) { |
||||||
return notify({ |
if (!options) { options = getOptions(); } |
||||||
type: toastType.info, |
$container = $('#' + options.containerId); |
||||||
iconClass: getOptions().iconClasses.info, |
if ($container.length) { |
||||||
message: message, |
return $container; |
||||||
optionsOverride: optionsOverride, |
} |
||||||
title: title |
if(create) { |
||||||
}); |
$container = createContainer(options); |
||||||
} |
} |
||||||
|
return $container; |
||||||
function subscribe(callback) { |
} |
||||||
listener = callback; |
|
||||||
} |
function info(message, title, optionsOverride) { |
||||||
|
return notify({ |
||||||
function success(message, title, optionsOverride) { |
type: toastType.info, |
||||||
return notify({ |
iconClass: getOptions().iconClasses.info, |
||||||
type: toastType.success, |
message: message, |
||||||
iconClass: getOptions().iconClasses.success, |
optionsOverride: optionsOverride, |
||||||
message: message, |
title: title |
||||||
optionsOverride: optionsOverride, |
}); |
||||||
title: title |
} |
||||||
}); |
|
||||||
} |
function subscribe(callback) { |
||||||
|
listener = callback; |
||||||
function warning(message, title, optionsOverride) { |
} |
||||||
return notify({ |
|
||||||
type: toastType.warning, |
function success(message, title, optionsOverride) { |
||||||
iconClass: getOptions().iconClasses.warning, |
return notify({ |
||||||
message: message, |
type: toastType.success, |
||||||
optionsOverride: optionsOverride, |
iconClass: getOptions().iconClasses.success, |
||||||
title: title |
message: message, |
||||||
}); |
optionsOverride: optionsOverride, |
||||||
} |
title: title |
||||||
|
}); |
||||||
function clear($toastElement) { |
} |
||||||
var options = getOptions(); |
|
||||||
if (!$container) { getContainer(options); } |
function warning(message, title, optionsOverride) { |
||||||
if ($toastElement && $(':focus', $toastElement).length === 0) { |
return notify({ |
||||||
$toastElement[options.hideMethod]({ |
type: toastType.warning, |
||||||
duration: options.hideDuration, |
iconClass: getOptions().iconClasses.warning, |
||||||
easing: options.hideEasing, |
message: message, |
||||||
complete: function () { removeToast($toastElement); } |
optionsOverride: optionsOverride, |
||||||
}); |
title: title |
||||||
return; |
}); |
||||||
} |
} |
||||||
if ($container.children().length) { |
|
||||||
$container[options.hideMethod]({ |
function clear($toastElement) { |
||||||
duration: options.hideDuration, |
var options = getOptions(); |
||||||
easing: options.hideEasing, |
if (!$container) { getContainer(options); } |
||||||
complete: function () { $container.remove(); } |
if (!clearToast($toastElement, options)) { |
||||||
}); |
clearContainer(options); |
||||||
} |
} |
||||||
} |
} |
||||||
//#endregion
|
|
||||||
|
function remove($toastElement) { |
||||||
//#region Internal Methods
|
var options = getOptions(); |
||||||
|
if (!$container) { getContainer(options); } |
||||||
function getDefaults() { |
if ($toastElement && $(':focus', $toastElement).length === 0) { |
||||||
return { |
removeToast($toastElement); |
||||||
tapToDismiss: true, |
return; |
||||||
toastClass: 'toast', |
} |
||||||
containerId: 'toast-container', |
if ($container.children().length) { |
||||||
debug: false, |
$container.remove(); |
||||||
|
} |
||||||
showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
|
} |
||||||
showDuration: 300, |
//#endregion
|
||||||
showEasing: 'swing', //swing and linear are built into jQuery
|
|
||||||
onShown: undefined, |
//#region Internal Methods
|
||||||
hideMethod: 'fadeOut', |
|
||||||
hideDuration: 1000, |
function clearContainer(options){ |
||||||
hideEasing: 'swing', |
var toastsToClear = $container.children(); |
||||||
onHidden: undefined, |
for (var i = toastsToClear.length - 1; i >= 0; i--) { |
||||||
|
clearToast($(toastsToClear[i]), options); |
||||||
extendedTimeOut: 1000, |
}; |
||||||
iconClasses: { |
} |
||||||
error: 'toast-error', |
|
||||||
info: 'toast-info', |
function clearToast($toastElement, options){ |
||||||
success: 'toast-success', |
if ($toastElement && $(':focus', $toastElement).length === 0) { |
||||||
warning: 'toast-warning' |
$toastElement[options.hideMethod]({ |
||||||
}, |
duration: options.hideDuration, |
||||||
iconClass: 'toast-info', |
easing: options.hideEasing, |
||||||
positionClass: 'toast-top-right', |
complete: function () { removeToast($toastElement); } |
||||||
timeOut: 5000, // Set timeOut and extendedTimeout to 0 to make it sticky
|
}); |
||||||
titleClass: 'toast-title', |
return true; |
||||||
messageClass: 'toast-message', |
} |
||||||
target: 'body', |
return false; |
||||||
closeHtml: '<button>×</button>', |
} |
||||||
newestOnTop: true |
|
||||||
}; |
function createContainer(options) { |
||||||
} |
$container = $('<div/>') |
||||||
|
.attr('id', options.containerId) |
||||||
function publish(args) { |
.addClass(options.positionClass) |
||||||
if (!listener) { |
.attr('aria-live', 'polite') |
||||||
return; |
.attr('role', 'alert'); |
||||||
} |
|
||||||
listener(args); |
$container.appendTo($(options.target)); |
||||||
} |
return $container; |
||||||
|
} |
||||||
function notify(map) { |
|
||||||
var |
function getDefaults() { |
||||||
options = getOptions(), |
return { |
||||||
iconClass = map.iconClass || options.iconClass; |
tapToDismiss: true, |
||||||
|
toastClass: 'toast', |
||||||
if (typeof (map.optionsOverride) !== 'undefined') { |
containerId: 'toast-container', |
||||||
options = $.extend(options, map.optionsOverride); |
debug: false, |
||||||
iconClass = map.optionsOverride.iconClass || iconClass; |
|
||||||
} |
showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
|
||||||
|
showDuration: 300, |
||||||
toastId++; |
showEasing: 'swing', //swing and linear are built into jQuery
|
||||||
|
onShown: undefined, |
||||||
$container = getContainer(options); |
hideMethod: 'fadeOut', |
||||||
var |
hideDuration: 1000, |
||||||
intervalId = null, |
hideEasing: 'swing', |
||||||
$toastElement = $('<div/>'), |
onHidden: undefined, |
||||||
$titleElement = $('<div/>'), |
|
||||||
$messageElement = $('<div/>'), |
extendedTimeOut: 1000, |
||||||
$closeElement = $(options.closeHtml), |
iconClasses: { |
||||||
response = { |
error: 'toast-error', |
||||||
toastId: toastId, |
info: 'toast-info', |
||||||
state: 'visible', |
success: 'toast-success', |
||||||
startTime: new Date(), |
warning: 'toast-warning' |
||||||
options: options, |
}, |
||||||
map: map |
iconClass: 'toast-info', |
||||||
}; |
positionClass: 'toast-top-right', |
||||||
|
timeOut: 5000, // Set timeOut and extendedTimeout to 0 to make it sticky
|
||||||
if (map.iconClass) { |
titleClass: 'toast-title', |
||||||
$toastElement.addClass(options.toastClass).addClass(iconClass); |
messageClass: 'toast-message', |
||||||
} |
target: 'body', |
||||||
|
closeHtml: '<button>×</button>', |
||||||
if (map.title) { |
newestOnTop: true |
||||||
$titleElement.append(map.title).addClass(options.titleClass); |
}; |
||||||
$toastElement.append($titleElement); |
} |
||||||
} |
|
||||||
|
function publish(args) { |
||||||
if (map.message) { |
if (!listener) { return; } |
||||||
$messageElement.append(map.message).addClass(options.messageClass); |
listener(args); |
||||||
$toastElement.append($messageElement); |
} |
||||||
} |
|
||||||
|
function notify(map) { |
||||||
if (options.closeButton) { |
var options = getOptions(), |
||||||
$closeElement.addClass('toast-close-button'); |
iconClass = map.iconClass || options.iconClass; |
||||||
$toastElement.prepend($closeElement); |
|
||||||
} |
if (typeof (map.optionsOverride) !== 'undefined') { |
||||||
|
options = $.extend(options, map.optionsOverride); |
||||||
$toastElement.hide(); |
iconClass = map.optionsOverride.iconClass || iconClass; |
||||||
if (options.newestOnTop) { |
} |
||||||
$container.prepend($toastElement); |
|
||||||
} else { |
toastId++; |
||||||
$container.append($toastElement); |
|
||||||
} |
$container = getContainer(options, true); |
||||||
|
var intervalId = null, |
||||||
|
$toastElement = $('<div/>'), |
||||||
$toastElement[options.showMethod]( |
$titleElement = $('<div/>'), |
||||||
{ duration: options.showDuration, easing: options.showEasing, complete: options.onShown } |
$messageElement = $('<div/>'), |
||||||
); |
$closeElement = $(options.closeHtml), |
||||||
if (options.timeOut > 0) { |
response = { |
||||||
intervalId = setTimeout(hideToast, options.timeOut); |
toastId: toastId, |
||||||
} |
state: 'visible', |
||||||
|
startTime: new Date(), |
||||||
$toastElement.hover(stickAround, delayedhideToast); |
options: options, |
||||||
if (!options.onclick && options.tapToDismiss) { |
map: map |
||||||
$toastElement.click(hideToast); |
}; |
||||||
} |
|
||||||
if (options.closeButton && $closeElement) { |
if (map.iconClass) { |
||||||
$closeElement.click(function (event) { |
$toastElement.addClass(options.toastClass).addClass(iconClass); |
||||||
if( event.stopPropagation ) { |
} |
||||||
event.stopPropagation(); |
|
||||||
} else if( event.cancelBubble !== undefined && event.cancelBubble !== true ) { |
if (map.title) { |
||||||
event.cancelBubble = true; |
$titleElement.append(map.title).addClass(options.titleClass); |
||||||
} |
$toastElement.append($titleElement); |
||||||
hideToast(true); |
} |
||||||
}); |
|
||||||
} |
if (map.message) { |
||||||
|
$messageElement.append(map.message).addClass(options.messageClass); |
||||||
if (options.onclick) { |
$toastElement.append($messageElement); |
||||||
$toastElement.click(function () { |
} |
||||||
options.onclick(); |
|
||||||
hideToast(); |
if (options.closeButton) { |
||||||
}); |
$closeElement.addClass('toast-close-button').attr("role", "button"); |
||||||
} |
$toastElement.prepend($closeElement); |
||||||
|
} |
||||||
publish(response); |
|
||||||
|
$toastElement.hide(); |
||||||
if (options.debug && console) { |
if (options.newestOnTop) { |
||||||
console.log(response); |
$container.prepend($toastElement); |
||||||
} |
} else { |
||||||
|
$container.append($toastElement); |
||||||
return $toastElement; |
} |
||||||
|
|
||||||
function hideToast(override) { |
|
||||||
if ($(':focus', $toastElement).length && !override) { |
$toastElement[options.showMethod]( |
||||||
return; |
{ duration: options.showDuration, easing: options.showEasing, complete: options.onShown } |
||||||
} |
); |
||||||
return $toastElement[options.hideMethod]({ |
|
||||||
duration: options.hideDuration, |
if (options.timeOut > 0) { |
||||||
easing: options.hideEasing, |
intervalId = setTimeout(hideToast, options.timeOut); |
||||||
complete: function () { |
} |
||||||
removeToast($toastElement); |
|
||||||
if (options.onHidden) { |
$toastElement.hover(stickAround, delayedHideToast); |
||||||
options.onHidden(); |
if (!options.onclick && options.tapToDismiss) { |
||||||
} |
$toastElement.click(hideToast); |
||||||
response.state = 'hidden'; |
} |
||||||
response.endTime = new Date(), |
|
||||||
publish(response); |
if (options.closeButton && $closeElement) { |
||||||
} |
$closeElement.click(function (event) { |
||||||
}); |
if( event.stopPropagation ) { |
||||||
} |
event.stopPropagation(); |
||||||
|
} else if( event.cancelBubble !== undefined && event.cancelBubble !== true ) { |
||||||
function delayedhideToast() { |
event.cancelBubble = true; |
||||||
if (options.timeOut > 0 || options.extendedTimeOut > 0) { |
} |
||||||
intervalId = setTimeout(hideToast, options.extendedTimeOut); |
hideToast(true); |
||||||
} |
}); |
||||||
} |
} |
||||||
|
|
||||||
function stickAround() { |
if (options.onclick) { |
||||||
clearTimeout(intervalId); |
$toastElement.click(function () { |
||||||
$toastElement.stop(true, true)[options.showMethod]( |
options.onclick(); |
||||||
{ duration: options.showDuration, easing: options.showEasing } |
hideToast(); |
||||||
); |
}); |
||||||
} |
} |
||||||
} |
|
||||||
function getContainer(options) { |
publish(response); |
||||||
if (!options) { options = getOptions(); } |
|
||||||
$container = $('#' + options.containerId); |
if (options.debug && console) { |
||||||
if ($container.length) { |
console.log(response); |
||||||
return $container; |
} |
||||||
} |
|
||||||
$container = $('<div/>') |
return $toastElement; |
||||||
.attr('id', options.containerId) |
|
||||||
.addClass(options.positionClass); |
function hideToast(override) { |
||||||
$container.appendTo($(options.target)); |
if ($(':focus', $toastElement).length && !override) { |
||||||
return $container; |
return; |
||||||
} |
} |
||||||
|
return $toastElement[options.hideMethod]({ |
||||||
function getOptions() { |
duration: options.hideDuration, |
||||||
return $.extend({}, getDefaults(), toastr.options); |
easing: options.hideEasing, |
||||||
} |
complete: function () { |
||||||
|
removeToast($toastElement); |
||||||
function removeToast($toastElement) { |
if (options.onHidden && response.state !== 'hidden') { |
||||||
if (!$container) { $container = getContainer(); } |
options.onHidden(); |
||||||
if ($toastElement.is(':visible')) { |
} |
||||||
return; |
response.state = 'hidden'; |
||||||
} |
response.endTime = new Date(); |
||||||
$toastElement.remove(); |
publish(response); |
||||||
$toastElement = null; |
} |
||||||
if ($container.children().length === 0) { |
}); |
||||||
$container.remove(); |
} |
||||||
} |
|
||||||
} |
function delayedHideToast() { |
||||||
//#endregion
|
if (options.timeOut > 0 || options.extendedTimeOut > 0) { |
||||||
|
intervalId = setTimeout(hideToast, options.extendedTimeOut); |
||||||
})(); |
} |
||||||
}); |
} |
||||||
|
|
||||||
|
function stickAround() { |
||||||
|
clearTimeout(intervalId); |
||||||
|
$toastElement.stop(true, true)[options.showMethod]( |
||||||
|
{ duration: options.showDuration, easing: options.showEasing } |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function getOptions() { |
||||||
|
return $.extend({}, getDefaults(), toastr.options); |
||||||
|
} |
||||||
|
|
||||||
|
function removeToast($toastElement) { |
||||||
|
if (!$container) { $container = getContainer(); } |
||||||
|
if ($toastElement.is(':visible')) { |
||||||
|
return; |
||||||
|
} |
||||||
|
$toastElement.remove(); |
||||||
|
$toastElement = null; |
||||||
|
if ($container.children().length === 0) { |
||||||
|
$container.remove(); |
||||||
|
} |
||||||
|
} |
||||||
|
//#endregion
|
||||||
|
|
||||||
|
})(); |
||||||
|
}); |
||||||
}(typeof define === 'function' && define.amd ? define : function (deps, factory) { |
}(typeof define === 'function' && define.amd ? define : function (deps, factory) { |
||||||
if (typeof module !== 'undefined' && module.exports) { //Node
|
if (typeof module !== 'undefined' && module.exports) { //Node
|
||||||
module.exports = factory(require('jquery')); |
module.exports = factory(require('jquery')); |
||||||
} else { |
} else { |
||||||
window['toastr'] = factory(window['jQuery']); |
window['toastr'] = factory(window['jQuery']); |
||||||
} |
} |
||||||
})); |
})); |
||||||
Loading…
Reference in new issue