b32c60af by Adam Heath

Move several simple functions to Util; no full tests yet.

1 parent 6bada521
......@@ -3,10 +3,7 @@ define(function(require) {
var _ = require('underscore');
var Backbone = require('backbone');
var Sort = require('./Sort');
function getItemKeyAccessor(item) {
return item.get('key');
}
var Util = require('./Util');
function wrapComparatorForAllBeforeAfter(comparator, doBefore, doAfter) {
if (!doBefore && !doAfter) {
......@@ -127,7 +124,7 @@ define(function(require) {
})(this);
var sortKeyExtractor = options.sortKeyExtractor;
if (!sortKeyExtractor) {
sortKeyExtractor = getItemKeyAccessor;
sortKeyExtractor = Util.getItemKeyAccessor;
}
var formatter = options.formatter;
if (!formatter) {
......@@ -139,7 +136,7 @@ define(function(require) {
return label ? label : value;
};
} else {
formatter = getItemKeyAccessor;
formatter = Util.getItemKeyAccessor;
}
}
this.formatter = formatter;
......
......@@ -5,14 +5,7 @@ define(function(require) {
var NestedModels = require('backbone-nested-models');
var Facet = require('./Facet');
var Sort = require('./Sort');
function getField(obj, key) {
return obj[key];
}
function getItemKeyAccessor(item) {
return item.get('key');
}
var Util = require('./Util');
var Facets = NestedModels.mixin(Backbone.Model.extend({
initialize: function(data, options) {
......@@ -59,12 +52,12 @@ define(function(require) {
},
applyFacetResults: function(data, options) {
options = options || {};
var facetCounts = getField(data, 'facet_counts');
var facetIntervals = getField(facetCounts, 'facet_intervals');
var facetRanges = getField(facetCounts, 'facet_ranges');
var facetFields = getField(facetCounts, 'facet_fields');
var facetQueries = getField(facetCounts, 'facet_queries');
var statsFields = getField(data.stats, 'stats_fields');
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':
......@@ -223,7 +216,7 @@ define(function(require) {
return new Date(value).toISOString();
};
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(getItemKeyAccessor(value));
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
......@@ -234,7 +227,7 @@ define(function(require) {
break;
case 'field':
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(getItemKeyAccessor(value));
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
......
......@@ -7,39 +7,10 @@ define(function(require) {
var Ordering = require('solr/model/Ordering');
var Pagination = require('solr/model/Pagination');
var QueryTextField = require('solr/model/QueryTextField');
var Util = require('solr/model/Util');
//var module = require('module');
function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
var result = filterFunc(obj);
var ptr = startPtr;
while (true) {
result = _.extend(result, filterFunc(_.result(ptr, fieldName)));
if (ptr === endPtr) {
break;
}
ptr = ptr.__super__.constructor;
}
return result;
}
function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
}
function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
var result = {};
_.each(array, function(value) {
result[value] = true;
});
return result;
}));
}
function getItemKeyAccessor(item) {
return item.get('key');
}
var Solr = Pagination.extend({
url: function url() {
return this.constructor.selectUrl;
......@@ -223,7 +194,7 @@ define(function(require) {
};
},
searchParseDoc: function searchParseDoc(doc, index, itemHighlighting) {
var fieldsToParse = mergeStaticProps(this.constructor, Solr, {}, 'parsedFieldMap');
var fieldsToParse = util.mergeStaticProps(this.constructor, Solr, {}, 'parsedFieldMap');
var result = {};
_.each(fieldsToParse, function(value, key) {
var parsed;
......@@ -271,7 +242,7 @@ define(function(require) {
result.sort = sort;
result.rows = pageSize;
result.start = (currentPage - 1) * pageSize;
result.fl = mergeStaticSets(constructor, Solr, [], 'returnFields');
result.fl = util.mergeStaticSets(constructor, Solr, [], 'returnFields');
if (this.get('options').get('highlighting')) {
result.hl = true;
result['hl.fl'] = ['content', 'title'].concat(constructor.extraHighlightFields);
......
define(function(require) {
'use strict';
function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
var result = filterFunc(obj);
var ptr = startPtr;
while (true) {
result = _.extend(result, filterFunc(_.result(ptr, fieldName)));
if (ptr === endPtr) {
break;
}
ptr = ptr.__super__.constructor;
}
return result;
}
return {
getField: function getField(obj, key) {
return obj[key];
},
getItemKeyAccessor: function getItemKeyAccessor(item) {
return item.get('key');
},
mergeStaticProps: function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
},
mergeStaticSets: function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
var result = {};
_.each(array, function(value) {
result[value] = true;
});
return result;
}));
},
};
});