{"size": 687, "ext": "js", "max_stars_repo_path": "resources/kcd/directives/kcd-autofocus.js", "max_stars_repo_name": "kentcdodds/kcd-angular", "max_stars_repo_stars_event_min_datetime": "2015-01-30T21:57:38.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-17T00:04:54.000Z", "max_issues_repo_path": "resources/kcd/directives/kcd-autofocus.js", "max_issues_repo_name": "kentcdodds/kcd-angular", "max_issues_repo_issues_event_min_datetime": "2015-06-19T13:32:38.000Z", "max_issues_repo_issues_event_max_datetime": "2016-09-16T13:44:20.000Z", "max_forks_repo_path": "resources/kcd/directives/kcd-autofocus.js", "max_forks_repo_name": "kentcdodds/kcd-angular", "max_forks_repo_forks_event_min_datetime": "2015-05-18T03:11:57.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-22T22:57:45.000Z"}
{"max_stars_count": 26, "max_issues_count": 7, "max_forks_count": 8, "avg_line_length": 28.625, "max_line_length": 90, "alphanum_fraction": 0.5181950509}
angular.module('kcd.directives').directive('kcdAutofocus', function($timeout, $document) {
  'use strict';
  return {
    link: function(scope, element, attrs) {
      var previousEl = null;
      var el = element[0];
      var doc = $document[0];
      attrs.$observe('kcdAutofocus', function(value) {
        if(value && value !== 'false') {
          $timeout(function() {
            previousEl = doc.activeElement;
            el.focus();
          }, ~~attrs.focusWait);
        } else {
          if (previousEl && attrs.hasOwnProperty('refocus') && doc.activeElement === el) {
            el.blur();
            previousEl.focus();
          }
        }
      });
    }
  };
});
