discourse/app/assets/javascripts/select-kit/addon/components/form-template-chooser.js
David Taylor 48193767bf DEV: Sort imports
Automatically generated by `eslint --fix` to satisfy the updated configuration
2023-10-10 21:46:54 +01:00

57 lines
1.3 KiB
JavaScript

import { computed } from "@ember/object";
import FormTemplate from "discourse/models/form-template";
import MultiSelectComponent from "select-kit/components/multi-select";
export default MultiSelectComponent.extend({
pluginApiIdentifiers: ["form-template-chooser"],
classNames: ["form-template-chooser"],
selectKitOptions: {
none: "form_template_chooser.select_template",
},
init() {
this._super(...arguments);
if (!this.templates) {
this._fetchTemplates();
}
},
didUpdateAttrs() {
this._super(...arguments);
this._fetchTemplates();
},
@computed("templates")
get content() {
if (!this.templates) {
return this._fetchTemplates();
}
return this.templates;
},
_fetchTemplates() {
FormTemplate.findAll().then((result) => {
let sortedTemplates = this._sortTemplatesByName(result);
if (this.filteredIds) {
sortedTemplates = sortedTemplates.filter((t) =>
this.filteredIds.includes(t.id)
);
}
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));
},
});