]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
cf0a36c97f38b69f46b8f3e8f5c01b3ce6585d9c
[working/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     applySearchDefaults(): void {
51         if (!this.searchContext.searchOrg) {
52             this.searchContext.searchOrg =
53                 this.defaultSearchOrg || this.org.root();
54         }
55
56         if (!this.searchContext.pager.limit) {
57             this.searchContext.pager.limit = this.defaultSearchLimit || 20;
58         }
59     }
60
61     /**
62      * Redirect to the search results page while propagating the current
63      * search paramters into the URL.  Let the search results component
64      * execute the actual search.
65      */
66     search(): void {
67         if (!this.searchContext.isSearchable()) { return; }
68
69         const params = this.catUrl.toUrlParams(this.searchContext);
70
71         // Force a new search every time this method is called, even if
72         // it's the same as the active search.  Since router navigation
73         // exits early when the route + params is identical, add a
74         // random token to the route params to force a full navigation.
75         // This also resolves a problem where only removing secondary+
76         // versions of a query param fail to cause a route navigation.
77         // (E.g. going from two query= params to one).  Investigation
78         // pending.
79         params.ridx = '' + this.routeIndex++;
80
81         this.router.navigate(
82           ['/staff/catalog/search'], {queryParams: params});
83     }
84
85     /**
86      * Redirect to the browse results page while propagating the current
87      * browse paramters into the URL.  Let the browse results component
88      * execute the actual browse.
89      */
90     browse(): void {
91         if (!this.searchContext.browseSearch.isSearchable()) { return; }
92
93         const params = this.catUrl.toUrlParams(this.searchContext);
94
95         // Force a new browse every time this method is called, even if
96         // it's the same as the active browse.  Since router navigation
97         // exits early when the route + params is identical, add a
98         // random token to the route params to force a full navigation.
99         // This also resolves a problem where only removing secondary+
100         // versions of a query param fail to cause a route navigation.
101         // (E.g. going from two query= params to one).
102         params.ridx = '' + this.routeIndex++;
103
104         this.router.navigate(
105           ['/staff/catalog/browse'], {queryParams: params});
106     }
107 }
108
109