]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
LP1872867 Staff catalog filters use search labels
[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 import {BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {PatronService} from '@eg/staff/share/patron/patron.service';
10
11 /**
12  * Shared bits needed by the staff version of the catalog.
13  */
14
15 @Injectable()
16 export class StaffCatalogService {
17
18     searchContext: CatalogSearchContext;
19     routeIndex = 0;
20     defaultSearchOrg: IdlObject;
21     defaultSearchLimit: number;
22     // Track the current template through route changes.
23     selectedTemplate: string;
24
25     // Display the Exclude Electronic checkbox
26     showExcludeElectronic = false;
27
28     // TODO: does unapi support pref-lib for result-page copy counts?
29     prefOrg: IdlObject;
30
31     // Default search tab
32     defaultTab: string;
33
34     // Patron barcode we hope to place a hold for.
35     holdForBarcode: string;
36     // User object for above barcode.
37     holdForUser: IdlObject;
38
39     // Cache the currently selected detail record (i.g. catalog/record/123)
40     // summary so the record detail component can avoid duplicate fetches
41     // during record tab navigation.
42     currentDetailRecordSummary: any;
43
44     // Add digital bookplate to search options.
45     enableBookplates = false;
46
47     constructor(
48         private router: Router,
49         private route: ActivatedRoute,
50         private org: OrgService,
51         private cat: CatalogService,
52         private patron: PatronService,
53         private catUrl: CatalogUrlService
54     ) { }
55
56     createContext(): void {
57         // Initialize the search context from the load-time URL params.
58         // Do this here so the search form and other context data are
59         // applied on every page, not just the search results page.  The
60         // search results pages will handle running the actual search.
61         this.searchContext =
62             this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
63
64         this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
65
66         if (this.holdForBarcode) {
67             this.patron.getByBarcode(this.holdForBarcode)
68             .then(user => this.holdForUser = user);
69         }
70
71         this.searchContext.org = this.org; // service, not searchOrg
72         this.searchContext.isStaff = true;
73         this.applySearchDefaults();
74     }
75
76     cloneContext(context: CatalogSearchContext): CatalogSearchContext {
77         const params: any = this.catUrl.toUrlParams(context);
78         return this.catUrl.fromUrlHash(params);
79     }
80
81     applySearchDefaults(): void {
82         if (!this.searchContext.searchOrg) {
83             this.searchContext.searchOrg =
84                 this.defaultSearchOrg || this.org.root();
85         }
86
87         if (!this.searchContext.pager.limit) {
88             this.searchContext.pager.limit = this.defaultSearchLimit || 10;
89         }
90     }
91
92     /**
93      * Redirect to the search results page while propagating the current
94      * search paramters into the URL.  Let the search results component
95      * execute the actual search.
96      */
97     search(): void {
98         if (!this.searchContext.isSearchable()) { return; }
99
100         const params = this.catUrl.toUrlParams(this.searchContext);
101
102         // Force a new search every time this method is called, even if
103         // it's the same as the active search.  Since router navigation
104         // exits early when the route + params is identical, add a
105         // random token to the route params to force a full navigation.
106         // This also resolves a problem where only removing secondary+
107         // versions of a query param fail to cause a route navigation.
108         // (E.g. going from two query= params to one).  Investigation
109         // pending.
110         params.ridx = '' + this.routeIndex++;
111
112         this.router.navigate(
113           ['/staff/catalog/search'], {queryParams: params});
114     }
115
116     /**
117      * Redirect to the browse results page while propagating the current
118      * browse paramters into the URL.  Let the browse results component
119      * execute the actual browse.
120      */
121     browse(): void {
122         if (!this.searchContext.browseSearch.isSearchable()) { return; }
123         const params = this.catUrl.toUrlParams(this.searchContext);
124
125         // Force a new browse every time this method is called, even if
126         // it's the same as the active browse.  Since router navigation
127         // exits early when the route + params is identical, add a
128         // random token to the route params to force a full navigation.
129         // This also resolves a problem where only removing secondary+
130         // versions of a query param fail to cause a route navigation.
131         // (E.g. going from two query= params to one).
132         params.ridx = '' + this.routeIndex++;
133
134         this.router.navigate(
135             ['/staff/catalog/browse'], {queryParams: params});
136     }
137
138     // Call number browse.
139     // Redirect to cn browse page and let its component perform the search
140     cnBrowse(): void {
141         if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
142         const params = this.catUrl.toUrlParams(this.searchContext);
143         params.ridx = '' + this.routeIndex++; // see comments above
144         this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
145     }
146
147     // Params to genreate a new author search based on a reset
148     // clone of the current page params.
149     getAuthorSearchParams(summary: BibRecordSummary): any {
150         const tmpContext = this.cloneContext(this.searchContext);
151         tmpContext.reset();
152         tmpContext.termSearch.fieldClass = ['author'];
153         tmpContext.termSearch.query = [summary.display.author];
154         return this.catUrl.toUrlParams(tmpContext);
155     }
156 }
157
158