2023-02-24 03:18:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import FormTemplate from "discourse/models/form-template";
|
|
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
2023-02-24 03:18:14 +08:00
|
|
|
|
|
|
|
export default MultiSelectComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["form-template-chooser"],
|
|
|
|
classNames: ["form-template-chooser"],
|
|
|
|
selectKitOptions: {
|
2023-05-30 05:47:18 +08:00
|
|
|
none: "form_template_chooser.select_template",
|
2023-02-24 03:18:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2023-12-29 14:15:56 +08:00
|
|
|
this.triggerSearch();
|
2023-02-24 03:18:14 +08:00
|
|
|
},
|
|
|
|
|
2023-05-30 05:47:18 +08:00
|
|
|
didUpdateAttrs() {
|
|
|
|
this._super(...arguments);
|
2023-12-29 14:15:56 +08:00
|
|
|
this.set("templatesLoaded", false);
|
|
|
|
this.triggerSearch();
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
search(filter) {
|
|
|
|
if (this.get("templatesLoaded")) {
|
|
|
|
return this._super(filter);
|
|
|
|
} else {
|
2023-02-24 03:18:14 +08:00
|
|
|
return this._fetchTemplates();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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;
|
2023-02-24 03:18:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_sortTemplatesByName(templates) {
|
|
|
|
return templates.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
},
|
|
|
|
});
|