]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/catalog.service.ts
LP1904788 Staff catalog browse results paging
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / catalog.service.ts
1 import {Injectable, EventEmitter} 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     prefOrg: IdlObject;
29
30     // Default search tab
31     defaultTab: string;
32
33     // Patron barcode we hope to place a hold for.
34     holdForBarcode: string;
35     // User object for above barcode.
36     holdForUser: IdlObject;
37
38     // Emit that the value has changed so components can detect
39     // the change even when the component is not itself digesting
40     // new values.
41     holdForChange: EventEmitter<void> = new EventEmitter<void>();
42
43     // Cache the currently selected detail record (i.g. catalog/record/123)
44     // summary so the record detail component can avoid duplicate fetches
45     // during record tab navigation.
46     currentDetailRecordSummary: any;
47
48     // Add digital bookplate to search options.
49     enableBookplates = false;
50
51     // Cache of browse results so the browse pager is not forced to
52     // re-run the browse search on each navigation.
53     browsePagerData: any[];
54
55     constructor(
56         private router: Router,
57         private route: ActivatedRoute,
58         private org: OrgService,
59         private cat: CatalogService,
60         private patron: PatronService,
61         private catUrl: CatalogUrlService
62     ) { }
63
64     createContext(): void {
65         // Initialize the search context from the load-time URL params.
66         // Do this here so the search form and other context data are
67         // applied on every page, not just the search results page.  The
68         // search results pages will handle running the actual search.
69         this.searchContext =
70             this.catUrl.fromUrlParams(this.route.snapshot.queryParamMap);
71
72         this.holdForBarcode = this.route.snapshot.queryParams['holdForBarcode'];
73
74         if (this.holdForBarcode) {
75             this.patron.getByBarcode(this.holdForBarcode)
76             .then(user => {
77                 this.holdForUser = user;
78                 this.holdForChange.emit();
79             });
80         }
81
82         this.searchContext.org = this.org; // service, not searchOrg
83         this.searchContext.isStaff = true;
84         this.applySearchDefaults();
85     }
86
87     clearHoldPatron() {
88         this.holdForUser = null;
89         this.holdForBarcode = null;
90         this.holdForChange.emit();
91     }
92
93     cloneContext(context: CatalogSearchContext): CatalogSearchContext {
94         const params: any = this.catUrl.toUrlParams(context);
95         const ctx = this.catUrl.fromUrlHash(params);
96         ctx.isStaff = true; // not carried in the URL
97         return ctx;
98     }
99
100     applySearchDefaults(): void {
101         if (!this.searchContext.searchOrg) {
102             this.searchContext.searchOrg =
103                 this.defaultSearchOrg || this.org.root();
104         }
105
106         if (!this.searchContext.pager.limit) {
107             this.searchContext.pager.limit = this.defaultSearchLimit || 10;
108         }
109     }
110
111     /**
112      * Redirect to the search results page while propagating the current
113      * search paramters into the URL.  Let the search results component
114      * execute the actual search.
115      */
116     search(): void {
117         if (!this.searchContext.isSearchable()) { return; }
118
119         // Clear cached detail summary for new searches.
120         this.currentDetailRecordSummary = null;
121
122         const params = this.catUrl.toUrlParams(this.searchContext);
123
124         // Force a new search every time this method is called, even if
125         // it's the same as the active search.  Since router navigation
126         // exits early when the route + params is identical, add a
127         // random token to the route params to force a full navigation.
128         // This also resolves a problem where only removing secondary+
129         // versions of a query param fail to cause a route navigation.
130         // (E.g. going from two query= params to one).  Investigation
131         // pending.
132         params.ridx = '' + this.routeIndex++;
133
134         this.router.navigate(
135           ['/staff/catalog/search'], {queryParams: params});
136     }
137
138     /**
139      * Redirect to the browse results page while propagating the current
140      * browse paramters into the URL.  Let the browse results component
141      * execute the actual browse.
142      */
143     browse(): void {
144         if (!this.searchContext.browseSearch.isSearchable()) { return; }
145         const params = this.catUrl.toUrlParams(this.searchContext);
146
147         // Force a new browse every time this method is called, even if
148         // it's the same as the active browse.  Since router navigation
149         // exits early when the route + params is identical, add a
150         // random token to the route params to force a full navigation.
151         // This also resolves a problem where only removing secondary+
152         // versions of a query param fail to cause a route navigation.
153         // (E.g. going from two query= params to one).
154         params.ridx = '' + this.routeIndex++;
155
156         this.router.navigate(
157             ['/staff/catalog/browse'], {queryParams: params});
158     }
159
160     // Call number browse.
161     // Redirect to cn browse page and let its component perform the search
162     cnBrowse(): void {
163         if (!this.searchContext.cnBrowseSearch.isSearchable()) { return; }
164         const params = this.catUrl.toUrlParams(this.searchContext);
165         params.ridx = '' + this.routeIndex++; // see comments above
166         this.router.navigate(['/staff/catalog/cnbrowse'], {queryParams: params});
167     }
168
169     // Params to genreate a new author search based on a reset
170     // clone of the current page params.
171     getAuthorSearchParams(summary: BibRecordSummary): any {
172         const tmpContext = this.cloneContext(this.searchContext);
173         tmpContext.reset();
174         tmpContext.termSearch.fieldClass = ['author'];
175         tmpContext.termSearch.query = [summary.display.author];
176         return this.catUrl.toUrlParams(tmpContext);
177     }
178 }
179
180