2017-11-21 05:53:09 -05:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2020-02-03 08:22:14 -05:00
|
|
|
import { computed } from "@ember/object";
|
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2020-02-18 17:41:15 -05:00
|
|
|
import { isPresent } from "@ember/utils";
|
2020-08-28 15:30:20 -04:00
|
|
|
import layout from "select-kit/templates/components/multi-select";
|
2020-03-06 17:49:28 -05:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
export default SelectKitComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["multi-select"],
|
2020-08-28 15:30:20 -04:00
|
|
|
layout,
|
2020-02-03 08:22:14 -05: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 05:42:57 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
deselect(item) {
|
|
|
|
this.clearErrors();
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
const newContent = this.selectedContent.filter(
|
|
|
|
(content) => this.getValue(item) !== this.getValue(content)
|
|
|
|
);
|
2018-01-11 03:54:39 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
this.selectKit.change(
|
|
|
|
this.valueProperty ? newContent.mapBy(this.valueProperty) : newContent,
|
|
|
|
newContent
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
select(value, item) {
|
2020-02-18 17:41:15 -05:00
|
|
|
if (!isPresent(value)) {
|
2020-02-03 08:22:14 -05:00
|
|
|
if (!this.validateSelect(this.selectKit.highlighted)) {
|
|
|
|
return;
|
2018-04-05 10:45:19 -04:00
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2020-02-03 08:22:14 -05: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 06:29:55 -04:00
|
|
|
} else {
|
2020-02-03 08:22:14 -05:00
|
|
|
const existingItem = this.findValue(
|
|
|
|
this.mainCollection,
|
|
|
|
this.selectKit.valueProperty ? item : value
|
|
|
|
);
|
|
|
|
if (existingItem) {
|
|
|
|
if (!this.validateSelect(item)) {
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
const newValues = makeArray(this.value).concat(makeArray(value));
|
|
|
|
const newContent = makeArray(this.selectedContent).concat(
|
|
|
|
makeArray(item)
|
2018-11-29 09:56:19 -05:00
|
|
|
);
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
this.selectKit.change(
|
|
|
|
newValues,
|
|
|
|
newContent.length
|
|
|
|
? newContent
|
|
|
|
: makeArray(this.defaultItem(value, value))
|
|
|
|
);
|
2018-11-15 09:21:40 -05:00
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
},
|
2018-11-15 09:21:40 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
selectedContent: computed("value.[]", "content.[]", function () {
|
2020-02-18 18:57:58 -05:00
|
|
|
const value = makeArray(this.value).map((v) =>
|
2020-02-14 04:00:40 -05:00
|
|
|
this.selectKit.options.castInteger && this._isNumeric(v) ? Number(v) : v
|
|
|
|
);
|
|
|
|
|
2020-02-04 18:41:10 -05:00
|
|
|
if (value.length) {
|
2020-02-03 08:22:14 -05:00
|
|
|
let content = [];
|
|
|
|
|
2020-02-04 18:41:10 -05:00
|
|
|
value.forEach((v) => {
|
2020-02-03 08:22:14 -05: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 06:29:55 -04:00
|
|
|
|
2020-05-07 10:25:44 -04:00
|
|
|
return this.selectKit.modifySelection(content);
|
2020-02-03 08:22:14 -05:00
|
|
|
} else {
|
|
|
|
return this.selectKit.noneItem;
|
2018-03-22 06:29:55 -04:00
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
}),
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
_onKeydown(event) {
|
|
|
|
if (event.keyCode === 8) {
|
|
|
|
event.stopPropagation();
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
const input = this.getFilterInput();
|
2020-05-08 03:19:36 -04:00
|
|
|
if (input && input.value.length === 0) {
|
2020-02-03 08:22:14 -05:00
|
|
|
const selected = this.element.querySelectorAll(
|
|
|
|
".select-kit-header .choice.select-kit-selected-name"
|
2018-03-22 06:29:55 -04:00
|
|
|
);
|
|
|
|
|
2020-02-03 08:22:14 -05: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 06:29:55 -04:00
|
|
|
} else {
|
2020-02-03 08:22:14 -05: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 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
return true;
|
2018-03-22 06:29:55 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
handleDeprecations() {
|
|
|
|
this._super(...arguments);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
this._deprecateValues();
|
2018-03-22 06:29:55 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 08:22:14 -05: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 06:29:55 -04:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
this.set("value", this.values);
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
});
|