Ghost/ghost/admin/app/mixins/text-input.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

88 lines
2.2 KiB
JavaScript

import Mixin from '@ember/object/mixin';
import {computed} from '@ember/object';
import {inject as service} from '@ember/service';
const keyCodes = {
13: 'Enter',
9: 'Tab'
};
export default Mixin.create({
userAgent: service(),
attributeBindings: ['autofocus'],
selectOnClick: false,
shouldFocus: false,
stopEnterKeyDownPropagation: false,
autofocus: computed(function () {
if (this.shouldFocus) {
return (this.userAgent.os.isIOS) ? false : 'autofocus';
}
return false;
}),
didInsertElement() {
this._super(...arguments);
this._focus();
},
click(event) {
if (this.selectOnClick) {
event.currentTarget.select();
}
},
keyDown(event) {
// stop event propagation when pressing "enter"
// most useful in the case when undesired (global) keyboard shortcuts
// are getting triggered while interacting with this particular input element.
if (event.keyCode === 13 && this.stopEnterKeyDownPropagation) {
event.stopPropagation();
return true;
}
// prevent default TAB behaviour if we have a keyEvent for it
if (event.keyCode === 9 && typeof this.get('keyEvents.Tab') === 'function') {
event.preventDefault();
}
this._super(...arguments);
},
keyPress(event) {
// prevent default ENTER behaviour if we have a keyEvent for it
if (event.keyCode === 13 && typeof this.get('keyEvents.Enter') === 'function') {
event.preventDefault();
}
this._super(...arguments);
},
keyUp(event) {
if (event.keyCode) {
let methodName = this._getMethodFromKeyCode(event.keyCode);
let method = this.get(`keyEvents.${methodName}`);
if (method) {
method(event.target.value);
}
}
},
_focus() {
// Until mobile safari has better support
// for focusing, we just ignore it
if (this.shouldFocus && !this.userAgent.os.isIOS) {
this.element.focus();
}
},
_getMethodFromKeyCode(keyCode) {
let methodName = keyCodes[keyCode.toString()];
return methodName;
}
});