]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/catalog/catalog-url.service.ts
LP2045292 Color contrast for AngularJS patron bills
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / catalog / catalog-url.service.ts
1 /* eslint-disable no-cond-assign */
2 import {Injectable} from '@angular/core';
3 import {ParamMap} from '@angular/router';
4 import {OrgService} from '@eg/core/org.service';
5 import {CatalogSearchContext, FacetFilter, CATALOG_CCVM_FILTERS} from './search-context';
6 import {HashParams} from '@eg/share/util/hash-params';
7
8 @Injectable()
9 export class CatalogUrlService {
10
11     // consider supporting a param name prefix/namespace
12
13     constructor(private org: OrgService) { }
14
15     /**
16      * Returns a URL query structure suitable for using with
17      * router.navigate(..., {queryParams:...}).
18      * No navigation is performed within.
19      */
20     toUrlParams(context: CatalogSearchContext):
21             {[key: string]: string | string[]} {
22
23         const params: any = {};
24
25         if (context.searchOrg) {
26             params.org = context.searchOrg.id();
27         }
28
29         if (context.pager.limit) {
30             params.limit = context.pager.limit;
31         }
32
33         if (context.pager.offset) {
34             params.offset = context.pager.offset;
35         }
36
37         // These fields can be copied directly into place
38         ['limit', 'offset', 'sort', 'global', 'showBasket', 'sort']
39             .forEach(field => {
40                 if (context[field]) {
41                 // Only propagate applied values to the URL.
42                     params[field] = context[field];
43                 }
44             });
45
46         if (context.marcSearch.isSearchable()) {
47             const ms = context.marcSearch;
48             params.marcTag = [];
49             params.marcSubfield = [];
50             params.marcValue = [];
51
52             ms.values.forEach((val, idx) => {
53                 if (val !== '') {
54                     params.marcTag.push(ms.tags[idx]);
55                     params.marcSubfield.push(ms.subfields[idx]);
56                     params.marcValue.push(ms.values[idx]);
57                 }
58             });
59         }
60
61         if (context.identSearch.isSearchable()) {
62             params.identQuery = context.identSearch.value;
63             params.identQueryType = context.identSearch.queryType;
64         }
65
66         if (context.browseSearch.isSearchable()) {
67             params.browseTerm = context.browseSearch.value;
68             params.browseClass = context.browseSearch.fieldClass;
69             if (context.browseSearch.pivot) {
70                 params.browsePivot = context.browseSearch.pivot;
71             }
72         }
73
74         if (context.termSearch.isSearchable()) {
75
76             const ts = context.termSearch;
77
78             params.query = [];
79             params.fieldClass = [];
80             params.joinOp = [];
81             params.matchOp = [];
82
83             ['format', 'available', 'hasBrowseEntry', 'date1',
84                 'date2', 'dateOp', 'groupByMetarecord', 'fromMetarecord',
85                 'onReserveFilter', 'onReserveFilterNegated']
86                 .forEach(field => {
87                     if (ts[field]) {
88                         params[field] = ts[field];
89                     }
90                 });
91
92             ts.query.forEach((val, idx) => {
93                 if (val !== '') {
94                     params.query.push(ts.query[idx]);
95                     params.fieldClass.push(ts.fieldClass[idx]);
96                     params.joinOp.push(ts.joinOp[idx]);
97                     params.matchOp.push(ts.matchOp[idx]);
98                 }
99             });
100
101             // CCVM filters are encoded as comma-separated lists
102             Object.keys(ts.ccvmFilters).forEach(code => {
103                 if (ts.ccvmFilters[code] &&
104                     ts.ccvmFilters[code][0] !== '') {
105                     params[code] = ts.ccvmFilters[code].join(',');
106                 }
107             });
108
109             // Each facet is a JSON encoded blob of class, name, and value
110             if (ts.facetFilters.length) {
111                 params.facets = [];
112                 ts.facetFilters.forEach(facet => {
113                     params.facets.push(JSON.stringify({
114                         c : facet.facetClass,
115                         n : facet.facetName,
116                         v : facet.facetValue
117                     }));
118                 });
119             }
120
121             if (ts.copyLocations.length && ts.copyLocations[0] !== '') {
122                 params.copyLocations = ts.copyLocations.join(',');
123             }
124
125             if (ts.excludeElectronic) {
126                 params.excludeElectronic = true;
127             }
128         }
129
130         if (context.cnBrowseSearch.isSearchable()) {
131             params.cnBrowseTerm = context.cnBrowseSearch.value;
132             params.cnBrowsePage = context.cnBrowseSearch.offset;
133             params.cnBrowsePageSize = context.cnBrowseSearch.limit;
134         }
135
136         return params;
137     }
138
139     fromUrlHash(params: any): CatalogSearchContext {
140         return this.fromUrlParams(new HashParams(params));
141     }
142
143     /**
144      * Creates a new search context from the active route params.
145      */
146     fromUrlParams(params: ParamMap): CatalogSearchContext {
147         const context = new CatalogSearchContext();
148
149         this.applyUrlParams(context, params);
150
151         return context;
152     }
153
154     applyUrlParams(context: CatalogSearchContext, params: ParamMap): void {
155
156         // Reset query/filter args.  The will be reconstructed below.
157         context.reset();
158         let val;
159
160         if (params.get('org')) {
161             context.searchOrg = this.org.get(+params.get('org'));
162         }
163
164         if (val = params.get('limit')) {
165             context.pager.limit = +val;
166         }
167
168         if (val = params.get('offset')) {
169             context.pager.offset = +val;
170         }
171
172         if (val = params.get('sort')) {
173             context.sort = val;
174         }
175
176         if (val = params.get('global')) {
177             context.global = val;
178         }
179
180         if (val = params.get('showBasket')) {
181             context.showBasket = val;
182         }
183
184         if (params.has('marcValue')) {
185             context.marcSearch.tags = params.getAll('marcTag');
186             context.marcSearch.subfields = params.getAll('marcSubfield');
187             context.marcSearch.values = params.getAll('marcValue');
188         }
189
190         if (params.has('identQuery')) {
191             context.identSearch.value = params.get('identQuery');
192             context.identSearch.queryType = params.get('identQueryType');
193         }
194
195         if (params.has('browseTerm')) {
196             context.browseSearch.value = params.get('browseTerm');
197             context.browseSearch.fieldClass = params.get('browseClass');
198             if (params.has('browsePivot')) {
199                 context.browseSearch.pivot = +params.get('browsePivot');
200             }
201         }
202
203         if (params.has('cnBrowseTerm')) {
204             context.cnBrowseSearch.value = params.get('cnBrowseTerm');
205             context.cnBrowseSearch.offset = Number(params.get('cnBrowsePage'));
206             context.cnBrowseSearch.limit = Number(params.get('cnBrowsePageSize'));
207         }
208
209         const ts = context.termSearch;
210
211         // browseEntry and query searches may be facet-limited
212         params.getAll('facets').forEach(blob => {
213             const facet = JSON.parse(blob);
214             ts.addFacet(new FacetFilter(facet.c, facet.n, facet.v));
215         });
216
217         if (params.has('hasBrowseEntry')) {
218
219             ts.hasBrowseEntry = params.get('hasBrowseEntry');
220
221         } else if (params.has('query')) {
222
223             // Scalars
224             ['format', 'available', 'date1', 'date2',
225                 'dateOp', 'groupByMetarecord', 'fromMetarecord',
226                 'onReserve']
227                 .forEach(field => {
228                     if (params.has(field)) {
229                         ts[field] = params.get(field);
230                     }
231                 });
232
233             // Arrays
234             ['query', 'fieldClass', 'joinOp', 'matchOp'].forEach(field => {
235                 const arr = params.getAll(field);
236                 if (params.has(field)) {
237                     ts[field] = params.getAll(field);
238                 }
239             });
240
241             CATALOG_CCVM_FILTERS.forEach(code => {
242                 const ccvmVal = params.get(code);
243                 if (ccvmVal) {
244                     ts.ccvmFilters[code] = ccvmVal.split(/,/);
245                 } else {
246                     ts.ccvmFilters[code] = [''];
247                 }
248             });
249
250             if (params.get('copyLocations')) {
251                 ts.copyLocations = params.get('copyLocations').split(/,/);
252             }
253
254             if (params.get('excludeElectronic')) {
255                 ts.excludeElectronic = true;
256             }
257         }
258     }
259 }
260
261