0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 17:30:29 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/custom-wizard-topic-selector.js.es6

74 Zeilen
1,8 KiB
Text

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;
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);
},
},
});