1 import {Injectable} 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 // TODO: does unapi support pref-lib for result-page copy counts?
31 // Patron barcode we hope to place a hold for.
32 holdForBarcode: string;
33 // User object for above barcode.
34 holdForUser: IdlObject;
36 // Cache the currently selected detail record (i.g. catalog/record/123)
37 // summary so the record detail component can avoid duplicate fetches
38 // during record tab navigation.
39 currentDetailRecordSummary: any;
41 // Add digital bookplate to search options.
42 enableBookplates = false;
45 private router: Router,
46 private route: ActivatedRoute,
47 private org: OrgService,
48 private cat: CatalogService,
49 private patron: PatronService,
50 private catUrl: CatalogUrlService
53 createContext(): void {
54 // Initialize the search context from the load-time URL params.
55 // Do this here so the search form and other context data are
56 // applied on every page, not just the search results page. The
57 // search results pages will handle running the actual search.
59 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
61 this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
63 if (this.holdForBarcode) {
64 this.patron.getByBarcode(this.holdForBarcode)
65 .then(user => this.holdForUser = user);
68 this.searchContext.org = this.org; // service, not searchOrg
69 this.searchContext.isStaff = true;
70 this.applySearchDefaults();
73 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
74 const params: any = this.catUrl.toUrlParams(context);
75 return this.catUrl.fromUrlHash(params);
78 applySearchDefaults(): void {
79 if (!this.searchContext.searchOrg) {
80 this.searchContext.searchOrg =
81 this.defaultSearchOrg || this.org.root();
84 if (!this.searchContext.pager.limit) {
85 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
90 * Redirect to the search results page while propagating the current
91 * search paramters into the URL. Let the search results component
92 * execute the actual search.
95 if (!this.searchContext.isSearchable()) { return; }
97 const params = this.catUrl.toUrlParams(this.searchContext);
99 // Force a new search every time this method is called, even if
100 // it's the same as the active search. Since router navigation
101 // exits early when the route + params is identical, add a
102 // random token to the route params to force a full navigation.
103 // This also resolves a problem where only removing secondary+
104 // versions of a query param fail to cause a route navigation.
105 // (E.g. going from two query= params to one). Investigation
107 params.ridx = '' + this.routeIndex++;
109 this.router.navigate(
110 ['/staff/catalog/search'], {queryParams: params});
114 * Redirect to the browse results page while propagating the current
115 * browse paramters into the URL. Let the browse results component
116 * execute the actual browse.
119 if (!this.searchContext.browseSearch.isSearchable()) { return; }
120 const params = this.catUrl.toUrlParams(this.searchContext);
122 // Force a new browse every time this method is called, even if
123 // it's the same as the active browse. Since router navigation
124 // exits early when the route + params is identical, add a
125 // random token to the route params to force a full navigation.
126 // This also resolves a problem where only removing secondary+
127 // versions of a query param fail to cause a route navigation.
128 // (E.g. going from two query= params to one).
129 params.ridx = '' + this.routeIndex++;
131 this.router.navigate(
132 ['/staff/catalog/browse'], {queryParams: params});
135 // Call number browse.
136 // Redirect to cn browse page and let its component perform the search
138 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
139 const params = this.catUrl.toUrlParams(this.searchContext);
140 params.ridx = '' + this.routeIndex++; // see comments above
141 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
144 // Params to genreate a new author search based on a reset
145 // clone of the current page params.
146 getAuthorSearchParams(summary: BibRecordSummary): any {
147 const tmpContext = this.cloneContext(this.searchContext);
149 tmpContext.termSearch.fieldClass = ['author'];
150 tmpContext.termSearch.query = [summary.display.author];
151 return this.catUrl.toUrlParams(tmpContext);