2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import {computed} from '@ember/object';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2018-02-28 14:44:03 +03:00
|
|
|
const keyCodes = {
|
|
|
|
13: 'Enter',
|
|
|
|
9: 'Tab'
|
|
|
|
};
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Mixin.create({
|
2017-05-18 11:01:30 +03:00
|
|
|
attributeBindings: ['autofocus'],
|
|
|
|
|
2014-04-20 18:48:34 +04:00
|
|
|
selectOnClick: false,
|
2017-05-18 11:01:30 +03:00
|
|
|
shouldFocus: false,
|
2014-08-03 07:17:25 +04:00
|
|
|
stopEnterKeyDownPropagation: false,
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2022-03-07 13:06:35 +03:00
|
|
|
constructor() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
|
|
},
|
|
|
|
|
2017-05-18 11:01:30 +03:00
|
|
|
autofocus: computed(function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.shouldFocus) {
|
2022-03-07 13:06:35 +03:00
|
|
|
return (this._isIOS) ? false : 'autofocus';
|
2017-05-18 11:01:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}),
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._focus();
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
click(event) {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.selectOnClick) {
|
2014-04-20 18:48:34 +04:00
|
|
|
event.currentTarget.select();
|
|
|
|
}
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2022-02-21 20:54:33 +03:00
|
|
|
input(event) {
|
|
|
|
this['on-input']?.(event);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
keyDown(event) {
|
2014-08-03 07:17:25 +04:00
|
|
|
// stop event propagation when pressing "enter"
|
2017-05-18 11:01:30 +03:00
|
|
|
// most useful in the case when undesired (global) keyboard shortcuts
|
|
|
|
// are getting triggered while interacting with this particular input element.
|
2019-03-06 16:53:54 +03:00
|
|
|
if (event.keyCode === 13 && this.stopEnterKeyDownPropagation) {
|
2014-08-03 07:17:25 +04:00
|
|
|
event.stopPropagation();
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-08-03 07:17:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-18 11:01:30 +03:00
|
|
|
|
|
|
|
// prevent default TAB behaviour if we have a keyEvent for it
|
2018-02-28 14:44:03 +03:00
|
|
|
if (event.keyCode === 9 && typeof this.get('keyEvents.Tab') === 'function') {
|
2017-05-18 11:01:30 +03:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
keyPress(event) {
|
|
|
|
// prevent default ENTER behaviour if we have a keyEvent for it
|
2018-02-28 14:44:03 +03:00
|
|
|
if (event.keyCode === 13 && typeof this.get('keyEvents.Enter') === 'function') {
|
2017-05-18 11:01:30 +03:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
2018-02-28 14:44:03 +03:00
|
|
|
keyUp(event) {
|
2018-05-01 16:54:37 +03:00
|
|
|
if (event.keyCode) {
|
|
|
|
let methodName = this._getMethodFromKeyCode(event.keyCode);
|
|
|
|
let method = this.get(`keyEvents.${methodName}`);
|
|
|
|
if (method) {
|
|
|
|
method(event.target.value);
|
|
|
|
}
|
2018-02-28 14:44:03 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-18 11:01:30 +03:00
|
|
|
_focus() {
|
|
|
|
// Until mobile safari has better support
|
|
|
|
// for focusing, we just ignore it
|
2022-03-07 13:06:35 +03:00
|
|
|
if (this.shouldFocus && !this._isIOS) {
|
2017-05-18 11:01:30 +03:00
|
|
|
this.element.focus();
|
|
|
|
}
|
2018-02-28 14:44:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_getMethodFromKeyCode(keyCode) {
|
|
|
|
let methodName = keyCodes[keyCode.toString()];
|
|
|
|
return methodName;
|
2014-04-20 18:48:34 +04:00
|
|
|
}
|
|
|
|
});
|