2018-01-20 00:46:47 -08:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { observer, get } from '@ember/object';
|
2017-02-13 14:58:44 -08:00
|
|
|
|
2018-01-20 00:46:47 -08:00
|
|
|
export default Component.extend({
|
2017-03-28 11:34:04 -07:00
|
|
|
classNames: ['nacho-select'],
|
2017-02-13 14:58:44 -08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.updateContent();
|
|
|
|
},
|
|
|
|
|
2018-01-20 00:46:47 -08:00
|
|
|
onSelectionChanged: observer('selected', 'values', function() {
|
2017-02-13 14:58:44 -08:00
|
|
|
this.updateContent();
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse and transform the values list into a list of objects with the currently
|
|
|
|
* selected option flagged as `isSelected`
|
|
|
|
*/
|
|
|
|
updateContent() {
|
2017-06-05 09:42:21 -07:00
|
|
|
const selected = this.get('selected') || null;
|
2017-02-13 14:58:44 -08:00
|
|
|
|
|
|
|
const options = this.get('values') || [];
|
|
|
|
const content = options.map(option => {
|
|
|
|
if (typeof option === 'object' && typeof option.value !== 'undefined') {
|
2017-06-05 09:42:21 -07:00
|
|
|
const isSelected = option.value === selected;
|
|
|
|
return { value: option.value, label: option.label, isSelected, isDisabled: option.isDisabled || false };
|
2017-02-13 14:58:44 -08:00
|
|
|
}
|
|
|
|
|
2017-06-05 09:42:21 -07:00
|
|
|
return { value: option, isSelected: option === selected };
|
2017-02-13 14:58:44 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.set('content', content);
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
// Reflect UI changes in the component and bubble the `selectionDidChange` action
|
|
|
|
change() {
|
2017-06-05 09:42:21 -07:00
|
|
|
const { selectedIndex } = this.$('select')[0];
|
2017-02-13 14:58:44 -08:00
|
|
|
const values = this.get('values');
|
|
|
|
const _selected = values[selectedIndex];
|
|
|
|
const selected = typeof _selected.value !== 'undefined' ? _selected.value : _selected;
|
|
|
|
|
|
|
|
this.set('selected', selected);
|
|
|
|
|
2018-01-20 00:46:47 -08:00
|
|
|
get(this, 'selectionDidChange')(_selected);
|
2017-02-13 14:58:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|