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

114 Zeilen
3 KiB
Text

2020-04-01 07:03:26 +02:00
import { default as discourseComputed, observes } from 'discourse-common/utils/decorators';
2020-03-30 01:53:28 +02:00
import { notEmpty } from "@ember/object/computed";
2017-11-01 05:21:14 +01:00
import showModal from 'discourse/lib/show-modal';
2020-04-02 07:21:57 +02:00
import { generateId } from '../lib/wizard';
import { buildProperties } from '../lib/wizard-json';
2020-03-30 01:53:28 +02:00
import { dasherize } from "@ember/string";
2020-04-02 07:21:57 +02:00
import EmberObject from "@ember/object";
import { scheduleOnce } from "@ember/runloop";
2017-10-06 04:59:02 +02:00
2017-09-23 04:34:07 +02:00
export default Ember.Controller.extend({
2020-03-30 01:53:28 +02:00
hasName: notEmpty('model.name'),
2020-04-02 07:21:57 +02:00
init() {
this._super();
scheduleOnce('afterRender', () => ($("body").addClass('admin-wizard')));
},
2020-03-30 01:53:28 +02:00
@observes('model.name')
setId() {
2020-04-01 07:03:26 +02:00
if (!this.model.existingId) this.set('model.id', generateId(this.model.name));
},
@discourseComputed('model.id')
wizardUrl(wizardId) {
return window.location.origin + '/w/' + dasherize(wizardId);
2017-10-06 04:59:02 +02:00
},
2020-04-01 07:03:26 +02:00
@discourseComputed('model.after_time_scheduled')
2017-11-01 05:21:14 +01:00
nextSessionScheduledLabel(scheduled) {
2020-03-29 09:49:33 +02:00
return scheduled ?
moment(scheduled).format('MMMM Do, HH:mm') :
I18n.t('admin.wizard.after_time_time_label');
2017-11-01 05:21:14 +01:00
},
2020-04-01 07:03:26 +02:00
@discourseComputed('currentStep.id', 'model.save_submissions', 'model.steps.@each.fields[]')
wizardFields(currentStepId, saveSubmissions) {
const allSteps = this.get('model.steps');
let steps = allSteps;
let fields = [];
if (!saveSubmissions) {
steps = [allSteps.findBy('id', currentStepId)];
}
steps.forEach((s) => {
if (s.fields && s.fields.length > 0) {
let stepFields = s.fields.map((f) => {
2020-04-02 07:21:57 +02:00
return EmberObject.create({
2020-04-01 07:03:26 +02:00
id: f.id,
label: `${f.id} (${s.id})`,
type: f.type
});
});
fields.push(...stepFields);
}
});
return fields;
},
2017-11-01 05:21:14 +01:00
2017-09-23 04:34:07 +02:00
actions: {
save() {
2017-10-13 15:02:34 +02:00
this.setProperties({
saving: true,
error: null
});
2020-03-29 09:49:33 +02:00
2020-04-01 14:16:26 +02:00
const wizard = this.model;
wizard.save().then((result) => {
this.model.setProperties(
buildProperties(result.wizard)
);
2020-03-29 09:49:33 +02:00
2017-10-13 15:02:34 +02:00
this.set('saving', false);
2020-04-01 14:16:26 +02:00
2017-10-07 04:27:38 +02:00
if (this.get('newWizard')) {
this.send("refreshAllWizards");
} else {
this.send("refreshWizard");
}
2017-10-30 07:24:51 +01:00
}).catch((result) => {
2017-10-13 15:02:34 +02:00
this.set('saving', false);
2017-10-30 07:24:51 +01:00
this.set('error', I18n.t(`admin.wizard.error.${result.error}`));
2017-10-13 15:02:34 +02:00
Ember.run.later(() => this.set('error', null), 10000);
2017-09-23 04:34:07 +02:00
});
},
remove() {
2017-11-01 05:21:14 +01:00
const wizard = this.get('model');
wizard.remove().then(() => {
2017-10-07 04:27:38 +02:00
this.send("refreshAllWizards");
2017-09-23 04:34:07 +02:00
});
2017-11-01 05:21:14 +01:00
},
setNextSessionScheduled() {
let controller = showModal('next-session-scheduled', {
model: {
dateTime: this.get('model.after_time_scheduled'),
update: (dateTime) => this.set('model.after_time_scheduled', dateTime)
}
});
controller.setup();
},
2020-04-02 07:21:57 +02:00
toggleAdvanced() {
this.toggleProperty('model.showAdvanced');
}
2017-09-23 04:34:07 +02:00
}
});