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;
20 // Track the current template through route changes.
21 selectedTemplate: string;
23 // TODO: does unapi support pref-lib for result-page copy counts?
29 // Cache the currently selected detail record (i.g. catalog/record/123)
30 // summary so the record detail component can avoid duplicate fetches
31 // during record tab navigation.
32 currentDetailRecordSummary: any;
35 private router: Router,
36 private route: ActivatedRoute,
37 private org: OrgService,
38 private cat: CatalogService,
39 private catUrl: CatalogUrlService
42 createContext(): void {
43 // Initialize the search context from the load-time URL params.
44 // Do this here so the search form and other context data are
45 // applied on every page, not just the search results page. The
46 // search results pages will handle running the actual search.
48 this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
50 this.searchContext.org = this.org; // service, not searchOrg
51 this.searchContext.isStaff = true;
52 this.applySearchDefaults();
55 cloneContext(context: CatalogSearchContext): CatalogSearchContext {
56 const params: any = this.catUrl.toUrlParams(context);
57 return this.catUrl.fromUrlHash(params);
60 applySearchDefaults(): void {
61 if (!this.searchContext.searchOrg) {
62 this.searchContext.searchOrg =
63 this.defaultSearchOrg || this.org.root();
66 if (!this.searchContext.pager.limit) {
67 this.searchContext.pager.limit = this.defaultSearchLimit || 20;
72 * Redirect to the search results page while propagating the current
73 * search paramters into the URL. Let the search results component
74 * execute the actual search.
77 if (!this.searchContext.isSearchable()) { return; }
79 const params = this.catUrl.toUrlParams(this.searchContext);
81 // Force a new search every time this method is called, even if
82 // it's the same as the active search. Since router navigation
83 // exits early when the route + params is identical, add a
84 // random token to the route params to force a full navigation.
85 // This also resolves a problem where only removing secondary+
86 // versions of a query param fail to cause a route navigation.
87 // (E.g. going from two query= params to one). Investigation
89 params.ridx = '' + this.routeIndex++;
92 ['/staff/catalog/search'], {queryParams: params});
96 * Redirect to the browse results page while propagating the current
97 * browse paramters into the URL. Let the browse results component
98 * execute the actual browse.
101 if (!this.searchContext.browseSearch.isSearchable()) { return; }
102 const params = this.catUrl.toUrlParams(this.searchContext);
104 // Force a new browse every time this method is called, even if
105 // it's the same as the active browse. Since router navigation
106 // exits early when the route + params is identical, add a
107 // random token to the route params to force a full navigation.
108 // This also resolves a problem where only removing secondary+
109 // versions of a query param fail to cause a route navigation.
110 // (E.g. going from two query= params to one).
111 params.ridx = '' + this.routeIndex++;
113 this.router.navigate(
114 ['/staff/catalog/browse'], {queryParams: params});
117 // Call number browse.
118 // Redirect to cn browse page and let its component perform the search
120 if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
121 const params = this.catUrl.toUrlParams(this.searchContext);
122 params.ridx = '' + this.routeIndex++; // see comments above
123 this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});