Remove window unload event listener from edit order cycle cancel link

This commit is contained in:
wandji20
2024-09-01 22:10:04 +01:00
committed by Sigmund Petersen
parent a11873559b
commit 37ab832b86
3 changed files with 14 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ angular.module("admin.utils")
$window.onbeforeunload = @onBeforeUnloadHandler
$rootScope.$on "$locationChangeStart", @locationChangeStartHandler
$window.onBeforeUnloadHandler = @onBeforeUnloadHandler
# Action for regular browser navigation.
onBeforeUnloadHandler: ($event) =>

View File

@@ -11,8 +11,7 @@
= t('.proceed')
%button.button.secondary{ "ng-click": "submit($event, '#{main_app.admin_order_cycles_path}')", type: "button", style: "display: none;", data: { action: 'click->modal#close', 'trigger-action': 'saveAndBack' } }
= t('.proceed')
= link_to t('.cancel'), admin_order_cycles_path, id: 'cancel', class: 'button primary'
= link_to t('.cancel'), admin_order_cycles_path, id: 'cancel', class: 'button primary', data: { 'order-cycle-form-target': 'cancel' }
- if action == 'bulk_update'
%button.button.secondary{ "ng-click": "saveAll($event)", type: "button", style: "display: none;", data: { action: 'click->modal#close', trigger_action: 'bulk_save' } }
= t('.proceed')

View File

@@ -1,13 +1,17 @@
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ['statusMessage']
static targets = ['statusMessage', 'cancel']
connect() {
this.observer = new MutationObserver(this.updateCallback);
this.observer.observe(
this.statusMessageTarget,
{ attributes: true, attributeOldValue: true, attributeFilter: ['data-type'] }
);
if (this.hasCancelTarget) {
this.cancelTarget.addEventListener('click', this.removeUnloadEvent)
}
}
// Callback to trigger warning modal
@@ -38,7 +42,14 @@ export default class extends Controller {
}
}
removeUnloadEvent() {
window.removeEventListener('beforeunload', window.onBeforeUnloadHandler)
}
disconnect() {
this.observer.disconnect();
if (this.hasCancelTarget) {
this.cancelTarget.removeEventListener('click', this.removeUnloadEvent)
}
}
}