2017-02-07 13:34:26 +00:00
|
|
|
import {module} from '../module';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
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
|
|
|
*/
|
2017-01-31 13:13:06 +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`);
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
input.focus();
|
|
|
|
input.select();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
module.directive('vnFocus', directive);
|