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