2020-02-03 21:22:14 +08:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2017-11-21 18:53:09 +08:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2020-02-03 21:22:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
2020-02-19 06:41:15 +08:00
|
|
|
import { isPresent } from "@ember/utils";
|
2020-03-07 06:49:28 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
export default SelectKitComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["multi-select"],
|
2018-02-26 18:42:57 +08:00
|
|
|
layoutName: "select-kit/templates/components/multi-select",
|
2020-02-03 21:22:14 +08:00
|
|
|
classNames: ["multi-select"],
|
|
|
|
multiSelect: true,
|
|
|
|
|
|
|
|
selectKitOptions: {
|
|
|
|
none: "select_kit.default_header_text",
|
|
|
|
clearable: true,
|
|
|
|
filterable: true,
|
|
|
|
filterIcon: null,
|
|
|
|
clearOnClick: true,
|
|
|
|
closeOnChange: false,
|
|
|
|
autoInsertNoneItem: false,
|
|
|
|
headerComponent: "multi-select/multi-select-header",
|
|
|
|
filterComponent: "multi-select/multi-select-filter"
|
|
|
|
},
|
|
|
|
|
|
|
|
search(filter) {
|
|
|
|
return this._super(filter).filter(
|
|
|
|
content => !makeArray(this.selectedContent).includes(content)
|
|
|
|
);
|
2018-02-26 18:42:57 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
deselect(item) {
|
|
|
|
this.clearErrors();
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
const newContent = this.selectedContent.filter(
|
|
|
|
content => this.getValue(item) !== this.getValue(content)
|
|
|
|
);
|
2018-01-11 16:54:39 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.change(
|
|
|
|
this.valueProperty ? newContent.mapBy(this.valueProperty) : newContent,
|
|
|
|
newContent
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
select(value, item) {
|
2020-02-19 06:41:15 +08:00
|
|
|
if (!isPresent(value)) {
|
2020-02-03 21:22:14 +08:00
|
|
|
if (!this.validateSelect(this.selectKit.highlighted)) {
|
|
|
|
return;
|
2018-04-05 22:45:19 +08:00
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.change(
|
|
|
|
makeArray(this.value).concat(
|
|
|
|
makeArray(this.getValue(this.selectKit.highlighted))
|
|
|
|
),
|
|
|
|
makeArray(this.selectedContent).concat(
|
|
|
|
makeArray(this.selectKit.highlighted)
|
|
|
|
)
|
|
|
|
);
|
2018-03-22 18:29:55 +08:00
|
|
|
} else {
|
2020-02-03 21:22:14 +08:00
|
|
|
const existingItem = this.findValue(
|
|
|
|
this.mainCollection,
|
|
|
|
this.selectKit.valueProperty ? item : value
|
|
|
|
);
|
|
|
|
if (existingItem) {
|
|
|
|
if (!this.validateSelect(item)) {
|
|
|
|
return;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
const newValues = makeArray(this.value).concat(makeArray(value));
|
|
|
|
const newContent = makeArray(this.selectedContent).concat(
|
|
|
|
makeArray(item)
|
2018-11-29 22:56:19 +08:00
|
|
|
);
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.selectKit.change(
|
|
|
|
newValues,
|
|
|
|
newContent.length
|
|
|
|
? newContent
|
|
|
|
: makeArray(this.defaultItem(value, value))
|
|
|
|
);
|
2018-11-15 22:21:40 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
},
|
2018-11-15 22:21:40 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
selectedContent: computed("value.[]", "content.[]", function() {
|
2020-02-19 07:57:58 +08:00
|
|
|
const value = makeArray(this.value).map(v =>
|
2020-02-14 17:00:40 +08:00
|
|
|
this.selectKit.options.castInteger && this._isNumeric(v) ? Number(v) : v
|
|
|
|
);
|
|
|
|
|
2020-02-05 07:41:10 +08:00
|
|
|
if (value.length) {
|
2020-02-03 21:22:14 +08:00
|
|
|
let content = [];
|
|
|
|
|
2020-02-05 07:41:10 +08:00
|
|
|
value.forEach(v => {
|
2020-02-03 21:22:14 +08:00
|
|
|
if (this.selectKit.valueProperty) {
|
|
|
|
const c = makeArray(this.content).findBy(
|
|
|
|
this.selectKit.valueProperty,
|
|
|
|
v
|
|
|
|
);
|
|
|
|
if (c) {
|
|
|
|
content.push(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (makeArray(this.content).includes(v)) {
|
|
|
|
content.push(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-05-07 22:25:44 +08:00
|
|
|
return this.selectKit.modifySelection(content);
|
2020-02-03 21:22:14 +08:00
|
|
|
} else {
|
|
|
|
return this.selectKit.noneItem;
|
2018-03-22 18:29:55 +08:00
|
|
|
}
|
2020-02-03 21:22:14 +08:00
|
|
|
}),
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_onKeydown(event) {
|
|
|
|
if (event.keyCode === 8) {
|
|
|
|
event.stopPropagation();
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
const input = this.getFilterInput();
|
2020-05-08 15:19:36 +08:00
|
|
|
if (input && input.value.length === 0) {
|
2020-02-03 21:22:14 +08:00
|
|
|
const selected = this.element.querySelectorAll(
|
|
|
|
".select-kit-header .choice.select-kit-selected-name"
|
2018-03-22 18:29:55 +08:00
|
|
|
);
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
if (selected.length) {
|
|
|
|
const lastSelected = selected[selected.length - 1];
|
|
|
|
if (lastSelected) {
|
|
|
|
if (lastSelected.classList.contains("is-highlighted")) {
|
|
|
|
this.deselect(this.selectedContent.lastObject);
|
|
|
|
} else {
|
|
|
|
lastSelected.classList.add("is-highlighted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
} else {
|
2020-02-03 21:22:14 +08:00
|
|
|
const selected = this.element.querySelectorAll(
|
|
|
|
".select-kit-header .choice.select-kit-selected-name"
|
|
|
|
);
|
|
|
|
selected.forEach(s => s.classList.remove("is-highlighted"));
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
return true;
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
handleDeprecations() {
|
|
|
|
this._super(...arguments);
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this._deprecateValues();
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
_deprecateValues() {
|
|
|
|
if (this.values && !this.value) {
|
|
|
|
deprecated(
|
|
|
|
"The `values` property is deprecated for multi-select. Use `value` instead",
|
|
|
|
{
|
|
|
|
since: "v2.4.0"
|
|
|
|
}
|
|
|
|
);
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.set("value", this.values);
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
});
|