-import {Component, OnInit} from '@angular/core';
+import {Component, HostListener, OnDestroy, OnInit} from '@angular/core';
import {IdlObject} from '@eg/core/idl.service';
import {StaffCatalogService} from './catalog.service';
import {BasketService} from '@eg/share/catalog/basket.service';
+import {Subject, takeUntil} from 'rxjs';
@Component({
templateUrl: 'catalog.component.html'
})
-export class CatalogComponent implements OnInit {
+export class CatalogComponent implements OnInit, OnDestroy {
+
+ private onDestroy = new Subject<void>();
constructor(
private basket: BasketService,
// reset and updated as needed to apply new search parameters.
this.staffCat.createContext();
+ // listen for hold patron target changes from other tabs
+ // until there's a route change
+ this.staffCat.onChangeHoldPatron().pipe(
+ takeUntil(this.onDestroy)
+ ).subscribe();
+
// Subscribe to these emissions so that we can force
// change detection in this component even though the
// hold-for value was modified by a child component.
clearHoldPatron() {
this.staffCat.clearHoldPatron();
}
+
+ @HostListener('window:beforeunload')
+ onBeforeUnload(): void {
+ this.staffCat.onBeforeUnload();
+ }
+
+ ngOnDestroy(): void {
+ this.clearHoldPatron();
+ this.onDestroy.next();
+ }
}
-import {Injectable, EventEmitter} from '@angular/core';
+import {Injectable, EventEmitter, NgZone} from '@angular/core';
import {Router, ActivatedRoute} from '@angular/router';
import {IdlObject} from '@eg/core/idl.service';
import {OrgService} from '@eg/core/org.service';
import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
import {PatronService} from '@eg/staff/share/patron/patron.service';
import {StoreService} from '@eg/core/store.service';
+import {BroadcastService} from '@eg/share/util/broadcast.service';
+import {Observable, tap} from 'rxjs';
const HOLD_FOR_PATRON_KEY = 'eg.circ.patron_hold_target';
private org: OrgService,
private cat: CatalogService,
private patron: PatronService,
- private catUrl: CatalogUrlService
+ private catUrl: CatalogUrlService,
+ private broadcaster: BroadcastService,
+ private zone: NgZone
) { }
createContext(): void {
this.applySearchDefaults();
}
- clearHoldPatron() {
+ clearHoldPatron(broadcast: boolean = true) {
+ const removedTarget = this.holdForBarcode;
+
this.holdForUser = null;
this.holdForBarcode = null;
this.store.removeLoginSessionItem(HOLD_FOR_PATRON_KEY);
this.holdForChange.emit();
+ if (!broadcast) return;
+
+ // clear hold patron on other tabs
+ this.broadcaster.broadcast(
+ HOLD_FOR_PATRON_KEY, { removedTarget }
+ );
+ }
+
+ onBeforeUnload(): void {
+ const closedTarget = this.holdForBarcode;
+ if (closedTarget) {
+ this.clearHoldPatron(false);
+ this.broadcaster.broadcast(HOLD_FOR_PATRON_KEY,
+ { closedTarget }
+ );
+ }
+ }
+
+ onChangeHoldPatron(): Observable<any> {
+ return this.broadcaster.listen(HOLD_FOR_PATRON_KEY).pipe(
+ tap(({ removedTarget, closedTarget }) => {
+ if (removedTarget && this.holdForBarcode) {
+ // broadcaster doesn't trigger change detection,
+ // so trigger it manually
+ this.zone.run(() => this.clearHoldPatron(false));
+
+ } else if (closedTarget) {
+ // if hold target was unset by another tab,
+ // restore the hold target
+ if (closedTarget === this.holdForBarcode) {
+ this.store.setLoginSessionItem(
+ HOLD_FOR_PATRON_KEY, closedTarget
+ );
+ }
+ }
+ })
+ );
}
cloneContext(context: CatalogSearchContext): CatalogSearchContext {
// of the startup routines when no valid token exists during startup.
$rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
+ // in case we just left an Angular context, clear the hold target
+ // and notify any open Angular catalog tabs to do the same
+ function clearHoldTarget() {
+ var patronHoldTarget = $cookies.get(
+ 'eg.circ.patron_hold_target'
+ );
+ if (patronHoldTarget) {
+ $cookies.remove(
+ 'eg.circ.patron_hold_target'
+ )
+ if (typeof BroadcastChannel !== 'undefined') {
+ var broadcaster = new BroadcastChannel(
+ 'eg.circ.patron_hold_target'
+ );
+ broadcaster.postMessage(
+ { removedTarget: patronHoldTarget }
+ );
+ broadcaster.close();
+ }
+ }
+ }
+ clearHoldTarget();
+
service.go = function () {
if (service.promise) {
// startup already started, return our existing promise