2016-06-18 14:44:23 +03:00
|
|
|
import OneWayTextarea from 'ember-one-way-controls/components/one-way-textarea';
|
2016-05-24 15:06:59 +03:00
|
|
|
import TextInputMixin from 'ghost-admin/mixins/text-input';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {inject as injectService} from '@ember/service';
|
|
|
|
import {run} from '@ember/runloop';
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2016-06-18 14:44:23 +03:00
|
|
|
export default OneWayTextarea.extend(TextInputMixin, {
|
2017-05-18 11:01:30 +03:00
|
|
|
resizeDetector: injectService(),
|
|
|
|
|
|
|
|
classNames: 'gh-input',
|
|
|
|
|
|
|
|
autoExpand: false,
|
|
|
|
|
|
|
|
willInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// disable the draggable resize element that browsers add to textareas
|
|
|
|
if (this.get('autoExpand')) {
|
|
|
|
this.element.style.resize = 'none';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// set up resize handler on element insert so that we can autoexpand
|
|
|
|
// when the element container changes size
|
|
|
|
if (this.get('autoExpand')) {
|
|
|
|
run.scheduleOnce('afterRender', this, this._setupAutoExpand);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// trigger auto-expand any time the value changes
|
|
|
|
if (this.get('autoExpand')) {
|
|
|
|
run.scheduleOnce('afterRender', this, this._autoExpand);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._teardownAutoExpand();
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
_autoExpand() {
|
|
|
|
let el = this.element;
|
|
|
|
|
|
|
|
// collapse the element first so that we can shrink as well as expand
|
|
|
|
// then set the height to match the text height
|
2017-07-10 18:09:50 +03:00
|
|
|
if (el) {
|
|
|
|
el.style.height = 0;
|
|
|
|
el.style.height = `${el.scrollHeight}px`;
|
|
|
|
}
|
2017-05-18 11:01:30 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_setupAutoExpand() {
|
|
|
|
this._resizeCallback = run.bind(this, this._onResize);
|
|
|
|
this.get('resizeDetector').setup(this.get('autoExpand'), this._resizeCallback);
|
|
|
|
this._autoExpand();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onResize() {
|
|
|
|
this._autoExpand();
|
|
|
|
},
|
|
|
|
|
|
|
|
_teardownAutoExpand() {
|
|
|
|
this.get('resizeDetector').teardown(this.get('autoExpand'), this._resizeCallback);
|
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
});
|