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

100% Statements 16/16
66.67% Branches 4/6
100% Functions 7/7
100% Lines 16/16
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                              23×     23×   21×                                      
/**
 * @ngdoc directive
 * @name app.components.directive:feAutoFocus
 * @restrict E
 * @description
 * Set focus to an HTML element.
 *
 */
 
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .directive('feAutoFocus', directive);
 
  /** @ngInject */
  function directive($timeout) {
    return {
      restrict: 'A',
      link: function (scope, element, attr) {
        if (attr.feAutoFocus) {
          // fe-auto-focus has a value, so wait for event.
          scope.$on('fe-autoFocus', function (e, name) {
            Eif (name === attr.feAutoFocus) {
              setFocus();
            }
          });
        }
        else {
          // fe-auto-focus does not have a value, so focus when linked.
          var timer = $timeout(function () {
            setFocus();
          }, 1);
          scope.$on('$destroy', function () {
              $timeout.cancel(timer);
            }
          );
        }
 
        function setFocus() {
          Eif (angular.isDefined(scope.autoFocused)) {
            // Indicate that focus was set
            scope.autoFocused = element[0].name; 
          }
          element[0].focus();
        }
      }
    }
  }
})();