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} from 'rxjs';
13 import {tap} from 'rxjs/operators';
15 const HOLD_FOR_PATRON_KEY = 'eg.circ.patron_hold_target';
18 * Shared bits needed by the staff version of the catalog.
22 export class StaffCatalogService {
24 searchContext: CatalogSearchContext;
26 defaultSearchOrg: IdlObject;
27 defaultSearchLimit: number;
28 // Track the current template through route changes.
29 selectedTemplate: string;
31 // Display the Exclude Electronic checkbox
32 showExcludeElectronic = false;
34 // Advanced search filters to display
35 searchFilters: string[];
37 // TODO: does unapi support pref-lib for result-page copy counts?
43 // Patron barcode we hope to place a hold for.
44 holdForBarcode: string;
45 // User object for above barcode.
46 holdForUser: IdlObject;
48 // Emit that the value has changed so components can detect
49 // the change even when the component is not itself digesting
51 holdForChange: EventEmitter<void> = new EventEmitter<void>();
53 // Cache the currently selected detail record (i.g. catalog/record/123)
54 // summary so the record detail component can avoid duplicate fetches
55 // during record tab navigation.
56 currentDetailRecordSummary: any;
58 // Add digital bookplate to search options.
59 enableBookplates = false;
61 // Cache of browse results so the browse pager is not forced to
62 // re-run the browse search on each navigation.
63 browsePagerData: any[];
65 // whether to redirect to record page upon a single search
67 jumpOnSingleHit = false;
70 private router: Router,
71 private route: ActivatedRoute,
72 private store: StoreService,
73 private org: OrgService,
74 private cat: CatalogService,
75 private patron: PatronService,
76 private catUrl: CatalogUrlService,
77 private broadcaster: BroadcastService,
81 createContext(): void {
82 // Initialize the search context from the load-time URL params.
83 // Do this here so the search form and other context data are
84 // applied on every page, not just the search results page. The
85 // search results pages will handle running the actual search.
87 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
89 this.holdForBarcode = this.store.getLoginSessionItem(HOLD_FOR_PATRON_KEY);
91 if (this.holdForBarcode) {
92 this.patron.getByBarcode(this.holdForBarcode)
94 this.holdForUser = user;
95 this.holdForChange.emit();
98 // In case the session item was cleared from another component.
99 this.clearHoldPatron();
102 this.searchContext.org = this.org; // service, not searchOrg
103 this.searchContext.isStaff = true;
104 this.applySearchDefaults();
107 clearHoldPatron(broadcast: boolean = true) {
108 const removedTarget = this.holdForBarcode;
110 this.holdForUser = null;
111 this.holdForBarcode = null;
112 this.store.removeLoginSessionItem(HOLD_FOR_PATRON_KEY);
113 this.holdForChange.emit();
114 if (!broadcast) return;
116 // clear hold patron on other tabs
117 this.broadcaster.broadcast(
118 HOLD_FOR_PATRON_KEY, { removedTarget }
122 onBeforeUnload(): void {
123 const closedTarget = this.holdForBarcode;
125 this.clearHoldPatron(false);
126 this.broadcaster.broadcast(HOLD_FOR_PATRON_KEY,
132 onChangeHoldPatron(): Observable<any> {
133 return this.broadcaster.listen(HOLD_FOR_PATRON_KEY).pipe(
134 tap(({ removedTarget, closedTarget }) => {
135 if (removedTarget && this.holdForBarcode) {
136 // broadcaster doesn't trigger change detection,
137 // so trigger it manually
138 this.zone.run(() => this.clearHoldPatron(false));
140 } else if (closedTarget) {
141 // if hold target was unset by another tab,
142 // restore the hold target
143 if (closedTarget === this.holdForBarcode) {
144 this.store.setLoginSessionItem(
145 HOLD_FOR_PATRON_KEY, closedTarget
153 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
154 const params: any = this.catUrl.toUrlParams(context);
155 const ctx = this.catUrl.fromUrlHash(params);
156 ctx.isStaff = true; // not carried in the URL
160 applySearchDefaults(): void {
161 if (!this.searchContext.searchOrg) {
162 this.searchContext.searchOrg =
163 this.defaultSearchOrg || this.org.root();
166 if (!this.searchContext.pager.limit) {
167 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
172 * Redirect to the search results page while propagating the current
173 * search paramters into the URL. Let the search results component
174 * execute the actual search.
177 if (!this.searchContext.isSearchable()) { return; }
179 // Clear cached detail summary for new searches.
180 this.currentDetailRecordSummary = null;
182 const params = this.catUrl.toUrlParams(this.searchContext);
184 // Force a new search every time this method is called, even if
185 // it's the same as the active search. Since router navigation
186 // exits early when the route + params is identical, add a
187 // random token to the route params to force a full navigation.
188 // This also resolves a problem where only removing secondary+
189 // versions of a query param fail to cause a route navigation.
190 // (E.g. going from two query= params to one). Investigation
192 params.ridx = '' + this.routeIndex++;
194 this.router.navigate(
195 ['/staff/catalog/search'], {queryParams: params});
199 * Redirect to the browse results page while propagating the current
200 * browse paramters into the URL. Let the browse results component
201 * execute the actual browse.
204 if (!this.searchContext.browseSearch.isSearchable()) { return; }
205 const params = this.catUrl.toUrlParams(this.searchContext);
207 // Force a new browse every time this method is called, even if
208 // it's the same as the active browse. Since router navigation
209 // exits early when the route + params is identical, add a
210 // random token to the route params to force a full navigation.
211 // This also resolves a problem where only removing secondary+
212 // versions of a query param fail to cause a route navigation.
213 // (E.g. going from two query= params to one).
214 params.ridx = '' + this.routeIndex++;
216 this.router.navigate(
217 ['/staff/catalog/browse'], {queryParams: params});
220 // Call number browse.
221 // Redirect to cn browse page and let its component perform the search
223 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
224 const params = this.catUrl.toUrlParams(this.searchContext);
225 params.ridx = '' + this.routeIndex++; // see comments above
226 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
229 // Params to genreate a new author search based on a reset
230 // clone of the current page params.
231 getAuthorSearchParams(summary: BibRecordSummary): any {
232 const tmpContext = this.cloneContext(this.searchContext);
234 tmpContext.termSearch.fieldClass = ['author'];
235 tmpContext.termSearch.query = [summary.display.author];
236 return this.catUrl.toUrlParams(tmpContext);