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";
|
2020-02-18 17:41:15 -05:00
|
|
|
import { isPresent } from "@ember/utils";
|
2021-08-23 04:44:19 -04:00
|
|
|
import { next } from "@ember/runloop";
|
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,
|
|
|
|
closeOnChange: false,
|
|
|
|
autoInsertNoneItem: false,
|
|
|
|
headerComponent: "multi-select/multi-select-header",
|
2022-07-12 15:38:17 -04:00
|
|
|
filterComponent: "multi-select/multi-select-filter",
|
2021-08-23 04:44:19 -04:00
|
|
|
autoFilterable: true,
|
|
|
|
caretDownIcon: "caretIcon",
|
|
|
|
caretUpIcon: "caretIcon",
|
2020-02-03 08:22:14 -05:00
|
|
|
},
|
|
|
|
|
2021-08-23 04:44:19 -04:00
|
|
|
caretIcon: computed("value.[]", function () {
|
|
|
|
const maximum = this.selectKit.options.maximum;
|
|
|
|
return maximum && makeArray(this.value).length >= parseInt(maximum, 10)
|
|
|
|
? null
|
|
|
|
: "plus";
|
|
|
|
}),
|
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
search(filter) {
|
|
|
|
return this._super(filter).filter(
|
|
|
|
(content) => !makeArray(this.selectedContent).includes(content)
|
|
|
|
);
|
2018-02-26 05:42:57 -05:00
|
|
|
},
|
|
|
|
|
2021-01-06 06:57:13 -05:00
|
|
|
append(values) {
|
|
|
|
const existingItems = values
|
|
|
|
.map((value) => {
|
|
|
|
const defaultItem = this.defaultItem(value, value);
|
|
|
|
const existingItem =
|
|
|
|
this.findValue(this.mainCollection, defaultItem) ||
|
|
|
|
this.findName(this.mainCollection, defaultItem);
|
|
|
|
if (!existingItem) {
|
|
|
|
if (this.validateCreate(value, this.content)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
} else if (this.validateSelect(existingItem)) {
|
|
|
|
return this.getValue(existingItem);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
const newValues = makeArray(this.value).concat(existingItems);
|
|
|
|
const newContent = makeArray(this.selectedContent).concat(
|
|
|
|
makeArray(existingItems)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.selectKit.change(newValues, newContent);
|
|
|
|
},
|
|
|
|
|
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) {
|
2021-08-23 04:44:19 -04:00
|
|
|
if (this.selectKit.hasSelection && this.selectKit.options.maximum === 1) {
|
|
|
|
this.selectKit.deselectByValue(
|
|
|
|
this.getValue(this.selectedContent.firstObject)
|
|
|
|
);
|
|
|
|
next(() => {
|
|
|
|
this.selectKit.select(value, item);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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(
|
2021-08-23 04:44:19 -04:00
|
|
|
[...new Set(newValues)],
|
2020-02-03 08:22:14 -05:00
|
|
|
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
|
|
|
|
2021-11-12 08:04:48 -05:00
|
|
|
selectedContent: computed(
|
|
|
|
"value.[]",
|
|
|
|
"content.[]",
|
|
|
|
"selectKit.noneItem",
|
|
|
|
function () {
|
|
|
|
const value = makeArray(this.value).map((v) =>
|
|
|
|
this.selectKit.options.castInteger && this._isNumeric(v) ? Number(v) : v
|
|
|
|
);
|
2020-02-14 04:00:40 -05:00
|
|
|
|
2021-11-12 08:04:48 -05:00
|
|
|
if (value.length) {
|
|
|
|
let content = [];
|
|
|
|
|
|
|
|
value.forEach((v) => {
|
|
|
|
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);
|
|
|
|
}
|
2020-02-03 08:22:14 -05:00
|
|
|
}
|
2021-11-12 08:04:48 -05:00
|
|
|
});
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2021-11-12 08:04:48 -05:00
|
|
|
return this.selectKit.modifySelection(content);
|
|
|
|
}
|
2021-08-23 04:44:19 -04:00
|
|
|
|
2022-07-18 05:26:12 -04:00
|
|
|
return null;
|
2021-11-12 08:04:48 -05:00
|
|
|
}
|
|
|
|
),
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2020-02-03 08:22:14 -05:00
|
|
|
_onKeydown(event) {
|
2021-02-22 08:34:31 -05:00
|
|
|
if (
|
|
|
|
event.code === "Enter" &&
|
|
|
|
event.target.classList.contains("selected-name")
|
|
|
|
) {
|
|
|
|
event.stopPropagation();
|
|
|
|
this.selectKit.deselectByValue(event.target.dataset.value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.code === "Backspace") {
|
2020-02-03 08:22:14 -05:00
|
|
|
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) {
|
2021-02-22 08:34:31 -05:00
|
|
|
if (lastSelected === document.activeElement) {
|
2020-02-03 08:22:14 -05:00
|
|
|
this.deselect(this.selectedContent.lastObject);
|
|
|
|
} else {
|
2021-02-22 08:34:31 -05:00
|
|
|
lastSelected.focus();
|
2020-02-03 08:22:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|