0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-15 22:32:54 +01:00
discourse-custom-wizard/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6

103 Zeilen
2,4 KiB
Text

2020-04-13 14:17:22 +02:00
import DiscourseRoute from "discourse/routes/discourse";
2021-02-24 08:43:35 +01:00
import { buildFieldTypes, buildFieldValidations } from "../lib/wizard-schema";
2020-10-20 07:40:23 +02:00
import EmberObject, { set } from "@ember/object";
import { A } from "@ember/array";
2020-04-13 14:17:22 +02:00
import { all } from "rsvp";
2021-02-24 08:43:35 +01:00
import { ajax } from "discourse/lib/ajax";
2020-04-13 14:17:22 +02:00
2021-02-24 08:43:35 +01:00
export default DiscourseRoute.extend({
2020-04-13 14:17:22 +02:00
model() {
2020-04-14 01:39:21 +02:00
return ajax("/admin/wizards/wizard");
2020-04-13 14:17:22 +02:00
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
afterModel(model) {
buildFieldTypes(model.field_types);
2021-01-27 06:08:26 +01:00
buildFieldValidations(model.realtime_validations);
2020-04-13 14:17:22 +02:00
return all([
this._getThemes(model),
this._getApis(model),
2021-02-24 08:43:35 +01:00
this._getUserFields(model),
2020-04-13 14:17:22 +02:00
]);
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
_getThemes(model) {
2021-02-24 08:43:35 +01:00
return ajax("/admin/themes").then((result) => {
set(
model,
"themes",
result.themes.map((t) => {
2020-04-13 14:17:22 +02:00
return {
id: t.id,
2021-02-24 08:43:35 +01:00
name: t.name,
};
})
);
});
2020-04-13 14:17:22 +02:00
},
_getApis(model) {
2021-02-24 08:43:35 +01:00
return ajax("/admin/wizards/api").then((result) =>
set(model, "apis", result)
);
2020-04-13 14:17:22 +02:00
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
_getUserFields(model) {
2021-02-24 08:43:35 +01:00
return this.store.findAll("user-field").then((result) => {
2020-04-13 14:17:22 +02:00
if (result && result.content) {
2021-02-24 08:43:35 +01:00
set(
model,
"userFields",
2020-04-13 14:17:22 +02:00
result.content.map((f) => ({
id: `user_field_${f.id}`,
2021-02-24 08:43:35 +01:00
name: f.name,
}))
2020-04-13 14:17:22 +02:00
);
}
});
},
2021-02-24 08:43:35 +01:00
2020-04-14 01:39:21 +02:00
currentWizard() {
2021-02-24 08:43:35 +01:00
const params = this.paramsFor("adminWizardsWizardShow");
2020-04-13 14:17:22 +02:00
if (params && params.wizardId) {
2020-04-14 01:39:21 +02:00
return params.wizardId;
} else {
return null;
}
},
2021-02-24 08:43:35 +01:00
2020-04-14 01:39:21 +02:00
setupController(controller, model) {
controller.setProperties({
2020-04-14 01:39:21 +02:00
wizardList: model.wizard_list,
2020-10-20 07:40:23 +02:00
wizardId: this.currentWizard(),
2021-02-24 08:43:35 +01:00
custom_fields: A(model.custom_fields.map((f) => EmberObject.create(f))),
});
2020-04-13 14:17:22 +02:00
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
actions: {
changeWizard(wizardId) {
2021-02-24 08:43:35 +01:00
this.controllerFor("adminWizardsWizard").set("wizardId", wizardId);
2020-04-13 14:17:22 +02:00
if (wizardId) {
2021-02-24 08:43:35 +01:00
this.transitionTo("adminWizardsWizardShow", wizardId);
2020-04-13 14:17:22 +02:00
} else {
2021-02-24 08:43:35 +01:00
this.transitionTo("adminWizardsWizard");
2020-04-13 14:17:22 +02:00
}
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
afterDestroy() {
2021-02-24 08:43:35 +01:00
this.transitionTo("adminWizardsWizard").then(() => this.refresh());
2020-04-13 14:17:22 +02:00
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
afterSave(wizardId) {
2021-02-24 08:43:35 +01:00
this.refresh().then(() => this.send("changeWizard", wizardId));
2020-04-13 14:17:22 +02:00
},
2021-02-24 08:43:35 +01:00
2020-04-13 14:17:22 +02:00
createWizard() {
2021-02-24 08:43:35 +01:00
this.controllerFor("adminWizardsWizard").set("wizardId", "create");
this.transitionTo("adminWizardsWizardShow", "create");
},
},
});