2022-03-16 12:33:34 +01:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
|
|
|
import Component from "@ember/component";
|
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
|
|
|
import { htmlSafe } from "@ember/template";
|
|
|
|
import { schedule } from "@ember/runloop";
|
2023-10-09 19:03:51 +02:00
|
|
|
import { cook } from "discourse/lib/text";
|
2022-07-26 16:18:09 +02:00
|
|
|
import CustomWizard, {
|
|
|
|
updateCachedWizard,
|
2022-07-27 12:47:50 +02:00
|
|
|
} from "discourse/plugins/discourse-custom-wizard/discourse/models/custom-wizard";
|
2024-05-15 13:46:56 +02:00
|
|
|
import { alias, not, or } from "@ember/object/computed";
|
2023-03-22 10:11:48 +01:00
|
|
|
import discourseLater from "discourse-common/lib/later";
|
2024-05-15 13:46:56 +02:00
|
|
|
import { wizardComposerEdtiorEventPrefix } from "./custom-wizard-composer-editor";
|
|
|
|
|
2024-05-29 17:00:11 +02:00
|
|
|
const uploadStartedEventKeys = ["upload-started"];
|
2024-05-15 13:46:56 +02:00
|
|
|
const uploadEndedEventKeys = [
|
|
|
|
"upload-success",
|
|
|
|
"upload-error",
|
|
|
|
"upload-cancelled",
|
|
|
|
"uploads-cancelled",
|
|
|
|
"uploads-aborted",
|
2024-05-29 17:00:11 +02:00
|
|
|
"all-uploads-complete",
|
2024-05-15 13:46:56 +02:00
|
|
|
];
|
2022-03-16 12:33:34 +01:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
classNameBindings: [":wizard-step", "step.id"],
|
|
|
|
saving: null,
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set("stylingDropdown", {});
|
|
|
|
},
|
|
|
|
|
2022-07-26 16:18:09 +02:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2023-10-09 19:03:51 +02:00
|
|
|
cook(this.step.translatedTitle).then((cookedTitle) => {
|
2022-07-26 16:18:09 +02:00
|
|
|
this.set("cookedTitle", cookedTitle);
|
|
|
|
});
|
2023-10-09 19:03:51 +02:00
|
|
|
cook(this.step.translatedDescription).then((cookedDescription) => {
|
2022-07-26 16:18:09 +02:00
|
|
|
this.set("cookedDescription", cookedDescription);
|
|
|
|
});
|
2024-05-15 13:46:56 +02:00
|
|
|
|
2024-05-29 17:00:11 +02:00
|
|
|
uploadStartedEventKeys.forEach((key) => {
|
2024-05-15 13:46:56 +02:00
|
|
|
this.appEvents.on(`${wizardComposerEdtiorEventPrefix}:${key}`, () => {
|
|
|
|
this.set("uploading", true);
|
|
|
|
});
|
|
|
|
});
|
2024-05-29 17:00:11 +02:00
|
|
|
uploadEndedEventKeys.forEach((key) => {
|
2024-05-15 13:46:56 +02:00
|
|
|
this.appEvents.on(`${wizardComposerEdtiorEventPrefix}:${key}`, () => {
|
|
|
|
this.set("uploading", false);
|
|
|
|
});
|
|
|
|
});
|
2022-07-26 16:18:09 +02:00
|
|
|
},
|
|
|
|
|
2022-03-16 12:33:34 +01:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.autoFocus();
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.index", "wizard.required")
|
2022-06-15 08:59:09 +02:00
|
|
|
showQuitButton: (index, required) => index === 0 && !required,
|
2022-03-16 12:33:34 +01:00
|
|
|
|
|
|
|
showNextButton: not("step.final"),
|
|
|
|
showDoneButton: alias("step.final"),
|
2024-05-15 13:46:56 +02:00
|
|
|
btnsDisabled: or("saving", "uploading"),
|
2022-03-16 12:33:34 +01:00
|
|
|
|
|
|
|
@discourseComputed(
|
|
|
|
"step.index",
|
|
|
|
"step.displayIndex",
|
|
|
|
"wizard.totalSteps",
|
|
|
|
"wizard.completed"
|
|
|
|
)
|
|
|
|
showFinishButton: (index, displayIndex, total, completed) => {
|
|
|
|
return index !== 0 && displayIndex !== total && completed;
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.index")
|
|
|
|
showBackButton: (index) => index > 0,
|
|
|
|
|
|
|
|
@discourseComputed("step.banner")
|
|
|
|
bannerImage(src) {
|
|
|
|
if (!src) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return getUrl(src);
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.id")
|
|
|
|
bannerAndDescriptionClass(id) {
|
|
|
|
return `wizard-banner-and-description wizard-banner-and-description-${id}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.fields.[]")
|
|
|
|
primaryButtonIndex(fields) {
|
|
|
|
return fields.length + 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.fields.[]")
|
|
|
|
secondaryButtonIndex(fields) {
|
|
|
|
return fields.length + 2;
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("step.id")
|
|
|
|
_stepChanged() {
|
|
|
|
this.set("saving", false);
|
|
|
|
this.autoFocus();
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("step.message")
|
|
|
|
_handleMessage: function () {
|
|
|
|
const message = this.get("step.message");
|
2022-03-16 14:09:23 +01:00
|
|
|
this.showMessage(message);
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.index", "wizard.totalSteps")
|
|
|
|
barStyle(displayIndex, totalSteps) {
|
|
|
|
let ratio = parseFloat(displayIndex) / parseFloat(totalSteps - 1);
|
|
|
|
if (ratio < 0) {
|
|
|
|
ratio = 0;
|
|
|
|
}
|
|
|
|
if (ratio > 1) {
|
|
|
|
ratio = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlSafe(`width: ${ratio * 200}px`);
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("step.fields")
|
|
|
|
includeSidebar(fields) {
|
|
|
|
return !!fields.findBy("show_in_sidebar");
|
|
|
|
},
|
|
|
|
|
|
|
|
autoFocus() {
|
2023-03-22 10:11:48 +01:00
|
|
|
discourseLater(() => {
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
if ($(".invalid .wizard-focusable").length) {
|
|
|
|
this.animateInvalidFields();
|
|
|
|
}
|
|
|
|
});
|
2022-03-16 12:33:34 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
animateInvalidFields() {
|
|
|
|
schedule("afterRender", () => {
|
2023-03-22 10:11:48 +01:00
|
|
|
let $invalid = $(".invalid .wizard-focusable");
|
|
|
|
if ($invalid.length) {
|
2022-03-16 12:33:34 +01:00
|
|
|
$([document.documentElement, document.body]).animate(
|
|
|
|
{
|
2023-03-22 10:11:48 +01:00
|
|
|
scrollTop: $invalid.offset().top - 200,
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
2022-08-24 11:35:17 +02:00
|
|
|
400
|
2022-03-16 12:33:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
advance() {
|
|
|
|
this.set("saving", true);
|
|
|
|
this.get("step")
|
|
|
|
.save()
|
|
|
|
.then((response) => {
|
|
|
|
updateCachedWizard(CustomWizard.build(response["wizard"]));
|
|
|
|
|
|
|
|
if (response["final"]) {
|
|
|
|
CustomWizard.finished(response);
|
|
|
|
} else {
|
2022-03-16 14:09:23 +01:00
|
|
|
this.goNext(response);
|
2022-03-16 12:33:34 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => this.animateInvalidFields())
|
|
|
|
.finally(() => this.set("saving", false));
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
quit() {
|
|
|
|
this.get("wizard").skip();
|
|
|
|
},
|
|
|
|
|
|
|
|
done() {
|
|
|
|
this.send("nextStep");
|
|
|
|
},
|
|
|
|
|
|
|
|
showMessage(message) {
|
2022-03-16 14:09:23 +01:00
|
|
|
this.sendAction(message);
|
2022-03-16 12:33:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
stylingDropdownChanged(id, value) {
|
|
|
|
this.set("stylingDropdown", { id, value });
|
|
|
|
},
|
|
|
|
|
|
|
|
exitEarly() {
|
|
|
|
const step = this.step;
|
|
|
|
step.validate();
|
|
|
|
|
|
|
|
if (step.get("valid")) {
|
|
|
|
this.set("saving", true);
|
|
|
|
|
|
|
|
step
|
|
|
|
.save()
|
|
|
|
.then(() => this.send("quit"))
|
|
|
|
.finally(() => this.set("saving", false));
|
|
|
|
} else {
|
|
|
|
this.autoFocus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
backStep() {
|
|
|
|
if (this.saving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.goBack();
|
|
|
|
},
|
|
|
|
|
|
|
|
nextStep() {
|
|
|
|
if (this.saving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-23 08:22:28 +01:00
|
|
|
this.step.validate();
|
2022-03-16 12:33:34 +01:00
|
|
|
|
2024-02-23 10:39:12 +01:00
|
|
|
if (this.step.get("valid")) {
|
2022-03-16 12:33:34 +01:00
|
|
|
this.advance();
|
|
|
|
} else {
|
|
|
|
this.autoFocus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|