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

261 Zeilen
6,1 KiB
Text

import { ajax } from 'discourse/lib/ajax';
2017-09-23 04:34:07 +02:00
2017-11-01 05:21:14 +01:00
const wizardProperties = [
'name',
'background',
'save_submissions',
'multiple_submissions',
'after_signup',
'after_time',
'after_time_scheduled',
2017-11-22 10:34:21 +01:00
'required',
2017-11-29 10:48:49 +01:00
'prompt_completion',
'min_trust',
'theme_key'
2017-11-01 05:21:14 +01:00
];
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');
2017-10-30 07:24:51 +01:00
if (!id || !id.underscore()) return reject({ error: 'id_required' });
2017-10-13 15:02:34 +02:00
let wizard = { id: id.underscore() };
2017-11-01 05:21:14 +01:00
wizardProperties.forEach((p) => {
const value = this.get(p);
if (value) wizard[p] = value;
});
2017-11-01 10:50:03 +01:00
if (wizard['after_time'] && !wizard['after_time_scheduled']) {
2017-11-01 05:21:14 +01:00
return reject({ error: 'after_time_need_time' });
};
2017-10-13 15:02:34 +02:00
const steps = this.get('steps');
2017-10-30 07:24:51 +01:00
if (steps.length > 0) {
const stepsResult = this.buildSteps(steps);
if (stepsResult.error) {
2017-11-01 05:21:14 +01:00
reject({ error: stepsResult.error });
2017-10-30 07:24:51 +01:00
} else {
2017-11-01 05:21:14 +01:00
wizard['steps'] = stepsResult.steps;
2017-10-30 07:24:51 +01:00
}
}
if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) {
return reject({ error: 'steps_required' });
}
2017-10-13 15:02:34 +02:00
ajax("/admin/wizards/custom/save", {
type: 'PUT',
data: {
wizard: JSON.stringify(wizard)
}
2017-10-30 07:24:51 +01:00
}).then((result) => {
if (result.error) {
reject(result);
} else {
resolve(result);
}
});
2017-10-13 15:02:34 +02:00
});
2017-10-07 04:27:38 +02:00
},
2017-09-24 05:01:18 +02:00
2017-10-30 07:24:51 +01:00
buildSteps(stepsObj) {
let steps = [];
2017-10-30 07:24:51 +01:00
let error = null;
2017-10-13 15:02:34 +02:00
stepsObj.some((s) => {
2017-10-30 07:24:51 +01:00
if (!s.id || !s.id.underscore()) {
error = 'id_required';
return;
};
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;
2017-11-03 09:22:50 +01:00
if (s.raw_description) step['raw_description'] = s.raw_description;
2017-10-07 04:27:38 +02:00
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) => {
2017-10-30 07:24:51 +01:00
let id = f.id;
2017-10-07 04:27:38 +02:00
2017-10-30 07:24:51 +01:00
if (!id || !id.underscore()) {
error = 'id_required';
return;
}
2017-11-23 10:02:25 +01:00
if (!f.type) {
error = 'type_required';
return;
}
2017-10-13 15:02:34 +02:00
f.set('id', id.underscore());
2017-10-05 02:36:46 +02:00
2017-10-30 07:24:51 +01:00
if (f.label === '') delete f.label;
if (f.description === '') delete f.description;
if (f.type === 'dropdown') {
const choices = f.choices;
2017-11-07 07:28:26 +01:00
if ((!choices || choices.length < 1) && !f.choices_key && !f.choices_preset) {
2017-11-01 05:21:14 +01:00
error = 'field.need_choices';
return;
}
2017-11-23 10:02:25 +01:00
if (f.dropdown_none === '') delete f.dropdown_none;
2017-10-13 15:02:34 +02:00
}
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-30 07:24:51 +01:00
if (error) return;
2017-10-13 15:02:34 +02:00
}
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');
2017-10-30 07:24:51 +01:00
if (!id || !id.underscore()) {
error = 'id_required';
return;
}
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);
});
2017-10-30 07:24:51 +01:00
if (error) return;
2017-10-13 15:02:34 +02:00
}
steps.push(step);
});
2017-10-30 07:24:51 +01:00
if (error) {
return { error };
} else {
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({
2017-10-22 05:37:58 +02:00
all() {
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-22 05:37:58 +02:00
submissions(wizardId) {
return ajax(`/admin/wizards/submissions/${wizardId}`, {
2017-10-05 02:36:46 +02:00
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-11-01 05:21:14 +01:00
wizardProperties.forEach((p) => {
props[p] = w[p];
});
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($.extend(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($.extend(a, actionParams));
2017-10-17 15:17:53 +02:00
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,
2017-11-03 09:22:50 +01:00
raw_description: s.raw_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-11-01 05:21:14 +01:00
props['after_signup'] = false;
props['after_time'] = false;
props['required'] = false;
2017-11-22 10:34:21 +01:00
props['prompt_completion'] = false;
2017-11-29 10:48:49 +01:00
props['min_trust'] = 0;
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;