Ghost/ghost/admin/app/mixins/text-input.js
Kevin Ansfield aa229973ec Remove ember-one-way-controls (#959)
closes https://github.com/TryGhost/Ghost/issues/9386
- remove usage of `{{one-way-checkbox}}` in favour of direct HTML+handlebars
- add `{{gh-text-input}}` that inherits from Ember's built-in `TextInput` component along with our custom `TextInput` mixin
- swap all uses of `{{gh-input}}` for `{{gh-text-input}}
- remove `{{gh-input}}` component
- update `{{gh-textarea}}` to inherit from Ember's `TextArea` component instead of `OneWayTextarea` and update all component uses accordingly
- update `{{gh-trim-focus-input}}` to inherit from `{{gh-text-input}}` and update all component uses accordingly
- standardize on using the `focus-out` action naming rather than `focusOut` for all text inputs, this is because the text input components (especially `{{gh-trim-focus-input}}`) have their own `focusOut` handler which gets overridden if consumers supply their own `focusOut` attr
- drop `ember-one-way-controls` package
- add `ember-one-way-select` package
2018-02-28 11:44:03 +00:00

84 lines
2.1 KiB
JavaScript

/* global device */
import Mixin from '@ember/object/mixin';
import {computed} from '@ember/object';
const keyCodes = {
13: 'Enter',
9: 'Tab'
};
export default Mixin.create({
attributeBindings: ['autofocus'],
selectOnClick: false,
shouldFocus: false,
stopEnterKeyDownPropagation: false,
autofocus: computed(function () {
if (this.get('shouldFocus')) {
return (device.ios()) ? false : 'autofocus';
}
return false;
}),
didInsertElement() {
this._super(...arguments);
this._focus();
},
click(event) {
if (this.get('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.get('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) {
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.get('shouldFocus') && !device.ios()) {
this.element.focus();
}
},
_getMethodFromKeyCode(keyCode) {
let methodName = keyCodes[keyCode.toString()];
return methodName;
}
});