1 import {Injectable, EventEmitter, NgZone} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {CatalogService} from '@eg/share/catalog/catalog.service';
6 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
7 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
8 import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {PatronService} from '@eg/staff/share/patron/patron.service';
10 import {StoreService} from '@eg/core/store.service';
11 import {BroadcastService} from '@eg/share/util/broadcast.service';
12 import {Observable, tap} from 'rxjs';
14 const HOLD_FOR_PATRON_KEY = 'eg.circ.patron_hold_target';
17 * Shared bits needed by the staff version of the catalog.
21 export class StaffCatalogService {
23 searchContext: CatalogSearchContext;
25 defaultSearchOrg: IdlObject;
26 defaultSearchLimit: number;
27 // Track the current template through route changes.
28 selectedTemplate: string;
30 // Display the Exclude Electronic checkbox
31 showExcludeElectronic = false;
33 // Advanced search filters to display
34 searchFilters: string[];
36 // TODO: does unapi support pref-lib for result-page copy counts?
42 // Patron barcode we hope to place a hold for.
43 holdForBarcode: string;
44 // User object for above barcode.
45 holdForUser: IdlObject;
47 // Emit that the value has changed so components can detect
48 // the change even when the component is not itself digesting
50 holdForChange: EventEmitter<void> = new EventEmitter<void>();
52 // Cache the currently selected detail record (i.g. catalog/record/123)
53 // summary so the record detail component can avoid duplicate fetches
54 // during record tab navigation.
55 currentDetailRecordSummary: any;
57 // Add digital bookplate to search options.
58 enableBookplates = false;
60 // Cache of browse results so the browse pager is not forced to
61 // re-run the browse search on each navigation.
62 browsePagerData: any[];
64 // whether to redirect to record page upon a single search
66 jumpOnSingleHit = false;
69 private router: Router,
70 private route: ActivatedRoute,
71 private store: StoreService,
72 private org: OrgService,
73 private cat: CatalogService,
74 private patron: PatronService,
75 private catUrl: CatalogUrlService,
76 private broadcaster: BroadcastService,
80 createContext(): void {
81 // Initialize the search context from the load-time URL params.
82 // Do this here so the search form and other context data are
83 // applied on every page, not just the search results page. The
84 // search results pages will handle running the actual search.
86 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
88 this.holdForBarcode = this.store.getLoginSessionItem(HOLD_FOR_PATRON_KEY);
90 if (this.holdForBarcode) {
91 this.patron.getByBarcode(this.holdForBarcode)
93 this.holdForUser = user;
94 this.holdForChange.emit();
97 // In case the session item was cleared from another component.
98 this.clearHoldPatron();
101 this.searchContext.org = this.org; // service, not searchOrg
102 this.searchContext.isStaff = true;
103 this.applySearchDefaults();
106 clearHoldPatron(broadcast: boolean = true) {
107 const removedTarget = this.holdForBarcode;
109 this.holdForUser = null;
110 this.holdForBarcode = null;
111 this.store.removeLoginSessionItem(HOLD_FOR_PATRON_KEY);
112 this.holdForChange.emit();
113 if (!broadcast) return;
115 // clear hold patron on other tabs
116 this.broadcaster.broadcast(
117 HOLD_FOR_PATRON_KEY, { removedTarget }
121 onBeforeUnload(): void {
122 const closedTarget = this.holdForBarcode;
124 this.clearHoldPatron(false);
125 this.broadcaster.broadcast(HOLD_FOR_PATRON_KEY,
131 onChangeHoldPatron(): Observable<any> {
132 return this.broadcaster.listen(HOLD_FOR_PATRON_KEY).pipe(
133 tap(({ removedTarget, closedTarget }) => {
134 if (removedTarget && this.holdForBarcode) {
135 // broadcaster doesn't trigger change detection,
136 // so trigger it manually
137 this.zone.run(() => this.clearHoldPatron(false));
139 } else if (closedTarget) {
140 // if hold target was unset by another tab,
141 // restore the hold target
142 if (closedTarget === this.holdForBarcode) {
143 this.store.setLoginSessionItem(
144 HOLD_FOR_PATRON_KEY, closedTarget
152 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
153 const params: any = this.catUrl.toUrlParams(context);
154 const ctx = this.catUrl.fromUrlHash(params);
155 ctx.isStaff = true; // not carried in the URL
159 applySearchDefaults(): void {
160 if (!this.searchContext.searchOrg) {
161 this.searchContext.searchOrg =
162 this.defaultSearchOrg || this.org.root();
165 if (!this.searchContext.pager.limit) {
166 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
171 * Redirect to the search results page while propagating the current
172 * search paramters into the URL. Let the search results component
173 * execute the actual search.
176 if (!this.searchContext.isSearchable()) { return; }
178 // Clear cached detail summary for new searches.
179 this.currentDetailRecordSummary = null;
181 const params = this.catUrl.toUrlParams(this.searchContext);
183 // Force a new search every time this method is called, even if
184 // it's the same as the active search. Since router navigation
185 // exits early when the route + params is identical, add a
186 // random token to the route params to force a full navigation.
187 // This also resolves a problem where only removing secondary+
188 // versions of a query param fail to cause a route navigation.
189 // (E.g. going from two query= params to one). Investigation
191 params.ridx = '' + this.routeIndex++;
193 this.router.navigate(
194 ['/staff/catalog/search'], {queryParams: params});
198 * Redirect to the browse results page while propagating the current
199 * browse paramters into the URL. Let the browse results component
200 * execute the actual browse.
203 if (!this.searchContext.browseSearch.isSearchable()) { return; }
204 const params = this.catUrl.toUrlParams(this.searchContext);
206 // Force a new browse every time this method is called, even if
207 // it's the same as the active browse. Since router navigation
208 // exits early when the route + params is identical, add a
209 // random token to the route params to force a full navigation.
210 // This also resolves a problem where only removing secondary+
211 // versions of a query param fail to cause a route navigation.
212 // (E.g. going from two query= params to one).
213 params.ridx = '' + this.routeIndex++;
215 this.router.navigate(
216 ['/staff/catalog/browse'], {queryParams: params});
219 // Call number browse.
220 // Redirect to cn browse page and let its component perform the search
222 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
223 const params = this.catUrl.toUrlParams(this.searchContext);
224 params.ridx = '' + this.routeIndex++; // see comments above
225 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
228 // Params to genreate a new author search based on a reset
229 // clone of the current page params.
230 getAuthorSearchParams(summary: BibRecordSummary): any {
231 const tmpContext = this.cloneContext(this.searchContext);
233 tmpContext.termSearch.fieldClass = ['author'];
234 tmpContext.termSearch.query = [summary.display.author];
235 return this.catUrl.toUrlParams(tmpContext);