0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-20 07:41:11 +02:00
discourse-custom-wizard/assets/javascripts/discourse/routes/admin-wizard.js.es6

109 Zeilen
2,5 KiB
Text

2017-09-23 04:34:07 +02:00
import CustomWizard from '../models/custom-wizard';
2017-10-05 02:36:46 +02:00
import { ajax } from 'discourse/lib/ajax';
2020-04-05 03:37:09 +02:00
import { selectKitContent, profileFields, generateName } from '../lib/wizard';
2020-03-21 18:30:11 +01:00
import DiscourseRoute from "discourse/routes/discourse";
2020-04-05 03:37:09 +02:00
import { all } from "rsvp";
2017-09-23 04:34:07 +02:00
2020-03-21 18:30:11 +01:00
export default DiscourseRoute.extend({
2017-10-13 15:02:34 +02:00
beforeModel() {
const param = this.paramsFor('adminWizard').wizard_id;
const wizards = this.modelFor('admin-wizards-custom');
if (wizards.length && (param === 'first' || param === 'last')) {
const wizard = wizards.get(`${param}Object`);
if (wizard) {
this.transitionTo('adminWizard', wizard.id.dasherize());
}
}
},
2017-09-23 04:34:07 +02:00
model(params) {
2017-10-13 15:02:34 +02:00
const wizardId = params.wizard_id;
2020-03-29 09:49:33 +02:00
this.set('newWizard', wizardId === 'new');
if (this.newWizard) {
return CustomWizard.create();
2020-03-29 09:49:33 +02:00
} else {
const wizard = this.modelFor('admin-wizards-custom')
.findBy('id', wizardId.underscore());
if (!wizard) {
return this.transitionTo('adminWizard', 'new');
} else {
return wizard;
}
}
2017-09-23 04:34:07 +02:00
},
2017-10-05 02:36:46 +02:00
afterModel(model) {
2020-04-05 03:37:09 +02:00
return all([
this._getFieldTypes(model),
2019-06-03 09:09:24 +02:00
this._getThemes(model),
2020-03-21 18:30:11 +01:00
this._getApis(model),
this._getUserFields(model)
]);
},
_getFieldTypes(model) {
2017-10-05 02:36:46 +02:00
return ajax('/admin/wizards/field-types')
2020-02-02 11:42:05 +01:00
.then((result) => {
model.set(
'fieldTypes',
2020-04-02 10:21:03 +02:00
selectKitContent([...result.types])
2020-02-02 11:42:05 +01:00
)
});
2017-10-05 02:36:46 +02:00
},
_getThemes(model) {
2020-03-29 09:49:33 +02:00
return ajax('/admin/themes')
.then((result) => {
model.set('themes', result.themes.map(t => {
return {
id: t.id,
name: t.name
}
}));
});
},
2019-06-03 09:09:24 +02:00
_getApis(model) {
return ajax('/admin/wizards/apis')
.then((result) => model.set('apis', result));
},
2020-03-21 18:30:11 +01:00
_getUserFields(model) {
return this.store.findAll('user-field').then((result) => {
if (result && result.content) {
2020-03-29 09:49:33 +02:00
model.set('userFields',
result.content.map((f) => ({
id: `user_field_${f.id}`,
name: f.name
})).concat(
profileFields.map((f) => ({
id: f,
name: generateName(f)
}))
)
);
2020-03-21 18:30:11 +01:00
}
});
},
2019-06-03 09:09:24 +02:00
2017-09-23 04:34:07 +02:00
setupController(controller, model) {
2017-10-07 04:27:38 +02:00
const newWizard = this.get('newWizard');
2020-04-08 09:59:54 +02:00
2017-10-07 04:27:38 +02:00
controller.setProperties({
newWizard,
model,
2020-04-08 09:59:54 +02:00
currentStep: model.steps[0],
currentAction: model.actions[0]
2017-10-07 04:27:38 +02:00
});
2017-10-06 04:59:02 +02:00
},
actions: {
2017-10-07 04:27:38 +02:00
refreshWizard() {
2017-10-06 04:59:02 +02:00
this.refresh();
}
2017-09-23 04:34:07 +02:00
}
});