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;
42 private router: Router,
43 private route: ActivatedRoute,
44 private org: OrgService,
45 private cat: CatalogService,
46 private patron: PatronService,
47 private catUrl: CatalogUrlService
50 createContext(): void {
51 // Initialize the search context from the load-time URL params.
52 // Do this here so the search form and other context data are
53 // applied on every page, not just the search results page. The
54 // search results pages will handle running the actual search.
56 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
58 this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
60 if (this.holdForBarcode) {
61 this.patron.getByBarcode(this.holdForBarcode)
62 .then(user => this.holdForUser = user);
65 this.searchContext.org = this.org; // service, not searchOrg
66 this.searchContext.isStaff = true;
67 this.applySearchDefaults();
70 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
71 const params: any = this.catUrl.toUrlParams(context);
72 return this.catUrl.fromUrlHash(params);
75 applySearchDefaults(): void {
76 if (!this.searchContext.searchOrg) {
77 this.searchContext.searchOrg =
78 this.defaultSearchOrg || this.org.root();
81 if (!this.searchContext.pager.limit) {
82 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
87 * Redirect to the search results page while propagating the current
88 * search paramters into the URL. Let the search results component
89 * execute the actual search.
92 if (!this.searchContext.isSearchable()) { return; }
94 const params = this.catUrl.toUrlParams(this.searchContext);
96 // Force a new search every time this method is called, even if
97 // it's the same as the active search. Since router navigation
98 // exits early when the route + params is identical, add a
99 // random token to the route params to force a full navigation.
100 // This also resolves a problem where only removing secondary+
101 // versions of a query param fail to cause a route navigation.
102 // (E.g. going from two query= params to one). Investigation
104 params.ridx = '' + this.routeIndex++;
106 this.router.navigate(
107 ['/staff/catalog/search'], {queryParams: params});
111 * Redirect to the browse results page while propagating the current
112 * browse paramters into the URL. Let the browse results component
113 * execute the actual browse.
116 if (!this.searchContext.browseSearch.isSearchable()) { return; }
117 const params = this.catUrl.toUrlParams(this.searchContext);
119 // Force a new browse every time this method is called, even if
120 // it's the same as the active browse. Since router navigation
121 // exits early when the route + params is identical, add a
122 // random token to the route params to force a full navigation.
123 // This also resolves a problem where only removing secondary+
124 // versions of a query param fail to cause a route navigation.
125 // (E.g. going from two query= params to one).
126 params.ridx = '' + this.routeIndex++;
128 this.router.navigate(
129 ['/staff/catalog/browse'], {queryParams: params});
132 // Call number browse.
133 // Redirect to cn browse page and let its component perform the search
135 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
136 const params = this.catUrl.toUrlParams(this.searchContext);
137 params.ridx = '' + this.routeIndex++; // see comments above
138 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
141 // Params to genreate a new author search based on a reset
142 // clone of the current page params.
143 getAuthorSearchParams(summary: BibRecordSummary): any {
144 const tmpContext = this.cloneContext(this.searchContext);
146 tmpContext.termSearch.fieldClass = ['author'];
147 tmpContext.termSearch.query = [summary.display.author];
148 return this.catUrl.toUrlParams(tmpContext);