2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-02-08 16:28:23 +00:00
|
|
|
/**
|
2018-02-10 15:18:01 +00:00
|
|
|
* Sets the focus and selects the text on the input.
|
2017-10-18 12:33:34 +00:00
|
|
|
*
|
|
|
|
* @return {Object} The directive
|
2017-02-08 16:28:23 +00:00
|
|
|
*/
|
2017-01-31 13:13:06 +00:00
|
|
|
export function directive() {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2019-02-22 11:27:56 +00:00
|
|
|
link: function($scope, $element) {
|
|
|
|
$scope.$watch('', () => focus($element[0]));
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.directive('vnFocus', directive);
|