salix/client/core/src/directives/focus.js

31 lines
799 B
JavaScript
Raw Normal View History

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