2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2019-10-28 16:31:33 +00:00
|
|
|
import isMobile from '../lib/is-mobile';
|
2019-10-15 14:19:35 +00:00
|
|
|
|
2020-04-01 14:05:43 +00:00
|
|
|
export function focus($timeout, input) {
|
2019-10-15 14:19:35 +00:00
|
|
|
if (isMobile) return;
|
|
|
|
|
2019-07-02 10:12:15 +00:00
|
|
|
const element = input;
|
2020-04-01 14:05:43 +00:00
|
|
|
const selector = 'input, textarea, button, submit';
|
2019-02-22 11:27:56 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:05:43 +00:00
|
|
|
$timeout(() => {
|
|
|
|
input.focus();
|
|
|
|
if (input.select)
|
2019-10-30 15:57:14 +00:00
|
|
|
input.select();
|
2020-04-01 14:05:43 +00:00
|
|
|
});
|
2019-02-22 11:27:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 14:05:43 +00:00
|
|
|
/*
|
2018-02-10 15:18:01 +00:00
|
|
|
* Sets the focus and selects the text on the input.
|
2017-02-08 16:28:23 +00:00
|
|
|
*/
|
2020-04-01 14:05:43 +00:00
|
|
|
export function directive($timeout) {
|
2017-01-31 13:13:06 +00:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2019-02-22 11:27:56 +00:00
|
|
|
link: function($scope, $element) {
|
2020-04-01 14:05:43 +00:00
|
|
|
$scope.$watch('', () => focus($timeout, $element[0]));
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-04-01 14:05:43 +00:00
|
|
|
directive.$inject = ['$timeout'];
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.directive('vnFocus', directive);
|