56db38ec by Adam Heath

Move most of the solr data parsing/building from Facets into Facet.

1 parent d91e8f81
......@@ -102,7 +102,7 @@ module.exports = function(grunt) {
}
}
url = parts.join('/');
if (0) if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) {
if (1) 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;
......@@ -129,8 +129,8 @@ module.exports = function(grunt) {
};
templateOptions = {
coverage: 'bin/coverage/coverage.json',
report: 'bin/coverage',
coverage: 'dist/coverage/coverage.json',
report: 'dist/coverage',
replace: false,
template: foo,
templateOptions: templateOptions,
......
......@@ -210,6 +210,253 @@ define(function(require) {
}
return Facet.__super__.initialize.apply(this, arguments);
},
applyFacetResults: function(facetName, data, options) {
options = options || {};
var facetCounts = Util.getField(data, 'facet_counts');
var facetIntervals = Util.getField(facetCounts, 'facet_intervals');
var facetRanges = Util.getField(facetCounts, 'facet_ranges');
var facetFields = Util.getField(facetCounts, 'facet_fields');
var facetQueries = Util.getField(facetCounts, 'facet_queries');
var statsFields = Util.getField(data.stats, 'stats_fields');
function getFacetList(facetName, type) {
switch (type) {
case 'year':
case 'range':
return facetRanges[facetName] ? facetRanges[facetName].counts : null;
case 'interval':
return facetIntervals[facetName] ? facetIntervals[facetName].counts : null;
case 'year-field':
case 'field':
return facetFields[facetName];
}
}
var type = this.facetType;
var suggestions = [];
_.each(getFacetList('suggestions:' + facetName, type), function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
suggestions.push({key: key, value: value});
}
});
this.set('suggestions', suggestions);
//if (!!options.facetSuggestions && options.childFacet === facet) {
if (!!options.facetSuggestions) {
return;
}
var facet = this;
var key;
var list = getFacetList(facetName, type);
var newItems = [];
var items = this.get('items');
items.invoke('set', 'hidden', true);
var valueOverrides = {};
var excludeValues = {};
function recordIncludeValueIntoExclude(includeValue, key) {
excludeValues[includeValue] = true;
}
switch (type) {
case 'year-field':
case 'field':
_.each(this.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
valueOverrides[key] = facetQueries[tag];
_.each(includeValues, recordIncludeValueIntoExclude);
});
break;
}
var addNewItem = _.bind(function addNewItem(key, value) {
if (valueOverrides[key] !== undefined) {
value = valueOverrides[key];
if (!value) {
return;
}
} else if (excludeValues[key]) {
return;
}
var item = items.get({id: key});
if (item) {
item.set({hidden: value === 0, value: value});
} else {
item = new Facet.Item({key: key, value: value});
}
newItems.push(item);
}, this);
function checkOther(set, key) {
if (set.indexOf(facet.other) !== -1) {
addNewItem(key, facetRanges[facetName][key]);
}
}
checkOther(['all', 'before'], 'before');
_.each(list, function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
addNewItem(key, value);
}
});
switch (type) {
case 'year-field':
case 'field':
_.each(this.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
addNewItem(key, facetQueries[tag]);
});
break;
}
items.set(newItems, {remove: false});
if (this.facetStats) {
var thisFacetStats = statsFields[facetName];
this.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max});
}
},
getFacetFormData: function(facetName, options) {
options = options || {};
var result = {
'facet.field': [],
'facet.range': [],
'facet.interval': [],
'facet.query': [],
'stats.field': [],
fq: [],
};
var facet = this;
var queryField = this.queryField;
var facetField = this.facetField ? this.facetField : queryField;
var type = this.facetType;
var solrType = type;
var valueFormatter;
var facetOptions = {};
var quoteFormatter = function(value) {
return value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
};
var rangeFormatter = function(from, to) {
return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']';
};
switch (type) {
case 'year':
solrType = 'range';
quoteFormatter = function(value) {
return value.toISOString();
};
valueFormatter = function(item) {
var key = item.get('key');
var fromDate, toDate;
if (key === 'before') {
return '[* TO ' + facet.rangeStart + '-1MILLISECOND]';
}
fromDate = new Date(key);
toDate = new Date(key);
toDate.setUTCFullYear(toDate.getUTCFullYear() + facet.rangeGap);
toDate.setTime(toDate.getTime() - 1);
return rangeFormatter(fromDate, toDate);
};
if (facet.other) {
facetOptions['facet.range.other'] = facet.other;
}
break;
case 'range':
case 'interval':
valueFormatter = function(item) {
var key = parseInt(item.get('key'));
return rangeFormatter(key, key + facet.rangeGap);
};
break;
case 'year-field':
solrType = 'field';
quoteFormatter = function(value) {
return new Date(value).toISOString();
};
valueFormatter = this.queryValue ? this.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(this.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':\'' + includeValue + '\'';
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
case 'field':
valueFormatter = this.queryValue ? this.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(this.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':' + includeValue;
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
}
switch (type) {
case 'year':
facetOptions['facet.range.start'] = this.rangeStart;
facetOptions['facet.range.end'] = this.rangeEnd;
facetOptions['facet.range.gap'] = '+' + this.rangeGap + 'YEAR';
break;
case 'range':
facetOptions['facet.range.start'] = this.rangeStart;
facetOptions['facet.range.end'] = this.rangeEnd;
facetOptions['facet.range.gap'] = this.rangeGap;
break;
case 'interval':
facetOptions['facet.interval.set'] = '[*,*]';
break;
}
var facetValues = [];
var queryMax = this.get('queryMax');
var queryMin = this.get('queryMin');
if (queryMax !== undefined && queryMin !== undefined) {
facetValues.push(rangeFormatter(queryMin, queryMax));
}
this.get('items').each(function(item, index) {
if (!item.get('hidden') && item.get('checked')) {
facetValues.push(valueFormatter(item));
}
});
facetOptions.key = facetName;
if (facetValues.length) {
facetOptions.ex = facetName;
result.fq.push('{!tag=' + facetName + '}' + facetField + ':(' + facetValues.join(' OR ') + ')');
} else {
facetOptions.tag = facetName;
}
if (!!options.facetSuggestions) {
var childFacet = options.childFacet;
if (childFacet === facet) {
facetOptions['facet.contains'] = this.get('query');
facetOptions['facet.contains.ignoreCase'] = true;
facetOptions['facet.mincount'] = 1;
}
}
var facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
if (this.facetStats) {
result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
var facetQuery = this.get('query');
if (facetQuery !== null && facetQuery !== '') {
facetOptions.key = 'suggestions:' + facetName;
facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
return result;
},
/*
toJSON: function toJSON(options) {
if (!!options.facetSuggestions) {
......
......@@ -52,108 +52,9 @@ define(function(require) {
},
applyFacetResults: function(data, options) {
options = options || {};
var facetCounts = Util.getField(data, 'facet_counts');
var facetIntervals = Util.getField(facetCounts, 'facet_intervals');
var facetRanges = Util.getField(facetCounts, 'facet_ranges');
var facetFields = Util.getField(facetCounts, 'facet_fields');
var facetQueries = Util.getField(facetCounts, 'facet_queries');
var statsFields = Util.getField(data.stats, 'stats_fields');
function getFacetList(facetName, type) {
switch (type) {
case 'year':
case 'range':
return facetRanges[facetName] ? facetRanges[facetName].counts : null;
case 'interval':
return facetIntervals[facetName] ? facetIntervals[facetName].counts : null;
case 'year-field':
case 'field':
return facetFields[facetName];
}
}
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
var type = facet.facetType;
var suggestions = [];
_.each(getFacetList('suggestions:' + facetName, type), function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
suggestions.push({key: key, value: value});
}
});
facet.set('suggestions', suggestions);
//if (!!options.facetSuggestions && options.childFacet === facet) {
if (!!options.facetSuggestions) {
return;
}
var key;
var list = getFacetList(facetName, type);
var newItems = [];
var items = facet.get('items');
items.invoke('set', 'hidden', true);
var valueOverrides = {};
var excludeValues = {};
function recordIncludeValueIntoExclude(includeValue, key) {
excludeValues[includeValue] = true;
}
switch (type) {
case 'year-field':
case 'field':
_.each(facet.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
valueOverrides[key] = facetQueries[tag];
_.each(includeValues, recordIncludeValueIntoExclude);
});
break;
}
var addNewItem = _.bind(function addNewItem(key, value) {
if (valueOverrides[key] !== undefined) {
value = valueOverrides[key];
if (!value) {
return;
}
} else if (excludeValues[key]) {
return;
}
var item = items.get({id: key});
if (item) {
item.set({hidden: value === 0, value: value});
} else {
item = new Facet.Item({key: key, value: value});
}
newItems.push(item);
}, this);
function checkOther(set, key) {
if (set.indexOf(facet.other) !== -1) {
addNewItem(key, facetRanges[facetName][key]);
}
}
checkOther(['all', 'before'], 'before');
_.each(list, function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
addNewItem(key, value);
}
});
switch (type) {
case 'year-field':
case 'field':
_.each(facet.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
addNewItem(key, facetQueries[tag]);
});
break;
}
items.set(newItems, {remove: false});
if (facet.facetStats) {
var thisFacetStats = statsFields[facetName];
facet.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max});
}
facet.applyFacetResults(facetName, data, options);
}, this));
},
getFacetFormData: function(options) {
......@@ -171,136 +72,17 @@ define(function(require) {
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
var queryField = facet.queryField;
var facetField = facet.facetField ? facet.facetField : queryField;
var type = facet.facetType;
var solrType = type;
var valueFormatter;
var facetOptions = {};
var quoteFormatter = function(value) {
return value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
};
var rangeFormatter = function(from, to) {
return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']';
};
switch (type) {
case 'year':
solrType = 'range';
quoteFormatter = function(value) {
return value.toISOString();
};
valueFormatter = function(item) {
var key = item.get('key');
var fromDate, toDate;
if (key === 'before') {
return '[* TO ' + facet.rangeStart + '-1MILLISECOND]';
}
fromDate = new Date(key);
toDate = new Date(key);
toDate.setUTCFullYear(toDate.getUTCFullYear() + facet.rangeGap);
toDate.setTime(toDate.getTime() - 1);
return rangeFormatter(fromDate, toDate);
};
if (facet.other) {
facetOptions['facet.range.other'] = facet.other;
}
break;
case 'range':
case 'interval':
valueFormatter = function(item) {
var key = parseInt(item.get('key'));
return rangeFormatter(key, key + facet.rangeGap);
};
break;
case 'year-field':
solrType = 'field';
quoteFormatter = function(value) {
return new Date(value).toISOString();
};
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':\'' + includeValue + '\'';
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
case 'field':
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':' + includeValue;
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
}
switch (type) {
case 'year':
facetOptions['facet.range.start'] = facet.rangeStart;
facetOptions['facet.range.end'] = facet.rangeEnd;
facetOptions['facet.range.gap'] = '+' + facet.rangeGap + 'YEAR';
break;
case 'range':
facetOptions['facet.range.start'] = facet.rangeStart;
facetOptions['facet.range.end'] = facet.rangeEnd;
facetOptions['facet.range.gap'] = facet.rangeGap;
break;
case 'interval':
facetOptions['facet.interval.set'] = '[*,*]';
break;
}
var facetValues = [];
var queryMax = facet.get('queryMax');
var queryMin = facet.get('queryMin');
if (queryMax !== undefined && queryMin !== undefined) {
facetValues.push(rangeFormatter(queryMin, queryMax));
}
facet.get('items').each(function(item, index) {
if (!item.get('hidden') && item.get('checked')) {
facetValues.push(valueFormatter(item));
}
var facetResult = facet.getFacetFormData(facetName, options);
_.each([
'facet.field',
'facet.range',
'facet.interval',
'facet.query',
'stats.field',
'fq',
], function(key) {
result[key] = result[key].concat(facetResult[key]);
});
facetOptions.key = facetName;
if (facetValues.length) {
facetOptions.ex = facetName;
result.fq.push('{!tag=' + facetName + '}' + facetField + ':(' + facetValues.join(' OR ') + ')');
} else {
facetOptions.tag = facetName;
}
if (!!options.facetSuggestions) {
var childFacet = options.childFacet;
if (childFacet === facet) {
facetOptions['facet.contains'] = facet.get('query');
facetOptions['facet.contains.ignoreCase'] = true;
facetOptions['facet.mincount'] = 1;
}
}
var facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
if (facet.facetStats) {
result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
var facetQuery = facet.get('query');
if (facetQuery !== null && facetQuery !== '') {
facetOptions.key = 'suggestions:' + facetName;
facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
}, this));
return result;
},
......