628b2310 by Adam Heath

Major rework, uses a builder pattern now.

1 parent 2b235279
......@@ -61,7 +61,6 @@ module.exports = function(grunt) {
var _ = require('lodash');
function genJasmineConfig(withCoverage, options) {
var template, templateOptions;
var src = [
'src/scripts/{,**/}*.js',
'!src/scripts/main.js',
......@@ -103,7 +102,7 @@ module.exports = function(grunt) {
}
}
url = parts.join('/');
if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) {
if (0) if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) {
url = './.grunt/grunt-contrib-jasmine/' + url;
} else if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.js$/)) {
url = './.grunt/grunt-contrib-jasmine/' + url;
......@@ -149,7 +148,7 @@ module.exports = function(grunt) {
template: template,
templateOptions: templateOptions,
},
}
};
return jasmineConfig;
}
......
......@@ -4,32 +4,89 @@ define(function(require) {
var Backbone = require('backbone');
var Facet = require('solr/model/Facet');
var Sort = require('solr/model/Sort');
var SolrSpecData = require('solr/model/Solr.spec.data');
describe('Facet', function() {
it('loads', function() {
expect(Facet).toBeDefined();
});
describe('items', function() {
var f, items, ascItemOrder;
function createPojoItems(pojoItems) {
var BaseHandler = Backbone.Model.extend({
constructor: function constructor(parent, data) {
Backbone.Model.call(this, data);
this.parent = parent;
this.builders = [];
this.addBuilder(function(builder) {
beforeEach(function() {
var newItems = [];
for (var i = 0; i < pojoItems.length; i++) {
var item = new Facet.Item(pojoItems[i]);
newItems.push(item);
builder.clear();
});
});
},
build: function build() {
_.each(this.builders, function(builder) {
builder(this);
}, this);
},
addBuilder: function addBuilder(builder) {
this.builders.push(builder);
return builder;
},
next: function next() {
var next = this.createNextHandler();
this.addBuilder(function() {
next.build();
});
return next;
},
end: function end() {
return this.parent;
},
endAll: function endAll() {
var ptr = this;
var end = ptr.end();
while (end) {
ptr = end;
end = ptr.end();
}
items.set(newItems);
return ptr;
},
});
var BaseBuilder = BaseHandler.extend({
type: 'BaseBuilder',
constructor: function constructor(parent, data) {
BaseHandler.call(this, parent, data);
},
build: function build() {
var self = this;
var description = this.description();
if (description) {
describe(this.description(), function() {
BaseBuilder.__super__.build.call(self, build);
});
} else {
BaseBuilder.__super__.build.call(self, build);
}
function createExpects(all, orderedItems) {
it('all', function() {
expect(f.get('all')).toBe(all);
});
},
});
var FacetBaseBuilder = BaseBuilder.extend({
type: 'FacetBaseBuilder',
constructor: function constructor(parent, data) {
BaseBuilder.call(
this,
parent,
data
);
},
description: function() {
return null;
},
facetBuilder: function facetBuilder() {
return this.parent.facetBuilder();
},
createNextHandler: function createNextHandler() {
return new FacetBaseBuilder(this);
},
createExpects: function createExpects(all, orderedItems, optionalHasQuery) {
var wantedCheckedKeysLength = 0;
var wantedCheckedKeys = {};
var wantedChecks = {};
var wantedItemOrder = [];
optionalHasQuery = optionalHasQuery || {};
function scanKey(key, wanted) {
wantedChecks[key] = wanted;
if (wanted) {
......@@ -38,9 +95,33 @@ define(function(require) {
}
wantedItemOrder.push(key);
}
var facetBuilder = this.facetBuilder();
var ascItemOrder = _.clone(facetBuilder.ascItemOrder);
if (facetBuilder.hasAfter) {
ascItemOrder.push('after');
} else if (facetBuilder.hasAfter !== undefined) {
orderedItems.pop();
}
if (facetBuilder.hasBefore) {
ascItemOrder.unshift('before');
} else if (facetBuilder.hasBefore !== undefined) {
orderedItems.shift();
}
for (var i = 0; i < orderedItems.length; i++) {
scanKey(ascItemOrder[i], orderedItems[i]);
}
var item2 = ascItemOrder[2];
this.addBuilder(function(builder) {
var f, items;
beforeEach(function() {
var facetBuilder = builder.facetBuilder();
f = facetBuilder.get('f');
items = facetBuilder.get('items');
});
it('all', function() {
expect(f.get('all')).toBe(all);
});
it('items', function() {
var gotChecks = {};
var gotOrder = [];
......@@ -70,177 +151,279 @@ define(function(require) {
});
expect(got).toEqual(wantedCheckedKeys);
});
var item2 = ascItemOrder[2];
it('remove not supported', function() {
expect(items.length).toBe(wantedItemOrder.length);
items.remove(item2);
expect(items.length).toBe(wantedItemOrder.length);
});
}
describe('type[field]', function() {
var facetOptions = {
queryField: 'value_d',
facetType: 'field',
facetStats: true,
};
var pojoItems = [
{key: 'e', value: 100},
{key: 'd', value: 200},
{key: 'c', value: 300},
{key: 'b', value: 400},
{key: 'a', value: 500},
];
ascItemOrder = ['a', 'b', 'c', 'd', 'e'];
var c0;
beforeEach(function() {
f = new Facet(null, facetOptions);
items = f.get('items');
c0 = f.get('checkedKeys');
});
createPojoItems(pojoItems);
createExpects(true, [false, false, false, false, false]);
describe('check c', function() {
var c1;
beforeEach(function() {
items.get('c').set('checked', true);
c1 = f.get('checkedKeys');
});
createExpects(false, [false, false, true, false, false]);
it('checkedKeys not same', function() {
expect(c1).not.toBe(c0);
});
describe('check d', function() {
var c2;
beforeEach(function() {
items.get('d').set('checked', true);
c2 = f.get('checkedKeys');
});
createExpects(false, [false, false, true, true, false]);
it('checkedKeys not same', function() {
expect(c2).not.toBe(c1);
expect(c2).not.toBe(c0);
});
describe('checkedKeys: [a, e]', function() {
var c3;
beforeEach(function() {
f.set('checkedKeys', ['a', 'e']);
c3 = f.get('checkedKeys');
});
createExpects(false, [true, false, false, false, true]);
it('checkedKeys not same', function() {
expect(c3).not.toBe(c2);
expect(c3).not.toBe(c1);
expect(c3).not.toBe(c0);
});
});
describe('uncheck c', function() {
var c3;
beforeEach(function() {
items.get('c').set('checked', false);
c3 = f.get('checkedKeys');
});
createExpects(false, [false, false, false, true, false]);
it('checkedKeys not same', function() {
expect(c3).not.toBe(c2);
expect(c3).not.toBe(c1);
expect(c3).not.toBe(c0);
});
});
describe('checkedKeys: [d, e]', function() {
var c3;
beforeEach(function() {
f.set('checkedKeys', ['d', 'e']);
c3 = f.get('checkedKeys');
});
createExpects(false, [false, false, false, true, true]);
it('checkedKeys not same', function() {
expect(c3).not.toBe(c2);
expect(c3).not.toBe(c1);
expect(c3).not.toBe(c0);
if (optionalHasQuery.min) {
if (optionalHasQuery.max) {
it('has query:min+max', function() {
expect(f.get('hasQuery')).toBe(true);
expect(f.get('all')).toBe(false);
});
} else {
it('has query:min', function() {
expect(f.get('hasQuery')).toBe(false);
});
describe('checked all', function() {
var c3;
beforeEach(function() {
f.set('all', true);
c3 = f.get('checkedKeys');
}
} else if (optionalHasQuery.max) {
it('has query:max', function() {
expect(f.get('hasQuery')).toBe(false);
});
createExpects(true, [false, false, false, false, false]);
it('checkedKeys not same', function() {
expect(c3).not.toBe(c2);
expect(c3).not.toBe(c1);
expect(c3).not.toBe(c0);
}
});
describe('checkedKeys: [d, e]', function() {
var c4;
return this;
},
setChecked: function setChecked(itemName, checkedValue) {
var next = new FacetSetCheckedBuilder(this, itemName, checkedValue);
this.addBuilder(function() {
next.build();
});
return next;
},
});
var FacetSetCheckedBuilder = FacetBaseBuilder.extend({
type: 'FacetSetCheckedBuilder',
constructor: function constructor(parent, itemName, checkedValue) {
FacetBaseBuilder.call(
this,
parent,
{}
);
this.itemName = itemName;
this.checkedValue = checkedValue;
this.addBuilder(function(builder) {
var checkedKeysList;
beforeEach(function() {
f.set('checkedKeys', ['d', 'e']);
c4 = f.get('checkedKeys');
var f = builder.facetBuilder().get('f');
var items = f.get('items');
checkedKeysList = builder.facetBuilder().get('checkedKeysList');
switch (builder.itemName) {
case 'all':
case 'checkedKeys':
case 'queryMin':
case 'queryMax':
f.set(builder.itemName, builder.checkedValue);
break;
default:
items.get(builder.itemName).set('checked', builder.checkedValue);
}
checkedKeysList.push(f.get('checkedKeys'));
});
createExpects(false, [false, false, false, true, true]);
it('checkedKeys not same', function() {
expect(c4).not.toBe(c3);
expect(c4).not.toBe(c2);
expect(c4).not.toBe(c1);
expect(c4).not.toBe(c0);
});
});
});
});
for (var i = 0; i < checkedKeysList.length; i++) {
for (var j = 0; j < checkedKeysList.length; j++) {
if (i != j) {
expect(checkedKeysList[i]).not.toBe(checkedKeysList[j]);
}
}
}
});
});
describe('type[year]', function() {
var facetOptions = {
queryField: 'value_d',
facetType: 'year',
facetStats: true,
},
description: function description() {
return 'checked[' + this.itemName + ']=' + this.checkedValue;
},
});
var FacetBuilder = FacetBaseBuilder.extend({
type: 'FacetBuilder',
constructor: function constructor(nameOrBuilder, description) {
FacetBaseBuilder.call(
this,
null,
{}
);
if (description) {
description = 'facet[:' + description + ']';
} else {
description = 'facet[' + nameOrBuilder + ']';
}
this.description = function () {
return description;
};
var pojoItems = [
{key: '2005', value: 500},
{key: '2004', value: 400},
{key: '2003', value: 300},
{key: '2002', value: 200},
{key: '2001', value: 100},
];
ascItemOrder = ['2001', '2002', '2003', '2004', '2005'];
var c0;
this.nameOrBuilder = nameOrBuilder;
this.addBuilder(function(builder) {
var f;
beforeEach(function() {
f = new Facet(null, facetOptions);
items = f.get('items');
c0 = f.get('checkedKeys');
});
createPojoItems(pojoItems);
describe('query', function() {
describe('has', function() {
it('min', function() {
f.set({queryMin: 1000, queryMax: undefined});
expect(f.get('hasQuery')).toBe(false);
});
it('max', function() {
f.set({queryMin: undefined, queryMax: 5000});
expect(f.get('hasQuery')).toBe(false);
});
it('min+max', function() {
f.set({queryMin: 1000, queryMax: 5000});
expect(f.get('hasQuery')).toBe(true);
if (typeof builder.nameOrBuilder === 'string') {
f = SolrSpecData.facetsBuilder(builder.nameOrBuilder);
} else {
f = builder.nameOrBuilder();
}
builder.set('f', f);
builder.set('items', f.get('items'));
builder.set('checkedKeysList', [f.get('checkedKeys')]);
});
it('exists[' + name + ']', function() {
expect(f).not.toBe(undefined);
expect(f).not.toBe(null);
});
});
},
facetBuilder: function facetBuilder() {
return this;
},
setAscItemOrder: function setAscItemOrder(ascItemOrder) {
this.ascItemOrder = ascItemOrder;
return this;
},
setBeforeAfter: function setBeforeAfter(hasBefore, hasAfter) {
this.hasBefore = hasBefore;
this.hasAfter = hasAfter;
return this;
},
createPojoItems: function createPojoItems(pojoItems) {
this.addBuilder(function(builder) {
beforeEach(function() {
var newItems = [];
if (builder.hasBefore) {
newItems.push(new Facet.Item(builder.hasBefore));
}
for (var i = 0; i < pojoItems.length; i++) {
newItems.push(new Facet.Item(pojoItems[i]));
}
if (builder.hasAfter) {
newItems.push(new Facet.Item(builder.hasAfter));
}
builder.get('items').set(newItems);
});
});
return this;
},
});
createExpects(true, [false, false, false, false, false]);
describe('Facet', function() {
it('loads', function() {
expect(Facet).toBeDefined();
});
function rangeSupportHelper(hasBefore, hasAfter) {
function createBeforeAfterExpects(all, orderedItems) {
if (!hasBefore) {
orderedItems.shift();
function setFacet(nameOrBuilder, description) {
return new FacetBuilder(nameOrBuilder, description);
}
if (!hasAfter) {
orderedItems.pop();
}
createExpects(all, orderedItems);
}
describe('type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : ''), function() {
setFacet('model')
.createPojoItems([
{key: 'toyota', value: 5},
{key: 'misc', value: 26},
{key: 'ford', value: 23},
{key: 'chevy', value: 18},
]).setAscItemOrder(['chevy', 'ford', 'misc', 'toyota'])
.createExpects(true, [false, false, false, false])
.setChecked('chevy', true)
.createExpects(false, [true, false, false, false])
.setChecked('misc', true)
.createExpects(false, [true, false, true, false])
.setChecked('checkedKeys', ['chevy', 'ford'])
.createExpects(false, [true, true, false, false])
.end()
.setChecked('checkedKeys', ['ford', 'toyota'])
.createExpects(false, [false, true, false, true])
.end()
.setChecked('chevy', false)
.createExpects(false, [false, false, true, false])
.setChecked('misc', false)
.createExpects(true, [false, false, false, false])
.end()
.end()
.end()
.setChecked('all', true)
.createExpects(true, [false, false, false, false])
.setChecked('checkedKeys', ['ford', 'toyota'])
.createExpects(false, [false, true, false, true])
.endAll()
.build();
setFacet('value')
.createPojoItems([
{key: '7890', value: 7},
{key: '6789', value: 6},
{key: '5678', value: 5},
{key: '4567', value: 4},
{key: '3456', value: 3},
{key: '2345', value: 2},
{key: '1234', value: 1},
])
.setAscItemOrder(['1234', '2345', '3456', '4567', '5678', '6789', '7890'])
.setChecked('5678', true)
.createExpects(false, [false, false, false, false, true, false, false])
.setChecked('queryMin', '1234')
.createExpects(false, [false, false, false, false, true, false, false], {min: true, max: false})
.setChecked('queryMax', '7890')
.createExpects(false, [false, false, false, false, true, false, false], {min: true, max: true})
.setChecked('checkedKeys', ['5678', '6789'])
.createExpects(false, [false, false, false, false, true, true, false], {min: true, max: true})
.end()
.setChecked('all', true)
.createExpects(true, [false, false, false, false, false, false, false], {min: false, max: false})
.setChecked('checkedKeys', ['2345', '6789'])
.createExpects(false, [false, true, false, false, false, true, false], {min: false, max: false})
.end()
.end()
.setChecked('checkedKeys', [])
.createExpects(false, [false, false, false, false, false, false, false], {min: true, max: true})
.setChecked('queryMin', undefined)
.createExpects(true, [false, false, false, false, false, false, false], {min: false, max: true})
.setChecked('queryMax', undefined)
.createExpects(true, [false, false, false, false, false, false, false], {min: false, max: false})
.endAll()
.build();
setFacet('year')
.createPojoItems([
{key: '2010-01-01T00:00:00.000Z', value: 1},
{key: '2009-01-01T00:00:00.000Z', value: 0},
{key: '2008-01-01T00:00:00.000Z', value: 7},
{key: '2007-01-01T00:00:00.000Z', value: 0},
{key: '2006-01-01T00:00:00.000Z', value: 0},
{key: '2005-01-01T00:00:00.000Z', value: 0},
{key: '2004-01-01T00:00:00.000Z', value: 0},
{key: '2003-01-01T00:00:00.000Z', value: 3},
{key: '2002-01-01T00:00:00.000Z', value: 0},
{key: '2001-01-01T00:00:00.000Z', value: 4},
{key: '2000-01-01T00:00:00.000Z', value: 1},
])
.setAscItemOrder([
'2000-01-01T00:00:00.000Z',
'2001-01-01T00:00:00.000Z',
'2003-01-01T00:00:00.000Z',
'2008-01-01T00:00:00.000Z',
'2010-01-01T00:00:00.000Z',
])
.setChecked('2003-01-01T00:00:00.000Z', true)
.endAll()
.build();
setFacet('dateOfBirth')
.createPojoItems([
{key: '1974-01-01T00:00:00.000Z', value: 1},
{key: '1972-01-01T00:00:00.000Z', value: 1},
{key: '1970-01-01T00:00:00.000Z', value: 1},
])
.setAscItemOrder([
'1970-01-01T00:00:00.000Z',
'1972-01-01T00:00:00.000Z',
'1974-01-01T00:00:00.000Z',
])
.setBeforeAfter({key: 'before', value: 5}, undefined)
.setChecked('1972-01-01T00:00:00.000Z', true)
.createExpects(false, [false, false, true, false])
.setChecked('before', true)
.createExpects(false, [true, false, true, false])
.setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z'])
.createExpects(false, [true, false, false, true])
.setChecked('checkedKeys', [])
.createExpects(true, [false, false, false, false])
.end()
.end()
.endAll()
.build();
function rangeSupportHelper(hasBefore, hasAfter) {
var description = 'type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : '');
var builder = setFacet(function() {
var facetOptions = {
queryField: 'value_d',
queryField: 'dateOfBirth_dt',
facetType: 'year',
facetStats: true,
};
......@@ -253,74 +436,46 @@ define(function(require) {
} else if (hasAfter) {
facetOptions.other = 'after';
}
var pojoItems = [
{key: '2005', value: 500},
{key: '2004', value: 400},
{key: '2003', value: 300},
{key: '2002', value: 200},
{key: '2001', value: 100},
];
var beforeItem = {key: 'before', value: 2000};
var afterItem = {key: 'after', value: 1500};
ascItemOrder = ['2001', '2002', '2003', '2004', '2005'];
var c0;
beforeEach(function() {
f = new Facet(null, facetOptions);
items = f.get('items');
c0 = f.get('checkedKeys');
});
if (hasAfter) {
ascItemOrder.push('after');
pojoItems.push(afterItem);
}
return new Facet(null, facetOptions);
}, description)
.createPojoItems([
{key: '1974-01-01T00:00:00.000Z', value: 1},
{key: '1972-01-01T00:00:00.000Z', value: 1},
{key: '1970-01-01T00:00:00.000Z', value: 1},
])
.setAscItemOrder([
'1970-01-01T00:00:00.000Z',
'1972-01-01T00:00:00.000Z',
'1974-01-01T00:00:00.000Z',
])
.setBeforeAfter(hasBefore, hasAfter)
.setChecked('1972-01-01T00:00:00.000Z', true)
.createExpects(false, [false, false, true, false, false])
;
if (hasBefore) {
ascItemOrder.unshift('before');
pojoItems.unshift(beforeItem);
builder.setChecked('before', true)
.createExpects(false, [true, false, true, false, false])
.setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z'])
.createExpects(false, [true, false, false, true, false])
.setChecked('checkedKeys', [])
.createExpects(true, [false, false, false, false, false])
;
}
createPojoItems(pojoItems);
describe('query', function() {
describe('min', function() {
beforeEach(function() {
f.set({queryMin: '2002', queryMax: undefined});
});
createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
it('result', function() {
expect(f.get('hasQuery')).toBe(false);
});
});
describe('max', function() {
beforeEach(function() {
f.set({queryMin: undefined, queryMax: '2004'});
});
createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
it('result', function() {
expect(f.get('hasQuery')).toBe(false);
});
});
describe('min+max', function() {
beforeEach(function() {
f.set({queryMin: '1980', queryMax: '1984'});
});
createBeforeAfterExpects(false, [false, false, false, false, false, false, false]);
it('result', function() {
expect(f.get('hasQuery')).toBe(true);
});
describe('checkedKeys: [2002, 2003]', function() {
beforeEach(function() {
f.set('checkedKeys', ['2002', '2003']);
});
createBeforeAfterExpects(false, [false, false, true, true, false, false, false]);
});
});
});
createBeforeAfterExpects(true, [false, false, false, false, false, false, false]);
});
if (hasAfter) {
builder.setChecked('after', true)
.createExpects(false, [false, false, true, false, true])
.setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z'])
.createExpects(false, [false, false, false, true, true])
.setChecked('checkedKeys', [])
.createExpects(true, [false, false, false, false, false])
;
}
rangeSupportHelper(true, true);
rangeSupportHelper(true, false);
rangeSupportHelper(false, false);
rangeSupportHelper(false, true);
});
builder.endAll().build();
}
rangeSupportHelper({key: 'before', value: 5}, null);
rangeSupportHelper({key: 'before', value: 5}, {key: 'after', value: 23});
rangeSupportHelper(null, {key: 'after', value: 23});
rangeSupportHelper(null, null);
});
return {};
});
......
......@@ -2,6 +2,7 @@ define(function(require) {
'use strict';
var _ = require('underscore');
var Util = require('./Util');
var Facet = require('./Facet');
var Facets = require('./Facets');
var Solr = require('./Solr');
......@@ -19,23 +20,26 @@ define(function(require) {
return solrData;
}
function makeSolrResponseMixedCase(solrData) {
var facetCounts = Util.getField(solrData, 'facet_counts');
function makeSolrResponseMixedCase(solrResponse) {
var facetCounts = Util.getField(solrResponse, 'facet_counts');
return {
facetCounts: facetCounts,
facetRanges: Util.getField(facetCounts, 'facet_ranges'),
facetFields: Util.getField(facetCounts, 'facet_fields'),
facetQueries: Util.getField(facetCounts, 'facet_queries'),
statsFields: Util.getField(solrData.stats, 'stats_fields'),
statsFields: Util.getField(solrResponse.stats, 'stats_fields'),
};
}
function createStaticSolrResponse(has, toMerge) {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
if (_.isFunction(toMerge)) {
toMerge = toMerge(solrData);
} else {
_.each(solrData, function(value, key) {
_.extend(value, toMerge[key]);
});
}
return solrResponse;
}
......@@ -45,26 +49,26 @@ define(function(require) {
expect(item.get('checked')).toEqual(checked);
}
var facetsBuilder = function facetsBuilder() {
return {
model: new Facets.Facet({
var definedFacets = {
model: [
{
formName: 'model',
label: 'Model',
},
{
}, {
queryField: 'model_s',
facetType: 'field',
bins: {
misc: ['saturn', 'jeep'],
startsWithB: ['buick'],
},
}),
year: new Facets.Facet({
},
],
year: [
{
formName: 'year',
orderByDirection: 'desc',
label: 'Year',
},
{
}, {
queryField: 'year_dt',
comparator: 'date',
facetType: 'year-field',
......@@ -80,8 +84,10 @@ define(function(require) {
var year = new Date(value).getUTCFullYear();
return year + ' - ' + (year + 2);
},
}),
dateOfBirth: new Facets.Facet({
},
],
dateOfBirth: [
{
formName: 'dateOfBirth',
label: 'Birthday',
}, {
......@@ -91,8 +97,10 @@ define(function(require) {
facetStats: true,
other: 'before',
start: 'NOW/YEAR-25YEAR',
}),
value: new Facets.Facet({
},
],
value: [
{
formName: 'value',
label: 'Value',
}, {
......@@ -104,15 +112,53 @@ define(function(require) {
var value = parseInt(item.get('key'));
return '$' + value + ' - $' + (value + 1000);
},
}),
},
],
bodyType: [
{
label: 'Type'
}, {
facetType: 'field',
labelMap: {
aluminum: 'Aluminum',
plastic: 'Plastic',
carbonComposite: 'Carbon Composite',
steel: 'Steel',
},
},
],
};
var facetsBuilder = function facetsBuilder(facetName) {
if (facetName) {
return new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
}
var result = {};
_.each(_.keys(definedFacets), function(facetName) {
result[facetName] = new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
});
return result;
};
var responses = {
sequential: [
createStaticSolrResponse({stats: true}, {
facetRanges: {
function createSolrDataMerger(modelData) {
return function(solrData) {
_.each(solrData, function(solrValue, solrKey) {
_.each(modelData, function(modelObj, modelKey) {
var modelValue = modelObj[solrKey];
if (modelValue) {
if (solrKey === 'facetQueries') {
_.extend(solrValue, modelValue);
} else {
solrValue[modelKey] = modelValue;
}
}
});
});
};
}
var defaultModelData = {
dateOfBirth: {
facetRanges: {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
......@@ -120,9 +166,18 @@ define(function(require) {
],
before: 5,
},
statsFields: {min: '2000', max: '2010'},
},
facetFields: {
model: [
bodyType: {
facetFields: [
'aluminum', 14,
'carbonComposite', 66,
'steel', 2,
],
},
model: {
facetFields: [
'ford', 23,
'chevy', 18,
'toyota', 5,
......@@ -131,12 +186,21 @@ define(function(require) {
'buick', 0, // keep here to test the bin/exclude feature
'dodge', 0,
],
value: [
facetQueries: {
'model:misc': 26,
'model:startsWithB': 0,
},
},
value: {
facetFields: [
'23456', 1,
'5678', 2,
'1234', 3,
],
year: [
statsFields: {min: '1234', max: '23456'},
},
year: {
facetFields: [
'2000-01-01T00:00:00.000Z', 1,
'2001-01-01T00:00:00.000Z', 4,
'2002-01-01T00:00:00.000Z', 0,
......@@ -149,17 +213,12 @@ define(function(require) {
'2009-01-01T00:00:00.000Z', 0,
'2010-01-01T00:00:00.000Z', 1,
],
statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'},
},
facetQueries: {
'model:misc': 26,
'model:startsWithB': 0,
},
statsFields: {
year: {min: '2000', max: '2010'},
dateOfBirth: {min: '2000', max: '2010'},
value: {min: '1234', max: '23456'},
},
}),
};
var responses = {
sequential: [
createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)),
],
suggestions: [
createStaticSolrResponse({stats: true}, {
......
......@@ -29,7 +29,8 @@ define(function(require) {
return obj[key];
},
setField: function getField(obj, key, value) {
return obj[key] = value;
obj[key] = value;
return value;
},
getItemKeyAccessor: function getItemKeyAccessor(item) {
return item.get('key');
......