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-10-06 04:59:02 +02:00
|
|
|
id: (name) => 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')));
|
2017-10-05 02:36:46 +02:00
|
|
|
|
|
|
|
if (f.get('type') === 'dropdown') {
|
|
|
|
const choices = f.get('choices');
|
|
|
|
choices.forEach((c) => {
|
|
|
|
c.set('id', c.get('label'));
|
|
|
|
});
|
|
|
|
}
|
2017-10-06 04:59:02 +02:00
|
|
|
|
2017-09-29 13:27:03 +02:00
|
|
|
step['fields'].push(f);
|
|
|
|
});
|
|
|
|
|
|
|
|
s.actions.forEach((a) => {
|
2017-10-06 04:59:02 +02:00
|
|
|
a.set('id', Ember.String.dasherize(a.get('label')));
|
2017-09-29 13:27:03 +02:00
|
|
|
step['actions'].push(a);
|
|
|
|
});
|
|
|
|
|
|
|
|
steps.push(step);
|
|
|
|
});
|
|
|
|
|
|
|
|
const id = this.get('id');
|
|
|
|
const name = this.get('name');
|
2017-10-05 02:36:46 +02:00
|
|
|
const save_submissions = this.get('save_submissions');
|
|
|
|
let wizard = { id, name, save_submissions, steps };
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
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-10-05 02:36:46 +02:00
|
|
|
findAllSubmissions() {
|
|
|
|
return ajax("/admin/wizards/submissions/all", {
|
|
|
|
type: "GET"
|
|
|
|
}).then(result => {
|
|
|
|
return result.submissions;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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) {
|
2017-10-05 02:36:46 +02:00
|
|
|
props['id'] = w.id;
|
|
|
|
props['name'] = w.name;
|
2017-09-29 13:27:03 +02:00
|
|
|
|
|
|
|
if (w.steps) {
|
|
|
|
w.steps.forEach((s) => {
|
|
|
|
let fields = Ember.A();
|
|
|
|
|
|
|
|
s.fields.forEach((f) => {
|
2017-10-05 02:36:46 +02:00
|
|
|
let field = Ember.Object.create(f);
|
2017-09-29 13:27:03 +02:00
|
|
|
let choices = Ember.A();
|
|
|
|
|
|
|
|
f.choices.forEach((c) => {
|
|
|
|
choices.pushObject(Ember.Object.create(c));
|
|
|
|
});
|
|
|
|
|
2017-10-05 02:36:46 +02:00
|
|
|
field.set('choices', choices);
|
|
|
|
|
|
|
|
fields.pushObject(field);
|
2017-09-29 13:27:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2017-10-06 04:59:02 +02:00
|
|
|
banner: s.banner,
|
2017-09-29 13:27:03 +02:00
|
|
|
fields,
|
|
|
|
actions
|
|
|
|
}));
|
|
|
|
});
|
2017-10-05 02:36:46 +02:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
props['save_submissions'] = true;
|
2017-09-29 13:27:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
wizard.setProperties(props);
|
2017-09-23 04:34:07 +02:00
|
|
|
|
|
|
|
return wizard;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomWizard;
|