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
Handle beforeunload for turbo in a separate controller
Oliver Günther00af042ddd73
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex eaf73613131..4dc295948d4 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -305,7 +305,7 @@ module ApplicationHelperdef body_data_attributes(local_assigns){- controller: "application hover-card-trigger",+ controller: "application hover-card-trigger beforeunload",relative_url_root: root_path,overflowing_identifier: ".__overflowing_body",rendered_at: Time.zone.now.iso8601,diff --git a/frontend/src/app/core/setup/globals/global-listeners.ts b/frontend/src/app/core/setup/globals/global-listeners.tsindex 044f2fa4037..27eddd36bb8 100644--- a/frontend/src/app/core/setup/globals/global-listeners.ts+++ b/frontend/src/app/core/setup/globals/global-listeners.ts@@ -96,24 +96,6 @@ export function initializeGlobalListeners():void {}}}-- // Global submitting hook,- // necessary to avoid a data loss warning on beforeunload- jQuery(document).on('submit', 'form', () => {- window.OpenProject.pageIsSubmitted = true;- });-- // Global beforeunload hook- jQuery(window).on('beforeunload', (e:JQuery.TriggeredEvent) => {- const event = e.originalEvent as BeforeUnloadEvent;- if (window.OpenProject.pageWasEdited && !window.OpenProject.pageIsSubmitted) {- // Cancel the event- event.preventDefault();- // Chrome requires returnValue to be set- event.returnValue = I18n.t('js.work_packages.confirm_edit_cancel');- }- });-// Disable global drag & drop handling, which results in the browser loading the image and losing the pagejQuery(document.documentElement).on('dragover drop', (evt:Event) => {diff --git a/frontend/src/app/core/setup/globals/openproject.ts b/frontend/src/app/core/setup/globals/openproject.tsindex 02e089ffbd5..579b44c7452 100644--- a/frontend/src/app/core/setup/globals/openproject.ts+++ b/frontend/src/app/core/setup/globals/openproject.ts@@ -39,13 +39,6 @@ export class OpenProject {public helpers = new GlobalHelpers();- /** Globally setable variable whether the page was edited */- public pageWasEdited = false;-- /** Globally setable variable whether the page form is submitted.- * Necessary to avoid a data loss warning on beforeunload */- public pageIsSubmitted = false;-/** Globally setable variable whether any of the EditFormComponent* contain changes.* Necessary to show a data loss warning on beforeunload when clickingdiff --git a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.tsindex 84cfb000efc..b5024e73609 100644--- a/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts+++ b/frontend/src/app/shared/components/editor/components/ckeditor-augmented-textarea/ckeditor-augmented-textarea.component.ts@@ -59,6 +59,7 @@ import { AttachmentCollectionResource } from 'core-app/features/hal/resources/atimport { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs';import { navigator } from '@hotwired/turbo';import { uniqueId } from 'lodash';+import { setPageWasEdited, setPageWasSubmitted } from '../../../../../../stimulus/controllers/beforeunload.controller';@Component({templateUrl: './ckeditor-augmented-textarea.html',@@ -191,7 +192,7 @@ export class CkeditorAugmentedTextareaComponent extends UntilDestroyedMixin implCkeditorAugmentedTextareaComponent.inFlight.set(this.formElement, true);this.syncToTextarea();- window.OpenProject.pageIsSubmitted = true;+ setPageWasSubmitted();setTimeout(() => {if (evt?.submitter) {@@ -232,7 +233,7 @@ export class CkeditorAugmentedTextareaComponent extends UntilDestroyedMixin impl}public syncToTextarea() {- window.OpenProject.pageWasEdited = true;+ setPageWasEdited();try {this.wrappedTextArea.value = this.ckEditorInstance.getTransformedContent(true);diff --git a/frontend/src/stimulus/controllers/beforeunload.controller.ts b/frontend/src/stimulus/controllers/beforeunload.controller.tsnew file mode 100644index 00000000000..77fe74c8e1d--- /dev/null+++ b/frontend/src/stimulus/controllers/beforeunload.controller.ts@@ -0,0 +1,75 @@+import { ApplicationController } from 'stimulus-use';+import { TurboBeforeVisitEvent } from '@hotwired/turbo';++export function setPageWasEdited() {+ const application = window.Stimulus;+ const controller = application.getControllerForElementAndIdentifier(document.body, 'beforeunload') as BeforeunloadController;+ if (controller) {+ controller.pageWasEdited = true;+ }+}++export function setPageWasSubmitted() {+ const application = window.Stimulus;+ const controller = application.getControllerForElementAndIdentifier(document.body, 'beforeunload') as BeforeunloadController;+ if (controller) {+ controller.pageWasEdited = true;+ }+}++export class BeforeunloadController extends ApplicationController {+ /** Globally setable variable whether the page was edited */+ public pageWasEdited = false;++ /** Globally setable variable whether the page form is submitted.+ * Necessary to avoid a data loss warning on beforeunload */+ public pageIsSubmitted = false;++ private boundBeforeUnloadHandler = this.beforeunloadHandler.bind(this);++ private submitHandler = () => {+ this.pageIsSubmitted = true;+ };++ private turboHandler = () => {+ this.pageWasEdited = false;+ this.pageIsSubmitted = false;+ };++ connect() {+ super.connect();++ window.addEventListener('beforeunload', this.boundBeforeUnloadHandler);+ document.addEventListener('turbo:before-visit', this.boundBeforeUnloadHandler);+ document.addEventListener('turbo:submit-end', this.turboHandler);+ document.addEventListener('submit', this.submitHandler);+ }++ disconnect() {+ window.removeEventListener('beforeunload', this.boundBeforeUnloadHandler);+ document.removeEventListener('turbo:before-visit', this.boundBeforeUnloadHandler);+ document.removeEventListener('turbo:submit-end', this.turboHandler);+ document.removeEventListener('submit', this.submitHandler);+ }++ private beforeunloadHandler(evt:BeforeUnloadEvent|TurboBeforeVisitEvent) {+ const { pageWasEdited, pageIsSubmitted } = this;++ if (!pageWasEdited || pageIsSubmitted) {+ return;+ }++ // eslint-disable-next-line no-alert+ if (window.confirm(I18n.t('js.work_packages.confirm_edit_cancel'))) {+ return;+ }++ // Cancel the event+ evt.preventDefault();++ // Chrome requires returnValue to be set+ if (evt.type === 'beforeunload') {+ evt.returnValue = '';+ }+ }+}diff --git a/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts b/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.tsindex 71fdd66f1ac..db838fb9e87 100644--- a/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/meetings/check-unsaved.controller.ts@@ -30,9 +30,11 @@import { ApplicationController, useMeta } from 'stimulus-use';import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';+import { BeforeunloadController } from '../../beforeunload.controller';export default class extends ApplicationController {private turboRequests:TurboRequestsService;+ private beforeUnloadController:BeforeunloadController;private boundBeforeUnloadHandler = this.beforeUnloadHandler.bind(this);static values = { unsavedChangesConfirmationMessage: String };@@ -50,6 +52,7 @@ export default class extends ApplicationController {const context = await window.OpenProject.getPluginContext();this.turboRequests = context.services.turboRequests;+ this.beforeUnloadController = this.application.getControllerForElementAndIdentifier(document.body, 'beforeunload') as BeforeunloadController;}disconnect():void {@@ -78,7 +81,7 @@ export default class extends ApplicationController {const textInputs = Array.from(document.querySelectorAll('input[type="text"], input[type="number"]'));const allTextSaved = textInputs.every((input) => (input as HTMLInputElement).value.trim().length === 0);- return !allTextSaved || window.OpenProject.pageWasEdited;+ return !allTextSaved || this.beforeUnloadController.pageWasEdited;}private beforeUnloadHandler(event:BeforeUnloadEvent):void {diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.tsindex 28c23d9c714..46e1bb3e7b4 100644--- a/frontend/src/stimulus/setup.ts+++ b/frontend/src/stimulus/setup.ts@@ -23,6 +23,7 @@ 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';+import { BeforeunloadController } from './controllers/beforeunload.controller';declare global {interface Window {@@ -51,6 +52,8 @@ OpenProjectStimulusApplication.preregister('pattern-input', PatternInputControllOpenProjectStimulusApplication.preregister('scroll-into-view', ScrollIntoViewController);OpenProjectStimulusApplication.preregister('ckeditor-focus', CkeditorFocusController);OpenProjectStimulusApplication.preregister('auto-submit', AutoSubmit);+OpenProjectStimulusApplication.preregister('beforeunload', BeforeunloadController);+const instance = OpenProjectStimulusApplication.start();window.Stimulus = instance;