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