2024-06-20 11:25:57 +02:00
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
|
|
|
import { isEmpty } from "@ember/utils";
|
|
|
|
import { searchForTerm } from "discourse/lib/search";
|
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
|
|
|
|
|
|
|
export default MultiSelectComponent.extend({
|
|
|
|
classNames: ["topic-selector", "wizard-topic-selector"],
|
|
|
|
topics: null,
|
|
|
|
value: [],
|
|
|
|
content: [],
|
|
|
|
nameProperty: "fancy_title",
|
|
|
|
labelProperty: "title",
|
|
|
|
titleProperty: "title",
|
|
|
|
|
|
|
|
selectKitOptions: {
|
|
|
|
clearable: true,
|
|
|
|
filterable: true,
|
|
|
|
filterPlaceholder: "choose_topic.title.placeholder",
|
|
|
|
allowAny: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
if (this.topics && !this.selectKit.hasSelection) {
|
2024-06-20 13:06:02 +02:00
|
|
|
const values = makeArray(this.topics.map((t) => t.id));
|
2024-06-20 11:25:57 +02:00
|
|
|
const content = makeArray(this.topics);
|
|
|
|
this.selectKit.change(values, content);
|
|
|
|
}
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
modifyComponentForRow() {
|
|
|
|
return "topic-row";
|
|
|
|
},
|
|
|
|
|
|
|
|
search(filter) {
|
|
|
|
if (isEmpty(filter)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const searchParams = {};
|
|
|
|
searchParams.typeFilter = "topic";
|
|
|
|
searchParams.restrictToArchetype = "regular";
|
|
|
|
searchParams.searchForId = true;
|
|
|
|
|
2024-06-24 12:08:51 +02:00
|
|
|
if (this.category) {
|
|
|
|
searchParams.searchContext = {
|
|
|
|
type: "category",
|
|
|
|
id: this.category,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-20 13:06:02 +02:00
|
|
|
return searchForTerm(filter, searchParams).then((results) => {
|
2024-06-20 11:25:57 +02:00
|
|
|
if (results?.posts?.length > 0) {
|
|
|
|
return results.posts.mapBy("topic");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
onChange(value, items) {
|
2024-06-20 13:06:02 +02:00
|
|
|
const content = items.map((t) => {
|
2024-06-20 11:25:57 +02:00
|
|
|
return {
|
|
|
|
id: t.id,
|
|
|
|
title: t.title,
|
|
|
|
fancy_title: t.fancy_title,
|
2024-06-20 13:06:02 +02:00
|
|
|
url: t.url,
|
|
|
|
};
|
2024-06-20 11:25:57 +02:00
|
|
|
});
|
|
|
|
this.setProperties({ value, content });
|
|
|
|
this.onChange(value, content);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|