xref: /openbmc/phosphor-webui/app/common/directives/input.js (revision b7ea279015fd33574eeab24b114b19d0cdb22e3c)
1*b7ea2790SGunnar Millswindow.angular && (function(angular) {
2*b7ea2790SGunnar Mills  'use strict';
3*b7ea2790SGunnar Mills
4*b7ea2790SGunnar Mills  angular.module('app.common.directives')
5*b7ea2790SGunnar Mills      .directive('setFocusOnNewInput', function() {
6*b7ea2790SGunnar Mills        return function(scope, element, attrs) {
7*b7ea2790SGunnar Mills          var elem = window.document.getElementById(element[0].id);
8*b7ea2790SGunnar Mills          // Focus on the newly created input.
9*b7ea2790SGunnar Mills          // Since this directive is also called when initializing fields
10*b7ea2790SGunnar Mills          // on a page load, need to determine if the call is from a page load
11*b7ea2790SGunnar Mills          // or from the user pressing the "Add new" button. The easiest way to
12*b7ea2790SGunnar Mills          // do this is to check if the field is empty, if it is we know this is
13*b7ea2790SGunnar Mills          // a new field since all empty fields are removed from the array.
14*b7ea2790SGunnar Mills          if (!scope[attrs.ngModel] && elem) {
15*b7ea2790SGunnar Mills            elem.focus();
16*b7ea2790SGunnar Mills          }
17*b7ea2790SGunnar Mills        };
18*b7ea2790SGunnar Mills      });
19*b7ea2790SGunnar Mills})(window.angular);
20