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

141 Zeilen
3,5 KiB
Text

import {
default as discourseComputed,
2021-04-12 08:12:20 +02:00
observes,
} from "discourse-common/utils/decorators";
2021-04-12 07:44:47 +02:00
import { notEmpty } from "@ember/object/computed";
import showModal from "discourse/lib/show-modal";
import { generateId, wizardFieldList } from "../lib/wizard";
2020-03-30 01:53:28 +02:00
import { dasherize } from "@ember/string";
2021-04-12 07:10:02 +02:00
import { later, scheduleOnce } from "@ember/runloop";
2020-04-05 03:37:09 +02:00
import Controller from "@ember/controller";
2020-04-11 08:22:12 +02:00
import copyText from "discourse/lib/copy-text";
2020-05-28 05:06:06 +02:00
import I18n from "I18n";
2017-10-06 04:59:02 +02:00
2020-04-05 03:37:09 +02:00
export default Controller.extend({
hasName: notEmpty("wizard.name"),
@observes("currentStep")
2020-04-06 03:54:16 +02:00
resetCurrentObjects() {
const currentStep = this.currentStep;
2020-04-07 09:54:30 +02:00
if (currentStep) {
const fields = currentStep.fields;
this.set("currentField", fields && fields.length ? fields[0] : null);
2020-04-07 09:54:30 +02:00
}
scheduleOnce("afterRender", () => $("body").addClass("admin-wizard"));
2020-04-02 07:21:57 +02:00
},
@observes("wizard.name")
2020-03-30 01:53:28 +02:00
setId() {
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
},
@discourseComputed("wizard.id")
2020-04-01 07:03:26 +02:00
wizardUrl(wizardId) {
2022-11-11 10:43:57 +01:00
let baseUrl = window.location.href.split("/admin");
return baseUrl[0] + "/w/" + dasherize(wizardId);
2017-10-06 04:59:02 +02:00
},
@discourseComputed("wizard.after_time_scheduled")
2017-11-01 05:21:14 +01:00
nextSessionScheduledLabel(scheduled) {
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
},
@discourseComputed(
"currentStep.id",
"wizard.save_submissions",
"currentStep.fields.@each.label"
)
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) {
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
},
getErrorMessage(result) {
if (result.backend_validation_error) {
return result.backend_validation_error;
}
let errorType = "failed";
let errorParams = {};
if (result.error) {
errorType = result.error.type;
errorParams = result.error.params;
}
return I18n.t(`admin.wizard.error.${errorType}`, errorParams);
},
2017-11-01 05:21:14 +01: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,
2017-10-13 15:02:34 +02:00
});
2020-04-13 14:17:22 +02:00
const wizard = this.wizard;
const creating = this.creating;
let opts = {};
2020-04-13 14:17:22 +02:00
if (creating) {
opts.create = true;
}
wizard
.save(opts)
.then((result) => {
2021-09-01 04:19:00 +02:00
if (result.wizard_id) {
this.send("afterSave", result.wizard_id);
} else if (result.errors) {
2021-09-07 14:13:01 +02:00
this.set("error", result.errors.join(", "));
2021-09-01 04:19:00 +02:00
}
})
.catch((result) => {
this.set("error", this.getErrorMessage(result));
later(() => this.set("error", null), 10000);
})
.finally(() => this.set("saving", false));
2017-09-23 04:34:07 +02:00
},
remove() {
this.wizard.remove().then(() => this.send("afterDestroy"));
2017-11-01 05:21:14 +01:00
},
setNextSessionScheduled() {
let controller = showModal("next-session-scheduled", {
2017-11-01 05:21:14 +01:00
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-11 08:22:12 +02:00
copyUrl() {
const $copyRange = $('<p id="copy-range"></p>');
$copyRange.html(this.wizardUrl);
2020-04-11 08:22:12 +02:00
$(document.body).append($copyRange);
2020-04-11 08:22:12 +02:00
if (copyText(this.wizardUrl, $copyRange[0])) {
this.set("copiedUrl", true);
later(() => this.set("copiedUrl", false), 2000);
}
2020-04-11 08:22:12 +02:00
$copyRange.remove();
},
},
2017-09-23 04:34:07 +02:00
});