salix/front/core/directives/focus.js

41 lines
856 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) {
const element = input;
2019-02-22 11:27:56 +00:00
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (!input) {
const focusEvent = new MouseEvent('focus', {
bubbles: true,
cancelable: true,
view: window
});
element.dispatchEvent(focusEvent);
2019-02-22 11:27:56 +00:00
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);