Skip to content

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

Merge pull request #19964 from opf/fix/turbo-load-bootstrap

Oliver Günther58fcb00d1a94
diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts
index 9422ceeb90b..3b0eb48fee6 100644
--- a/frontend/src/app/app.module.ts
+++ b/frontend/src/app/app.module.ts
@@ -26,7 +26,7 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { ApplicationRef, DoBootstrap, Injector, NgModule, inject, provideAppInitializer } from '@angular/core';
+import { ApplicationRef, DoBootstrap, inject, Injector, NgModule, provideAppInitializer } from '@angular/core';
import { A11yModule } from '@angular/cdk/a11y';
import { HTTP_INTERCEPTORS, HttpClient, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { ReactiveFormsModule } from '@angular/forms';
@@ -237,6 +237,16 @@ export function initializeServices(injector:Injector) {
};
}
+export function runBootstrap(appRef:ApplicationRef) {
+ // Try to bootstrap a dynamic root element
+ const root = document.querySelector(appBaseSelector);
+ if (root) {
+ appRef.bootstrap(ApplicationBaseComponent, root);
+ }
+
+ document.body.classList.add('__ng2-bootstrap-has-run');
+}
+
@NgModule({
declarations: [
OpContextMenuTrigger,
@@ -360,34 +370,10 @@ export function initializeServices(injector:Injector) {
export class OpenProjectModule implements DoBootstrap {
// noinspection JSUnusedGlobalSymbols
ngDoBootstrap(appRef:ApplicationRef) {
- this.runBootstrap(appRef);
-
- // Connect ui router to turbo drive
- document.addEventListener('turbo:load', () => {
- // Remove all previous references to components
- // This is mainly the bsae component
- appRef.components.slice().forEach((component) => {
- appRef.detachView(component.hostView);
- component.destroy();
- });
-
- // Run bootstrap again to initialize the new application
- this.runBootstrap(appRef);
- });
-
+ runBootstrap(appRef);
this.registerCustomElements(appRef.injector);
}
- private runBootstrap(appRef:ApplicationRef) {
- // Try to bootstrap a dynamic root element
- const root = document.querySelector(appBaseSelector);
- if (root) {
- appRef.bootstrap(ApplicationBaseComponent, root);
- }
-
- document.body.classList.add('__ng2-bootstrap-has-run');
- }
-
private registerCustomElements(injector:Injector) {
registerCustomElement('opce-macro-embedded-table', EmbeddedTablesMacroComponent, { injector });
registerCustomElement('opce-principal', OpPrincipalComponent, { injector });
diff --git a/frontend/src/app/features/plugins/plugin-context.ts b/frontend/src/app/features/plugins/plugin-context.ts
index bf8d4757b01..ca58096005a 100644
--- a/frontend/src/app/features/plugins/plugin-context.ts
+++ b/frontend/src/app/features/plugins/plugin-context.ts
@@ -1,4 +1,4 @@
-import { Injector, NgZone } from '@angular/core';
+import { ApplicationRef, Injector, NgZone } from '@angular/core';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import {
@@ -88,6 +88,9 @@ export class OpenProjectPluginContext {
// Angular zone reference
@InjectField() public readonly zone:NgZone;
+ // Angular application reference
+ @InjectField() public readonly appRef:ApplicationRef;
+
// Angular2 global injector reference
constructor(public readonly injector:Injector) {
this
diff --git a/frontend/src/turbo/setup.ts b/frontend/src/turbo/setup.ts
index d4259509579..a98aada7c96 100644
--- a/frontend/src/turbo/setup.ts
+++ b/frontend/src/turbo/setup.ts
@@ -10,6 +10,7 @@ import { applyTurboNavigationPatch } from './turbo-navigation-patch';
import { debugLog, whenDebugging } from 'core-app/shared/helpers/debug_output';
import { TURBO_EVENTS } from './constants';
import { StreamActions } from '@hotwired/turbo';
+import { addTurboAngularWrapper } from "core-turbo/turbo-angular-wrapper";
Turbo.session.drive = true;
Turbo.setProgressBarDelay(100);
@@ -33,6 +34,7 @@ registerDialogStreamAction();
registerFlashStreamAction();
registerLiveRegionStreamAction();
registerInputCaptionStreamAction();
+addTurboAngularWrapper();
StreamActions.reloadPage = function reloadPage() {
window.location.reload();
diff --git a/frontend/src/turbo/turbo-angular-wrapper.ts b/frontend/src/turbo/turbo-angular-wrapper.ts
new file mode 100644
index 00000000000..31e37a04255
--- /dev/null
+++ b/frontend/src/turbo/turbo-angular-wrapper.ts
@@ -0,0 +1,31 @@
+import { skip } from 'rxjs/operators';
+import { fromEvent } from 'rxjs';
+import { runBootstrap } from 'core-app/app.module';
+import { OpenProjectPluginContext } from 'core-app/features/plugins/plugin-context';
+
+export function addTurboAngularWrapper() {
+ // When turbo:load fires, the angular application needs to be rebootstrapped.
+ // However, we don't want this to happen on the initial page load
+ fromEvent(document, 'turbo:load')
+ .pipe(
+ skip(1), // Skip the first turbo:load event
+ )
+ .subscribe(() => {
+ void window
+ .OpenProject
+ .getPluginContext()
+ .then((pluginContext:OpenProjectPluginContext) => {
+ const appRef = pluginContext.appRef;
+
+ // Remove all previous references to components
+ // This is mainly the base component
+ appRef.components.slice().forEach((component) => {
+ appRef.detachView(component.hostView);
+ component.destroy();
+ });
+
+ // Run bootstrap again to initialize the new application
+ runBootstrap(appRef);
+ });
+ });
+}