/* * toastr * copyright 2012-2014 john papa and hans fjällemark. * 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 * * author: john papa and hans fjällemark * aria support: greta krafsig * project: https://github.com/codeseven/toastr */ ; (function (define) { define(['jquery'], function ($) { return (function () { var $container; var listener; var toastid = 0; var toasttype = { error: 'error', info: 'info', success: 'success', warning: 'warning' }; var toastr = { clear: clear, remove: remove, error: error, getcontainer: getcontainer, info: info, options: {}, subscribe: subscribe, success: success, version: '2.0.3', warning: warning }; return toastr; //#region accessible methods function error(message, title, optionsoverride) { return notify({ type: toasttype.error, iconclass: getoptions().iconclasses.error, message: message, optionsoverride: optionsoverride, title: title }); } function getcontainer(options, create) { if (!options) { options = getoptions(); } $container = $('#' + options.containerid); if ($container.length) { return $container; } if(create) { $container = createcontainer(options); } return $container; } function info(message, title, optionsoverride) { return notify({ type: toasttype.info, iconclass: getoptions().iconclasses.info, message: message, optionsoverride: optionsoverride, title: title }); } function subscribe(callback) { listener = callback; } function success(message, title, optionsoverride) { return notify({ type: toasttype.success, iconclass: getoptions().iconclasses.success, message: message, optionsoverride: optionsoverride, title: title }); } function warning(message, title, optionsoverride) { return notify({ type: toasttype.warning, iconclass: getoptions().iconclasses.warning, message: message, optionsoverride: optionsoverride, title: title }); } function clear($toastelement) { var options = getoptions(); if (!$container) { getcontainer(options); } if (!cleartoast($toastelement, options)) { clearcontainer(options); } } function remove($toastelement) { var options = getoptions(); if (!$container) { getcontainer(options); } if ($toastelement && $(':focus', $toastelement).length === 0) { removetoast($toastelement); return; } if ($container.children().length) { $container.remove(); } } //#endregion //#region internal methods 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) { $toastelement[options.hidemethod]({ duration: options.hideduration, easing: options.hideeasing, complete: function () { removetoast($toastelement); } }); return true; } return false; } function createcontainer(options) { $container = $('
') .attr('id', options.containerid) .addclass(options.positionclass) .attr('aria-live', 'polite') .attr('role', 'alert'); $container.appendto($(options.target)); return $container; } function getdefaults() { return { taptodismiss: true, toastclass: 'toast', containerid: 'toast-container', debug: false, showmethod: 'fadein', //fadein, slidedown, and show are built into jquery showduration: 300, showeasing: 'swing', //swing and linear are built into jquery onshown: undefined, hidemethod: 'fadeout', hideduration: 1000, hideeasing: 'swing', onhidden: undefined, extendedtimeout: 1000, iconclasses: { error: 'toast-error', info: 'toast-info', success: 'toast-success', warning: 'toast-warning' }, 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', target: 'body', closehtml: '', newestontop: true }; } function publish(args) { if (!listener) { return; } listener(args); } function notify(map) { var options = getoptions(), iconclass = map.iconclass || options.iconclass; if (typeof (map.optionsoverride) !== 'undefined') { options = $.extend(options, map.optionsoverride); iconclass = map.optionsoverride.iconclass || iconclass; } toastid++; $container = getcontainer(options, true); var intervalid = null, $toastelement = $('
'), $titleelement = $('
'), $messageelement = $('
'), $closeelement = $(options.closehtml), response = { toastid: toastid, state: 'visible', starttime: new date(), options: options, map: map }; if (map.iconclass) { $toastelement.addclass(options.toastclass).addclass(iconclass); } if (map.title) { $titleelement.append(map.title).addclass(options.titleclass); $toastelement.append($titleelement); } if (map.message) { $messageelement.append(map.message).addclass(options.messageclass); $toastelement.append($messageelement); } if (options.closebutton) { $closeelement.addclass('toast-close-button').attr("role", "button"); $toastelement.prepend($closeelement); } $toastelement.hide(); 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); } $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(); }); } publish(response); if (options.debug && console) { console.log(response); } return $toastelement; function hidetoast(override) { if ($(':focus', $toastelement).length && !override) { return; } return $toastelement[options.hidemethod]({ duration: options.hideduration, easing: options.hideeasing, complete: function () { removetoast($toastelement); if (options.onhidden && response.state !== 'hidden') { options.onhidden(); } response.state = 'hidden'; response.endtime = new date(); publish(response); } }); } function delayedhidetoast() { 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) { if (typeof module !== 'undefined' && module.exports) { //node module.exports = factory(require('jquery')); } else { window['toastr'] = factory(window['jquery']); } }));