all files / app/components/waitIndicator/ waitIndicator.directive.js

100% Statements 18/18
100% Branches 2/2
100% Functions 7/7
100% Lines 18/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                                           
/**
 * @ngdoc directive
 * @name app.components.directive:feWaitIndicator
 * @restrict E
 * @description
 * Displays a spinner when the application is in a waiting state
 *
 */
 
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .directive('feWaitIndicator', directive);
 
  /** @ngInject */
  function directive() {
    var directive = {
      restrict: 'E',
      templateUrl: 'app/components/waitIndicator/waitIndicator.html',
      controller: Controller,
      controllerAs: 'vm'
    };
 
    return directive;
  }
 
  /** @ngInject */
  function Controller(waitingState, $scope, $timeout, waitInterval) {
    var vm = this;
    vm.waiting = false;
 
    activate();
 
    function activate() {
      waitingState.subscribeChanged($scope, changed);
    }
 
    function changed() {
      if (waitingState.waiting()) {
        // Wait before showing indicator
        var timer = $timeout(function () {
            vm.waiting = waitingState.waiting();
          },
          waitInterval);
        $scope.$on('$destroy', function() {
          $timeout.cancel(timer);
        });
      }
      else
        vm.waiting = false;
    }
  }
})();