2017-09-24 05:01:18 +02:00
|
|
|
import { default as computed } from 'ember-addons/ember-computed-decorators';
|
2017-09-29 13:27:03 +02:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
const CustomWizard = Discourse.Model.extend({
|
2017-09-29 13:27:03 +02:00
|
|
|
init() {
|
|
|
|
const id = this.get('id');
|
|
|
|
if (id) this.set('existingId', id);
|
|
|
|
},
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-09-24 05:01:18 +02:00
|
|
|
@computed('name')
|
2017-09-29 13:27:03 +02:00
|
|
|
id(name) {
|
|
|
|
return name ? Ember.String.dasherize(name) : null;
|
2017-09-24 05:01:18 +02:00
|
|
|
},
|
|
|
|
|
2017-09-23 04:34:07 +02:00
|
|
|
save() {
|
2017-09-29 13:27:03 +02:00
|
|
|
const stepsObj = this.get('steps');
|
|
|
|
let steps = [];
|
|
|
|
|
|
|
|
stepsObj.forEach((s) => {
|
|
|
|
let step = {
|
|
|
|
id: Ember.String.dasherize(s.title),
|
|
|
|
title: s.title,
|
|
|
|
banner: s.banner,
|
|
|
|
description: s.description,
|
|
|
|
fields: [],
|
|
|
|
actions: []
|
|
|
|
};
|
|
|
|
|
|
|
|
const fields = s.get('fields');
|
|
|
|
fields.forEach((f) => {
|
|
|
|
f.set('id', Ember.String.dasherize(f.get('label')));
|
|
|
|
step['fields'].push(f);
|
|
|
|
});
|
|
|
|
|
|
|
|
s.actions.forEach((a) => {
|
|
|
|
a['id'] = Ember.String.dasherize(a.label);
|
|
|
|
step['actions'].push(a);
|
|
|
|
});
|
|
|
|
|
|
|
|
steps.push(step);
|
|
|
|
});
|
|
|
|
|
|
|
|
const id = this.get('id');
|
|
|
|
const name = this.get('name');
|
|
|
|
let wizard = { id, name, steps };
|
|
|
|
|
|
|
|
const existingId = this.get('existingId');
|
|
|
|
if (existingId && existingId !== id) {
|
|
|
|
wizard['existing_id'] = existingId;
|
2017-09-24 05:01:18 +02:00
|
|
|
};
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
return ajax("/admin/wizards/custom/save", {
|
2017-09-23 04:34:07 +02:00
|
|
|
type: 'PUT',
|
2017-09-24 05:01:18 +02:00
|
|
|
data: {
|
|
|
|
wizard: JSON.stringify(wizard)
|
|
|
|
}
|
2017-09-23 04:34:07 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-24 05:01:18 +02:00
|
|
|
remove() {
|
2017-09-29 13:27:03 +02:00
|
|
|
return ajax("/admin/wizards/custom/remove", {
|
2017-09-24 05:01:18 +02:00
|
|
|
type: 'DELETE',
|
|
|
|
data: {
|
|
|
|
id: this.get('id')
|
|
|
|
}
|
|
|
|
}).then(() => this.destroy());
|
2017-09-23 04:34:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CustomWizard.reopenClass({
|
|
|
|
findAll() {
|
2017-09-29 13:27:03 +02:00
|
|
|
return ajax("/admin/wizards/custom/all", {
|
|
|
|
type: 'GET'
|
|
|
|
}).then(result => {
|
2017-09-23 04:34:07 +02:00
|
|
|
return result.wizards.map(w => CustomWizard.create(w));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
create(w) {
|
|
|
|
const wizard = this._super.apply(this);
|
2017-09-23 04:34:07 +02:00
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
let steps = Ember.A();
|
|
|
|
let props = { steps };
|
|
|
|
|
|
|
|
if (w) {
|
|
|
|
props['id'] = w.id; props['name'] = w.name;
|
|
|
|
|
|
|
|
if (w.steps) {
|
|
|
|
w.steps.forEach((s) => {
|
|
|
|
let fields = Ember.A();
|
|
|
|
|
|
|
|
s.fields.forEach((f) => {
|
|
|
|
let choices = Ember.A();
|
|
|
|
|
|
|
|
f.choices.forEach((c) => {
|
|
|
|
choices.pushObject(Ember.Object.create(c));
|
|
|
|
});
|
|
|
|
|
|
|
|
fields.pushObject(Ember.Object.create(f));
|
|
|
|
});
|
|
|
|
|
|
|
|
let actions = Ember.A();
|
|
|
|
s.actions.forEach((a) => {
|
|
|
|
actions.pushObject(Ember.Object.create(a));
|
|
|
|
});
|
|
|
|
|
|
|
|
steps.pushObject(Ember.Object.create({
|
|
|
|
id: s.id,
|
|
|
|
title: s.title,
|
|
|
|
description: s.description,
|
|
|
|
fields,
|
|
|
|
actions
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wizard.setProperties(props);
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
return wizard;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomWizard;
|