123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { observes, on } from 'ember-addons/ember-computed-decorators';
- import { ajax } from 'discourse/lib/ajax';
- const CustomWizard = Discourse.Model.extend({
- @on('init')
- setup() {
- const id = this.get('id');
- if (id) this.set('existingId', id);
- },
- @observes('name')
- updateId() {
- const name = this.get('name');
- this.set('id', name.underscore());
- },
- save() {
- const stepsObj = this.get('steps');
- let steps = [];
- stepsObj.forEach((s) => {
- if (!s.title && !s.translation_key) return;
- let step = {
- id: (s.title || s.translation_key.split('.').pop()).underscore(),
- fields: [],
- actions: []
- };
- 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) => {
- const fl = f.get('label');
- const fkey = f.get('translation_key');
- if (!fl && !fkey) return;
- f.set('id', (fl || fkey.split('.').pop()).underscore());
- if (f.get('type') === 'dropdown') {
- const choices = f.get('choices');
- choices.forEach((c) => {
- const cl = c.get('label');
- const ckey = c.get('translation_key');
- if (!cl && !ckey) return;
- c.set('id', (cl || ckey.split('.').pop()).underscore());
- });
- }
- step['fields'].push(f);
- });
- s.actions.forEach((a) => {
- 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');
- const background = this.get('background');
- const save_submissions = this.get('save_submissions');
- let wizard = { id, name, background, save_submissions, steps };
- const existingId = this.get('existingId');
- if (existingId && existingId !== id) {
- wizard['existing_id'] = existingId;
- };
- return ajax("/admin/wizards/custom/save", {
- type: 'PUT',
- data: {
- wizard: JSON.stringify(wizard)
- }
- });
- },
- remove() {
- return ajax("/admin/wizards/custom/remove", {
- type: 'DELETE',
- data: {
- id: this.get('id')
- }
- }).then(() => this.destroy());
- }
- });
- CustomWizard.reopenClass({
- findAll() {
- return ajax("/admin/wizards/custom/all", {
- type: 'GET'
- }).then(result => {
- return result.wizards.map(w => CustomWizard.create(w));
- });
- },
- 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) {
- props['id'] = w.id;
- props['name'] = w.name;
- props['background'] = w.background;
- if (w.steps) {
- w.steps.forEach((s) => {
- let fields = Ember.A();
- s.fields.forEach((f) => {
- let field = Ember.Object.create(f);
- let choices = Ember.A();
- f.choices.forEach((c) => {
- choices.pushObject(Ember.Object.create(c));
- });
- 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,
- translation_key: s.translation_key,
- title: s.title,
- description: s.description,
- banner: s.banner,
- fields,
- actions
- }));
- });
- };
- } else {
- props['id'] = '';
- props['name'] = '';
- props['background'] = '';
- props['save_submissions'] = true;
- props['steps'] = Ember.A();
- };
- wizard.setProperties(props);
- return wizard;
- }
- });
- export default CustomWizard;
|