8534addb by Adam Heath

Much more testing of Facet, getting closer to 100%.

1 parent 2cc67ace
...@@ -33,6 +33,7 @@ define(function(require) { ...@@ -33,6 +33,7 @@ define(function(require) {
33 var Facet = Backbone.Model.extend({ 33 var Facet = Backbone.Model.extend({
34 defaults: function defaults() { 34 defaults: function defaults() {
35 return { 35 return {
36 checkedKeys: [],
36 formName: null, 37 formName: null,
37 query: null, 38 query: null,
38 suggestions: [], 39 suggestions: [],
...@@ -110,7 +111,7 @@ define(function(require) { ...@@ -110,7 +111,7 @@ define(function(require) {
110 var newChecked = checkedKeys[facetItem.get('key')]; 111 var newChecked = checkedKeys[facetItem.get('key')];
111 var currentChecked = facetItem.get('checked'); 112 var currentChecked = facetItem.get('checked');
112 if (newChecked !== currentChecked) { 113 if (newChecked !== currentChecked) {
113 facetItem.set('checked', newChecked); 114 facetItem.set('checked', !!newChecked);
114 } 115 }
115 if (facetItem.get('checked')) { 116 if (facetItem.get('checked')) {
116 checkedCount++; 117 checkedCount++;
...@@ -128,19 +129,6 @@ define(function(require) { ...@@ -128,19 +129,6 @@ define(function(require) {
128 if (!sortKeyExtractor) { 129 if (!sortKeyExtractor) {
129 sortKeyExtractor = getItemKeyAccessor; 130 sortKeyExtractor = getItemKeyAccessor;
130 } 131 }
131 var comparator = options.comparator;
132 if (!comparator) {
133 comparator = Sort.standard;
134 } else if (typeof comparator === 'string') {
135 comparator = Sort[comparator];
136 }
137 switch (options.facetType) {
138 case 'year':
139 case 'range':
140 case 'interval':
141 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
142 break;
143 }
144 var formatter = options.formatter; 132 var formatter = options.formatter;
145 if (!formatter) { 133 if (!formatter) {
146 var labelMap = options.labelMap; 134 var labelMap = options.labelMap;
...@@ -155,21 +143,46 @@ define(function(require) { ...@@ -155,21 +143,46 @@ define(function(require) {
155 } 143 }
156 } 144 }
157 this.formatter = formatter; 145 this.formatter = formatter;
158 comparator = (function(comparator) { 146 this.on('change:comparator change:orderByDirection', function() {
159 return function facetItemValue(a, b) { 147 var comparator = this.get('comparator');
160 var keyA = sortKeyExtractor(a); 148 if (!comparator) {
161 var keyB = sortKeyExtractor(b); 149 comparator = Sort.standard;
162 switch (this.get('orderByDirection')) { 150 } else if (typeof comparator === 'string') {
163 case 'desc': 151 comparator = Sort[comparator];
164 var tmp = keyA; 152 }
165 keyA = keyB; 153 switch (options.facetType) {
166 keyB = tmp; 154 case 'year':
167 break; 155 case 'range':
168 } 156 case 'interval':
169 return comparator.call(this, sortKeyExtractor(a), sortKeyExtractor(b)); 157 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
170 }; 158 break;
171 })(comparator); 159 }
172 this.set('items', new ItemCollection([], {comparator: comparator})); 160 var facet = this;
161 comparator = (function(comparator) {
162 return function facetItemValue(a, b) {
163 var keyA = sortKeyExtractor(a);
164 var keyB = sortKeyExtractor(b);
165 switch (facet.get('orderByDirection')) {
166 case 'desc':
167 var tmp = keyA;
168 keyA = keyB;
169 keyB = tmp;
170 break;
171 }
172 return comparator.call(facet, keyA, keyB);
173 };
174 })(comparator);
175 var items = this.get('items');
176 items.comparator = comparator;
177 items.sort();
178 }, this);
179
180 this.set('items', new ItemCollection([]));
181 this.set('comparator', options.comparator, {silent: true});
182 this.trigger('change:comparator');
183 this.get('items').on('item-change', function() {
184 this.trigger('item-change');
185 }, this);
173 this.facetType = options.facetType; 186 this.facetType = options.facetType;
174 this.facetStats = options.facetStats; 187 this.facetStats = options.facetStats;
175 this.other = options.other; 188 this.other = options.other;
...@@ -243,7 +256,7 @@ define(function(require) { ...@@ -243,7 +256,7 @@ define(function(require) {
243 checked: false, 256 checked: false,
244 hidden: false, 257 hidden: false,
245 }, 258 },
246 _initialize: function(data, options) { 259 initialize: function(data, options) {
247 this.on('add', function(model, parent, options) { 260 this.on('add', function(model, parent, options) {
248 // added to a collection 261 // added to a collection
249 this.on('change:checked', function() { 262 this.on('change:checked', function() {
......