2014-11-30 22:27:09 +03:00
|
|
|
/*global device*/
|
2015-10-28 14:36:45 +03:00
|
|
|
import Ember from 'ember';
|
2016-06-18 14:44:23 +03:00
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
|
|
|
import {InvokeActionMixin} from 'ember-invoke-action';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
const {Component, computed} = Ember;
|
2015-08-19 14:55:40 +03:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
/**
|
|
|
|
* This doesn't override the OneWayInput component because
|
|
|
|
* we need finer control. It borrows
|
|
|
|
* parts from both the OneWayInput component and Ember's default
|
|
|
|
* input component
|
|
|
|
*/
|
|
|
|
const TrimFocusInputComponent = Component.extend(InvokeActionMixin, {
|
|
|
|
tagName: 'input',
|
|
|
|
type: 'text',
|
2014-08-08 17:30:51 +04:00
|
|
|
focus: true,
|
2015-08-25 22:36:40 +03:00
|
|
|
classNames: 'gh-input',
|
2016-06-18 14:44:23 +03:00
|
|
|
|
|
|
|
// This is taken from Ember's input component
|
|
|
|
attributeBindings: [
|
|
|
|
'autofocus',
|
|
|
|
'_value:value',
|
|
|
|
'accept',
|
|
|
|
'autocomplete',
|
|
|
|
'autosave',
|
|
|
|
'dir',
|
|
|
|
'formaction',
|
|
|
|
'formenctype',
|
|
|
|
'formmethod',
|
|
|
|
'formnovalidate',
|
|
|
|
'formtarget',
|
|
|
|
'height',
|
|
|
|
'inputmode',
|
|
|
|
'lang',
|
|
|
|
'list',
|
|
|
|
'multiple',
|
|
|
|
'name',
|
|
|
|
'pattern',
|
|
|
|
'size',
|
|
|
|
'step',
|
|
|
|
'type',
|
|
|
|
'value',
|
|
|
|
'width'
|
|
|
|
],
|
|
|
|
|
|
|
|
// These were in Ember's component
|
|
|
|
// so they are reproduced here
|
|
|
|
size: null,
|
|
|
|
pattern: null,
|
|
|
|
max: null,
|
|
|
|
min: null,
|
|
|
|
|
|
|
|
_value: boundOneWay('value'),
|
2014-11-30 22:27:09 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
autofocus: computed(function () {
|
2015-01-31 01:18:26 +03:00
|
|
|
if (this.get('focus')) {
|
|
|
|
return (device.ios()) ? false : 'autofocus';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-30 22:27:09 +03:00
|
|
|
}),
|
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
keyEvents: {
|
|
|
|
'13': 'onenter',
|
|
|
|
'27': 'onescape'
|
|
|
|
},
|
|
|
|
|
|
|
|
input() {
|
|
|
|
this._handleChangeEvent();
|
|
|
|
},
|
|
|
|
|
|
|
|
change() {
|
|
|
|
this._handleChangeEvent();
|
|
|
|
},
|
|
|
|
|
|
|
|
keyUp(event) {
|
|
|
|
this._interpretKeyEvents(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
_interpretKeyEvents(event) {
|
|
|
|
let method = this.get(`keyEvents.${event.keyCode}`);
|
|
|
|
|
|
|
|
if (method) {
|
|
|
|
this._sanitizedValue = null;
|
|
|
|
this._handleChangeEvent(method);
|
2014-08-08 17:30:51 +04:00
|
|
|
}
|
2015-12-15 14:09:34 +03:00
|
|
|
},
|
2014-06-23 21:50:28 +04:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
_handleChangeEvent(method = 'update') {
|
|
|
|
let value = this.readDOMAttr('value');
|
|
|
|
this.invokeAction(method, value);
|
|
|
|
},
|
|
|
|
|
2015-12-15 14:09:34 +03:00
|
|
|
_trimValue() {
|
2016-06-18 14:44:23 +03:00
|
|
|
let text = this.readDOMAttr('value');
|
|
|
|
this.invokeAction('update', text.trim());
|
2015-12-15 14:09:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2016-06-18 14:44:23 +03:00
|
|
|
this._setFocus();
|
|
|
|
},
|
|
|
|
|
|
|
|
_setFocus() {
|
|
|
|
// Until mobile safari has better support
|
|
|
|
// for focusing, we just ignore it
|
|
|
|
if (this.get('focus') && !device.ios()) {
|
|
|
|
this.$().focus();
|
|
|
|
}
|
2015-12-15 14:09:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
focusOut() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._trimValue();
|
|
|
|
}
|
2014-06-23 21:50:28 +04:00
|
|
|
});
|
2016-06-18 14:44:23 +03:00
|
|
|
|
|
|
|
TrimFocusInputComponent.reopenClass({
|
|
|
|
positionalParams: ['value']
|
|
|
|
});
|
|
|
|
|
|
|
|
export default TrimFocusInputComponent;
|