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
Angus McLeod 87a53a8c85 wip
2020-04-02 16:21:57 +11:00

113 Zeilen
3 KiB
JavaScript

import { default as discourseComputed, observes } from 'discourse-common/utils/decorators';
import { notEmpty } from "@ember/object/computed";
import showModal from 'discourse/lib/show-modal';
import { generateId } from '../lib/wizard';
import { buildProperties } from '../lib/wizard-json';
import { dasherize } from "@ember/string";
import EmberObject from "@ember/object";
import { scheduleOnce } from "@ember/runloop";
export default Ember.Controller.extend({
hasName: notEmpty('model.name'),
init() {
this._super();
scheduleOnce('afterRender', () => ($("body").addClass('admin-wizard')));
},
@observes('model.name')
setId() {
if (!this.model.existingId) this.set('model.id', generateId(this.model.name));
},
@discourseComputed('model.id')
wizardUrl(wizardId) {
return window.location.origin + '/w/' + dasherize(wizardId);
},
@discourseComputed('model.after_time_scheduled')
nextSessionScheduledLabel(scheduled) {
return scheduled ?
moment(scheduled).format('MMMM Do, HH:mm') :
I18n.t('admin.wizard.after_time_time_label');
},
@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) => {
return EmberObject.create({
id: f.id,
label: `${f.id} (${s.id})`,
type: f.type
});
});
fields.push(...stepFields);
}
});
return fields;
},
actions: {
save() {
this.setProperties({
saving: true,
error: null
});
const wizard = this.model;
wizard.save().then((result) => {
this.model.setProperties(
buildProperties(result.wizard)
);
this.set('saving', false);
if (this.get('newWizard')) {
this.send("refreshAllWizards");
} else {
this.send("refreshWizard");
}
}).catch((result) => {
this.set('saving', false);
this.set('error', I18n.t(`admin.wizard.error.${result.error}`));
Ember.run.later(() => this.set('error', null), 10000);
});
},
remove() {
const wizard = this.get('model');
wizard.remove().then(() => {
this.send("refreshAllWizards");
});
},
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();
},
toggleAdvanced() {
this.toggleProperty('model.showAdvanced');
}
}
});