1 import {Injectable, EventEmitter} 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';
12 const HOLD_FOR_PATRON_KEY = 'eg.circ.patron_hold_target';
15 * Shared bits needed by the staff version of the catalog.
19 export class StaffCatalogService {
21 searchContext: CatalogSearchContext;
23 defaultSearchOrg: IdlObject;
24 defaultSearchLimit: number;
25 // Track the current template through route changes.
26 selectedTemplate: string;
28 // Display the Exclude Electronic checkbox
29 showExcludeElectronic = false;
31 // Advanced search filters to display
32 searchFilters: string[];
34 // TODO: does unapi support pref-lib for result-page copy counts?
40 // Patron barcode we hope to place a hold for.
41 holdForBarcode: string;
42 // User object for above barcode.
43 holdForUser: IdlObject;
45 // Emit that the value has changed so components can detect
46 // the change even when the component is not itself digesting
48 holdForChange: EventEmitter<void> = new EventEmitter<void>();
50 // Cache the currently selected detail record (i.g. catalog/record/123)
51 // summary so the record detail component can avoid duplicate fetches
52 // during record tab navigation.
53 currentDetailRecordSummary: any;
55 // Add digital bookplate to search options.
56 enableBookplates = false;
58 // Cache of browse results so the browse pager is not forced to
59 // re-run the browse search on each navigation.
60 browsePagerData: any[];
62 // whether to redirect to record page upon a single search
64 jumpOnSingleHit = false;
67 private router: Router,
68 private route: ActivatedRoute,
69 private store: StoreService,
70 private org: OrgService,
71 private cat: CatalogService,
72 private patron: PatronService,
73 private catUrl: CatalogUrlService
76 createContext(): void {
77 // Initialize the search context from the load-time URL params.
78 // Do this here so the search form and other context data are
79 // applied on every page, not just the search results page. The
80 // search results pages will handle running the actual search.
82 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
84 this.holdForBarcode = this.store.getLoginSessionItem(HOLD_FOR_PATRON_KEY);
86 if (this.holdForBarcode) {
87 this.patron.getByBarcode(this.holdForBarcode)
89 this.holdForUser = user;
90 this.holdForChange.emit();
94 this.searchContext.org = this.org; // service, not searchOrg
95 this.searchContext.isStaff = true;
96 this.applySearchDefaults();
100 this.holdForUser = null;
101 this.holdForBarcode = null;
102 this.store.removeLoginSessionItem(HOLD_FOR_PATRON_KEY);
103 this.holdForChange.emit();
106 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
107 const params: any = this.catUrl.toUrlParams(context);
108 const ctx = this.catUrl.fromUrlHash(params);
109 ctx.isStaff = true; // not carried in the URL
113 applySearchDefaults(): void {
114 if (!this.searchContext.searchOrg) {
115 this.searchContext.searchOrg =
116 this.defaultSearchOrg || this.org.root();
119 if (!this.searchContext.pager.limit) {
120 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
125 * Redirect to the search results page while propagating the current
126 * search paramters into the URL. Let the search results component
127 * execute the actual search.
130 if (!this.searchContext.isSearchable()) { return; }
132 // Clear cached detail summary for new searches.
133 this.currentDetailRecordSummary = null;
135 const params = this.catUrl.toUrlParams(this.searchContext);
137 // Force a new search every time this method is called, even if
138 // it's the same as the active search. Since router navigation
139 // exits early when the route + params is identical, add a
140 // random token to the route params to force a full navigation.
141 // This also resolves a problem where only removing secondary+
142 // versions of a query param fail to cause a route navigation.
143 // (E.g. going from two query= params to one). Investigation
145 params.ridx = '' + this.routeIndex++;
147 this.router.navigate(
148 ['/staff/catalog/search'], {queryParams: params});
152 * Redirect to the browse results page while propagating the current
153 * browse paramters into the URL. Let the browse results component
154 * execute the actual browse.
157 if (!this.searchContext.browseSearch.isSearchable()) { return; }
158 const params = this.catUrl.toUrlParams(this.searchContext);
160 // Force a new browse every time this method is called, even if
161 // it's the same as the active browse. Since router navigation
162 // exits early when the route + params is identical, add a
163 // random token to the route params to force a full navigation.
164 // This also resolves a problem where only removing secondary+
165 // versions of a query param fail to cause a route navigation.
166 // (E.g. going from two query= params to one).
167 params.ridx = '' + this.routeIndex++;
169 this.router.navigate(
170 ['/staff/catalog/browse'], {queryParams: params});
173 // Call number browse.
174 // Redirect to cn browse page and let its component perform the search
176 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
177 const params = this.catUrl.toUrlParams(this.searchContext);
178 params.ridx = '' + this.routeIndex++; // see comments above
179 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
182 // Params to genreate a new author search based on a reset
183 // clone of the current page params.
184 getAuthorSearchParams(summary: BibRecordSummary): any {
185 const tmpContext = this.cloneContext(this.searchContext);
187 tmpContext.termSearch.fieldClass = ['author'];
188 tmpContext.termSearch.query = [summary.display.author];
189 return this.catUrl.toUrlParams(tmpContext);