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);
|
|
|
|
|
|
|
|
if (!this.templates) {
|
|
|
|
this._fetchTemplates();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-05-30 05:47:18 +08:00
|
|
|
didUpdateAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._fetchTemplates();
|
|
|
|
},
|
|
|
|
|
2023-02-24 03:18:14 +08:00
|
|
|
@computed("templates")
|
|
|
|
get content() {
|
|
|
|
if (!this.templates) {
|
|
|
|
return this._fetchTemplates();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.templates;
|
|
|
|
},
|
|
|
|
|
|
|
|
_fetchTemplates() {
|
|
|
|
FormTemplate.findAll().then((result) => {
|
2023-05-30 05:47:18 +08:00
|
|
|
let sortedTemplates = this._sortTemplatesByName(result);
|
|
|
|
|
|
|
|
if (this.filteredIds) {
|
|
|
|
sortedTemplates = sortedTemplates.filter((t) =>
|
|
|
|
this.filteredIds.includes(t.id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-24 03:18:14 +08:00
|
|
|
if (sortedTemplates.length > 0) {
|
|
|
|
return this.set("templates", sortedTemplates);
|
|
|
|
} else {
|
|
|
|
this.set("templates", sortedTemplates);
|
|
|
|
this.set("selectKit.options.disabled", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_sortTemplatesByName(templates) {
|
|
|
|
return templates.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
},
|
|
|
|
});
|