2016-11-14 16:16:51 +03:00
|
|
|
/* global device */
|
2016-06-30 21:14:25 +03:00
|
|
|
import computed from 'ember-computed';
|
2016-06-20 17:20:25 +03:00
|
|
|
import GhostInput from 'ghost-admin/components/gh-input';
|
2015-10-28 14:36:45 +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
|
|
|
|
*/
|
2016-06-20 17:20:25 +03:00
|
|
|
const TrimFocusInputComponent = GhostInput.extend({
|
2016-06-18 14:44:23 +03:00
|
|
|
|
2016-06-20 17:20:25 +03:00
|
|
|
shouldFocus: true,
|
2016-06-18 14:44:23 +03:00
|
|
|
|
2016-06-20 17:20:25 +03:00
|
|
|
attributeBindings: ['autofocus'],
|
2014-11-30 22:27:09 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
autofocus: computed(function () {
|
2016-06-20 17:20:25 +03:00
|
|
|
if (this.get('shouldFocus')) {
|
2015-01-31 01:18:26 +03:00
|
|
|
return (device.ios()) ? false : 'autofocus';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-30 22:27:09 +03:00
|
|
|
}),
|
|
|
|
|
2016-06-20 17:20:25 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2015-12-15 14:09:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2016-06-20 17:20:25 +03:00
|
|
|
this._focus();
|
|
|
|
},
|
|
|
|
|
2017-01-23 15:03:05 +03:00
|
|
|
focusOut(event) {
|
|
|
|
this._trimInput(event.target.value);
|
|
|
|
},
|
|
|
|
|
|
|
|
_trimInput(value) {
|
|
|
|
if (value && typeof value.trim === 'function') {
|
|
|
|
value = value.trim();
|
2016-07-22 16:36:50 +03:00
|
|
|
}
|
2017-01-23 15:03:05 +03:00
|
|
|
|
|
|
|
this._processNewValue(value);
|
2016-06-18 14:44:23 +03:00
|
|
|
},
|
|
|
|
|
2016-06-20 17:20:25 +03:00
|
|
|
_focus() {
|
2016-06-18 14:44:23 +03:00
|
|
|
// Until mobile safari has better support
|
|
|
|
// for focusing, we just ignore it
|
2016-06-20 17:20:25 +03:00
|
|
|
if (this.get('shouldFocus') && !device.ios()) {
|
|
|
|
this.element.focus();
|
2016-06-18 14:44:23 +03: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
|
|
|
|
|
|
|
export default TrimFocusInputComponent;
|