Code coverage report for src/autofocus/autofocus.js

Statements: 100% (8 / 8)      Branches: 75% (3 / 4)      Functions: 100% (4 / 4)      Lines: 100% (8 / 8)      Ignored: none     

All files » src/autofocus/ » autofocus.js
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 57 58 59                                                                              1     3     4 9 9 4 4 4                
/**
 * @ngdoc directive
 * @name patternfly.autofocus:pfFocused
 * @restrict A
 * @element ANY
 * @param {expression=} pfFocused If the expression is true, the element is focused and selected (if possible).
 *
 * @description
 * The focus on element is evaluated from given expression. If the expression provided as an attribute to this directive
 * is evaluated as true, the element is selected (and focused).
 *
 * @example
 <example module="patternfly.autofocus">
 
 <file name="index.html">
   <div>
   <form class="form-horizontal">
 
     <div class="form-group">
       <label class="col-sm-2 control-label" for="i1">Focus next input:</label>
       <div class="col-sm-10">
         <input id="i1" ng-model="isFocus" type="checkbox"></input>
       </div>
     </div>
 
     <div class="form-group">
       <label class="col-sm-2 control-label" for="i2">Focused input:</label>
       <div class="col-sm-10">
         <input class="form-control" id="i1" ng-model="i2" pf-focused="isFocus" placeholder="This will be selected after checking the box above."></input>
       </div>
     </div>
 
   </form>
   </div>
 </file>
 
 </example>
 */
 
angular.module('patternfly.autofocus', []).directive('pfFocused', function ($timeout) {
  'use strict';
 
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      scope.$watch(attrs.pfFocused, function (newValue) {
        $timeout(function () {
          if (newValue) {
            element[0].focus();
            Eif (element[0].select) {
              element[0].select();
            }
          }
        });
      });
    }
  };
});