0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 10:40:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/custom-wizard-step.js.es6

230 Zeilen
5,3 KiB
Text

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";
import { cook } from "discourse/lib/text";
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";
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", {});
},
didReceiveAttrs() {
this._super(...arguments);
cook(this.step.translatedTitle).then((cookedTitle) => {
this.set("cookedTitle", cookedTitle);
});
cook(this.step.translatedDescription).then((cookedDescription) => {
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-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");
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() {
discourseLater(() => {
schedule("afterRender", () => {
if ($(".invalid .wizard-focusable").length) {
this.animateInvalidFields();
}
});
2022-03-16 12:33:34 +01:00
});
},
animateInvalidFields() {
schedule("afterRender", () => {
let $invalid = $(".invalid .wizard-focusable");
if ($invalid.length) {
2022-03-16 12:33:34 +01:00
$([document.documentElement, document.body]).animate(
{
scrollTop: $invalid.offset().top - 200,
2022-03-16 12:33:34 +01: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 {
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) {
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();
}
},
},
});