0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 17:30:29 +01:00
discourse-custom-wizard/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6

108 Zeilen
3,2 KiB
Text

import DiscourseURL from "discourse/lib/url";
2021-09-24 11:58:42 +02:00
import { withPluginApi } from "discourse/lib/plugin-api";
import getUrl from "discourse-common/lib/get-url";
import { observes } from "discourse-common/utils/decorators";
2023-07-11 18:49:08 +02:00
import { dasherize } from "@ember/string";
2017-11-22 10:34:21 +01:00
export default {
name: "custom-wizard-edits",
initialize(container) {
2023-07-11 20:49:19 +02:00
const messageBus = container.lookup("service:message-bus");
const siteSettings = container.lookup("service:site-settings");
2021-04-12 08:12:20 +02:00
if (!siteSettings.custom_wizard_enabled) {
return;
}
2023-07-11 20:49:19 +02:00
messageBus.subscribe("/redirect_to_wizard", function (wizardId) {
const wizardUrl = window.location.origin + "/w/" + wizardId;
window.location.href = wizardUrl;
});
2021-09-24 11:58:42 +02:00
withPluginApi("0.8.36", (api) => {
2023-07-10 08:03:45 +02:00
api.onAppEvent("page:changed", (data) => {
const currentUser = container.lookup("service:current-user");
const settings = container.lookup("service:site-settings");
2023-07-11 09:48:20 +02:00
if (currentUser) {
const redirectToWizard = currentUser.redirect_to_wizard;
const excludedPaths = settings.wizard_redirect_exclude_paths
.split("|")
.concat(["loading"]);
if (
redirectToWizard &&
data.currentRouteName !== "customWizardStep" &&
!excludedPaths.find((p) => {
return data.currentRouteName.indexOf(p) > -1;
})
) {
2023-07-11 18:49:08 +02:00
DiscourseURL.routeTo(`/w/${dasherize(redirectToWizard)}`);
2023-07-11 09:48:20 +02:00
}
}
});
api.modifyClass("component:d-navigation", {
2021-11-11 13:08:53 +01:00
pluginId: "custom-wizard",
actions: {
2021-11-11 13:08:53 +01:00
clickCreateTopicButton() {
let createTopicWizard = this.get(
"category.custom_fields.create_topic_wizard"
);
if (createTopicWizard) {
window.location.href = getUrl(`/w/${createTopicWizard}`);
} else {
this._super();
}
2021-11-11 13:08:53 +01:00
},
2021-11-17 13:48:11 +01:00
},
2021-09-24 11:58:42 +02:00
});
api.modifyClass("component:uppy-image-uploader", {
2022-07-27 12:47:50 +02:00
pluginId: "custom-wizard",
// Needed to ensure appEvents get registered when navigating between steps
@observes("id")
initOnStepChange() {
2023-07-11 15:32:04 +02:00
if (/wizard-field|wizard-step/.test(this.id)) {
2023-07-10 08:03:45 +02:00
this._initialize();
}
},
});
2023-07-10 08:03:45 +02:00
api.modifyClass("component:d-editor", {
pluginId: "custom-wizard",
2023-07-10 08:03:45 +02:00
didInsertElement() {
this._super(...arguments);
2023-07-10 08:03:45 +02:00
if (this.wizardComposer) {
this.appEvents.on(
`wizard-editor:insert-text`,
this,
"_wizardInsertText"
);
this.appEvents.on(
"wizard-editor:replace-text",
this,
"_wizardReplaceText"
);
}
},
2023-07-10 08:03:45 +02:00
_wizardInsertText(text, options) {
if (
this.session.wizardEventFieldId === this.fieldId &&
this.element
) {
this.insertText(text, options);
}
},
2023-07-10 08:03:45 +02:00
_wizardReplaceText(oldVal, newVal, opts = {}) {
if (this.session.wizardEventFieldId === this.fieldId) {
this.replaceText(oldVal, newVal, opts);
}
},
});
});
},
2017-11-22 10:34:21 +01:00
};