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 // Display the Exclude Electronic checkbox
26 showExcludeElectronic = false;
28 // TODO: does unapi support pref-lib for result-page copy counts?
34 // Patron barcode we hope to place a hold for.
35 holdForBarcode: string;
36 // User object for above barcode.
37 holdForUser: IdlObject;
39 // Cache the currently selected detail record (i.g. catalog/record/123)
40 // summary so the record detail component can avoid duplicate fetches
41 // during record tab navigation.
42 currentDetailRecordSummary: any;
44 // Add digital bookplate to search options.
45 enableBookplates = false;
48 private router: Router,
49 private route: ActivatedRoute,
50 private org: OrgService,
51 private cat: CatalogService,
52 private patron: PatronService,
53 private catUrl: CatalogUrlService
56 createContext(): void {
57 // Initialize the search context from the load-time URL params.
58 // Do this here so the search form and other context data are
59 // applied on every page, not just the search results page. The
60 // search results pages will handle running the actual search.
62 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
64 this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
66 if (this.holdForBarcode) {
67 this.patron.getByBarcode(this.holdForBarcode)
68 .then(user => this.holdForUser = user);
71 this.searchContext.org = this.org; // service, not searchOrg
72 this.searchContext.isStaff = true;
73 this.applySearchDefaults();
76 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
77 const params: any = this.catUrl.toUrlParams(context);
78 return this.catUrl.fromUrlHash(params);
81 applySearchDefaults(): void {
82 if (!this.searchContext.searchOrg) {
83 this.searchContext.searchOrg =
84 this.defaultSearchOrg || this.org.root();
87 if (!this.searchContext.pager.limit) {
88 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
93 * Redirect to the search results page while propagating the current
94 * search paramters into the URL. Let the search results component
95 * execute the actual search.
98 if (!this.searchContext.isSearchable()) { return; }
100 const params = this.catUrl.toUrlParams(this.searchContext);
102 // Force a new search every time this method is called, even if
103 // it's the same as the active search. Since router navigation
104 // exits early when the route + params is identical, add a
105 // random token to the route params to force a full navigation.
106 // This also resolves a problem where only removing secondary+
107 // versions of a query param fail to cause a route navigation.
108 // (E.g. going from two query= params to one). Investigation
110 params.ridx = '' + this.routeIndex++;
112 this.router.navigate(
113 ['/staff/catalog/search'], {queryParams: params});
117 * Redirect to the browse results page while propagating the current
118 * browse paramters into the URL. Let the browse results component
119 * execute the actual browse.
122 if (!this.searchContext.browseSearch.isSearchable()) { return; }
123 const params = this.catUrl.toUrlParams(this.searchContext);
125 // Force a new browse every time this method is called, even if
126 // it's the same as the active browse. Since router navigation
127 // exits early when the route + params is identical, add a
128 // random token to the route params to force a full navigation.
129 // This also resolves a problem where only removing secondary+
130 // versions of a query param fail to cause a route navigation.
131 // (E.g. going from two query= params to one).
132 params.ridx = '' + this.routeIndex++;
134 this.router.navigate(
135 ['/staff/catalog/browse'], {queryParams: params});
138 // Call number browse.
139 // Redirect to cn browse page and let its component perform the search
141 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
142 const params = this.catUrl.toUrlParams(this.searchContext);
143 params.ridx = '' + this.routeIndex++; // see comments above
144 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
147 // Params to genreate a new author search based on a reset
148 // clone of the current page params.
149 getAuthorSearchParams(summary: BibRecordSummary): any {
150 const tmpContext = this.cloneContext(this.searchContext);
152 tmpContext.termSearch.fieldClass = ['author'];
153 tmpContext.termSearch.query = [summary.display.author];
154 return this.catUrl.toUrlParams(tmpContext);