b32c60af by Adam Heath

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

1 parent 6bada521
...@@ -3,10 +3,7 @@ define(function(require) { ...@@ -3,10 +3,7 @@ define(function(require) {
3 var _ = require('underscore'); 3 var _ = require('underscore');
4 var Backbone = require('backbone'); 4 var Backbone = require('backbone');
5 var Sort = require('./Sort'); 5 var Sort = require('./Sort');
6 6 var Util = require('./Util');
7 function getItemKeyAccessor(item) {
8 return item.get('key');
9 }
10 7
11 function wrapComparatorForAllBeforeAfter(comparator, doBefore, doAfter) { 8 function wrapComparatorForAllBeforeAfter(comparator, doBefore, doAfter) {
12 if (!doBefore && !doAfter) { 9 if (!doBefore && !doAfter) {
...@@ -127,7 +124,7 @@ define(function(require) { ...@@ -127,7 +124,7 @@ define(function(require) {
127 })(this); 124 })(this);
128 var sortKeyExtractor = options.sortKeyExtractor; 125 var sortKeyExtractor = options.sortKeyExtractor;
129 if (!sortKeyExtractor) { 126 if (!sortKeyExtractor) {
130 sortKeyExtractor = getItemKeyAccessor; 127 sortKeyExtractor = Util.getItemKeyAccessor;
131 } 128 }
132 var formatter = options.formatter; 129 var formatter = options.formatter;
133 if (!formatter) { 130 if (!formatter) {
...@@ -139,7 +136,7 @@ define(function(require) { ...@@ -139,7 +136,7 @@ define(function(require) {
139 return label ? label : value; 136 return label ? label : value;
140 }; 137 };
141 } else { 138 } else {
142 formatter = getItemKeyAccessor; 139 formatter = Util.getItemKeyAccessor;
143 } 140 }
144 } 141 }
145 this.formatter = formatter; 142 this.formatter = formatter;
......
...@@ -5,14 +5,7 @@ define(function(require) { ...@@ -5,14 +5,7 @@ define(function(require) {
5 var NestedModels = require('backbone-nested-models'); 5 var NestedModels = require('backbone-nested-models');
6 var Facet = require('./Facet'); 6 var Facet = require('./Facet');
7 var Sort = require('./Sort'); 7 var Sort = require('./Sort');
8 8 var Util = require('./Util');
9 function getField(obj, key) {
10 return obj[key];
11 }
12
13 function getItemKeyAccessor(item) {
14 return item.get('key');
15 }
16 9
17 var Facets = NestedModels.mixin(Backbone.Model.extend({ 10 var Facets = NestedModels.mixin(Backbone.Model.extend({
18 initialize: function(data, options) { 11 initialize: function(data, options) {
...@@ -59,12 +52,12 @@ define(function(require) { ...@@ -59,12 +52,12 @@ define(function(require) {
59 }, 52 },
60 applyFacetResults: function(data, options) { 53 applyFacetResults: function(data, options) {
61 options = options || {}; 54 options = options || {};
62 var facetCounts = getField(data, 'facet_counts'); 55 var facetCounts = Util.getField(data, 'facet_counts');
63 var facetIntervals = getField(facetCounts, 'facet_intervals'); 56 var facetIntervals = Util.getField(facetCounts, 'facet_intervals');
64 var facetRanges = getField(facetCounts, 'facet_ranges'); 57 var facetRanges = Util.getField(facetCounts, 'facet_ranges');
65 var facetFields = getField(facetCounts, 'facet_fields'); 58 var facetFields = Util.getField(facetCounts, 'facet_fields');
66 var facetQueries = getField(facetCounts, 'facet_queries'); 59 var facetQueries = Util.getField(facetCounts, 'facet_queries');
67 var statsFields = getField(data.stats, 'stats_fields'); 60 var statsFields = Util.getField(data.stats, 'stats_fields');
68 function getFacetList(facetName, type) { 61 function getFacetList(facetName, type) {
69 switch (type) { 62 switch (type) {
70 case 'year': 63 case 'year':
...@@ -223,7 +216,7 @@ define(function(require) { ...@@ -223,7 +216,7 @@ define(function(require) {
223 return new Date(value).toISOString(); 216 return new Date(value).toISOString();
224 }; 217 };
225 valueFormatter = facet.queryValue ? facet.queryValue : function(value) { 218 valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
226 return quoteFormatter(getItemKeyAccessor(value)); 219 return quoteFormatter(Util.getItemKeyAccessor(value));
227 }; 220 };
228 _.each(facet.bins, function(includeValues, key) { 221 _.each(facet.bins, function(includeValues, key) {
229 var query = _.map(includeValues, function(includeValue, index) { 222 var query = _.map(includeValues, function(includeValue, index) {
...@@ -234,7 +227,7 @@ define(function(require) { ...@@ -234,7 +227,7 @@ define(function(require) {
234 break; 227 break;
235 case 'field': 228 case 'field':
236 valueFormatter = facet.queryValue ? facet.queryValue : function(value) { 229 valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
237 return quoteFormatter(getItemKeyAccessor(value)); 230 return quoteFormatter(Util.getItemKeyAccessor(value));
238 }; 231 };
239 _.each(facet.bins, function(includeValues, key) { 232 _.each(facet.bins, function(includeValues, key) {
240 var query = _.map(includeValues, function(includeValue, index) { 233 var query = _.map(includeValues, function(includeValue, index) {
......
...@@ -7,39 +7,10 @@ define(function(require) { ...@@ -7,39 +7,10 @@ define(function(require) {
7 var Ordering = require('solr/model/Ordering'); 7 var Ordering = require('solr/model/Ordering');
8 var Pagination = require('solr/model/Pagination'); 8 var Pagination = require('solr/model/Pagination');
9 var QueryTextField = require('solr/model/QueryTextField'); 9 var QueryTextField = require('solr/model/QueryTextField');
10 var Util = require('solr/model/Util');
10 11
11 //var module = require('module'); 12 //var module = require('module');
12 13
13 function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
14 var result = filterFunc(obj);
15 var ptr = startPtr;
16 while (true) {
17 result = _.extend(result, filterFunc(_.result(ptr, fieldName)));
18 if (ptr === endPtr) {
19 break;
20 }
21 ptr = ptr.__super__.constructor;
22 }
23 return result;
24 }
25
26 function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
27 return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
28 }
29
30 function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
31 return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
32 var result = {};
33 _.each(array, function(value) {
34 result[value] = true;
35 });
36 return result;
37 }));
38 }
39
40 function getItemKeyAccessor(item) {
41 return item.get('key');
42 }
43 var Solr = Pagination.extend({ 14 var Solr = Pagination.extend({
44 url: function url() { 15 url: function url() {
45 return this.constructor.selectUrl; 16 return this.constructor.selectUrl;
...@@ -223,7 +194,7 @@ define(function(require) { ...@@ -223,7 +194,7 @@ define(function(require) {
223 }; 194 };
224 }, 195 },
225 searchParseDoc: function searchParseDoc(doc, index, itemHighlighting) { 196 searchParseDoc: function searchParseDoc(doc, index, itemHighlighting) {
226 var fieldsToParse = mergeStaticProps(this.constructor, Solr, {}, 'parsedFieldMap'); 197 var fieldsToParse = util.mergeStaticProps(this.constructor, Solr, {}, 'parsedFieldMap');
227 var result = {}; 198 var result = {};
228 _.each(fieldsToParse, function(value, key) { 199 _.each(fieldsToParse, function(value, key) {
229 var parsed; 200 var parsed;
...@@ -271,7 +242,7 @@ define(function(require) { ...@@ -271,7 +242,7 @@ define(function(require) {
271 result.sort = sort; 242 result.sort = sort;
272 result.rows = pageSize; 243 result.rows = pageSize;
273 result.start = (currentPage - 1) * pageSize; 244 result.start = (currentPage - 1) * pageSize;
274 result.fl = mergeStaticSets(constructor, Solr, [], 'returnFields'); 245 result.fl = util.mergeStaticSets(constructor, Solr, [], 'returnFields');
275 if (this.get('options').get('highlighting')) { 246 if (this.get('options').get('highlighting')) {
276 result.hl = true; 247 result.hl = true;
277 result['hl.fl'] = ['content', 'title'].concat(constructor.extraHighlightFields); 248 result['hl.fl'] = ['content', 'title'].concat(constructor.extraHighlightFields);
......
1 define(function(require) {
2 'use strict';
3
4 function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
5 var result = filterFunc(obj);
6 var ptr = startPtr;
7 while (true) {
8 result = _.extend(result, filterFunc(_.result(ptr, fieldName)));
9 if (ptr === endPtr) {
10 break;
11 }
12 ptr = ptr.__super__.constructor;
13 }
14 return result;
15 }
16
17 return {
18 getField: function getField(obj, key) {
19 return obj[key];
20 },
21 getItemKeyAccessor: function getItemKeyAccessor(item) {
22 return item.get('key');
23 },
24 mergeStaticProps: function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
25 return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
26 },
27 mergeStaticSets: function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
28 return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
29 var result = {};
30 _.each(array, function(value) {
31 result[value] = true;
32 });
33 return result;
34 }));
35 },
36 };
37 });