0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 04:12:53 +01:00
discourse-custom-wizard/assets/javascripts/discourse/models/custom-wizard.js.es6

199 Zeilen
5,1 KiB
Text

import { ajax } from 'discourse/lib/ajax';
2017-09-23 04:34:07 +02:00
const CustomWizard = Discourse.Model.extend({
2017-10-13 15:02:34 +02:00
save() {
return new Ember.RSVP.Promise((resolve, reject) => {
const id = this.get('id');
if (!id || !id.underscore()) reject('id_required');
let wizard = { id: id.underscore() };
const steps = this.get('steps');
if (steps.length) wizard['steps'] = this.buildSteps(steps, reject);
const name = this.get('name');
if (name) wizard['name'] = name;
const background = this.get('background');
if (background) wizard['background'] = background;
const save_submissions = this.get('save_submissions');
if (save_submissions) wizard['save_submissions'] = save_submissions;
const multiple_submissions = this.get('multiple_submissions');
if (multiple_submissions) wizard['multiple_submissions'] = multiple_submissions;
2017-09-23 04:34:07 +02:00
2017-10-13 15:02:34 +02:00
ajax("/admin/wizards/custom/save", {
type: 'PUT',
data: {
wizard: JSON.stringify(wizard)
}
}).then((result) => resolve(result));
});
2017-10-07 04:27:38 +02:00
},
2017-09-24 05:01:18 +02:00
2017-10-13 15:02:34 +02:00
buildSteps(stepsObj, reject) {
let steps = [];
2017-10-13 15:02:34 +02:00
stepsObj.some((s) => {
if (!s.id || !s.id.underscore()) reject('id_required');
2017-10-07 04:27:38 +02:00
2017-10-13 15:02:34 +02:00
let step = { id: s.id.underscore() };
2017-10-07 04:27:38 +02:00
if (s.title) step['title'] = s.title;
2017-10-13 15:02:34 +02:00
if (s.key) step['key'] = s.key;
2017-10-07 04:27:38 +02:00
if (s.banner) step['banner'] = s.banner;
if (s.description) step['description'] = s.description;
const fields = s.get('fields');
2017-10-13 15:02:34 +02:00
if (fields.length) {
step['fields'] = [];
2017-10-07 04:27:38 +02:00
2017-10-13 15:02:34 +02:00
fields.some((f) => {
let id = f.get('id');
2017-10-07 04:27:38 +02:00
2017-10-13 15:02:34 +02:00
if (!id || !id.underscore()) reject('id_required');
f.set('id', id.underscore());
2017-10-05 02:36:46 +02:00
2017-10-13 15:02:34 +02:00
if (f.get('type') === 'dropdown') {
const choices = f.get('choices');
if (choices && choices.length < 1 && !f.get('choices_key') && !f.get('choices_categories')) {
reject('field.need_choices');
}
}
2017-10-07 04:27:38 +02:00
2017-10-17 15:17:53 +02:00
delete f.isNew;
2017-10-13 15:02:34 +02:00
step['fields'].push(f);
});
}
2017-10-07 04:27:38 +02:00
2017-10-13 15:02:34 +02:00
const actions = s.actions;
if (actions.length) {
step['actions'] = [];
2017-10-07 04:27:38 +02:00
2017-10-13 15:02:34 +02:00
actions.some((a) => {
let id = a.get('id');
if (!id || !id.underscore()) reject('id_required');
2017-10-06 04:59:02 +02:00
2017-10-13 15:02:34 +02:00
a.set('id', id.underscore());
2017-10-17 15:17:53 +02:00
delete a.isNew;
2017-10-13 15:02:34 +02:00
step['actions'].push(a);
});
}
steps.push(step);
});
2017-10-13 15:02:34 +02:00
return steps;
2017-09-23 04:34:07 +02:00
},
2017-09-24 05:01:18 +02:00
remove() {
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() {
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;
});
},
create(w) {
const wizard = this._super.apply(this);
let steps = Ember.A();
let props = { steps };
if (w) {
2017-10-05 02:36:46 +02:00
props['id'] = w.id;
2017-10-13 15:02:34 +02:00
props['existingId'] = true;
2017-10-05 02:36:46 +02:00
props['name'] = w.name;
2017-10-07 04:27:38 +02:00
props['background'] = w.background;
2017-10-09 07:52:09 +02:00
props['save_submissions'] = w.save_submissions;
2017-10-13 15:02:34 +02:00
props['multiple_submissions'] = w.multiple_submissions;
2017-10-13 15:02:34 +02:00
if (w.steps && w.steps.length) {
w.steps.forEach((s) => {
2017-10-13 15:02:34 +02:00
// clean empty strings
Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
2017-10-13 15:02:34 +02:00
let fields = Ember.A();
2017-10-13 15:02:34 +02:00
if (s.fields && s.fields.length) {
s.fields.forEach((f) => {
Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
2017-10-17 15:17:53 +02:00
const fieldParams = { isNew: false };
let field = Ember.Object.create(Object.assign(f, fieldParams));
2017-10-13 15:02:34 +02:00
if (f.choices) {
let choices = Ember.A();
2017-10-13 15:02:34 +02:00
f.choices.forEach((c) => {
choices.pushObject(Ember.Object.create(c));
});
2017-10-05 02:36:46 +02:00
2017-10-13 15:02:34 +02:00
field.set('choices', choices);
}
fields.pushObject(field);
});
}
let actions = Ember.A();
2017-10-13 15:02:34 +02:00
if (s.actions && s.actions.length) {
s.actions.forEach((a) => {
2017-10-17 15:17:53 +02:00
const actionParams = { isNew: false };
const action = Ember.Object.create(Object.assign(a, actionParams));
actions.pushObject(action);
2017-10-13 15:02:34 +02:00
});
}
steps.pushObject(Ember.Object.create({
id: s.id,
2017-10-13 15:02:34 +02:00
key: s.key,
title: s.title,
description: s.description,
2017-10-06 04:59:02 +02:00
banner: s.banner,
fields,
2017-10-17 15:17:53 +02:00
actions,
isNew: false
}));
});
2017-10-05 02:36:46 +02:00
};
} else {
2017-10-07 04:27:38 +02:00
props['id'] = '';
props['name'] = '';
props['background'] = '';
2017-10-05 02:36:46 +02:00
props['save_submissions'] = true;
2017-10-13 15:02:34 +02:00
props['multiple_submissions'] = false;
2017-10-07 04:27:38 +02:00
props['steps'] = Ember.A();
};
wizard.setProperties(props);
2017-09-23 04:34:07 +02:00
return wizard;
}
});
export default CustomWizard;