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

26 lines
684 B
JavaScript

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) {
let input = $element[0];
let isInput =
input instanceof HTMLInputElement ||
input instanceof HTMLTextAreaElement;
if(!isInput)
input = input.querySelector('input, textarea');
if(!input)
throw new Error(`vnFocus: Can't find an input element`);
input.focus();
input.select();
}
};
}
module.directive('vnFocus', directive);