From 2309b603a0413a014b92a88e65c2a508a84269eb Mon Sep 17 00:00:00 2001
From: Bill Erickson <berickxx@gmail.com>
Date: Tue, 22 Oct 2019 15:54:13 -0400
Subject: [PATCH] LP1849372 Close all open dialogs on Angular route change

On all angular route changes, force close any open dialogs, since it
makes little sense for them to persist across interfaces.

To test:

[1] Navigate to Server Administration
[2] Navigate to Age Hold Protection Rule Configuration
[3] Double-click a grid row to open an edit dialog
[4] Click browser back button to return to the Server Admin page
[5] Confirm edit dialog closes once the navigation is complete.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
---
 Open-ILS/src/eg2/src/app/app.component.ts          | 12 ++++++++++++
 .../eg2/src/app/share/dialog/dialog.component.ts   | 22 ++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/Open-ILS/src/eg2/src/app/app.component.ts b/Open-ILS/src/eg2/src/app/app.component.ts
index 3f95092c5a..c70938ff99 100644
--- a/Open-ILS/src/eg2/src/app/app.component.ts
+++ b/Open-ILS/src/eg2/src/app/app.component.ts
@@ -1,4 +1,6 @@
 import {Component} from '@angular/core';
+import {Router, NavigationEnd} from '@angular/router';
+import {DialogComponent} from '@eg/share/dialog/dialog.component';
 
 @Component({
   selector: 'eg-root',
@@ -6,6 +8,16 @@ import {Component} from '@angular/core';
 })
 
 export class BaseComponent {
+
+    constructor(private router: Router) {
+        this.router.events.subscribe(routeEvent => {
+            if (routeEvent instanceof NavigationEnd) {
+                // Prevent dialogs from persisting across navigation.
+                DialogComponent.closeAll();
+            }
+        });
+    }
+
 }
 
 
diff --git a/Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts b/Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
index 1532a1fc78..cafbbcfe60 100644
--- a/Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
+++ b/Open-ILS/src/eg2/src/app/share/dialog/dialog.component.ts
@@ -33,6 +33,13 @@ import {NgbModal, NgbModalRef, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap
 })
 export class DialogComponent implements OnInit {
 
+    // Track instances so we can refer to them later in closeAll()
+    // NOTE this could also be done by importing router and subscribing
+    // to route events here, but that would require all subclassed
+    // components to import and pass the router via the constructor.
+    static counter = 0;
+    static instances: {[ident: number]: any} = {};
+
     // Assume all dialogs support a title attribute.
     @Input() public dialogTitle: string;
 
@@ -40,6 +47,8 @@ export class DialogComponent implements OnInit {
     @ViewChild('dialogContent', {static: false})
     private dialogContent: TemplateRef<any>;
 
+    identifier: number = DialogComponent.counter++;
+
     // Emitted after open() is called on the ngbModal.
     // Note when overriding open(), this will not fire unless also
     // called in the overridding method.
@@ -53,6 +62,16 @@ export class DialogComponent implements OnInit {
 
     constructor(private modalService: NgbModal) {}
 
+    // Close all active dialogs
+    static closeAll() {
+        Object.keys(DialogComponent.instances).forEach(id => {
+            if (DialogComponent.instances[id]) {
+                DialogComponent.instances[id].close();
+                delete DialogComponent.instances[id];
+            }
+        });
+    }
+
     ngOnInit() {
         this.onOpen$ = new EventEmitter<any>();
     }
@@ -65,6 +84,7 @@ export class DialogComponent implements OnInit {
         }
 
         this.modalRef = this.modalService.open(this.dialogContent, options);
+        DialogComponent.instances[this.identifier] = this;
 
         if (this.onOpen$) {
             // Let the digest cycle complete
@@ -127,7 +147,9 @@ export class DialogComponent implements OnInit {
             this.observer = null;
         }
         this.modalRef = null;
+        delete DialogComponent.instances[this.identifier];
     }
+
 }
 
 
-- 
2.11.0