LP1806087 Angular catalog Ang7 & lint repairs
[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
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             .forEach(field => {
86                 if (ts[field]) {
87                     params[field] = ts[field];
88                 }
89             });
90
91             ts.query.forEach((val, idx) => {
92                 if (val !== '') {
93                     params.query.push(ts.query[idx]);
94                     params.fieldClass.push(ts.fieldClass[idx]);
95                     params.joinOp.push(ts.joinOp[idx]);
96                     params.matchOp.push(ts.matchOp[idx]);
97                 }
98             });
99
100             // CCVM filters are encoded as comma-separated lists
101             Object.keys(ts.ccvmFilters).forEach(code => {
102                 if (ts.ccvmFilters[code] &&
103                     ts.ccvmFilters[code][0] !== '') {
104                     params[code] = ts.ccvmFilters[code].join(',');
105                 }
106             });
107
108             // Each facet is a JSON encoded blob of class, name, and value
109             if (ts.facetFilters.length) {
110                 params.facets = [];
111                 ts.facetFilters.forEach(facet => {
112                     params.facets.push(JSON.stringify({
113                         c : facet.facetClass,
114                         n : facet.facetName,
115                         v : facet.facetValue
116                     }));
117                 });
118             }
119
120             if (ts.copyLocations.length && ts.copyLocations[0] !== '') {
121                 params.copyLocations = ts.copyLocations.join(',');
122             }
123         }
124
125         return params;
126     }
127
128     /**
129      * Creates a new search context from the active route params.
130      */
131     fromUrlParams(params: ParamMap): CatalogSearchContext {
132         const context = new CatalogSearchContext();
133
134         this.applyUrlParams(context, params);
135
136         return context;
137     }
138
139     applyUrlParams(context: CatalogSearchContext, params: ParamMap): void {
140
141         // Reset query/filter args.  The will be reconstructed below.
142         context.reset();
143         let val;
144
145         if (params.get('org')) {
146             context.searchOrg = this.org.get(+params.get('org'));
147         }
148
149         if (val = params.get('limit')) {
150             context.pager.limit = +val;
151         }
152
153         if (val = params.get('offset')) {
154             context.pager.offset = +val;
155         }
156
157         if (val = params.get('sort')) {
158             context.sort = val;
159         }
160
161         if (val = params.get('global')) {
162             context.global = val;
163         }
164
165         if (val = params.get('showBasket')) {
166             context.showBasket = val;
167         }
168
169         if (params.get('marcValue')) {
170             context.marcSearch.tags = params.getAll('marcTag');
171             context.marcSearch.subfields = params.getAll('marcSubfield');
172             context.marcSearch.values = params.getAll('marcValue');
173         }
174
175         if (params.get('identQuery')) {
176             context.identSearch.value = params.get('identQuery');
177             context.identSearch.queryType = params.get('identQueryType');
178         }
179
180         if (params.get('browseTerm')) {
181             context.browseSearch.value = params.get('browseTerm');
182             context.browseSearch.fieldClass = params.get('browseClass');
183             if (params.has('browsePivot')) {
184                 context.browseSearch.pivot = +params.get('browsePivot');
185             }
186         }
187
188         const ts = context.termSearch;
189
190         // browseEntry and query searches may be facet-limited
191         params.getAll('facets').forEach(blob => {
192             const facet = JSON.parse(blob);
193             ts.addFacet(new FacetFilter(facet.c, facet.n, facet.v));
194         });
195
196         if (params.has('hasBrowseEntry')) {
197
198             ts.hasBrowseEntry = params.get('hasBrowseEntry');
199
200         } else if (params.has('query')) {
201
202             // Scalars
203             ['format', 'available', 'date1', 'date2',
204                 'dateOp', 'groupByMetarecord', 'fromMetarecord']
205             .forEach(field => {
206                 if (params.has(field)) {
207                     ts[field] = params.get(field);
208                 }
209             });
210
211             // Arrays
212             ['query', 'fieldClass', 'joinOp', 'matchOp'].forEach(field => {
213                 const arr = params.getAll(field);
214                 if (params.has(field)) {
215                     ts[field] = params.getAll(field);
216                 }
217             });
218
219             CATALOG_CCVM_FILTERS.forEach(code => {
220                 const ccvmVal = params.get(code);
221                 if (ccvmVal) {
222                     ts.ccvmFilters[code] = ccvmVal.split(/,/);
223                 } else {
224                     ts.ccvmFilters[code] = [''];
225                 }
226             });
227
228             if (params.get('copyLocations')) {
229                 ts.copyLocations = params.get('copyLocations').split(/,/);
230             }
231         }
232     }
233 }
234
235