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

26 lines
684 B
JavaScript
Raw Normal View History

2017-02-07 13:34:26 +00:00
import {module} from '../module';
2017-02-07 13:34:26 +00:00
/**
2017-02-07 15:04:30 +00:00
* Sets the focus and selects the text on the input.
2017-02-07 13:34:26 +00:00
*/
export function directive() {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
2017-02-07 16:38:30 +00:00
let input = $element[0];
let isInput =
input instanceof HTMLInputElement ||
input instanceof HTMLTextAreaElement;
2017-02-07 15:04:30 +00:00
2017-02-07 16:52:11 +00:00
if(!isInput)
2017-02-07 16:38:30 +00:00
input = input.querySelector('input, textarea');
2017-02-07 15:04:30 +00:00
if(!input)
throw new Error(`vnFocus: Can't find an input element`);
input.focus();
input.select();
}
};
}
module.directive('vnFocus', directive);