33 lines
840 B
JavaScript
33 lines
840 B
JavaScript
import ngModule from '../module';
|
|
|
|
/**
|
|
* Sets the focus and selects the text on the input.
|
|
*
|
|
* @return {Object} The directive
|
|
*/
|
|
export function directive() {
|
|
return {
|
|
restrict: 'A',
|
|
link: function($scope, $element, $attrs) {
|
|
$scope.$watch('', function() {
|
|
let input = $element[0];
|
|
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();
|
|
});
|
|
}
|
|
};
|
|
}
|
|
ngModule.directive('vnFocus', directive);
|