"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('slaProgressBar', ['slaCalculatorService', '$interval', '$window', '$timeout', 'userModel', '$modal', function (slaCalculatorService, $interval, $window, $timeout, userModel, $modal) { return { restrict: 'EA', replace: false, scope: { ticket: '=' }, templateUrl: 'views/common/sla-progress-bar.html', link: function (scope) { //TODO: need dynamic auto refresh interval scope.isAccessibleUser = userModel.isAccessibleUser; scope.$watch(function () { return userModel.isAccessibleUser; }, function () { scope.isAccessibleUser = userModel.isAccessibleUser; }); scope.isRtl = window.isRtl; var progressBarUpdatePeriod = 60000, autoRefreshInterval, adjustSLAPositions = function () { _.each(scope.ticket.SLA.items, function (item) { item.left = Math.round(item.position * 100 < 1 ? 1 : item.position * 100); }); }, calculateSlaProgress = function () { if (scope.ticket.SLA.slaRenderType === 'partial') { slaCalculatorService.calculate(scope.ticket).then(adjustSLAPositions); } else { slaCalculatorService.getAndCalculateSLA(scope.ticket).then(adjustSLAPositions); } }, updateProgressBar = function () { scope.progressBar = { length: scope.ticket.SLA.reachedPercent > 100 ? 100 : (scope.ticket.SLA.reachedPercent < 1 ? 1 : scope.ticket.SLA.reachedPercent), type: scope.ticket.SLA.divClassType }; }, cancelAutoRefresh = function () { if (autoRefreshInterval) { $interval.cancel(autoRefreshInterval); } }, autoRefresh = function () { autoRefreshInterval = $interval(calculateSlaProgress, progressBarUpdatePeriod); }; $timeout(adjustSLAPositions); scope.$watch('ticket.SLA.reachedPercent', updateProgressBar); scope.$watch('ticket.SLA.autoRefresh', function (newValue) { if (newValue) { cancelAutoRefresh(); autoRefresh(); } else { cancelAutoRefresh(); } }); var resizeHandler = function () { adjustSLAPositions(); scope.$apply(); }; angular.element($window).bind('resize', resizeHandler); scope.$on('$destroy', function () { cancelAutoRefresh(); angular.element($window).off('resize', resizeHandler); }); scope.showSLA = function (sla) { console.log(sla); }; scope.showDetails = function () { return $modal.open({ templateUrl: 'views/common/sla-tooltip-detail.html', windowClass: 'action-blade-narrow', scope: scope }); }; } }; }]); })();