(function () { 'use strict'; var temporaryTimeout = 10000; var name = 'adNotificationBlockItem'; angular.module('app.directive') .directive('adNotificationBlockItem', ['$timeout', 'notification', function ($timeout, notification) { return { templateUrl: 'directive/adNotificationBlock/' + name + '.html', scope: { item: '=' + name }, link: function (scope, element, attr) { var cleanup = angular.noop, time, startTime, timerId, onTimeout, pauseTimer, startTimer; if (scope.item.temporary) { time = temporaryTimeout; cleanup = function (unbind) { if (timerId) { $timeout.cancel(timerId); timerId = null; } if (unbind) { element.off('mouseenter', pauseTimer); element.off('mouseleave', startTimer); } }; onTimeout = function () { cleanup(true); scope.close(); }; pauseTimer = function () { cleanup(); time -= Date.now() - startTime; startTime = 0; }; startTimer = function () { cleanup(); if (time > 0) { time < 1000 && (time = 1000); startTime = Date.now(); timerId = $timeout(onTimeout, time); } }; element.on('mouseenter', pauseTimer); element.on('mouseleave', startTimer); startTimer(); } scope.close = function () { cleanup(true); notification.remove(scope.item); }; scope.$on('$destroy', function () { cleanup(true); }); } }; }]); })();