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';
10 * Shared bits needed by the staff version of the catalog.
14 export class StaffCatalogService {
16 searchContext: CatalogSearchContext;
18 defaultSearchOrg: IdlObject;
19 defaultSearchLimit: number;
21 // TODO: does unapi support pref-lib for result-page copy counts?
24 // Cache the currently selected detail record (i.g. catalog/record/123)
25 // summary so the record detail component can avoid duplicate fetches
26 // during record tab navigation.
27 currentDetailRecordSummary: any;
30 private router: Router,
31 private route: ActivatedRoute,
32 private org: OrgService,
33 private cat: CatalogService,
34 private catUrl: CatalogUrlService
37 createContext(): void {
38 // Initialize the search context from the load-time URL params.
39 // Do this here so the search form and other context data are
40 // applied on every page, not just the search results page. The
41 // search results pages will handle running the actual search.
43 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
45 this.searchContext.org = this.org; // service, not searchOrg
46 this.searchContext.isStaff = true;
47 this.applySearchDefaults();
50 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
51 const params: any = this.catUrl.toUrlParams(context);
52 return this.catUrl.fromUrlHash(params);
55 applySearchDefaults(): void {
56 if (!this.searchContext.searchOrg) {
57 this.searchContext.searchOrg =
58 this.defaultSearchOrg || this.org.root();
61 if (!this.searchContext.pager.limit) {
62 this.searchContext.pager.limit = this.defaultSearchLimit || 20;
67 * Redirect to the search results page while propagating the current
68 * search paramters into the URL. Let the search results component
69 * execute the actual search.
72 if (!this.searchContext.isSearchable()) { return; }
74 const params = this.catUrl.toUrlParams(this.searchContext);
76 // Force a new search every time this method is called, even if
77 // it's the same as the active search. Since router navigation
78 // exits early when the route + params is identical, add a
79 // random token to the route params to force a full navigation.
80 // This also resolves a problem where only removing secondary+
81 // versions of a query param fail to cause a route navigation.
82 // (E.g. going from two query= params to one). Investigation
84 params.ridx = '' + this.routeIndex++;
87 ['/staff/catalog/search'], {queryParams: params});
91 * Redirect to the browse results page while propagating the current
92 * browse paramters into the URL. Let the browse results component
93 * execute the actual browse.
96 if (!this.searchContext.browseSearch.isSearchable()) { return; }
97 const params = this.catUrl.toUrlParams(this.searchContext);
99 // Force a new browse every time this method is called, even if
100 // it's the same as the active browse. 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).
106 params.ridx = '' + this.routeIndex++;
108 this.router.navigate(
109 ['/staff/catalog/browse'], {queryParams: params});
112 // Call number browse.
113 // Redirect to cn browse page and let its component perform the search
115 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
116 const params = this.catUrl.toUrlParams(this.searchContext);
117 params.ridx = '' + this.routeIndex++; // see comments above
118 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});