2016-03-10 12:20:58 -05:00
import { on, observes } from 'ember-addons/ember-computed-decorators';
2015-04-28 17:05:06 -04:00
export default Ember.Component.extend({
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
2015-03-23 13:04:33 -04:00
render(buffer) {
2015-07-28 12:29:40 -04:00
const nameProperty = this.get('nameProperty');
const none = this.get('none');
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") {
2016-03-10 12:20:58 -05:00
buffer.push("<option value=\"\">" + 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
2013-02-22 15:41:12 -05:00
if (this.get('content')) {
2016-03-10 12:20:58 -05:00
this.get('content').forEach(o => {
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);
buffer.push(`<option ${selectedText} value="${val}">${name}</option>`);
2013-02-20 13:15:50 -05:00
});
}
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 20:45:31 +02: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() {
2014-03-25 15:29:13 +05:30
this.rerender();
2016-03-10 12:20:58 -05:00
},
2014-03-25 15:29:13 +05:30
2016-03-10 12:20:58 -05:00
@on('didInsertElement')
_initializeCombo() {
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 16:00:43 -07:00
// observer for item names changing (optional)
if (this.get('nameChanges')) {
2015-07-28 12:29:40 -04:00
this.addObserver('content.@each.' + this.get('nameProperty'), this.rerender);
2015-06-30 16:00:43 -07:00
}
2015-06-30 12:23:02 -07:00
2015-07-28 12:29:40 -04:00
const $elem = this.$();
2016-08-31 13:35:49 -04:00
const caps = this.capabilities;
const minimumResultsForSearch = (caps && caps.isIOS) ? -1 : 5;
2016-05-27 14:34:44 +08:00
$elem.select2({
formatResult: this.comboTemplate, minimumResultsForSearch,
2016-08-31 13:35:49 -04:00
width: this.get('width') || 'resolve',
2016-05-27 14:34:44 +08:00
allowClear: true
});
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 11:47:02 -07: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
});
2015-06-30 10:13:07 -07:00
$elem.trigger('change');
2016-03-10 12:20:58 -05:00
},
2014-03-25 15:29:13 +05:30
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
});