2023-02-24 03:18:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
2024-08-23 19:17:07 +08:00
|
|
|
import { classNames } from "@ember-decorators/component";
|
2023-10-11 02:38:59 +08:00
|
|
|
import FormTemplate from "discourse/models/form-template";
|
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
2024-08-23 19:17:07 +08:00
|
|
|
import {
|
|
|
|
pluginApiIdentifiers,
|
|
|
|
selectKitOptions,
|
|
|
|
} from "select-kit/components/select-kit";
|
2023-02-24 03:18:14 +08:00
|
|
|
|
2024-08-23 19:17:07 +08:00
|
|
|
@classNames("form-template-chooser")
|
|
|
|
@selectKitOptions({
|
|
|
|
none: "form_template_chooser.select_template",
|
|
|
|
})
|
|
|
|
@pluginApiIdentifiers("form-template-chooser")
|
|
|
|
export default class FormTemplateChooser extends MultiSelectComponent {
|
2023-02-24 03:18:14 +08:00
|
|
|
init() {
|
2024-08-23 19:17:07 +08:00
|
|
|
super.init(...arguments);
|
2023-12-29 14:15:56 +08:00
|
|
|
this.triggerSearch();
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2023-02-24 03:18:14 +08:00
|
|
|
|
2023-05-30 05:47:18 +08:00
|
|
|
didUpdateAttrs() {
|
2024-08-23 19:17:07 +08:00
|
|
|
super.didUpdateAttrs(...arguments);
|
2023-12-29 14:15:56 +08:00
|
|
|
this.set("templatesLoaded", false);
|
|
|
|
this.triggerSearch();
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2023-05-30 05:47:18 +08:00
|
|
|
|
2023-02-24 03:18:14 +08:00
|
|
|
@computed("templates")
|
|
|
|
get content() {
|
2023-12-29 14:15:56 +08:00
|
|
|
return this.templates;
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2023-12-29 14:15:56 +08:00
|
|
|
|
|
|
|
search(filter) {
|
|
|
|
if (this.get("templatesLoaded")) {
|
2024-08-23 19:17:07 +08:00
|
|
|
return super.search(filter);
|
2023-12-29 14:15:56 +08:00
|
|
|
} else {
|
2023-02-24 03:18:14 +08:00
|
|
|
return this._fetchTemplates();
|
|
|
|
}
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2023-02-24 03:18:14 +08:00
|
|
|
|
2023-12-29 14:15:56 +08:00
|
|
|
async _fetchTemplates() {
|
|
|
|
if (this.get("loadingTemplates")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("templatesLoaded", false);
|
|
|
|
this.set("loadingTemplates", true);
|
|
|
|
|
|
|
|
const result = await FormTemplate.findAll();
|
|
|
|
|
|
|
|
let sortedTemplates = this._sortTemplatesByName(result);
|
|
|
|
|
|
|
|
if (this.filteredIds) {
|
|
|
|
sortedTemplates = sortedTemplates.filter((t) =>
|
|
|
|
this.filteredIds.includes(t.id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sortedTemplates.length === 0) {
|
|
|
|
this.set("selectKit.options.disabled", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
templates: sortedTemplates,
|
|
|
|
loadingTemplates: false,
|
|
|
|
templatesLoaded: true,
|
2023-02-24 03:18:14 +08:00
|
|
|
});
|
2023-12-29 14:15:56 +08:00
|
|
|
|
|
|
|
return this.templates;
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
2023-02-24 03:18:14 +08:00
|
|
|
|
|
|
|
_sortTemplatesByName(templates) {
|
|
|
|
return templates.sort((a, b) => a.name.localeCompare(b.name));
|
2024-08-23 19:17:07 +08:00
|
|
|
}
|
|
|
|
}
|