(function() { 'use strict'; angular.module('toastr', []) .factory('toastr', toastr); toastr.$inject = ['$animate', '$injector', '$document', '$rootScope', '$sce', 'toastrConfig', '$q']; function toastr($animate, $injector, $document, $rootScope, $sce, toastrConfig, $q) { var container, index = 0, toasts = []; var containerDefer = $q.defer(); var toast = { clear: clear, error: error, info: info, remove: remove, success: success, warning: warning }; return toast; /* Public API */ function clear(toast) { if (toast) { remove(toast.toastId); } else { for (var i = 0; i < toasts.length; i++) { remove(toasts[i].toastId); } } } function error(message, title, optionsOverride) { var type = _getOptions().iconClasses.error; return _buildNotification(type, message, title, optionsOverride); } function info(message, title, optionsOverride) { var type = _getOptions().iconClasses.info; return _buildNotification(type, message, title, optionsOverride); } function success(message, title, optionsOverride) { var type = _getOptions().iconClasses.success; return _buildNotification(type, message, title, optionsOverride); } function warning(message, title, optionsOverride) { var type = _getOptions().iconClasses.warning; return _buildNotification(type, message, title, optionsOverride); } function remove(toastId, wasClicked) { var toast = findToast(toastId); if (toast && ! toast.deleting) { // Avoid clicking when fading out toast.deleting = true; $animate.leave(toast.el).then(function() { if (toast.scope.options.onHidden) { toast.scope.options.onHidden(wasClicked); } toast.scope.$destroy(); var index = toasts.indexOf(toast); toasts.splice(index, 1); var maxOpened = toastrConfig.maxOpened; if (maxOpened && toasts.length >= maxOpened) { toasts[maxOpened - 1].open.resolve(); } if (lastToast()) { container.remove(); container = null; containerDefer = $q.defer(); } }); } function findToast(toastId) { for (var i = 0; i < toasts.length; i++) { if (toasts[i].toastId === toastId) { return toasts[i]; } } } function lastToast() { return !toasts.length; } } /* Internal functions */ function _buildNotification(type, message, title, optionsOverride) { if (typeof title === 'object') { optionsOverride = title; title = null; } return _notify({ iconClass: type, message: message, optionsOverride: optionsOverride, title: title }); } function _getOptions() { return angular.extend({}, toastrConfig); } function _createOrGetContainer(options) { if(container) { return containerDefer.promise; } container = angular.element('
'); container.attr('id', options.containerId); container.addClass(options.positionClass); container.css({'pointer-events': 'auto'}); var target = angular.element(document.querySelector(options.target)); if ( ! target || ! target.length) { throw 'Target for toasts doesn\'t exist'; } $animate.enter(container, target).then(function() { containerDefer.resolve(); }); return containerDefer.promise; } function _notify(map) { var options = _getOptions(); var newToast = createToast(); toasts.push(newToast); if (maxOpenedNotReached()) { newToast.open.resolve(); } newToast.open.promise.then(function() { _createOrGetContainer(options).then(function() { if (options.newestOnTop) { $animate.enter(newToast.el, container).then(function() { newToast.scope.init(); }); } else { $animate.enter(newToast.el, container, container[0].lastChild).then(function() { newToast.scope.init(); }); } }); }); return newToast; function createScope(toast, map, options) { if (options.allowHtml) { toast.scope.allowHtml = true; toast.scope.title = $sce.trustAsHtml(map.title); toast.scope.message = $sce.trustAsHtml(map.message); } else { toast.scope.title = map.title; toast.scope.message = map.message; } toast.scope.toastType = toast.iconClass; toast.scope.toastId = toast.toastId; toast.scope.options = { extendedTimeOut: options.extendedTimeOut, messageClass: options.messageClass, onHidden: options.onHidden, onShown: options.onShown, tapToDismiss: options.tapToDismiss, timeOut: options.timeOut, titleClass: options.titleClass, toastClass: options.toastClass }; if (options.closeButton) { toast.scope.options.closeHtml = options.closeHtml; } } function createToast() { var newToast = { toastId: index++, scope: $rootScope.$new(), open: $q.defer() }; newToast.iconClass = map.iconClass; if (map.optionsOverride) { options = angular.extend(options, map.optionsOverride); newToast.iconClass = map.optionsOverride.iconClass || newToast.iconClass; } createScope(newToast, map, options); newToast.el = createToastEl(newToast.scope); return newToast; } function createToastEl(scope) { var angularDomEl = angular.element(''), $compile = $injector.get('$compile'); return $compile(angularDomEl)(scope); } function maxOpenedNotReached() { return options.maxOpened && toasts.length <= options.maxOpened || !options.maxOpened; } } } }()); (function() { 'use strict'; angular.module('toastr') .constant('toastrConfig', { allowHtml: false, closeButton: false, closeHtml: '', containerId: 'toast-container', extendedTimeOut: 1000, iconClasses: { error: 'toast-error', info: 'toast-info', success: 'toast-success', warning: 'toast-warning' }, maxOpened: 0, messageClass: 'toast-message', newestOnTop: true, onHidden: null, onShown: null, positionClass: 'toast-top-right', tapToDismiss: true, target: 'body', timeOut: 5000, titleClass: 'toast-title', toastClass: 'toast' }); }()); (function() { 'use strict'; angular.module('toastr') .directive('toast', toast); toast.$inject = ['$injector', '$interval', 'toastr']; function toast($injector, $interval, toastr) { return { replace: true, templateUrl: 'templates/toastr/toastr.html', link: toastLinkFunction }; function toastLinkFunction(scope, element, attrs) { var timeout; scope.toastClass = scope.options.toastClass; scope.titleClass = scope.options.titleClass; scope.messageClass = scope.options.messageClass; if (wantsCloseButton()) { var button = angular.element(scope.options.closeHtml), $compile = $injector.get('$compile'); button.addClass('toast-close-button'); button.attr('ng-click', 'close()'); $compile(button)(scope); element.prepend(button); } scope.init = function() { if (scope.options.timeOut) { timeout = createTimeout(scope.options.timeOut); } if (scope.options.onShown) { scope.options.onShown(); } }; element.on('mouseenter', function() { if (timeout) { $interval.cancel(timeout); } }); scope.tapToast = function () { if (scope.options.tapToDismiss) { scope.close(true); } }; scope.close = function (wasClicked) { toastr.remove(scope.toastId, wasClicked); }; element.on('mouseleave', function() { if (scope.options.timeOut === 0 && scope.options.extendedTimeOut === 0) { return; } timeout = createTimeout(scope.options.extendedTimeOut); }); function createTimeout(time) { return $interval(function() { toastr.remove(scope.toastId); }, time, 1); } function wantsCloseButton() { return scope.options.closeHtml; } } } }()); angular.module("toastr").run(["$templateCache", function($templateCache) {$templateCache.put("templates/toastr/toastr.html","