(function () { 'use strict'; var name = 'adNotificationBlock'; angular.module('app.directive') .directive('adNotificationBlock', ['$document', '$location', 'notification', function ($document, $location, notification) { return { replace: true, scope: {}, templateUrl: 'directive/' + name + '/' + name + '.html', link: function(scope, element, attrs) { scope.notification = notification; var link = element.find('[data-notification-link]'); var menu = element.find('[data-notification-menu]'); var menuOpened = false; var closeMenu = function (e) { if (!menuOpened) { return; } if (e) { e.preventDefault(); e.stopPropagation(); } $document.off('click.' + name, closeMenu); element.removeClass('open'); menuOpened = false; }; var openMenu = function () { if (menuOpened) { return; } element.addClass('open'); $document.on('click.' + name, closeMenu); menuOpened = true; }; scope.$watch('$location.path', function () { closeMenu(); }); scope.$watch(function () { return notification.list.length; }, function (length, oldLentgh) { if (notification.list.length === 0) { closeMenu(); } else if (length > oldLentgh) { openMenu(); } }); element.on('click.' + name, function () { closeMenu(); }); menu.on('click.' + name, function (e) { e.preventDefault(); e.stopPropagation(); }); link.on('click.' + name, function (e) { e.preventDefault(); e.stopPropagation(); if (menuOpened) { closeMenu(); } else if (!link.hasClass('disabled') && !link.prop('disabled')) { openMenu(); } }); } }; }]); })();