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) {
|
2019-07-02 10:12:15 +00:00
|
|
|
const element = input;
|
2019-02-22 11:27:56 +00:00
|
|
|
let selector = 'input, textarea, button, submit';
|
|
|
|
|
|
|
|
if (!input.matches(selector))
|
|
|
|
input = input.querySelector(selector);
|
|
|
|
|
|
|
|
if (!input) {
|
2019-07-02 10:12:15 +00:00
|
|
|
const focusEvent = new MouseEvent('focus', {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
view: window
|
|
|
|
});
|
|
|
|
element.dispatchEvent(focusEvent);
|
|
|
|
|
2019-02-22 11:27:56 +00:00
|
|
|
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);
|