(function () { "use strict"; angular.module("EnrApp") .directive("busyWorking", [function () { return { restrict: "E", templateUrl: "Scripts/EnrAngular/Directives/busyWorking.html", scope: {}, controller: busyWorkingController }; }]); busyWorkingController.$inject = ["$scope", "$timeout"]; function busyWorkingController($scope, $timeout) { var modalElement = $('#myModal'); var isDisplayed = false; var pushCounter = 0; var displayDelayInterval = 0; var delayedDisplayPromise = null; $scope.$on("PUSH_BUSY_WORKING", handlePushBusyWorkingEvent); $scope.$on("POP_BUSY_WORKING", handlePopBusyWorkingEvent); function handlePushBusyWorkingEvent() { pushCounter++; var shouldDisplayBusyWorking = !isDisplayed && delayedDisplayPromise === null; if (shouldDisplayBusyWorking) { if (displayDelayInterval > 0) delayedDisplayPromise = $timeout(displayBusyWorking, displayDelayInterval); else displayBusyWorking(); } } function handlePopBusyWorkingEvent() { pushCounter--; if (pushCounter < 0) pushCounter = 0; if (pushCounter === 0) { if (delayedDisplayPromise !== null) { $timeout.cancel(delayedDisplayPromise); delayedDisplayPromise = null; } if (isDisplayed) removeBusyWorking(); } } function displayBusyWorking() { isDisplayed = true; modalElement.modal({ backdrop: 'static' }); delayedDisplayPromise = null; } function removeBusyWorking() { isDisplayed = false; modalElement.modal('hide'); } } }());