0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2025-02-01 02:48:56 +01:00
discourse-custom-wizard/assets/javascripts/discourse/controllers/admin-wizards-wizard-show.js.es6

136 Zeilen
3,3 KiB
Text

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