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