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,
|
2022-09-15 08:43:17 +00:00
|
|
|
onchange: function () {},
|
|
|
|
addOption: function (option) {
|
2020-08-11 17:02:51 +00:00
|
|
|
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
|
|
|
},
|
2022-09-15 08:43:17 +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: {
|
2022-09-15 08:43:17 +00:00
|
|
|
click: function (option, e) {
|
2021-01-25 18:13:54 +00:00
|
|
|
// required={{true}} ?
|
|
|
|
if (!this.multiple) {
|
|
|
|
if (option.selected && this.required) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
[...this._options]
|
2022-09-15 08:43:17 +00:00
|
|
|
.filter((item) => item !== option)
|
|
|
|
.forEach((item) => {
|
2021-01-25 18:13:54 +00:00
|
|
|
item.selected = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (option.selected && this.required) {
|
2022-09-15 08:43:17 +00:00
|
|
|
const other = [...this._options].find((item) => item !== option && item.selected);
|
2021-01-25 18:13:54 +00:00
|
|
|
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, {
|
2022-09-15 08:43:17 +00:00
|
|
|
selected: (target) => option.args.value,
|
|
|
|
selectedItems: (target) => {
|
2021-01-25 18:13:54 +00:00
|
|
|
return [...this._options]
|
2022-09-15 08:43:17 +00:00
|
|
|
.filter((item) => item.selected)
|
|
|
|
.map((item) => item.args.value)
|
2021-01-25 18:13:54 +00:00
|
|
|
.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
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|