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';
11 * Shared bits needed by the staff version of the catalog.
15 export class StaffCatalogService {
17 searchContext: CatalogSearchContext;
19 defaultSearchOrg: IdlObject;
20 defaultSearchLimit: number;
21 // Track the current template through route changes.
22 selectedTemplate: string;
24 // TODO: does unapi support pref-lib for result-page copy counts?
30 // Cache the currently selected detail record (i.g. catalog/record/123)
31 // summary so the record detail component can avoid duplicate fetches
32 // during record tab navigation.
33 currentDetailRecordSummary: any;
36 private router: Router,
37 private route: ActivatedRoute,
38 private org: OrgService,
39 private cat: CatalogService,
40 private catUrl: CatalogUrlService
43 createContext(): void {
44 // Initialize the search context from the load-time URL params.
45 // Do this here so the search form and other context data are
46 // applied on every page, not just the search results page. The
47 // search results pages will handle running the actual search.
49 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
51 this.searchContext.org = this.org; // service, not searchOrg
52 this.searchContext.isStaff = true;
53 this.applySearchDefaults();
56 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
57 const params: any = this.catUrl.toUrlParams(context);
58 return this.catUrl.fromUrlHash(params);
61 applySearchDefaults(): void {
62 if (!this.searchContext.searchOrg) {
63 this.searchContext.searchOrg =
64 this.defaultSearchOrg || this.org.root();
67 if (!this.searchContext.pager.limit) {
68 this.searchContext.pager.limit = this.defaultSearchLimit || 10;
73 * Redirect to the search results page while propagating the current
74 * search paramters into the URL. Let the search results component
75 * execute the actual search.
78 if (!this.searchContext.isSearchable()) { return; }
80 const params = this.catUrl.toUrlParams(this.searchContext);
82 // Force a new search every time this method is called, even if
83 // it's the same as the active search. Since router navigation
84 // exits early when the route + params is identical, add a
85 // random token to the route params to force a full navigation.
86 // This also resolves a problem where only removing secondary+
87 // versions of a query param fail to cause a route navigation.
88 // (E.g. going from two query= params to one). Investigation
90 params.ridx = '' + this.routeIndex++;
93 ['/staff/catalog/search'], {queryParams: params});
97 * Redirect to the browse results page while propagating the current
98 * browse paramters into the URL. Let the browse results component
99 * execute the actual browse.
102 if (!this.searchContext.browseSearch.isSearchable()) { return; }
103 const params = this.catUrl.toUrlParams(this.searchContext);
105 // Force a new browse every time this method is called, even if
106 // it's the same as the active browse. Since router navigation
107 // exits early when the route + params is identical, add a
108 // random token to the route params to force a full navigation.
109 // This also resolves a problem where only removing secondary+
110 // versions of a query param fail to cause a route navigation.
111 // (E.g. going from two query= params to one).
112 params.ridx = '' + this.routeIndex++;
114 this.router.navigate(
115 ['/staff/catalog/browse'], {queryParams: params});
118 // Call number browse.
119 // Redirect to cn browse page and let its component perform the search
121 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
122 const params = this.catUrl.toUrlParams(this.searchContext);
123 params.ridx = '' + this.routeIndex++; // see comments above
124 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
127 // Params to genreate a new author search based on a reset
128 // clone of the current page params.
129 getAuthorSearchParams(summary: BibRecordSummary): any {
130 const tmpContext = this.cloneContext(this.searchContext);
132 tmpContext.termSearch.fieldClass = ['author'];
133 tmpContext.termSearch.query = [summary.display.author];
134 return this.catUrl.toUrlParams(tmpContext);