]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
LP1819745 Ang staff result page link repairs
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / catalog.service.ts
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
9 /**
10  * Shared bits needed by the staff version of the catalog.
11  */
12
13 @Injectable()
14 export class StaffCatalogService {
15
16     searchContext: CatalogSearchContext;
17     routeIndex = 0;
18     defaultSearchOrg: IdlObject;
19     defaultSearchLimit: number;
20
21     // TODO: does unapi support pref-lib for result-page copy counts?
22     prefOrg: IdlObject;
23
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;
28
29     constructor(
30         private router: Router,
31         private route: ActivatedRoute,
32         private org: OrgService,
33         private cat: CatalogService,
34         private catUrl: CatalogUrlService
35     ) { }
36
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.
42         this.searchContext =
43             this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
44
45         this.searchContext.org = this.org; // service, not searchOrg
46         this.searchContext.isStaff = true;
47         this.applySearchDefaults();
48     }
49
50     cloneContext(context: CatalogSearchContext): CatalogSearchContext {
51         const params: any = this.catUrl.toUrlParams(context);
52         return this.catUrl.fromUrlHash(params);
53     }
54
55     applySearchDefaults(): void {
56         if (!this.searchContext.searchOrg) {
57             this.searchContext.searchOrg =
58                 this.defaultSearchOrg || this.org.root();
59         }
60
61         if (!this.searchContext.pager.limit) {
62             this.searchContext.pager.limit = this.defaultSearchLimit || 20;
63         }
64     }
65
66     /**
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.
70      */
71     search(): void {
72         if (!this.searchContext.isSearchable()) { return; }
73
74         const params = this.catUrl.toUrlParams(this.searchContext);
75
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
83         // pending.
84         params.ridx = '' + this.routeIndex++;
85
86         this.router.navigate(
87           ['/staff/catalog/search'], {queryParams: params});
88     }
89
90     /**
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.
94      */
95     browse(): void {
96         if (!this.searchContext.browseSearch.isSearchable()) { return; }
97         const params = this.catUrl.toUrlParams(this.searchContext);
98
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++;
107
108         this.router.navigate(
109             ['/staff/catalog/browse'], {queryParams: params});
110     }
111
112     // Call number browse.
113     // Redirect to cn browse page and let its component perform the search
114     cnBrowse(): void {
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});
119     }
120 }
121
122