Much work towards more coverage.
Showing
5 changed files
with
536 additions
and
255 deletions
... | @@ -37,6 +37,10 @@ define(function(require) { | ... | @@ -37,6 +37,10 @@ define(function(require) { |
37 | }; | 37 | }; |
38 | }, | 38 | }, |
39 | initialize: function(data, options) { | 39 | initialize: function(data, options) { |
40 | options = _.defaults({}, options, { | ||
41 | gap: 1, | ||
42 | sortKeyExtractor: Util.getItemKeyAccessor, | ||
43 | }); | ||
40 | this.set('all', true); | 44 | this.set('all', true); |
41 | this.on('change:queryMax change:queryMin', function() { | 45 | this.on('change:queryMax change:queryMin', function() { |
42 | this.set('hasQuery', this.get('queryMin') !== undefined && this.get('queryMax') !== undefined); | 46 | this.set('hasQuery', this.get('queryMin') !== undefined && this.get('queryMax') !== undefined); |
... | @@ -123,9 +127,6 @@ define(function(require) { | ... | @@ -123,9 +127,6 @@ define(function(require) { |
123 | }, self); | 127 | }, self); |
124 | })(this); | 128 | })(this); |
125 | var sortKeyExtractor = options.sortKeyExtractor; | 129 | var sortKeyExtractor = options.sortKeyExtractor; |
126 | if (!sortKeyExtractor) { | ||
127 | sortKeyExtractor = Util.getItemKeyAccessor; | ||
128 | } | ||
129 | var formatter = options.formatter; | 130 | var formatter = options.formatter; |
130 | if (!formatter) { | 131 | if (!formatter) { |
131 | var labelMap = options.labelMap; | 132 | var labelMap = options.labelMap; |
... | @@ -154,21 +155,21 @@ define(function(require) { | ... | @@ -154,21 +155,21 @@ define(function(require) { |
154 | comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1); | 155 | comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1); |
155 | break; | 156 | break; |
156 | } | 157 | } |
157 | var facet = this; | ||
158 | comparator = (function(comparator) { | 158 | comparator = (function(comparator) { |
159 | return function facetItemValue(a, b) { | 159 | return function facetItemValue(a, b) { |
160 | var keyA = sortKeyExtractor(a); | 160 | var keyA = sortKeyExtractor(a); |
161 | var keyB = sortKeyExtractor(b); | 161 | var keyB = sortKeyExtractor(b); |
162 | switch (facet.get('orderByDirection')) { | ||
163 | case 'desc': | ||
164 | var tmp = keyA; | ||
165 | keyA = keyB; | ||
166 | keyB = tmp; | ||
167 | break; | ||
168 | } | ||
169 | return comparator.call(facet, keyA, keyB); | 162 | return comparator.call(facet, keyA, keyB); |
170 | }; | 163 | }; |
171 | })(comparator); | 164 | })(comparator); |
165 | var facet = this; | ||
166 | if (this.get('orderByDirection') === 'desc') { | ||
167 | comparator = (function(comparator) { | ||
168 | return function reverseSort(a, b) { | ||
169 | return -comparator.call(facet, a, b); | ||
170 | } | ||
171 | })(comparator); | ||
172 | } | ||
172 | var items = this.get('items'); | 173 | var items = this.get('items'); |
173 | items.comparator = comparator; | 174 | items.comparator = comparator; |
174 | items.sort(); | 175 | items.sort(); |
... | @@ -191,7 +192,7 @@ define(function(require) { | ... | @@ -191,7 +192,7 @@ define(function(require) { |
191 | case 'year': | 192 | case 'year': |
192 | this.rangeStart = options.start; | 193 | this.rangeStart = options.start; |
193 | this.rangeEnd = options.end; | 194 | this.rangeEnd = options.end; |
194 | this.rangeGap = '+' + (options.gap ? options.gap : 1) + 'YEAR'; | 195 | this.rangeGap = options.gap; |
195 | break; | 196 | break; |
196 | case 'range': | 197 | case 'range': |
197 | this.rangeStart = options.start; | 198 | this.rangeStart = options.start; | ... | ... |
... | @@ -26,13 +26,6 @@ define(function(require) { | ... | @@ -26,13 +26,6 @@ define(function(require) { |
26 | this.builders.push(builder); | 26 | this.builders.push(builder); |
27 | return builder; | 27 | return builder; |
28 | }, | 28 | }, |
29 | next: function next() { | ||
30 | var next = this.createNextHandler(); | ||
31 | this.addBuilder(function() { | ||
32 | next.build(); | ||
33 | }); | ||
34 | return next; | ||
35 | }, | ||
36 | end: function end() { | 29 | end: function end() { |
37 | return this.parent; | 30 | return this.parent; |
38 | }, | 31 | }, |
... | @@ -53,14 +46,9 @@ define(function(require) { | ... | @@ -53,14 +46,9 @@ define(function(require) { |
53 | }, | 46 | }, |
54 | build: function build() { | 47 | build: function build() { |
55 | var self = this; | 48 | var self = this; |
56 | var description = this.description(); | 49 | describe(this.description(), function() { |
57 | if (description) { | ||
58 | describe(this.description(), function() { | ||
59 | BaseBuilder.__super__.build.call(self, build); | ||
60 | }); | ||
61 | } else { | ||
62 | BaseBuilder.__super__.build.call(self, build); | 50 | BaseBuilder.__super__.build.call(self, build); |
63 | } | 51 | }); |
64 | }, | 52 | }, |
65 | }); | 53 | }); |
66 | var FacetBaseBuilder = BaseBuilder.extend({ | 54 | var FacetBaseBuilder = BaseBuilder.extend({ |
... | @@ -72,49 +60,53 @@ define(function(require) { | ... | @@ -72,49 +60,53 @@ define(function(require) { |
72 | data | 60 | data |
73 | ); | 61 | ); |
74 | }, | 62 | }, |
75 | description: function() { | ||
76 | return null; | ||
77 | }, | ||
78 | facetBuilder: function facetBuilder() { | 63 | facetBuilder: function facetBuilder() { |
79 | return this.parent.facetBuilder(); | 64 | return this.parent.facetBuilder(); |
80 | }, | 65 | }, |
81 | createNextHandler: function createNextHandler() { | 66 | validateFormatting: function validateFormatting(wantedFormatting) { |
82 | return new FacetBaseBuilder(this); | 67 | this.addBuilder(function(builder) { |
68 | var f, items; | ||
69 | beforeEach(function() { | ||
70 | var facetBuilder = builder.facetBuilder(); | ||
71 | f = facetBuilder.get('f'); | ||
72 | items = facetBuilder.get('items'); | ||
73 | }); | ||
74 | it('formatting', function() { | ||
75 | _.each(wantedFormatting, function(wantedValue, key) { | ||
76 | var item = items.get(key); | ||
77 | expect(f.formatter(item)).toEqual(wantedValue); | ||
78 | }); | ||
79 | }); | ||
80 | }); | ||
81 | return this; | ||
83 | }, | 82 | }, |
84 | createExpects: function createExpects(all, orderedItems, optionalHasQuery) { | 83 | createExpects: function createExpects(all, orderedItems, optionalHasQuery) { |
85 | var wantedCheckedKeysLength = 0; | ||
86 | var wantedCheckedKeys = {}; | ||
87 | var wantedChecks = {}; | ||
88 | var wantedItemOrder = []; | ||
89 | optionalHasQuery = optionalHasQuery || {}; | 84 | optionalHasQuery = optionalHasQuery || {}; |
90 | function scanKey(key, wanted) { | ||
91 | wantedChecks[key] = wanted; | ||
92 | if (wanted) { | ||
93 | wantedCheckedKeys[key] = true; | ||
94 | wantedCheckedKeysLength++; | ||
95 | } | ||
96 | wantedItemOrder.push(key); | ||
97 | } | ||
98 | var facetBuilder = this.facetBuilder(); | ||
99 | var ascItemOrder = _.clone(facetBuilder.ascItemOrder); | ||
100 | if (facetBuilder.hasAfter) { | ||
101 | ascItemOrder.push('after'); | ||
102 | } else if (facetBuilder.hasAfter !== undefined) { | ||
103 | orderedItems.pop(); | ||
104 | } | ||
105 | if (facetBuilder.hasBefore) { | ||
106 | ascItemOrder.unshift('before'); | ||
107 | } else if (facetBuilder.hasBefore !== undefined) { | ||
108 | orderedItems.shift(); | ||
109 | } | ||
110 | for (var i = 0; i < orderedItems.length; i++) { | ||
111 | scanKey(ascItemOrder[i], orderedItems[i]); | ||
112 | } | ||
113 | var item2 = ascItemOrder[2]; | ||
114 | this.addBuilder(function(builder) { | 85 | this.addBuilder(function(builder) { |
115 | var f, items; | 86 | var f, items; |
87 | var wantedCheckedKeysLength = 0; | ||
88 | var wantedCheckedKeys = {}; | ||
89 | var wantedChecks = {}; | ||
90 | function scanKey(key, wanted) { | ||
91 | wantedChecks[key] = wanted; | ||
92 | if (wanted) { | ||
93 | wantedCheckedKeys[key] = true; | ||
94 | wantedCheckedKeysLength++; | ||
95 | } | ||
96 | } | ||
97 | var facetBuilder = builder.facetBuilder(); | ||
98 | var ascItemOrder = facetBuilder.getAscItemOrder(); | ||
99 | if (facetBuilder.hasAfter === null) { | ||
100 | orderedItems.pop(); | ||
101 | } | ||
102 | if (facetBuilder.hasBefore === null) { | ||
103 | orderedItems.shift(); | ||
104 | } | ||
105 | for (var i = 0; i < orderedItems.length; i++) { | ||
106 | scanKey(ascItemOrder[i], orderedItems[i]); | ||
107 | } | ||
108 | var item2 = ascItemOrder[2]; | ||
116 | beforeEach(function() { | 109 | beforeEach(function() { |
117 | var facetBuilder = builder.facetBuilder(); | ||
118 | f = facetBuilder.get('f'); | 110 | f = facetBuilder.get('f'); |
119 | items = facetBuilder.get('items'); | 111 | items = facetBuilder.get('items'); |
120 | }); | 112 | }); |
... | @@ -124,25 +116,11 @@ define(function(require) { | ... | @@ -124,25 +116,11 @@ define(function(require) { |
124 | 116 | ||
125 | it('items', function() { | 117 | it('items', function() { |
126 | var gotChecks = {}; | 118 | var gotChecks = {}; |
127 | var gotOrder = []; | ||
128 | items.each(function(item) { | 119 | items.each(function(item) { |
129 | var key = item.get('key'); | 120 | var key = item.get('key'); |
130 | gotChecks[key] = item.get('checked'); | 121 | gotChecks[key] = item.get('checked'); |
131 | gotOrder.push(key); | ||
132 | }); | 122 | }); |
133 | expect(gotChecks).toEqual(wantedChecks); | 123 | expect(gotChecks).toEqual(wantedChecks); |
134 | expect(gotOrder).toEqual(wantedItemOrder); | ||
135 | f.set('orderByDirection', 'desc'); | ||
136 | wantedItemOrder.reverse(); | ||
137 | gotOrder = []; | ||
138 | items.each(function(item) { | ||
139 | var key = item.get('key'); | ||
140 | gotOrder.push(key); | ||
141 | }); | ||
142 | expect(gotOrder).toEqual(wantedItemOrder); | ||
143 | }); | ||
144 | it('checkedKeys.length', function() { | ||
145 | expect(f.get('checkedKeys').length).toBe(wantedCheckedKeysLength); | ||
146 | }); | 124 | }); |
147 | it('checkedKeys', function() { | 125 | it('checkedKeys', function() { |
148 | var got = {}; | 126 | var got = {}; |
... | @@ -152,9 +130,9 @@ define(function(require) { | ... | @@ -152,9 +130,9 @@ define(function(require) { |
152 | expect(got).toEqual(wantedCheckedKeys); | 130 | expect(got).toEqual(wantedCheckedKeys); |
153 | }); | 131 | }); |
154 | it('remove not supported', function() { | 132 | it('remove not supported', function() { |
155 | expect(items.length).toBe(wantedItemOrder.length); | 133 | expect(items.length).toBe(ascItemOrder.length); |
156 | items.remove(item2); | 134 | items.remove(item2); |
157 | expect(items.length).toBe(wantedItemOrder.length); | 135 | expect(items.length).toBe(ascItemOrder.length); |
158 | }); | 136 | }); |
159 | if (optionalHasQuery.min) { | 137 | if (optionalHasQuery.min) { |
160 | if (optionalHasQuery.max) { | 138 | if (optionalHasQuery.max) { |
... | @@ -259,7 +237,31 @@ define(function(require) { | ... | @@ -259,7 +237,31 @@ define(function(require) { |
259 | expect(f).not.toBe(undefined); | 237 | expect(f).not.toBe(undefined); |
260 | expect(f).not.toBe(null); | 238 | expect(f).not.toBe(null); |
261 | }); | 239 | }); |
240 | it('order', function() { | ||
241 | var gotOrder = []; | ||
242 | var facetBuilder = builder.facetBuilder(); | ||
243 | var f = facetBuilder.get('f'); | ||
244 | var items = f.get('items'); | ||
245 | items.each(function(item) { | ||
246 | gotOrder.push(item.get('key')); | ||
247 | }); | ||
248 | var wantedItemOrder = _.clone(facetBuilder.getAscItemOrder()); | ||
249 | expect(gotOrder).toEqual(wantedItemOrder); | ||
250 | f.set('orderByDirection', 'desc'); | ||
251 | wantedItemOrder.reverse(); | ||
252 | gotOrder = []; | ||
253 | items.each(function(item) { | ||
254 | gotOrder.push(item.get('key')); | ||
255 | }); | ||
256 | expect(gotOrder).toEqual(wantedItemOrder); | ||
257 | }); | ||
262 | }); | 258 | }); |
259 | if (typeof nameOrBuilder === 'string') { | ||
260 | var facetInfo = SolrSpecData.extractFacetData(nameOrBuilder); | ||
261 | this.createPojoItems(facetInfo.items); | ||
262 | this.setAscItemOrder(facetInfo.order); | ||
263 | this.setBeforeAfter(facetInfo.beforeItem, facetInfo.afterItem); | ||
264 | } | ||
263 | }, | 265 | }, |
264 | facetBuilder: function facetBuilder() { | 266 | facetBuilder: function facetBuilder() { |
265 | return this; | 267 | return this; |
... | @@ -268,6 +270,16 @@ define(function(require) { | ... | @@ -268,6 +270,16 @@ define(function(require) { |
268 | this.ascItemOrder = ascItemOrder; | 270 | this.ascItemOrder = ascItemOrder; |
269 | return this; | 271 | return this; |
270 | }, | 272 | }, |
273 | getAscItemOrder: function getAscItemOrder() { | ||
274 | var ascItemOrder = _.clone(this.ascItemOrder); | ||
275 | if (this.hasAfter) { | ||
276 | ascItemOrder.push('after'); | ||
277 | } | ||
278 | if (this.hasBefore) { | ||
279 | ascItemOrder.unshift('before'); | ||
280 | } | ||
281 | return ascItemOrder; | ||
282 | }, | ||
271 | setBeforeAfter: function setBeforeAfter(hasBefore, hasAfter) { | 283 | setBeforeAfter: function setBeforeAfter(hasBefore, hasAfter) { |
272 | this.hasBefore = hasBefore; | 284 | this.hasBefore = hasBefore; |
273 | this.hasAfter = hasAfter; | 285 | this.hasAfter = hasAfter; |
... | @@ -277,14 +289,14 @@ define(function(require) { | ... | @@ -277,14 +289,14 @@ define(function(require) { |
277 | this.addBuilder(function(builder) { | 289 | this.addBuilder(function(builder) { |
278 | beforeEach(function() { | 290 | beforeEach(function() { |
279 | var newItems = []; | 291 | var newItems = []; |
280 | if (builder.hasBefore) { | 292 | if (builder.hasAfter) { |
281 | newItems.push(new Facet.Item(builder.hasBefore)); | 293 | newItems.push(new Facet.Item(builder.hasAfter)); |
282 | } | 294 | } |
283 | for (var i = 0; i < pojoItems.length; i++) { | 295 | for (var i = 0; i < pojoItems.length; i++) { |
284 | newItems.push(new Facet.Item(pojoItems[i])); | 296 | newItems.push(new Facet.Item(pojoItems[i])); |
285 | } | 297 | } |
286 | if (builder.hasAfter) { | 298 | if (builder.hasBefore) { |
287 | newItems.push(new Facet.Item(builder.hasAfter)); | 299 | newItems.push(new Facet.Item(builder.hasBefore)); |
288 | } | 300 | } |
289 | builder.get('items').set(newItems); | 301 | builder.get('items').set(newItems); |
290 | }); | 302 | }); |
... | @@ -300,13 +312,18 @@ define(function(require) { | ... | @@ -300,13 +312,18 @@ define(function(require) { |
300 | return new FacetBuilder(nameOrBuilder, description); | 312 | return new FacetBuilder(nameOrBuilder, description); |
301 | } | 313 | } |
302 | 314 | ||
315 | it('unsupported facet type', function() { | ||
316 | expect(function() { | ||
317 | new Facet(null, {facetType: 'foobar'}); | ||
318 | }).toThrow(new Error('Unsupported facet type: foobar')); | ||
319 | }); | ||
303 | setFacet('model') | 320 | setFacet('model') |
304 | .createPojoItems([ | 321 | .validateFormatting({ |
305 | {key: 'toyota', value: 5}, | 322 | 'toyota': 'Toyota', |
306 | {key: 'misc', value: 26}, | 323 | 'misc': 'Miscellaneous', |
307 | {key: 'ford', value: 23}, | 324 | 'ford': 'Ford', |
308 | {key: 'chevy', value: 18}, | 325 | 'chevy': 'chevy', |
309 | ]).setAscItemOrder(['chevy', 'ford', 'misc', 'toyota']) | 326 | }) |
310 | .createExpects(true, [false, false, false, false]) | 327 | .createExpects(true, [false, false, false, false]) |
311 | .setChecked('chevy', true) | 328 | .setChecked('chevy', true) |
312 | .createExpects(false, [true, false, false, false]) | 329 | .createExpects(false, [true, false, false, false]) |
... | @@ -332,16 +349,15 @@ define(function(require) { | ... | @@ -332,16 +349,15 @@ define(function(require) { |
332 | .endAll() | 349 | .endAll() |
333 | .build(); | 350 | .build(); |
334 | setFacet('value') | 351 | setFacet('value') |
335 | .createPojoItems([ | 352 | .validateFormatting({ |
336 | {key: '7890', value: 7}, | 353 | '1234': '$1234 - $2234', |
337 | {key: '6789', value: 6}, | 354 | '2345': '$2345 - $3345', |
338 | {key: '5678', value: 5}, | 355 | '3456': '$3456 - $4456', |
339 | {key: '4567', value: 4}, | 356 | '4567': '$4567 - $5567', |
340 | {key: '3456', value: 3}, | 357 | '5678': '$5678 - $6678', |
341 | {key: '2345', value: 2}, | 358 | '6789': '$6789 - $7789', |
342 | {key: '1234', value: 1}, | 359 | '7890': '$7890 - $8890', |
343 | ]) | 360 | }) |
344 | .setAscItemOrder(['1234', '2345', '3456', '4567', '5678', '6789', '7890']) | ||
345 | .setChecked('5678', true) | 361 | .setChecked('5678', true) |
346 | .createExpects(false, [false, false, false, false, true, false, false]) | 362 | .createExpects(false, [false, false, false, false, true, false, false]) |
347 | .setChecked('queryMin', '1234') | 363 | .setChecked('queryMin', '1234') |
... | @@ -367,43 +383,21 @@ define(function(require) { | ... | @@ -367,43 +383,21 @@ define(function(require) { |
367 | .build(); | 383 | .build(); |
368 | 384 | ||
369 | setFacet('year') | 385 | setFacet('year') |
370 | .createPojoItems([ | 386 | .validateFormatting({ |
371 | {key: '2010-01-01T00:00:00.000Z', value: 1}, | 387 | '2000-01-01T00:00:00.000Z': '2000', |
372 | {key: '2009-01-01T00:00:00.000Z', value: 0}, | 388 | '2004-01-01T00:00:00.000Z': '2004', |
373 | {key: '2008-01-01T00:00:00.000Z', value: 7}, | 389 | '2010-01-01T00:00:00.000Z': '2010', |
374 | {key: '2007-01-01T00:00:00.000Z', value: 0}, | 390 | }) |
375 | {key: '2006-01-01T00:00:00.000Z', value: 0}, | 391 | .setChecked('2004-01-01T00:00:00.000Z', true) |
376 | |||
377 | {key: '2005-01-01T00:00:00.000Z', value: 0}, | ||
378 | |||
379 | {key: '2004-01-01T00:00:00.000Z', value: 0}, | ||
380 | {key: '2003-01-01T00:00:00.000Z', value: 3}, | ||
381 | {key: '2002-01-01T00:00:00.000Z', value: 0}, | ||
382 | {key: '2001-01-01T00:00:00.000Z', value: 4}, | ||
383 | {key: '2000-01-01T00:00:00.000Z', value: 1}, | ||
384 | ]) | ||
385 | .setAscItemOrder([ | ||
386 | '2000-01-01T00:00:00.000Z', | ||
387 | '2001-01-01T00:00:00.000Z', | ||
388 | '2003-01-01T00:00:00.000Z', | ||
389 | '2008-01-01T00:00:00.000Z', | ||
390 | '2010-01-01T00:00:00.000Z', | ||
391 | ]) | ||
392 | .setChecked('2003-01-01T00:00:00.000Z', true) | ||
393 | .endAll() | 392 | .endAll() |
394 | .build(); | 393 | .build(); |
395 | setFacet('dateOfBirth') | 394 | setFacet('dateOfBirth') |
396 | .createPojoItems([ | 395 | .validateFormatting({ |
397 | {key: '1974-01-01T00:00:00.000Z', value: 1}, | 396 | 'before': 'Older', |
398 | {key: '1972-01-01T00:00:00.000Z', value: 1}, | 397 | '1970-01-01T00:00:00.000Z': '1970 - 1972', |
399 | {key: '1970-01-01T00:00:00.000Z', value: 1}, | 398 | '1972-01-01T00:00:00.000Z': '1972 - 1974', |
400 | ]) | 399 | '1974-01-01T00:00:00.000Z': '1974 - 1976', |
401 | .setAscItemOrder([ | 400 | }) |
402 | '1970-01-01T00:00:00.000Z', | ||
403 | '1972-01-01T00:00:00.000Z', | ||
404 | '1974-01-01T00:00:00.000Z', | ||
405 | ]) | ||
406 | .setBeforeAfter({key: 'before', value: 5}, undefined) | ||
407 | .setChecked('1972-01-01T00:00:00.000Z', true) | 401 | .setChecked('1972-01-01T00:00:00.000Z', true) |
408 | .createExpects(false, [false, false, true, false]) | 402 | .createExpects(false, [false, false, true, false]) |
409 | .setChecked('before', true) | 403 | .setChecked('before', true) |
... | @@ -418,6 +412,32 @@ define(function(require) { | ... | @@ -418,6 +412,32 @@ define(function(require) { |
418 | .endAll() | 412 | .endAll() |
419 | .build(); | 413 | .build(); |
420 | 414 | ||
415 | setFacet('bodyType') | ||
416 | .validateFormatting({ | ||
417 | 'aluminum': 'aluminum', | ||
418 | 'carbonComposite': 'carbonComposite', | ||
419 | 'steel': 'steel', | ||
420 | }) | ||
421 | .endAll() | ||
422 | .build(); | ||
423 | |||
424 | setFacet('category') | ||
425 | .validateFormatting({ | ||
426 | '10000:First Category': 'First Category', | ||
427 | '20000:Second Category': 'Second Category', | ||
428 | '30000:Third Category': 'Third Category', | ||
429 | }) | ||
430 | .endAll() | ||
431 | .build(); | ||
432 | |||
433 | setFacet('odometer') | ||
434 | .validateFormatting({ | ||
435 | '0': '0 - 5000', | ||
436 | '50000': '50000 - 55000', | ||
437 | '150000': '150000 - 155000', | ||
438 | }) | ||
439 | .endAll() | ||
440 | .build(); | ||
421 | 441 | ||
422 | function rangeSupportHelper(hasBefore, hasAfter) { | 442 | function rangeSupportHelper(hasBefore, hasAfter) { |
423 | var description = 'type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''); | 443 | var description = 'type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''); |
... | @@ -426,6 +446,16 @@ define(function(require) { | ... | @@ -426,6 +446,16 @@ define(function(require) { |
426 | queryField: 'dateOfBirth_dt', | 446 | queryField: 'dateOfBirth_dt', |
427 | facetType: 'year', | 447 | facetType: 'year', |
428 | facetStats: true, | 448 | facetStats: true, |
449 | formatter: function(item) { | ||
450 | var value = item.get('key'); | ||
451 | if (value === 'before') { | ||
452 | return 'Before'; | ||
453 | } | ||
454 | if (value === 'after') { | ||
455 | return 'Newer'; | ||
456 | } | ||
457 | return value; | ||
458 | }, | ||
429 | }; | 459 | }; |
430 | if (hasBefore) { | 460 | if (hasBefore) { |
431 | if (hasAfter) { | 461 | if (hasAfter) { |
... | @@ -438,7 +468,7 @@ define(function(require) { | ... | @@ -438,7 +468,7 @@ define(function(require) { |
438 | } | 468 | } |
439 | return new Facet(null, facetOptions); | 469 | return new Facet(null, facetOptions); |
440 | }, description) | 470 | }, description) |
441 | .createPojoItems([ | 471 | .createPojoItems([ |
442 | {key: '1974-01-01T00:00:00.000Z', value: 1}, | 472 | {key: '1974-01-01T00:00:00.000Z', value: 1}, |
443 | {key: '1972-01-01T00:00:00.000Z', value: 1}, | 473 | {key: '1972-01-01T00:00:00.000Z', value: 1}, |
444 | {key: '1970-01-01T00:00:00.000Z', value: 1}, | 474 | {key: '1970-01-01T00:00:00.000Z', value: 1}, |
... | @@ -449,11 +479,25 @@ define(function(require) { | ... | @@ -449,11 +479,25 @@ define(function(require) { |
449 | '1974-01-01T00:00:00.000Z', | 479 | '1974-01-01T00:00:00.000Z', |
450 | ]) | 480 | ]) |
451 | .setBeforeAfter(hasBefore, hasAfter) | 481 | .setBeforeAfter(hasBefore, hasAfter) |
482 | ; | ||
483 | var validFormatting = { | ||
484 | '1970-01-01T00:00:00.000Z': '1970-01-01T00:00:00.000Z', | ||
485 | '1972-01-01T00:00:00.000Z': '1972-01-01T00:00:00.000Z', | ||
486 | '1974-01-01T00:00:00.000Z': '1974-01-01T00:00:00.000Z', | ||
487 | }; | ||
488 | if (hasBefore) { | ||
489 | validFormatting.before = 'Before'; | ||
490 | } | ||
491 | if (hasAfter) { | ||
492 | validFormatting.after = 'Newer'; | ||
493 | } | ||
494 | builder.validateFormatting(validFormatting); | ||
495 | var l1 = builder | ||
452 | .setChecked('1972-01-01T00:00:00.000Z', true) | 496 | .setChecked('1972-01-01T00:00:00.000Z', true) |
453 | .createExpects(false, [false, false, true, false, false]) | 497 | .createExpects(false, [false, false, true, false, false]) |
454 | ; | 498 | ; |
455 | if (hasBefore) { | 499 | if (hasBefore) { |
456 | builder.setChecked('before', true) | 500 | l1.setChecked('before', true) |
457 | .createExpects(false, [true, false, true, false, false]) | 501 | .createExpects(false, [true, false, true, false, false]) |
458 | .setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z']) | 502 | .setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z']) |
459 | .createExpects(false, [true, false, false, true, false]) | 503 | .createExpects(false, [true, false, false, true, false]) |
... | @@ -462,7 +506,7 @@ define(function(require) { | ... | @@ -462,7 +506,7 @@ define(function(require) { |
462 | ; | 506 | ; |
463 | } | 507 | } |
464 | if (hasAfter) { | 508 | if (hasAfter) { |
465 | builder.setChecked('after', true) | 509 | l1.setChecked('after', true) |
466 | .createExpects(false, [false, false, true, false, true]) | 510 | .createExpects(false, [false, false, true, false, true]) |
467 | .setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z']) | 511 | .setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z']) |
468 | .createExpects(false, [false, false, false, true, true]) | 512 | .createExpects(false, [false, false, false, true, true]) | ... | ... |
... | @@ -174,6 +174,7 @@ define(function(require) { | ... | @@ -174,6 +174,7 @@ define(function(require) { |
174 | var queryField = facet.queryField; | 174 | var queryField = facet.queryField; |
175 | var facetField = facet.facetField ? facet.facetField : queryField; | 175 | var facetField = facet.facetField ? facet.facetField : queryField; |
176 | var type = facet.facetType; | 176 | var type = facet.facetType; |
177 | var solrType = type; | ||
177 | var valueFormatter; | 178 | var valueFormatter; |
178 | var facetOptions = {}; | 179 | var facetOptions = {}; |
179 | var quoteFormatter = function(value) { | 180 | var quoteFormatter = function(value) { |
... | @@ -184,7 +185,7 @@ define(function(require) { | ... | @@ -184,7 +185,7 @@ define(function(require) { |
184 | }; | 185 | }; |
185 | switch (type) { | 186 | switch (type) { |
186 | case 'year': | 187 | case 'year': |
187 | type = 'range'; | 188 | solrType = 'range'; |
188 | quoteFormatter = function(value) { | 189 | quoteFormatter = function(value) { |
189 | return value.toISOString(); | 190 | return value.toISOString(); |
190 | }; | 191 | }; |
... | @@ -192,11 +193,12 @@ define(function(require) { | ... | @@ -192,11 +193,12 @@ define(function(require) { |
192 | var key = item.get('key'); | 193 | var key = item.get('key'); |
193 | var fromDate, toDate; | 194 | var fromDate, toDate; |
194 | if (key === 'before') { | 195 | if (key === 'before') { |
195 | return '[* TO ' + facet.rangeStart + '-1SECOND]'; | 196 | return '[* TO ' + facet.rangeStart + '-1MILLISECOND]'; |
196 | } | 197 | } |
197 | fromDate = new Date(key); | 198 | fromDate = new Date(key); |
198 | toDate = new Date(key); | 199 | toDate = new Date(key); |
199 | toDate.setUTCFullYear(toDate.getUTCFullYear() + 1); | 200 | toDate.setUTCFullYear(toDate.getUTCFullYear() + facet.rangeGap); |
201 | toDate.setTime(toDate.getTime() - 1); | ||
200 | return rangeFormatter(fromDate, toDate); | 202 | return rangeFormatter(fromDate, toDate); |
201 | }; | 203 | }; |
202 | if (facet.other) { | 204 | if (facet.other) { |
... | @@ -211,7 +213,7 @@ define(function(require) { | ... | @@ -211,7 +213,7 @@ define(function(require) { |
211 | }; | 213 | }; |
212 | break; | 214 | break; |
213 | case 'year-field': | 215 | case 'year-field': |
214 | type = 'field'; | 216 | solrType = 'field'; |
215 | quoteFormatter = function(value) { | 217 | quoteFormatter = function(value) { |
216 | return new Date(value).toISOString(); | 218 | return new Date(value).toISOString(); |
217 | }; | 219 | }; |
... | @@ -238,6 +240,11 @@ define(function(require) { | ... | @@ -238,6 +240,11 @@ define(function(require) { |
238 | break; | 240 | break; |
239 | } | 241 | } |
240 | switch (type) { | 242 | switch (type) { |
243 | case 'year': | ||
244 | facetOptions['facet.range.start'] = facet.rangeStart; | ||
245 | facetOptions['facet.range.end'] = facet.rangeEnd; | ||
246 | facetOptions['facet.range.gap'] = '+' + facet.rangeGap + 'YEAR'; | ||
247 | break; | ||
241 | case 'range': | 248 | case 'range': |
242 | facetOptions['facet.range.start'] = facet.rangeStart; | 249 | facetOptions['facet.range.start'] = facet.rangeStart; |
243 | facetOptions['facet.range.end'] = facet.rangeEnd; | 250 | facetOptions['facet.range.end'] = facet.rangeEnd; |
... | @@ -279,7 +286,7 @@ define(function(require) { | ... | @@ -279,7 +286,7 @@ define(function(require) { |
279 | facetOptionList.push(key + '=' + value); | 286 | facetOptionList.push(key + '=' + value); |
280 | } | 287 | } |
281 | }); | 288 | }); |
282 | result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField); | 289 | result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField); |
283 | if (facet.facetStats) { | 290 | if (facet.facetStats) { |
284 | result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField); | 291 | result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField); |
285 | } | 292 | } |
... | @@ -292,7 +299,7 @@ define(function(require) { | ... | @@ -292,7 +299,7 @@ define(function(require) { |
292 | facetOptionList.push(key + '=' + value); | 299 | facetOptionList.push(key + '=' + value); |
293 | } | 300 | } |
294 | }); | 301 | }); |
295 | result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField); | 302 | result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField); |
296 | } | 303 | } |
297 | }, this)); | 304 | }, this)); |
298 | return result; | 305 | return result; | ... | ... |
... | @@ -46,6 +46,8 @@ define(function(require) { | ... | @@ -46,6 +46,8 @@ define(function(require) { |
46 | var yearFacet, yearItems; | 46 | var yearFacet, yearItems; |
47 | var dobFacet, dobItems; | 47 | var dobFacet, dobItems; |
48 | var valueFacet, valueItems; | 48 | var valueFacet, valueItems; |
49 | var categoryFacet, categoryItems; | ||
50 | var odometerFacet, odometerItems; | ||
49 | beforeEach(function() { | 51 | beforeEach(function() { |
50 | f.applyFacetResults(SolrSpecData.responses.sequential[0]); | 52 | f.applyFacetResults(SolrSpecData.responses.sequential[0]); |
51 | modelFacet = f.get('model'); | 53 | modelFacet = f.get('model'); |
... | @@ -56,15 +58,17 @@ define(function(require) { | ... | @@ -56,15 +58,17 @@ define(function(require) { |
56 | dobItems = dobFacet.get('items'); | 58 | dobItems = dobFacet.get('items'); |
57 | valueFacet = f.get('value'); | 59 | valueFacet = f.get('value'); |
58 | valueItems = valueFacet.get('items'); | 60 | valueItems = valueFacet.get('items'); |
61 | categoryFacet = f.get('category'); | ||
62 | categoryItems = categoryFacet.get('items'); | ||
63 | odometerFacet = f.get('odometer'); | ||
64 | odometerItems = odometerFacet.get('items'); | ||
59 | }); | 65 | }); |
60 | it('initial', function() { | 66 | it('initial', function() { |
61 | //console.log(JSON.stringify(yearItems)); | 67 | //console.log(JSON.stringify(yearItems)); |
62 | expect(yearItems.length).toBe(5); | 68 | expect(yearItems.length).toBe(3); |
63 | SolrSpecData.expectItem(yearItems.at(0), '2010-01-01T00:00:00.000Z', 1, false); | 69 | SolrSpecData.expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false); |
64 | SolrSpecData.expectItem(yearItems.at(1), '2008-01-01T00:00:00.000Z', 7, false); | 70 | SolrSpecData.expectItem(yearItems.at(1), '2004-01-01T00:00:00.000Z', 3, false); |
65 | SolrSpecData.expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false); | 71 | SolrSpecData.expectItem(yearItems.at(2), '2010-01-01T00:00:00.000Z', 1, false); |
66 | SolrSpecData.expectItem(yearItems.at(3), '2001-01-01T00:00:00.000Z', 4, false); | ||
67 | SolrSpecData.expectItem(yearItems.at(4), '2000-01-01T00:00:00.000Z', 1, false); | ||
68 | expect(modelItems.length).toBe(4); | 72 | expect(modelItems.length).toBe(4); |
69 | SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false); | 73 | SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false); |
70 | SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false); | 74 | SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false); |
... | @@ -75,10 +79,22 @@ define(function(require) { | ... | @@ -75,10 +79,22 @@ define(function(require) { |
75 | SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false); | 79 | SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false); |
76 | SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false); | 80 | SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false); |
77 | SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false); | 81 | SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false); |
78 | expect(valueItems.length).toBe(3); | 82 | expect(valueItems.length).toBe(7); |
79 | SolrSpecData.expectItem(valueItems.at(0), '1234', 3, false); | 83 | SolrSpecData.expectItem(valueItems.at(0), '1234', 1, false); |
80 | SolrSpecData.expectItem(valueItems.at(1), '5678', 2, false); | 84 | SolrSpecData.expectItem(valueItems.at(1), '2345', 2, false); |
81 | SolrSpecData.expectItem(valueItems.at(2), '23456', 1, false); | 85 | SolrSpecData.expectItem(valueItems.at(2), '3456', 3, false); |
86 | SolrSpecData.expectItem(valueItems.at(3), '4567', 4, false); | ||
87 | SolrSpecData.expectItem(valueItems.at(4), '5678', 5, false); | ||
88 | SolrSpecData.expectItem(valueItems.at(5), '6789', 6, false); | ||
89 | SolrSpecData.expectItem(valueItems.at(6), '7890', 7, false); | ||
90 | expect(categoryItems.length).toBe(3); | ||
91 | SolrSpecData.expectItem(categoryItems.at(0), '10000:First Category', 5, false); | ||
92 | SolrSpecData.expectItem(categoryItems.at(1), '20000:Second Category', 42, false); | ||
93 | SolrSpecData.expectItem(categoryItems.at(2), '30000:Third Category', 75, false); | ||
94 | expect(odometerItems.length).toBe(3); | ||
95 | SolrSpecData.expectItem(odometerItems.at(0), '0', 27, false); | ||
96 | SolrSpecData.expectItem(odometerItems.at(1), '50000', 73, false); | ||
97 | SolrSpecData.expectItem(odometerItems.at(2), '150000', 96, false); | ||
82 | expect(Backbone.ajax).not.toHaveBeenCalled(); | 98 | expect(Backbone.ajax).not.toHaveBeenCalled(); |
83 | expect(onItemChange).not.toHaveBeenCalled(); | 99 | expect(onItemChange).not.toHaveBeenCalled(); |
84 | }); | 100 | }); |
... | @@ -86,26 +102,53 @@ define(function(require) { | ... | @@ -86,26 +102,53 @@ define(function(require) { |
86 | beforeEach(function() { | 102 | beforeEach(function() { |
87 | modelItems.get('ford').set('checked', true); | 103 | modelItems.get('ford').set('checked', true); |
88 | modelItems.get('toyota').set('checked', true); | 104 | modelItems.get('toyota').set('checked', true); |
89 | yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true); | 105 | yearItems.get('2004-01-01T00:00:00.000Z').set('checked', true); |
90 | dobItems.get('before').set('checked', true); | 106 | dobItems.get('before').set('checked', true); |
107 | dobItems.get('1972-01-01T00:00:00.000Z').set('checked', true); | ||
91 | valueFacet.set('queryMin', 1234); | 108 | valueFacet.set('queryMin', 1234); |
92 | valueFacet.set('queryMax', 6000); | 109 | valueFacet.set('queryMax', 6000); |
110 | categoryItems.get('20000:Second Category').set('checked', true); | ||
111 | odometerItems.get('50000').set('checked', true); | ||
93 | }); | 112 | }); |
94 | it('result', function() { | 113 | it('result', function() { |
95 | expect(Backbone.ajax).not.toHaveBeenCalled(); | 114 | expect(Backbone.ajax).not.toHaveBeenCalled(); |
96 | expect(onItemChange).toHaveBeenCalled(); | 115 | expect(onItemChange).toHaveBeenCalled(); |
97 | expect(onItemChange.calls.count()).toEqual(6); | 116 | expect(onItemChange.calls.count()).toEqual(9); |
98 | console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1)); | 117 | console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1)); |
99 | var facetFormData = f.getFacetFormData(); | 118 | var facetFormData = f.getFacetFormData(); |
100 | console.log(JSON.stringify(facetFormData, null, 1)); | 119 | console.log(JSON.stringify(facetFormData, null, 1)); |
101 | expect(facetFormData.facet).toBe(true); | 120 | expect(facetFormData.facet).toBe(true); |
102 | expect(facetFormData.stats).toBe(true); | 121 | expect(facetFormData.stats).toBe(true); |
103 | expect(facetFormData['facet.missing']).toBe(true); | 122 | expect(facetFormData['facet.missing']).toBe(true); |
123 | expect(facetFormData['facet.field']).toEqual([ | ||
124 | '{!key=model ex=model}model_s', | ||
125 | '{!key=year ex=year}year_dt', | ||
126 | '{!key=value ex=value}value_d', | ||
127 | '{!key=bodyType tag=bodyType}bodyType_s', | ||
128 | '{!key=category ex=category}category_and_name_s', | ||
129 | ]); | ||
130 | expect(facetFormData['facet.range']).toEqual([ | ||
131 | '{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt', | ||
132 | '{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i', | ||
133 | ]); | ||
134 | expect(facetFormData['facet.query']).toEqual([ | ||
135 | '{!key=model:misc tag=model:misc}(model_s:saturn OR model_s:jeep)', | ||
136 | '{!key=model:startsWithB tag=model:startsWithB}(model_s:buick)', | ||
137 | '{!key=year:ancient tag=year:ancient}(year_dt:\'1950-01-01T00:00:00.000Z\' OR year_dt:\'1952-01-01T00:00:00.000Z\')', | ||
138 | ]); | ||
139 | expect(facetFormData['stats.field']).toEqual([ | ||
140 | '{!key=year ex=year}year_dt', | ||
141 | '{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt', | ||
142 | '{!key=value ex=value}value_d', | ||
143 | '{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i', | ||
144 | ]); | ||
104 | expect(facetFormData.fq).toEqual([ | 145 | expect(facetFormData.fq).toEqual([ |
105 | '{!tag=model}model_s:("ford" OR "toyota")', | 146 | '{!tag=model}model_s:("ford" OR "toyota")', |
106 | '{!tag=year}year_dt:(2003-01-01T00:00:00.000Z)', | 147 | '{!tag=year}year_dt:(2004-01-01T00:00:00.000Z)', |
107 | '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1SECOND])', | 148 | '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1MILLISECOND] OR [1972-01-01T00:00:00.000Z TO 1973-12-31T23:59:59.999Z])', |
108 | '{!tag=value}value_d:([1234 TO 6000])', | 149 | '{!tag=value}value_d:([1234 TO 6000])', |
150 | '{!tag=category}category_id_s:(20000)', | ||
151 | '{!tag=odometer}odometer_i:([50000 TO 55000])', | ||
109 | ]); | 152 | ]); |
110 | }); | 153 | }); |
111 | }); | 154 | }); | ... | ... |
... | @@ -5,6 +5,7 @@ define(function(require) { | ... | @@ -5,6 +5,7 @@ define(function(require) { |
5 | var Facet = require('./Facet'); | 5 | var Facet = require('./Facet'); |
6 | var Facets = require('./Facets'); | 6 | var Facets = require('./Facets'); |
7 | var Solr = require('./Solr'); | 7 | var Solr = require('./Solr'); |
8 | var Sort = require('./Sort'); | ||
8 | 9 | ||
9 | function createSolrResponse(has) { | 10 | function createSolrResponse(has) { |
10 | var solrData = {}; | 11 | var solrData = {}; |
... | @@ -49,63 +50,172 @@ define(function(require) { | ... | @@ -49,63 +50,172 @@ define(function(require) { |
49 | expect(item.get('checked')).toEqual(checked); | 50 | expect(item.get('checked')).toEqual(checked); |
50 | } | 51 | } |
51 | 52 | ||
53 | function categorySplitter(item, handler) { | ||
54 | var key = item.get('key'); | ||
55 | if (!key) { | ||
56 | return key; | ||
57 | } | ||
58 | var parts = key.split(':', 2); | ||
59 | if (parts.length === 1) { | ||
60 | return key; | ||
61 | } else { | ||
62 | return handler(parts); | ||
63 | } | ||
64 | } | ||
65 | function categoryNameExtractor(item) { | ||
66 | return categorySplitter(item, function(parts) { | ||
67 | return parts[1]; | ||
68 | }); | ||
69 | } | ||
70 | function categoryIdExtractor(item) { | ||
71 | return categorySplitter(item, function(parts) { | ||
72 | return parts[0]; | ||
73 | }); | ||
74 | } | ||
75 | |||
52 | var definedFacets = { | 76 | var definedFacets = { |
53 | model: [ | 77 | model: { |
54 | { | 78 | facetData: { |
55 | formName: 'model', | 79 | formName: 'model', |
56 | label: 'Model', | 80 | label: 'Model', |
57 | }, { | 81 | }, |
82 | facetOptions: { | ||
58 | queryField: 'model_s', | 83 | queryField: 'model_s', |
59 | facetType: 'field', | 84 | facetType: 'field', |
60 | bins: { | 85 | bins: { |
61 | misc: ['saturn', 'jeep'], | 86 | misc: ['saturn', 'jeep'], |
62 | startsWithB: ['buick'], | 87 | startsWithB: ['buick'], |
63 | }, | 88 | }, |
89 | labelMap: { | ||
90 | FORD: 'Ford', | ||
91 | TOYOTA: 'Toyota', | ||
92 | MISC: 'Miscellaneous', | ||
93 | }, | ||
64 | }, | 94 | }, |
65 | ], | 95 | facetItems: [ |
66 | year: [ | 96 | {key: 'toyota', value: 5}, |
67 | { | 97 | {key: 'misc', value: 26}, |
98 | {key: 'ford', value: 23}, | ||
99 | {key: 'chevy', value: 18}, | ||
100 | ], | ||
101 | facetItemOrder: [ | ||
102 | 'chevy', | ||
103 | 'ford', | ||
104 | 'misc', | ||
105 | 'toyota', | ||
106 | ], | ||
107 | searchData: { | ||
108 | facetFields: [ | ||
109 | 'ford', 23, | ||
110 | 'chevy', 18, | ||
111 | 'toyota', 5, | ||
112 | 'saturn', 7, // keep here to test the bin/exclude feature | ||
113 | 'jeep', 19, // keep here to test the bin/exclude feature | ||
114 | 'buick', 0, // keep here to test the bin/exclude feature | ||
115 | 'dodge', 0, | ||
116 | ], | ||
117 | facetQueries: { | ||
118 | 'model:misc': 26, | ||
119 | 'model:startsWithB': 0, | ||
120 | }, | ||
121 | }, | ||
122 | }, | ||
123 | year: { | ||
124 | facetData: { | ||
68 | formName: 'year', | 125 | formName: 'year', |
69 | orderByDirection: 'desc', | ||
70 | label: 'Year', | 126 | label: 'Year', |
71 | }, { | 127 | }, |
128 | facetOptions: { | ||
72 | queryField: 'year_dt', | 129 | queryField: 'year_dt', |
73 | comparator: 'date', | 130 | comparator: 'date', |
74 | facetType: 'year-field', | 131 | facetType: 'year-field', |
75 | facetStats: true, | 132 | facetStats: true, |
133 | gap: 2, | ||
76 | formatter: function(item) { | 134 | formatter: function(item) { |
77 | var value = item.get('key'); | 135 | return '' + new Date(item.get('key')).getUTCFullYear(); |
78 | if (value === 'before') { | 136 | }, |
79 | return 'Older'; | 137 | bins: { |
80 | } | 138 | ancient: ['1950-01-01T00:00:00.000Z', '1952-01-01T00:00:00.000Z'], |
81 | if (value === 'after') { | ||
82 | return 'Newer'; | ||
83 | } | ||
84 | var year = new Date(value).getUTCFullYear(); | ||
85 | return year + ' - ' + (year + 2); | ||
86 | }, | 139 | }, |
87 | }, | 140 | }, |
88 | ], | 141 | facetItems: [ |
89 | dateOfBirth: [ | 142 | {key: '2010-01-01T00:00:00.000Z', value: 1}, |
90 | { | 143 | {key: '2004-01-01T00:00:00.000Z', value: 3}, |
144 | {key: '2000-01-01T00:00:00.000Z', value: 1}, | ||
145 | ], | ||
146 | facetItemOrder: [ | ||
147 | '2000-01-01T00:00:00.000Z', | ||
148 | '2004-01-01T00:00:00.000Z', | ||
149 | '2010-01-01T00:00:00.000Z', | ||
150 | ], | ||
151 | searchData: { | ||
152 | facetFields: [ | ||
153 | '2000-01-01T00:00:00.000Z', 1, | ||
154 | '2002-01-01T00:00:00.000Z', 0, | ||
155 | '2004-01-01T00:00:00.000Z', 3, | ||
156 | '2006-01-01T00:00:00.000Z', 0, | ||
157 | '2008-01-01T00:00:00.000Z', 0, | ||
158 | '2010-01-01T00:00:00.000Z', 1, | ||
159 | ], | ||
160 | facetQueries: { | ||
161 | 'year:ancient': 0, | ||
162 | }, | ||
163 | statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'}, | ||
164 | }, | ||
165 | }, | ||
166 | dateOfBirth: { | ||
167 | facetData: { | ||
91 | formName: 'dateOfBirth', | 168 | formName: 'dateOfBirth', |
92 | label: 'Birthday', | 169 | label: 'Birthday', |
93 | }, { | 170 | }, |
171 | facetOptions: { | ||
94 | queryField: 'dateOfBirth_dt', | 172 | queryField: 'dateOfBirth_dt', |
95 | comparator: 'date', | 173 | comparator: 'date', |
96 | facetType: 'year', | 174 | facetType: 'year', |
97 | facetStats: true, | 175 | facetStats: true, |
98 | other: 'before', | 176 | other: 'before', |
99 | start: 'NOW/YEAR-25YEAR', | 177 | start: 'NOW/YEAR-25YEAR', |
178 | gap: 2, | ||
179 | formatter: function(item) { | ||
180 | var value = item.get('key'); | ||
181 | if (value === 'before') { | ||
182 | return 'Older'; | ||
183 | } | ||
184 | var year = new Date(value).getUTCFullYear(); | ||
185 | return year + ' - ' + (year + 2); | ||
186 | }, | ||
100 | }, | 187 | }, |
101 | ], | 188 | facetItems: [ |
102 | value: [ | 189 | {key: '1974-01-01T00:00:00.000Z', value: 1}, |
103 | { | 190 | {key: '1972-01-01T00:00:00.000Z', value: 1}, |
191 | {key: '1970-01-01T00:00:00.000Z', value: 1}, | ||
192 | ], | ||
193 | facetItemOrder: [ | ||
194 | '1970-01-01T00:00:00.000Z', | ||
195 | '1972-01-01T00:00:00.000Z', | ||
196 | '1974-01-01T00:00:00.000Z', | ||
197 | ], | ||
198 | facetItemBefore: {key: 'before', value: 5}, | ||
199 | searchData: { | ||
200 | facetRanges: { | ||
201 | counts: [ | ||
202 | '1974-01-01T00:00:00.000Z', 1, | ||
203 | '1972-01-01T00:00:00.000Z', 1, | ||
204 | '1970-01-01T00:00:00.000Z', 1, | ||
205 | ], | ||
206 | before: 5, | ||
207 | }, | ||
208 | statsFields: {min: '2000', max: '2010'}, | ||
209 | }, | ||
210 | }, | ||
211 | value: { | ||
212 | facetData: { | ||
104 | formName: 'value', | 213 | formName: 'value', |
105 | label: 'Value', | 214 | label: 'Value', |
106 | }, { | 215 | }, |
216 | facetOptions: { | ||
107 | queryField: 'value_d', | 217 | queryField: 'value_d', |
108 | comparator: 'number', | 218 | comparator: Sort.number, |
109 | facetType: 'field', | 219 | facetType: 'field', |
110 | facetStats: true, | 220 | facetStats: true, |
111 | formatter: function(item) { | 221 | formatter: function(item) { |
... | @@ -113,42 +223,175 @@ define(function(require) { | ... | @@ -113,42 +223,175 @@ define(function(require) { |
113 | return '$' + value + ' - $' + (value + 1000); | 223 | return '$' + value + ' - $' + (value + 1000); |
114 | }, | 224 | }, |
115 | }, | 225 | }, |
116 | ], | 226 | facetItems: [ |
117 | bodyType: [ | 227 | {key: '7890', value: 7}, |
118 | { | 228 | {key: '6789', value: 6}, |
229 | {key: '5678', value: 5}, | ||
230 | {key: '4567', value: 4}, | ||
231 | {key: '3456', value: 3}, | ||
232 | {key: '2345', value: 2}, | ||
233 | {key: '1234', value: 1}, | ||
234 | ], | ||
235 | facetItemOrder: [ | ||
236 | '1234', | ||
237 | '2345', | ||
238 | '3456', | ||
239 | '4567', | ||
240 | '5678', | ||
241 | '6789', | ||
242 | '7890', | ||
243 | ], | ||
244 | searchData: { | ||
245 | facetFields: [ | ||
246 | '7890', 7, | ||
247 | '6789', 6, | ||
248 | '5678', 5, | ||
249 | '4567', 4, | ||
250 | '3456', 3, | ||
251 | '2345', 2, | ||
252 | '1234', 1, | ||
253 | ], | ||
254 | statsFields: {min: '1234', max: '7890'}, | ||
255 | }, | ||
256 | }, | ||
257 | bodyType: { | ||
258 | facetData: { | ||
119 | label: 'Type' | 259 | label: 'Type' |
120 | }, { | 260 | }, |
261 | facetOptions: { | ||
262 | queryField: 'bodyType_s', | ||
121 | facetType: 'field', | 263 | facetType: 'field', |
122 | labelMap: { | 264 | _labelMap: { |
123 | aluminum: 'Aluminum', | 265 | aluminum: 'Aluminum', |
124 | plastic: 'Plastic', | 266 | plastic: 'Plastic', |
125 | carbonComposite: 'Carbon Composite', | 267 | carbonComposite: 'Carbon Composite', |
126 | steel: 'Steel', | 268 | steel: 'Steel', |
127 | }, | 269 | }, |
128 | }, | 270 | }, |
129 | ], | 271 | facetItems: [ |
272 | {key: 'steel', value: 2}, | ||
273 | {key: 'carbonComposite', value: 66}, | ||
274 | {key: 'aluminum', value: 14}, | ||
275 | ], | ||
276 | facetItemOrder: [ | ||
277 | 'aluminum', | ||
278 | 'carbonComposite', | ||
279 | 'steel', | ||
280 | ], | ||
281 | searchData: { | ||
282 | facetFields: [ | ||
283 | 'aluminum', 14, | ||
284 | 'carbonComposite', 66, | ||
285 | 'steel', 2, | ||
286 | ], | ||
287 | }, | ||
288 | }, | ||
289 | category: { | ||
290 | facetData: { | ||
291 | formName: 'category', | ||
292 | label: 'Category', | ||
293 | }, | ||
294 | facetOptions: { | ||
295 | queryField: 'category_and_name_s', | ||
296 | facetField: 'category_id_s', | ||
297 | facetType: 'field', | ||
298 | formatter: categoryNameExtractor, | ||
299 | queryValue: categoryIdExtractor, | ||
300 | sortKeyExtractor: categoryNameExtractor, | ||
301 | }, | ||
302 | facetItems: [ | ||
303 | {key: '30000:Third Category', value: 75}, | ||
304 | {key: '20000:Second Category', value: 42}, | ||
305 | {key: '10000:First Category', value: 5}, | ||
306 | ], | ||
307 | facetItemOrder: [ | ||
308 | '10000:First Category', | ||
309 | '20000:Second Category', | ||
310 | '30000:Third Category', | ||
311 | ], | ||
312 | searchData: { | ||
313 | facetFields: [ | ||
314 | '10000:First Category', 5, | ||
315 | '20000:Second Category', 42, | ||
316 | '30000:Third Category', 75, | ||
317 | ], | ||
318 | }, | ||
319 | }, | ||
320 | odometer: { | ||
321 | facetData: { | ||
322 | label: 'Type' | ||
323 | }, | ||
324 | facetOptions: { | ||
325 | queryField: 'odometer_i', | ||
326 | comparator: 'number', | ||
327 | facetType: 'range', | ||
328 | facetStats: true, | ||
329 | gap: 5000, | ||
330 | formatter: function(item) { | ||
331 | var value = parseInt(item.get('key')); | ||
332 | return value + ' - ' + (value + 5000); | ||
333 | }, | ||
334 | }, | ||
335 | facetItems: [ | ||
336 | {key: '150000', value: 96}, | ||
337 | {key: '50000', value: 73}, | ||
338 | {key: '0', value: 27}, | ||
339 | ], | ||
340 | facetItemOrder: [ | ||
341 | '0', | ||
342 | '50000', | ||
343 | '150000', | ||
344 | ], | ||
345 | searchData: { | ||
346 | facetRanges: { | ||
347 | counts: [ | ||
348 | '0', 27, | ||
349 | '50000', 73, | ||
350 | '150000', 96, | ||
351 | ], | ||
352 | }, | ||
353 | statsFields: {min: '0', max: '154123'}, | ||
354 | }, | ||
355 | }, | ||
130 | }; | 356 | }; |
131 | var facetsBuilder = function facetsBuilder(facetName) { | 357 | |
132 | if (facetName) { | 358 | function facetsBuilder(wantedFacetName) { |
133 | return new Facet(definedFacets[facetName][0], definedFacets[facetName][1]); | 359 | if (wantedFacetName) { |
360 | return new Facet(definedFacets[wantedFacetName].facetData, definedFacets[wantedFacetName].facetOptions); | ||
134 | } | 361 | } |
135 | var result = {}; | 362 | var result = {}; |
136 | _.each(_.keys(definedFacets), function(facetName) { | 363 | _.each(_.keys(definedFacets), function(facetName) { |
137 | result[facetName] = new Facet(definedFacets[facetName][0], definedFacets[facetName][1]); | 364 | result[facetName] = new Facet(definedFacets[facetName].facetData, definedFacets[facetName].facetOptions); |
138 | }); | 365 | }); |
139 | return result; | 366 | return result; |
140 | }; | 367 | } |
141 | 368 | ||
142 | function createSolrDataMerger(modelData) { | 369 | function extractFacetData(wantedFacetName) { |
370 | var facetInfo = definedFacets[wantedFacetName]; | ||
371 | return { | ||
372 | items: facetInfo.facetItems, | ||
373 | order: facetInfo.facetItemOrder, | ||
374 | beforeItem: facetInfo.facetItemBefore, | ||
375 | afterItem: facetInfo.facetItemAfter, | ||
376 | }; | ||
377 | } | ||
378 | |||
379 | function createSolrDataMerger() { | ||
380 | var wantedFacetNames; | ||
381 | if (arguments.length > 0) { | ||
382 | wantedFacetNames = arguments; | ||
383 | } else { | ||
384 | wantedFacetNames = _.keys(definedFacets); | ||
385 | } | ||
143 | return function(solrData) { | 386 | return function(solrData) { |
144 | _.each(solrData, function(solrValue, solrKey) { | 387 | _.each(solrData, function(solrValue, solrKey) { |
145 | _.each(modelData, function(modelObj, modelKey) { | 388 | _.each(_.pick(definedFacets, wantedFacetNames), function(facetObj, facetName) { |
146 | var modelValue = modelObj[solrKey]; | 389 | var modelValue = facetObj.searchData[solrKey]; |
147 | if (modelValue) { | 390 | if (modelValue) { |
148 | if (solrKey === 'facetQueries') { | 391 | if (solrKey === 'facetQueries') { |
149 | _.extend(solrValue, modelValue); | 392 | _.extend(solrValue, modelValue); |
150 | } else { | 393 | } else { |
151 | solrValue[modelKey] = modelValue; | 394 | solrValue[facetName] = modelValue; |
152 | } | 395 | } |
153 | } | 396 | } |
154 | }); | 397 | }); |
... | @@ -156,69 +399,9 @@ define(function(require) { | ... | @@ -156,69 +399,9 @@ define(function(require) { |
156 | }; | 399 | }; |
157 | } | 400 | } |
158 | 401 | ||
159 | var defaultModelData = { | ||
160 | dateOfBirth: { | ||
161 | facetRanges: { | ||
162 | counts: [ | ||
163 | '1974-01-01T00:00:00.000Z', 1, | ||
164 | '1972-01-01T00:00:00.000Z', 1, | ||
165 | '1970-01-01T00:00:00.000Z', 1, | ||
166 | ], | ||
167 | before: 5, | ||
168 | }, | ||
169 | statsFields: {min: '2000', max: '2010'}, | ||
170 | }, | ||
171 | bodyType: { | ||
172 | facetFields: [ | ||
173 | 'aluminum', 14, | ||
174 | 'carbonComposite', 66, | ||
175 | 'steel', 2, | ||
176 | ], | ||
177 | }, | ||
178 | |||
179 | model: { | ||
180 | facetFields: [ | ||
181 | 'ford', 23, | ||
182 | 'chevy', 18, | ||
183 | 'toyota', 5, | ||
184 | 'saturn', 7, // keep here to test the bin/exclude feature | ||
185 | 'jeep', 19, // keep here to test the bin/exclude feature | ||
186 | 'buick', 0, // keep here to test the bin/exclude feature | ||
187 | 'dodge', 0, | ||
188 | ], | ||
189 | facetQueries: { | ||
190 | 'model:misc': 26, | ||
191 | 'model:startsWithB': 0, | ||
192 | }, | ||
193 | }, | ||
194 | value: { | ||
195 | facetFields: [ | ||
196 | '23456', 1, | ||
197 | '5678', 2, | ||
198 | '1234', 3, | ||
199 | ], | ||
200 | statsFields: {min: '1234', max: '23456'}, | ||
201 | }, | ||
202 | year: { | ||
203 | facetFields: [ | ||
204 | '2000-01-01T00:00:00.000Z', 1, | ||
205 | '2001-01-01T00:00:00.000Z', 4, | ||
206 | '2002-01-01T00:00:00.000Z', 0, | ||
207 | '2003-01-01T00:00:00.000Z', 3, | ||
208 | '2004-01-01T00:00:00.000Z', 0, | ||
209 | '2005-01-01T00:00:00.000Z', 0, | ||
210 | '2006-01-01T00:00:00.000Z', 0, | ||
211 | '2007-01-01T00:00:00.000Z', 0, | ||
212 | '2008-01-01T00:00:00.000Z', 7, | ||
213 | '2009-01-01T00:00:00.000Z', 0, | ||
214 | '2010-01-01T00:00:00.000Z', 1, | ||
215 | ], | ||
216 | statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'}, | ||
217 | }, | ||
218 | }; | ||
219 | var responses = { | 402 | var responses = { |
220 | sequential: [ | 403 | sequential: [ |
221 | createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)), | 404 | createStaticSolrResponse({stats: true}, createSolrDataMerger()), |
222 | ], | 405 | ], |
223 | suggestions: [ | 406 | suggestions: [ |
224 | createStaticSolrResponse({stats: true}, { | 407 | createStaticSolrResponse({stats: true}, { |
... | @@ -246,6 +429,8 @@ define(function(require) { | ... | @@ -246,6 +429,8 @@ define(function(require) { |
246 | 'year_dt', | 429 | 'year_dt', |
247 | 'value_d', | 430 | 'value_d', |
248 | 'dateOfBirth_dt', | 431 | 'dateOfBirth_dt', |
432 | 'category_id_s', | ||
433 | 'category_and_name_s', | ||
249 | ], | 434 | ], |
250 | facets: facetsBuilder(), | 435 | facets: facetsBuilder(), |
251 | }); | 436 | }); |
... | @@ -256,5 +441,6 @@ define(function(require) { | ... | @@ -256,5 +441,6 @@ define(function(require) { |
256 | expectItem: expectItem, | 441 | expectItem: expectItem, |
257 | facetsBuilder: facetsBuilder, | 442 | facetsBuilder: facetsBuilder, |
258 | responses: responses, | 443 | responses: responses, |
444 | extractFacetData: extractFacetData, | ||
259 | }; | 445 | }; |
260 | }); | 446 | }); | ... | ... |
-
Please register or sign in to post a comment