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;
146 this.on('change:comparator change:orderByDirection', function() {
147 var comparator = this.get('comparator');
148 if (!comparator) {
149 comparator = Sort.standard;
150 } else if (typeof comparator === 'string') {
151 comparator = Sort[comparator];
152 }
153 switch (options.facetType) {
154 case 'year':
155 case 'range':
156 case 'interval':
157 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
158 break;
159 }
160 var facet = this;
158 comparator = (function(comparator) { 161 comparator = (function(comparator) {
159 return function facetItemValue(a, b) { 162 return function facetItemValue(a, b) {
160 var keyA = sortKeyExtractor(a); 163 var keyA = sortKeyExtractor(a);
161 var keyB = sortKeyExtractor(b); 164 var keyB = sortKeyExtractor(b);
162 switch (this.get('orderByDirection')) { 165 switch (facet.get('orderByDirection')) {
163 case 'desc': 166 case 'desc':
164 var tmp = keyA; 167 var tmp = keyA;
165 keyA = keyB; 168 keyA = keyB;
166 keyB = tmp; 169 keyB = tmp;
167 break; 170 break;
168 } 171 }
169 return comparator.call(this, sortKeyExtractor(a), sortKeyExtractor(b)); 172 return comparator.call(facet, keyA, keyB);
170 }; 173 };
171 })(comparator); 174 })(comparator);
172 this.set('items', new ItemCollection([], {comparator: 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() {
......