From 10df3208df861480edd5e55e335a7897bc6ccdca Mon Sep 17 00:00:00 2001 From: merefield Date: Fri, 7 Jul 2023 17:16:51 +0100 Subject: [PATCH] IMPROVE: Move pending wizard check to page change event --- .../initializers/custom-wizard-edits.js.es6 | 90 +++++++++++-------- .../custom-wizard-redirect.js.es6 | 44 --------- 2 files changed, 54 insertions(+), 80 deletions(-) delete mode 100644 assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index c974fafb..5da38fe8 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -21,6 +21,24 @@ export default { }; withPluginApi("0.8.36", (api) => { + api.onAppEvent('page:changed', (data) => { + const currentUser = container.lookup("service:current-user"); + const settings = container.lookup("service:site-settings"); + 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; + })) + ) { + window.location = "/w/" + redirectToWizard.dasherize(); + } + }); + api.modifyClass("component:d-navigation", { pluginId: "custom-wizard", actions: { @@ -42,47 +60,47 @@ export default { // Needed to ensure appEvents get registered when navigating between steps @observes("id") initOnStepChange() { - if (/wizard-field|wizard-step/.test(this.id)) { - this._initialize(); - } - }, + if(/ wizard - field | wizard - step /.test(this.id)) { + this._initialize(); + } + }, }); - api.modifyClass("component:d-editor", { - pluginId: "custom-wizard", + api.modifyClass("component:d-editor", { + pluginId: "custom-wizard", - didInsertElement() { - this._super(...arguments); + didInsertElement() { + this._super(...arguments); - if (this.wizardComposer) { - this.appEvents.on( - `wizard-editor:insert-text`, - this, - "_wizardInsertText" - ); - this.appEvents.on( - "wizard-editor:replace-text", - this, - "_wizardReplaceText" - ); - } - }, + if (this.wizardComposer) { + this.appEvents.on( + `wizard-editor:insert-text`, + this, + "_wizardInsertText" + ); + this.appEvents.on( + "wizard-editor:replace-text", + this, + "_wizardReplaceText" + ); + } + }, - _wizardInsertText(text, options) { - if ( - this.session.wizardEventFieldId === this.fieldId && - this.element - ) { - this.insertText(text, options); - } - }, + _wizardInsertText(text, options) { + if ( + this.session.wizardEventFieldId === this.fieldId && + this.element + ) { + this.insertText(text, options); + } + }, - _wizardReplaceText(oldVal, newVal, opts = {}) { - if (this.session.wizardEventFieldId === this.fieldId) { - this.replaceText(oldVal, newVal, opts); - } - }, - }); - }); + _wizardReplaceText(oldVal, newVal, opts = {}) { + if (this.session.wizardEventFieldId === this.fieldId) { + this.replaceText(oldVal, newVal, opts); + } + }, + }); +}); }, }; diff --git a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 deleted file mode 100644 index 1438f0d6..00000000 --- a/assets/javascripts/discourse/initializers/custom-wizard-redirect.js.es6 +++ /dev/null @@ -1,44 +0,0 @@ -export default { - name: "custom-wizard-redirect", - after: "message-bus", - - initialize: function (container) { - const messageBus = container.lookup("service:message-bus"); - const siteSettings = container.lookup("service:site-settings"); - - if (!siteSettings.custom_wizard_enabled || !messageBus) { - return; - } - - messageBus.subscribe("/redirect_to_wizard", function (wizardId) { - const wizardUrl = window.location.origin + "/w/" + wizardId; - window.location.href = wizardUrl; - }); - - const ApplicationRoute = requirejs("discourse/routes/application").default; - - ApplicationRoute.reopen({ - actions: { - willTransition(transition) { - const redirectToWizard = this.get("currentUser.redirect_to_wizard"); - const excludedPaths = this.siteSettings.wizard_redirect_exclude_paths - .split("|") - .concat(["loading"]); - - if ( - redirectToWizard && - (!transition.intent.name || - !excludedPaths.find((p) => { - return transition.intent.name.indexOf(p) > -1; - })) - ) { - transition.abort(); - window.location = "/w/" + redirectToWizard.dasherize(); - } - - return this._super(transition); - }, - }, - }); - }, -};