OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add static stimulus registry to allow registering from plugins
Oliver Günthere3408c9c4dc4
diff --git a/frontend/src/stimulus/openproject-stimulus-application.ts b/frontend/src/stimulus/openproject-stimulus-application.tsnew file mode 100644index 00000000000..7d1eb4819d8--- /dev/null+++ b/frontend/src/stimulus/openproject-stimulus-application.ts@@ -0,0 +1,31 @@+import { ControllerConstructor } from '@hotwired/stimulus/dist/types/core/controller';+import { Application } from '@hotwired/stimulus';++export class OpenProjectStimulusApplication extends Application {+ static controllers = new Map<string, ControllerConstructor>();++ /**+ * Register a controller to be used in the application,+ * allowing it to be registered before the Stimulus application is being initialized.+ *+ * This is useful for plugins that execute code before we call setup.ts+ *+ * @param name the name/identifier of the controller+ * @param controller the controller class+ */+ static preregister(name:string, controller:ControllerConstructor) {+ this.controllers.set(name, controller);+ }++ async start():Promise<void> {+ this.preregisteredControllers.forEach((controller, name) => {+ this.register(name, controller);+ });++ await super.start();+ }++ get preregisteredControllers() {+ return OpenProjectStimulusApplication.controllers;+ }+}diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.tsindex bf00fe387a8..28c23d9c714 100644--- a/frontend/src/stimulus/setup.ts+++ b/frontend/src/stimulus/setup.ts@@ -1,4 +1,3 @@-import { Application } from '@hotwired/stimulus';import { environment } from '../environments/environment';import { OpApplicationController } from './controllers/op-application.controller';import MainMenuController from './controllers/dynamic/menus/main.controller';@@ -22,6 +21,8 @@ import ScrollIntoViewController from './controllers/scroll-into-view.controller'import CkeditorFocusController from './controllers/ckeditor-focus.controller';import AutoSubmit from '@stimulus-components/auto-submit';+import { OpenProjectStimulusApplication } from 'core-stimulus/openproject-stimulus-application';+import { Application } from '@hotwired/stimulus';declare global {interface Window {@@ -29,35 +30,32 @@ declare global {}}-const instance = Application.start();+OpenProjectStimulusApplication.preregister('application', OpApplicationController);+OpenProjectStimulusApplication.preregister('async-dialog', AsyncDialogController);+OpenProjectStimulusApplication.preregister('disable-when-checked', OpDisableWhenCheckedController);+OpenProjectStimulusApplication.preregister('flash', FlashController);+OpenProjectStimulusApplication.preregister('menus--main', MainMenuController);+OpenProjectStimulusApplication.preregister('password-confirmation-dialog', PasswordConfirmationDialogController);+OpenProjectStimulusApplication.preregister('poll-for-changes', PollForChangesController);+OpenProjectStimulusApplication.preregister('print', PrintController);+OpenProjectStimulusApplication.preregister('refresh-on-form-changes', RefreshOnFormChangesController);+OpenProjectStimulusApplication.preregister('form-preview', FormPreviewController);+OpenProjectStimulusApplication.preregister('hover-card-trigger', HoverCardTriggerController);+OpenProjectStimulusApplication.preregister('show-when-checked', OpShowWhenCheckedController);+OpenProjectStimulusApplication.preregister('show-when-value-selected', OpShowWhenValueSelectedController);+OpenProjectStimulusApplication.preregister('table-highlighting', TableHighlightingController);+OpenProjectStimulusApplication.preregister('projects-zen-mode', OpProjectsZenModeController);+OpenProjectStimulusApplication.preregister('work-packages--date-picker--preview', PreviewController);+OpenProjectStimulusApplication.preregister('keep-scroll-position', KeepScrollPositionController);+OpenProjectStimulusApplication.preregister('pattern-input', PatternInputController);+OpenProjectStimulusApplication.preregister('scroll-into-view', ScrollIntoViewController);+OpenProjectStimulusApplication.preregister('ckeditor-focus', CkeditorFocusController);+OpenProjectStimulusApplication.preregister('auto-submit', AutoSubmit);++const instance = OpenProjectStimulusApplication.start();window.Stimulus = instance;instance.debug = !environment.production;instance.handleError = (error, message, detail) => {console.warn(error, message, detail);};--instance.register('async-dialog', AsyncDialogController);-instance.register('disable-when-checked', OpDisableWhenCheckedController);-instance.register('flash', FlashController);-instance.register('menus--main', MainMenuController);-instance.register('password-confirmation-dialog', PasswordConfirmationDialogController);-instance.register('poll-for-changes', PollForChangesController);-instance.register('print', PrintController);-instance.register('refresh-on-form-changes', RefreshOnFormChangesController);-instance.register('form-preview', FormPreviewController);-instance.register('hover-card-trigger', HoverCardTriggerController);-instance.register('show-when-checked', OpShowWhenCheckedController);-instance.register('show-when-value-selected', OpShowWhenValueSelectedController);-instance.register('table-highlighting', TableHighlightingController);-instance.register('projects-zen-mode', OpProjectsZenModeController);-instance.register('work-packages--date-picker--preview', PreviewController);-instance.register('keep-scroll-position', KeepScrollPositionController);-instance.register('pattern-input', PatternInputController);-instance.register('scroll-into-view', ScrollIntoViewController);-instance.register('ckeditor-focus', CkeditorFocusController);-instance.register('auto-submit', AutoSubmit);--// Application controller must be registered last, as it tries to automatically load other controllers-// not yet registered.-instance.register('application', OpApplicationController);