44 lines
990 B
JavaScript
44 lines
990 B
JavaScript
import ngModule from '../module';
|
|
import isMobile from '../lib/is-mobile';
|
|
|
|
export function focus($timeout, input) {
|
|
if (isMobile) return;
|
|
|
|
const element = input;
|
|
const selector = 'input, textarea, button, submit';
|
|
|
|
if (!input.matches(selector))
|
|
input = input.querySelector(selector);
|
|
|
|
if (!input) {
|
|
const focusEvent = new MouseEvent('focus', {
|
|
bubbles: true,
|
|
cancelable: true,
|
|
view: window
|
|
});
|
|
element.dispatchEvent(focusEvent);
|
|
return;
|
|
}
|
|
|
|
$timeout(() => {
|
|
input.focus();
|
|
if (input.select)
|
|
input.select();
|
|
});
|
|
}
|
|
|
|
/*
|
|
* Sets the focus and selects the text on the input.
|
|
*/
|
|
export function directive($timeout) {
|
|
return {
|
|
restrict: 'A',
|
|
link: function($scope, $element) {
|
|
$scope.$watch('', () => focus($timeout, $element[0]));
|
|
}
|
|
};
|
|
}
|
|
directive.$inject = ['$timeout'];
|
|
|
|
ngModule.directive('vnFocus', directive);
|