2016-03-11 01:20:58 +08:00
|
|
|
import { on, observes } from 'ember-addons/ember-computed-decorators';
|
|
|
|
|
2015-04-29 05:05:06 +08:00
|
|
|
export default Ember.Component.extend({
|
2014-12-05 04:21:47 +08:00
|
|
|
tagName: 'select',
|
2016-06-18 01:40:32 +08:00
|
|
|
attributeBindings: ['tabindex', 'disabled'],
|
2013-02-23 04:41:12 +08:00
|
|
|
classNames: ['combobox'],
|
|
|
|
valueAttribute: 'id',
|
2015-07-29 00:29:40 +08:00
|
|
|
nameProperty: 'name',
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2015-03-24 01:04:33 +08:00
|
|
|
render(buffer) {
|
2015-07-29 00:29:40 +08:00
|
|
|
const nameProperty = this.get('nameProperty');
|
|
|
|
const none = this.get('none');
|
2013-07-13 04:24:15 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
// Add none option if required
|
2014-02-11 00:15:42 +08:00
|
|
|
if (typeof none === "string") {
|
|
|
|
buffer.push('<option value="">' + I18n.t(none) + "</option>");
|
|
|
|
} else if (typeof none === "object") {
|
2016-03-11 01:20:58 +08:00
|
|
|
buffer.push("<option value=\"\">" + Em.get(none, nameProperty) + "</option>");
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
|
|
|
|
2015-03-24 01:04:33 +08:00
|
|
|
let selected = this.get('value');
|
2014-07-05 01:11:43 +08:00
|
|
|
if (!Em.isNone(selected)) { selected = selected.toString(); }
|
2013-05-21 01:42:26 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
if (this.get('content')) {
|
2016-03-11 01:20:58 +08:00
|
|
|
this.get('content').forEach(o => {
|
|
|
|
let val = o[this.get('valueAttribute')];
|
2015-08-06 04:19:21 +08:00
|
|
|
if (typeof val === "undefined") { val = o; }
|
2014-07-15 03:15:30 +08:00
|
|
|
if (!Em.isNone(val)) { val = val.toString(); }
|
2013-06-08 00:13:46 +08:00
|
|
|
|
2015-03-24 01:04:33 +08:00
|
|
|
const selectedText = (val === selected) ? "selected" : "";
|
2016-03-11 01:20:58 +08:00
|
|
|
const name = Handlebars.Utils.escapeExpression(Ember.get(o, nameProperty) || o);
|
|
|
|
buffer.push(`<option ${selectedText} value="${val}">${name}</option>`);
|
2013-02-21 02:15:50 +08:00
|
|
|
});
|
|
|
|
}
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
2016-03-11 01:20:58 +08:00
|
|
|
@observes('value')
|
|
|
|
valueChanged() {
|
2015-03-24 01:04:33 +08:00
|
|
|
const $combo = this.$(),
|
2015-07-29 00:29:40 +08:00
|
|
|
val = this.get('value');
|
|
|
|
|
2013-06-08 02:45:31 +08:00
|
|
|
if (val !== undefined && val !== null) {
|
2015-03-03 01:12:19 +08:00
|
|
|
$combo.select2('val', val.toString());
|
2013-05-21 01:42:26 +08:00
|
|
|
} else {
|
2015-03-03 01:12:19 +08:00
|
|
|
$combo.select2('val', null);
|
2013-05-21 01:42:26 +08:00
|
|
|
}
|
2016-03-11 01:20:58 +08:00
|
|
|
},
|
2013-05-21 01:42:26 +08:00
|
|
|
|
2016-04-29 04:49:24 +08:00
|
|
|
@observes('content.[]')
|
2016-03-11 01:20:58 +08:00
|
|
|
_rerenderOnChange() {
|
2014-03-25 17:59:13 +08:00
|
|
|
this.rerender();
|
2016-03-11 01:20:58 +08:00
|
|
|
},
|
2014-03-25 17:59:13 +08:00
|
|
|
|
2016-03-11 01:20:58 +08:00
|
|
|
@on('didInsertElement')
|
|
|
|
_initializeCombo() {
|
2013-06-08 00:13:46 +08:00
|
|
|
|
2014-12-05 04:21:47 +08: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-11 01:20:58 +08:00
|
|
|
this.$('option').each((i, o) => o.selected = !!$(o).attr('selected'));
|
2014-12-05 04:21:47 +08:00
|
|
|
|
2015-07-01 07:00:43 +08:00
|
|
|
// observer for item names changing (optional)
|
|
|
|
if (this.get('nameChanges')) {
|
2015-07-29 00:29:40 +08:00
|
|
|
this.addObserver('content.@each.' + this.get('nameProperty'), this.rerender);
|
2015-07-01 07:00:43 +08:00
|
|
|
}
|
2015-07-01 03:23:02 +08:00
|
|
|
|
2015-07-29 00:29:40 +08:00
|
|
|
const $elem = this.$();
|
2016-09-01 01:35:49 +08: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-09-01 01:35:49 +08:00
|
|
|
width: this.get('width') || 'resolve',
|
2016-05-27 14:34:44 +08:00
|
|
|
allowClear: true
|
|
|
|
});
|
2013-05-21 01:42:26 +08:00
|
|
|
|
2015-03-24 01:04:33 +08:00
|
|
|
const castInteger = this.get('castInteger');
|
2015-12-12 04:07:38 +08:00
|
|
|
$elem.on("change", e => {
|
2015-03-24 01:04:33 +08:00
|
|
|
let val = $(e.target).val();
|
2015-07-01 02:47:02 +08:00
|
|
|
if (val && val.length && castInteger) {
|
2015-01-03 02:47:07 +08:00
|
|
|
val = parseInt(val, 10);
|
|
|
|
}
|
2015-12-12 04:07:38 +08:00
|
|
|
this.set('value', val);
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
2015-07-01 01:13:07 +08:00
|
|
|
$elem.trigger('change');
|
2016-03-11 01:20:58 +08:00
|
|
|
},
|
2014-03-25 17:59:13 +08:00
|
|
|
|
2016-03-11 01:20:58 +08:00
|
|
|
@on('willDestroyElement')
|
|
|
|
_destroyDropdown() {
|
2015-03-24 01:04:33 +08:00
|
|
|
this.$().select2('destroy');
|
2016-03-11 01:20:58 +08:00
|
|
|
}
|
2013-02-23 04:41:12 +08:00
|
|
|
|
|
|
|
});
|