LP1885767 Staff catalog exclude electronic option
[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             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             .forEach(field => {
227                 if (params.has(field)) {
228                     ts[field] = params.get(field);
229                 }
230             });
231
232             // Arrays
233             ['query', 'fieldClass', 'joinOp', 'matchOp'].forEach(field => {
234                 const arr = params.getAll(field);
235                 if (params.has(field)) {
236                     ts[field] = params.getAll(field);
237                 }
238             });
239
240             CATALOG_CCVM_FILTERS.forEach(code => {
241                 const ccvmVal = params.get(code);
242                 if (ccvmVal) {
243                     ts.ccvmFilters[code] = ccvmVal.split(/,/);
244                 } else {
245                     ts.ccvmFilters[code] = [''];
246                 }
247             });
248
249             if (params.get('copyLocations')) {
250                 ts.copyLocations = params.get('copyLocations').split(/,/);
251             }
252
253             if (params.get('excludeElectronic')) {
254                 ts.excludeElectronic = true;
255             }
256         }
257     }
258 }
259
260