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