]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
e6da35879b4e19b0ba7d41699199e9ea1e54ed13
[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     // 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     // Default search tab
27     defaultTab: string;
28
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;
33
34     constructor(
35         private router: Router,
36         private route: ActivatedRoute,
37         private org: OrgService,
38         private cat: CatalogService,
39         private catUrl: CatalogUrlService
40     ) { }
41
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.
47         this.searchContext =
48             this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
49
50         this.searchContext.org = this.org; // service, not searchOrg
51         this.searchContext.isStaff = true;
52         this.applySearchDefaults();
53     }
54
55     cloneContext(context: CatalogSearchContext): CatalogSearchContext {
56         const params: any = this.catUrl.toUrlParams(context);
57         return this.catUrl.fromUrlHash(params);
58     }
59
60     applySearchDefaults(): void {
61         if (!this.searchContext.searchOrg) {
62             this.searchContext.searchOrg =
63                 this.defaultSearchOrg || this.org.root();
64         }
65
66         if (!this.searchContext.pager.limit) {
67             this.searchContext.pager.limit = this.defaultSearchLimit || 20;
68         }
69     }
70
71     /**
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.
75      */
76     search(): void {
77         if (!this.searchContext.isSearchable()) { return; }
78
79         const params = this.catUrl.toUrlParams(this.searchContext);
80
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
88         // pending.
89         params.ridx = '' + this.routeIndex++;
90
91         this.router.navigate(
92           ['/staff/catalog/search'], {queryParams: params});
93     }
94
95     /**
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.
99      */
100     browse(): void {
101         if (!this.searchContext.browseSearch.isSearchable()) { return; }
102         const params = this.catUrl.toUrlParams(this.searchContext);
103
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++;
112
113         this.router.navigate(
114             ['/staff/catalog/browse'], {queryParams: params});
115     }
116
117     // Call number browse.
118     // Redirect to cn browse page and let its component perform the search
119     cnBrowse(): void {
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});
124     }
125 }
126
127