2020-05-11 14:04:27 +00:00
|
|
|
import Component from '@ember/component';
|
2020-05-29 15:42:46 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-08-11 17:02:51 +00:00
|
|
|
import Slotted from 'block-slots';
|
2020-05-11 14:04:27 +00:00
|
|
|
|
2020-08-11 17:02:51 +00:00
|
|
|
export default Component.extend(Slotted, {
|
2020-05-29 15:42:46 +00:00
|
|
|
tagName: '',
|
|
|
|
dom: service('dom'),
|
2020-08-10 08:26:02 +00:00
|
|
|
multiple: false,
|
2021-01-25 18:13:54 +00:00
|
|
|
required: false,
|
2020-08-10 08:26:02 +00:00
|
|
|
onchange: function() {},
|
2020-08-11 17:02:51 +00:00
|
|
|
addOption: function(option) {
|
|
|
|
if (typeof this._options === 'undefined') {
|
|
|
|
this._options = new Set();
|
|
|
|
}
|
2021-01-25 18:13:54 +00:00
|
|
|
this._options.add(option);
|
2020-08-11 17:02:51 +00:00
|
|
|
},
|
|
|
|
removeOption: function(option) {
|
2021-01-25 18:13:54 +00:00
|
|
|
this._options.delete(option);
|
2020-08-11 17:02:51 +00:00
|
|
|
},
|
2020-05-11 14:04:27 +00:00
|
|
|
actions: {
|
2021-01-25 18:13:54 +00:00
|
|
|
click: function(option, e) {
|
|
|
|
// required={{true}} ?
|
|
|
|
if (!this.multiple) {
|
|
|
|
if (option.selected && this.required) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
[...this._options]
|
|
|
|
.filter(item => item !== option)
|
|
|
|
.forEach(item => {
|
|
|
|
item.selected = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (option.selected && this.required) {
|
|
|
|
const other = [...this._options].find(item => item !== option && item.selected);
|
|
|
|
if (!other) {
|
|
|
|
return e;
|
|
|
|
}
|
2020-08-11 17:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 18:13:54 +00:00
|
|
|
option.selected = !option.selected;
|
2020-08-11 17:02:51 +00:00
|
|
|
this.onchange(
|
|
|
|
this.dom.setEventTargetProperties(e, {
|
2021-01-25 18:13:54 +00:00
|
|
|
selected: target => option.args.value,
|
2020-08-11 17:02:51 +00:00
|
|
|
selectedItems: target => {
|
2021-01-25 18:13:54 +00:00
|
|
|
return [...this._options]
|
|
|
|
.filter(item => item.selected)
|
|
|
|
.map(item => item.args.value)
|
|
|
|
.join(',');
|
2020-08-11 17:02:51 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2021-01-25 18:13:54 +00:00
|
|
|
return e;
|
2020-05-11 14:04:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|