2017-02-07 13:34:26 +00:00
|
|
|
import {module} from '../module';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-02-08 16:28:23 +00:00
|
|
|
/**
|
|
|
|
* Sets the focus and selects the text on the input.
|
|
|
|
*/
|
2017-01-31 13:13:06 +00:00
|
|
|
export function directive() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function($scope, $element, $attrs) {
|
2017-02-08 16:28:23 +00:00
|
|
|
$scope.$watch('', function() {
|
|
|
|
let input = $element[0];
|
|
|
|
let selector = 'input, textarea, button, submit';
|
|
|
|
|
|
|
|
if(!input.matches(selector))
|
|
|
|
input = input.querySelector(selector);
|
|
|
|
if(!input)
|
|
|
|
throw new Error(`vnFocus: Can't find a focusable element`);
|
|
|
|
|
|
|
|
input.focus();
|
|
|
|
|
|
|
|
if(input.select)
|
|
|
|
input.select();
|
2017-02-08 13:45:27 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
module.directive('vnFocus', directive);
|