2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
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',
|
|
|
|
link: function($scope, $element, $attrs) {
|
2017-02-08 16:28:23 +00:00
|
|
|
$scope.$watch('', function() {
|
|
|
|
let input = $element[0];
|
|
|
|
let selector = 'input, textarea, button, submit';
|
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
if (!input.matches(selector))
|
2017-02-08 16:28:23 +00:00
|
|
|
input = input.querySelector(selector);
|
2017-02-08 16:37:54 +00:00
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
if (!input) {
|
2017-02-08 16:34:39 +00:00
|
|
|
console.warn(`vnFocus: Can't find a focusable element`);
|
2017-02-08 16:37:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-02-08 16:28:23 +00:00
|
|
|
|
|
|
|
input.focus();
|
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
if (input.select)
|
2017-02-08 16:28:23 +00:00
|
|
|
input.select();
|
2017-02-08 13:45:27 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.directive('vnFocus', directive);
|