2017-08-22 10:53:26 +03:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import {computed} from '@ember/object';
|
2019-03-05 20:34:00 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
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({
|
2019-03-05 20:34:00 +03:00
|
|
|
userAgent: service(),
|
|
|
|
|
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
|
|
|
|
2017-05-18 11:01:30 +03:00
|
|
|
autofocus: computed(function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.shouldFocus) {
|
2019-03-05 20:34:00 +03:00
|
|
|
return (this.userAgent.os.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
|
|
|
|
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
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.shouldFocus && !this.userAgent.os.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
|
|
|
}
|
|
|
|
});
|