src/directives/toast/toast.directive.js
(function() {
'use strict';
angular.module('toastr')
.directive('toast', toast);
toast.$inject = ['$injector', '$interval', 'toastrConfig', 'toastr'];
function toast($injector, $interval, toastrConfig, toastr) {
return {
templateUrl: function() {
return toastrConfig.templates.toast;
},
controller: 'ToastController',
link: toastLinkFunction
};
function toastLinkFunction(scope, element, attrs, toastCtrl) {
var timeout;
scope.toastClass = scope.options.toastClass;
scope.titleClass = scope.options.titleClass;
scope.messageClass = scope.options.messageClass;
scope.progressBar = scope.options.progressBar;
if (wantsCloseButton()) {
var button = angular.element(scope.options.closeHtml),
$compile = $injector.get('$compile');
button.addClass('toast-close-button');
button.attr('ng-click', 'close(true, $event)');
$compile(button)(scope);
element.children().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() {
hideAndStopProgressBar();
if (timeout) {
$interval.cancel(timeout);
}
});
scope.tapToast = function () {
if (angular.isFunction(scope.options.onTap)) {
scope.options.onTap();
}
if (scope.options.tapToDismiss) {
scope.close(true);
}
};
scope.close = function (wasClicked, $event) {
if ($event && angular.isFunction($event.stopPropagation)) {
$event.stopPropagation();
}
toastr.remove(scope.toastId, wasClicked);
};
scope.refreshTimer = function(newTime) {
if (timeout) {
$interval.cancel(timeout);
timeout = createTimeout(newTime || scope.options.timeOut);
}
};
element.on('mouseleave', function() {
if (scope.options.timeOut === 0 && scope.options.extendedTimeOut === 0) { return; }
scope.$apply(function() {
scope.progressBar = scope.options.progressBar;
});
timeout = createTimeout(scope.options.extendedTimeOut);
});
function createTimeout(time) {
toastCtrl.startProgressBar(time);
return $interval(function() {
toastCtrl.stopProgressBar();
toastr.remove(scope.toastId);
}, time, 1);
}
function hideAndStopProgressBar() {
scope.progressBar = false;
toastCtrl.stopProgressBar();
}
function wantsCloseButton() {
return scope.options.closeHtml;
}
}
}
}());