salix/front/core/directives/focus.js

34 lines
703 B
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
2019-02-22 11:27:56 +00:00
export function focus(input) {
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (!input) {
console.warn(`vnFocus: Can't find a focusable element`);
return;
}
input.focus();
if (input.select)
input.select();
}
/**
2018-02-10 15:18:01 +00:00
* Sets the focus and selects the text on the input.
*
* @return {Object} The directive
*/
export function directive() {
return {
restrict: 'A',
2019-02-22 11:27:56 +00:00
link: function($scope, $element) {
$scope.$watch('', () => focus($element[0]));
}
};
}
2018-02-10 15:18:01 +00:00
ngModule.directive('vnFocus', directive);