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';
12 * Shared bits needed by the staff version of the catalog.
16 export class StaffCatalogService {
18 searchContext: CatalogSearchContext;
20 defaultSearchOrg: IdlObject;
21 defaultSearchLimit: number;
22 // Track the current template through route changes.
23 selectedTemplate: string;
25 // Display the Exclude Electronic checkbox
26 showExcludeElectronic = false;
33 // Patron barcode we hope to place a hold for.
34 holdForBarcode: string;
35 // User object for above barcode.
36 holdForUser: IdlObject;
38 // Emit that the value has changed so components can detect
39 // the change even when the component is not itself digesting
41 holdForChange: EventEmitter<void> = new EventEmitter<void>();
43 // Cache the currently selected detail record (i.g. catalog/record/123)
44 // summary so the record detail component can avoid duplicate fetches
45 // during record tab navigation.
46 currentDetailRecordSummary: any;
48 // Add digital bookplate to search options.
49 enableBookplates = false;
52 private router: Router,
53 private route: ActivatedRoute,
54 private org: OrgService,
55 private cat: CatalogService,
56 private patron: PatronService,
57 private catUrl: CatalogUrlService
60 createContext(): void {
61 // Initialize the search context from the load-time URL params.
62 // Do this here so the search form and other context data are
63 // applied on every page, not just the search results page. The
64 // search results pages will handle running the actual search.
66 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
68 this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
70 if (this.holdForBarcode) {
71 this.patron.getByBarcode(this.holdForBarcode)
73 this.holdForUser = user;
74 this.holdForChange.emit();
78 this.searchContext.org = this.org; // service, not searchOrg
79 this.searchContext.isStaff = true;
80 this.applySearchDefaults();
84 this.holdForUser = null;
85 this.holdForBarcode = null;
86 this.holdForChange.emit();
89 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
90 const params: any = this.catUrl.toUrlParams(context);
91 const ctx = this.catUrl.fromUrlHash(params);
92 ctx.isStaff = true; // not carried in the URL
96 applySearchDefaults(): void {
97 if (!this.searchContext.searchOrg) {
98 this.searchContext.searchOrg =
99 this.defaultSearchOrg || this.org.root();
102 if (!this.searchContext.pager.limit) {
103 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
108 * Redirect to the search results page while propagating the current
109 * search paramters into the URL. Let the search results component
110 * execute the actual search.
113 if (!this.searchContext.isSearchable()) { return; }
115 // Clear cached detail summary for new searches.
116 this.currentDetailRecordSummary = null;
118 const params = this.catUrl.toUrlParams(this.searchContext);
120 // Force a new search every time this method is called, even if
121 // it's the same as the active search. Since router navigation
122 // exits early when the route + params is identical, add a
123 // random token to the route params to force a full navigation.
124 // This also resolves a problem where only removing secondary+
125 // versions of a query param fail to cause a route navigation.
126 // (E.g. going from two query= params to one). Investigation
128 params.ridx = '' + this.routeIndex++;
130 this.router.navigate(
131 ['/staff/catalog/search'], {queryParams: params});
135 * Redirect to the browse results page while propagating the current
136 * browse paramters into the URL. Let the browse results component
137 * execute the actual browse.
140 if (!this.searchContext.browseSearch.isSearchable()) { return; }
141 const params = this.catUrl.toUrlParams(this.searchContext);
143 // Force a new browse every time this method is called, even if
144 // it's the same as the active browse. Since router navigation
145 // exits early when the route + params is identical, add a
146 // random token to the route params to force a full navigation.
147 // This also resolves a problem where only removing secondary+
148 // versions of a query param fail to cause a route navigation.
149 // (E.g. going from two query= params to one).
150 params.ridx = '' + this.routeIndex++;
152 this.router.navigate(
153 ['/staff/catalog/browse'], {queryParams: params});
156 // Call number browse.
157 // Redirect to cn browse page and let its component perform the search
159 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
160 const params = this.catUrl.toUrlParams(this.searchContext);
161 params.ridx = '' + this.routeIndex++; // see comments above
162 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
165 // Params to genreate a new author search based on a reset
166 // clone of the current page params.
167 getAuthorSearchParams(summary: BibRecordSummary): any {
168 const tmpContext = this.cloneContext(this.searchContext);
170 tmpContext.termSearch.fieldClass = ['author'];
171 tmpContext.termSearch.query = [summary.display.author];
172 return this.catUrl.toUrlParams(tmpContext);