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

147 Zeilen
3,3 KiB
Text

2017-09-24 05:01:18 +02:00
import { default as computed } from 'ember-addons/ember-computed-decorators';
import { ajax } from 'discourse/lib/ajax';
2017-09-23 04:34:07 +02:00
const CustomWizard = Discourse.Model.extend({
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() {
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
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')));
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 };
const existingId = this.get('existingId');
if (existingId && existingId !== id) {
wizard['existing_id'] = existingId;
2017-09-24 05:01:18 +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() {
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);
2017-09-23 04:34:07 +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;
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);
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);
});
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,
fields,
actions
}));
});
2017-10-05 02:36:46 +02:00
};
} else {
props['save_submissions'] = true;
};
wizard.setProperties(props);
2017-09-23 04:34:07 +02:00
return wizard;
}
});
export default CustomWizard;