0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-27 03:20:27 +01:00
discourse-custom-wizard/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6

123 Zeilen
3,5 KiB
Text

2020-04-06 03:54:16 +02:00
import { default as discourseComputed, observes, on } from 'discourse-common/utils/decorators';
import { notEmpty, alias } from "@ember/object/computed";
2017-11-01 05:21:14 +01:00
import showModal from 'discourse/lib/show-modal';
2020-04-14 07:46:06 +02:00
import { generateId, wizardFieldList } from '../lib/wizard';
2020-04-02 07:21:57 +02:00
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";
2020-04-05 03:37:09 +02:00
import { scheduleOnce, later } from "@ember/runloop";
import Controller from "@ember/controller";
2020-04-11 08:22:12 +02:00
import copyText from "discourse/lib/copy-text";
2020-04-13 14:17:22 +02:00
import CustomWizard from '../models/custom-wizard';
2017-10-06 04:59:02 +02:00
2020-04-05 03:37:09 +02:00
export default Controller.extend({
2020-04-13 14:17:22 +02:00
hasName: notEmpty('wizard.name'),
2020-04-08 09:59:54 +02:00
2020-04-06 03:54:16 +02:00
@observes('currentStep')
resetCurrentObjects() {
const currentStep = this.currentStep;
2020-04-07 09:54:30 +02:00
if (currentStep) {
const fields = currentStep.fields;
2020-04-08 09:59:54 +02:00
this.set('currentField', fields && fields.length ? fields[0] : null)
2020-04-07 09:54:30 +02:00
}
2020-04-06 03:54:16 +02:00
2020-04-02 07:21:57 +02:00
scheduleOnce('afterRender', () => ($("body").addClass('admin-wizard')));
},
2020-04-13 14:17:22 +02:00
@observes('wizard.name')
2020-03-30 01:53:28 +02:00
setId() {
2020-04-13 14:17:22 +02:00
const wizard = this.wizard;
if (wizard && !wizard.existingId) {
this.set('wizard.id', generateId(wizard.name));
2020-04-06 03:54:16 +02:00
}
2020-04-01 07:03:26 +02:00
},
2020-04-13 14:17:22 +02:00
@discourseComputed('wizard.id')
2020-04-01 07:03:26 +02:00
wizardUrl(wizardId) {
return window.location.origin + '/w/' + dasherize(wizardId);
2017-10-06 04:59:02 +02:00
},
2020-04-13 14:17:22 +02:00
@discourseComputed('wizard.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
2020-04-13 14:17:22 +02:00
@discourseComputed('currentStep.id', 'wizard.save_submissions', 'wizard.steps.@each.fields[]')
2020-04-01 07:03:26 +02:00
wizardFields(currentStepId, saveSubmissions) {
2020-04-14 07:46:06 +02:00
let steps = this.wizard.steps;
2020-04-01 07:03:26 +02:00
if (!saveSubmissions) {
2020-04-14 07:46:06 +02:00
steps = [steps.findBy('id', currentStepId)];
2020-04-01 07:03:26 +02:00
}
2020-04-14 07:46:06 +02:00
return wizardFieldList(steps);
2020-04-01 07:03:26 +02:00
},
2017-11-01 05:21:14 +01:00
2020-04-13 14:17:22 +02:00
actions: {
2017-09-23 04:34:07 +02:00
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-13 14:17:22 +02:00
const wizard = this.wizard;
const creating = this.creating;
let opts = {};
2020-04-01 14:16:26 +02:00
2020-04-13 14:17:22 +02:00
if (creating) {
opts.create = true;
}
2020-03-29 09:49:33 +02:00
2020-04-13 14:17:22 +02:00
wizard.save(opts).then((result) => {
this.send('afterSave', result.wizard_id);
}).catch((result) => {
2020-04-11 08:22:12 +02:00
let errorType = 'failed';
let errorParams = {};
2020-04-10 09:57:49 +02:00
if (result.error) {
2020-04-11 08:22:12 +02:00
errorType = result.error.type;
errorParams = result.error.params;
2020-04-10 09:57:49 +02:00
}
2020-04-11 08:22:12 +02:00
this.set('error', I18n.t(`admin.wizard.error.${errorType}`, errorParams));
2020-04-10 09:57:49 +02:00
2020-04-05 03:37:09 +02:00
later(() => this.set('error', null), 10000);
2020-04-13 14:17:22 +02:00
}).finally(() => this.set('saving', false));
2017-09-23 04:34:07 +02:00
},
remove() {
2020-04-13 14:17:22 +02:00
this.wizard.remove().then(() => this.send('afterDestroy'));
2017-11-01 05:21:14 +01:00
},
setNextSessionScheduled() {
let controller = showModal('next-session-scheduled', {
model: {
2020-04-13 14:17:22 +02:00
dateTime: this.wizard.after_time_scheduled,
update: (dateTime) => this.set('wizard.after_time_scheduled', dateTime)
2017-11-01 05:21:14 +01:00
}
});
controller.setup();
},
2020-04-02 07:21:57 +02:00
toggleAdvanced() {
2020-04-13 14:17:22 +02:00
this.toggleProperty('wizard.showAdvanced');
2020-04-11 08:22:12 +02:00
},
copyUrl() {
const $copyRange = $('<p id="copy-range"></p>');
$copyRange.html(this.wizardUrl);
$(document.body).append($copyRange);
if (copyText(this.wizardUrl, $copyRange[0])) {
this.set("copiedUrl", true);
later(() => this.set("copiedUrl", false), 2000);
}
$copyRange.remove();
2020-04-02 07:21:57 +02:00
}
2017-09-23 04:34:07 +02:00
}
});