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

176 Zeilen
4,1 KiB
Text

2017-10-07 04:27:38 +02:00
import { observes, on } 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({
2017-10-07 04:27:38 +02:00
@on('init')
setup() {
const id = this.get('id');
if (id) this.set('existingId', id);
},
2017-09-23 04:34:07 +02:00
2017-10-07 04:27:38 +02:00
@observes('name')
updateId() {
const name = this.get('name');
this.set('id', name.underscore());
},
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) => {
2017-10-07 04:27:38 +02:00
if (!s.title && !s.translation_key) return;
let step = {
2017-10-07 04:27:38 +02:00
id: (s.title || s.translation_key.split('.').pop()).underscore(),
fields: [],
actions: []
};
2017-10-07 04:27:38 +02:00
if (s.title) step['title'] = s.title;
if (s.translation_key) step['translation_key'] = s.translation_key;
if (s.banner) step['banner'] = s.banner;
if (s.description) step['description'] = s.description;
const fields = s.get('fields');
fields.forEach((f) => {
2017-10-07 04:27:38 +02:00
const fl = f.get('label');
const fkey = f.get('translation_key');
if (!fl && !fkey) return;
f.set('id', (fl || fkey.split('.').pop()).underscore());
2017-10-05 02:36:46 +02:00
if (f.get('type') === 'dropdown') {
const choices = f.get('choices');
2017-10-07 04:27:38 +02:00
2017-10-05 02:36:46 +02:00
choices.forEach((c) => {
2017-10-07 04:27:38 +02:00
const cl = c.get('label');
const ckey = c.get('translation_key');
if (!cl && !ckey) return;
c.set('id', (cl || ckey.split('.').pop()).underscore());
2017-10-05 02:36:46 +02:00
});
}
2017-10-06 04:59:02 +02:00
step['fields'].push(f);
});
s.actions.forEach((a) => {
2017-10-07 04:27:38 +02:00
const al = a.get('label');
if (!al) return;
a.set('id', al.underscore());
step['actions'].push(a);
});
steps.push(step);
});
const id = this.get('id');
const name = this.get('name');
2017-10-07 04:27:38 +02:00
const background = this.get('background');
2017-10-05 02:36:46 +02:00
const save_submissions = this.get('save_submissions');
2017-10-07 04:27:38 +02:00
let wizard = { id, name, background, 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);
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-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;
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,
2017-10-07 04:27:38 +02:00
translation_key: s.translation_key,
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 {
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-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;