discourse/app/assets/javascripts/discourse-common/components/combo-box.js.es6

146 lines
4.0 KiB
Plaintext
Raw Normal View History

import { bufferedRender } from 'discourse-common/lib/buffered-render';
import { on, observes } from 'ember-addons/ember-computed-decorators';
export default Ember.Component.extend(bufferedRender({
tagName: 'select',
attributeBindings: ['tabindex', 'disabled'],
classNames: ['combobox'],
valueAttribute: 'id',
nameProperty: 'name',
buildBuffer(buffer) {
const nameProperty = this.get('nameProperty');
const none = this.get('none');
let noneValue = null;
2013-07-13 04:24:15 +08:00
// Add none option if required
if (typeof none === "string") {
buffer.push('<option value="">' + I18n.t(none) + "</option>");
} else if (typeof none === "object") {
noneValue = Em.get(none, this.get('valueAttribute'));
buffer.push(`<option value="${noneValue}">${Em.get(none, nameProperty)}</option>`);
}
2015-03-24 01:04:33 +08:00
let selected = this.get('value');
if (!Em.isNone(selected)) { selected = selected.toString(); }
let selectedFound = false;
let firstVal = undefined;
const content = this.get('content');
if (content) {
let first = true;
content.forEach(o => {
let val = o[this.get('valueAttribute')];
if (typeof val === "undefined") { val = o; }
if (!Em.isNone(val)) { val = val.toString(); }
2015-03-24 01:04:33 +08:00
const selectedText = (val === selected) ? "selected" : "";
const name = Handlebars.Utils.escapeExpression(Ember.get(o, nameProperty) || o);
if (val === selected) {
selectedFound = true;
}
if (first) {
firstVal = val;
first = false;
}
buffer.push(`<option ${selectedText} value="${val}">${name}</option>`);
});
}
if (!selectedFound && !noneValue) {
if (none) {
this.set('value', null);
} else {
this.set('value', firstVal);
}
}
Ember.run.scheduleOnce('afterRender', this, this._updateSelect2);
},
@observes('value')
valueChanged() {
2015-03-24 01:04:33 +08:00
const $combo = this.$(),
val = this.get('value');
if (val !== undefined && val !== null) {
2015-03-03 01:12:19 +08:00
$combo.select2('val', val.toString());
} else {
2015-03-03 01:12:19 +08:00
$combo.select2('val', null);
}
},
@observes('content.[]')
_rerenderOnChange() {
this.rerenderBuffer();
},
didInsertElement() {
this._super();
// Workaround for https://github.com/emberjs/ember.js/issues/9813
// Can be removed when fixed. Without it, the wrong option is selected
this.$('option').each((i, o) => o.selected = !!$(o).attr('selected'));
2015-07-01 07:00:43 +08:00
// observer for item names changing (optional)
if (this.get('nameChanges')) {
this.addObserver('content.@each.' + this.get('nameProperty'), this.rerenderBuffer);
2015-07-01 07:00:43 +08:00
}
const $elem = this.$();
const caps = this.capabilities;
const minimumResultsForSearch = this.get('minimumResultsForSearch') || ((caps && caps.isIOS) ? -1 : 5);
if (!this.get("selectionTemplate") && this.get("selectionIcon")) {
this.selectionTemplate = (item) => {
let name = Em.get(item, 'text');
name = Handlebars.escapeExpression(name);
return `<i class='fa fa-${this.get("selectionIcon")}'></i>${name}`;
};
}
const options = {
minimumResultsForSearch,
width: this.get('width') || 'resolve',
2016-05-27 14:34:44 +08:00
allowClear: true
};
if (this.comboTemplate) {
options.formatResult = this.comboTemplate.bind(this);
}
if (this.selectionTemplate) {
options.formatSelection = this.selectionTemplate.bind(this);
}
$elem.select2(options);
2015-03-24 01:04:33 +08:00
const castInteger = this.get('castInteger');
$elem.on("change", e => {
2015-03-24 01:04:33 +08:00
let val = $(e.target).val();
if (val && val.length && castInteger) {
val = parseInt(val, 10);
}
this.set('value', val);
});
Ember.run.scheduleOnce('afterRender', this, this._triggerChange);
},
_updateSelect2() {
this.$().trigger('change.select2');
},
_triggerChange() {
this.$().trigger('change');
},
@on('willDestroyElement')
_destroyDropdown() {
2015-03-24 01:04:33 +08:00
this.$().select2('destroy');
}
}));