LP1888723 Detect unapplied changes; improve tab change
authorBill Erickson <berickxx@gmail.com>
Wed, 16 Dec 2020 16:03:54 +0000 (08:03 -0800)
committerGalen Charlton <gmc@equinoxOLI.org>
Sun, 15 Aug 2021 23:55:53 +0000 (19:55 -0400)
1. Detect when a change was initiated for one of the copy attributes,
but not applied during change detection for warning alerts.

2. Improve handling of inter-tab navigation for change alerts.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Ruth Frasur <rfrasur@library.in.gov>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/eg2/src/app/staff/cat/volcopy/copy-attrs.component.ts
Open-ILS/src/eg2/src/app/staff/cat/volcopy/volcopy.component.html
Open-ILS/src/eg2/src/app/staff/cat/volcopy/volcopy.component.ts

index ed49aae..1967a77 100644 (file)
@@ -645,6 +645,12 @@ export class CopyAttrsComponent implements OnInit, AfterViewInit {
             this.canSaveChange.emit(canSave);
         });
     }
+
+    // True if one of our batch editors has been put into edit
+    // mode and left there without an Apply, Cancel, or Clear
+    hasActiveInput(): boolean {
+        return this.batchAttrs.filter(attr => attr.editing).length > 0;
+    }
 }
 
 
index fc6fcd1..2f4ac1f 100644 (file)
@@ -28,7 +28,7 @@
         </div>
         <ng-container *ngIf="volcopy.defaults.values.unified_display">
           <div class="mt-2">
-            <eg-copy-attrs [context]="context" 
+            <eg-copy-attrs [context]="context" #copyAttrs
               (canSaveChange)="attrsCanSaveChange($event)"></eg-copy-attrs>
           </div>
         </ng-container>
@@ -39,7 +39,7 @@
         <a ngbNavLink i18n>Item Attributes</a>
         <ng-template ngbNavContent>
           <div class="mt-2">
-            <eg-copy-attrs [context]="context"
+            <eg-copy-attrs [context]="context" #copyAttrs
               (canSaveChange)="attrsCanSaveChange($event)"></eg-copy-attrs>
           </div>
         </ng-template>
index 0d8b797..6d09b11 100644 (file)
@@ -15,6 +15,7 @@ import {AnonCacheService} from '@eg/share/util/anon-cache.service';
 import {VolCopyService} from './volcopy.service';
 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
 import {BroadcastService} from '@eg/share/util/broadcast.service';
+import {CopyAttrsComponent} from './copy-attrs.component';
 
 const COPY_FLESH = {
     flesh: 1,
@@ -62,10 +63,13 @@ export class VolCopyComponent implements OnInit {
     volsCanSave = true;
     attrsCanSave = true;
     changesPending = false;
+    routingAllowed = false;
 
     @ViewChild('pendingChangesDialog', {static: false})
         pendingChangesDialog: ConfirmDialogComponent;
 
+    @ViewChild('copyAttrs', {static: false}) copyAttrs: CopyAttrsComponent;
+
     constructor(
         private router: Router,
         private route: ActivatedRoute,
@@ -174,8 +178,19 @@ export class VolCopyComponent implements OnInit {
     // Changing the route ultimately results in changing the tab.
     beforeTabChange(evt: NgbNavChangeEvent) {
         evt.preventDefault();
-        this.tab = evt.nextId;
-        this.routeToTab();
+
+        let promise = this.canDeactivate();
+        if (!(promise instanceof Promise)) {
+            promise = Promise.resolve(promise);
+        }
+
+        promise.then(ok => {
+            if (ok) {
+                this.routingAllowed = true;
+                this.tab = evt.nextId;
+                this.routeToTab();
+            }
+        });
     }
 
     routeToTab() {
@@ -515,13 +530,24 @@ export class VolCopyComponent implements OnInit {
     }
 
     @HostListener('window:beforeunload', ['$event'])
-    canDeactivate($event?: Event): Promise<boolean> {
+    canDeactivate($event?: Event): boolean | Promise<boolean> {
+
+        if (this.routingAllowed) {
+            // We call canDeactive manually when routing between volcopy
+            // tabs.  If routingAllowed, it means we'ave already confirmed
+            // the tag change is OK.
+            this.routingAllowed = false;
+            return true;
+        }
+
+        const editing = this.copyAttrs ? this.copyAttrs.hasActiveInput() : false;
 
-        if (!this.changesPending) { return Promise.resolve(true); }
+        if (!editing && !this.changesPending) { return true; }
 
         // Each warning dialog clears the current "changes are pending"
         // flag so the user is not presented with the dialog again
-        // unless new changes are made.
+        // unless new changes are made.  The 'editing' value will reset
+        // since the attrs component is getting destroyed.
         this.changesPending = false;
 
         if ($event) { // window.onbeforeunload