8534addb by Adam Heath

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

1 parent 2cc67ace
......@@ -33,6 +33,7 @@ define(function(require) {
var Facet = Backbone.Model.extend({
defaults: function defaults() {
return {
checkedKeys: [],
formName: null,
query: null,
suggestions: [],
......@@ -110,7 +111,7 @@ define(function(require) {
var newChecked = checkedKeys[facetItem.get('key')];
var currentChecked = facetItem.get('checked');
if (newChecked !== currentChecked) {
facetItem.set('checked', newChecked);
facetItem.set('checked', !!newChecked);
}
if (facetItem.get('checked')) {
checkedCount++;
......@@ -128,19 +129,6 @@ define(function(require) {
if (!sortKeyExtractor) {
sortKeyExtractor = getItemKeyAccessor;
}
var comparator = options.comparator;
if (!comparator) {
comparator = Sort.standard;
} else if (typeof comparator === 'string') {
comparator = Sort[comparator];
}
switch (options.facetType) {
case 'year':
case 'range':
case 'interval':
comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
break;
}
var formatter = options.formatter;
if (!formatter) {
var labelMap = options.labelMap;
......@@ -155,21 +143,46 @@ define(function(require) {
}
}
this.formatter = formatter;
this.on('change:comparator change:orderByDirection', function() {
var comparator = this.get('comparator');
if (!comparator) {
comparator = Sort.standard;
} else if (typeof comparator === 'string') {
comparator = Sort[comparator];
}
switch (options.facetType) {
case 'year':
case 'range':
case 'interval':
comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
break;
}
var facet = this;
comparator = (function(comparator) {
return function facetItemValue(a, b) {
var keyA = sortKeyExtractor(a);
var keyB = sortKeyExtractor(b);
switch (this.get('orderByDirection')) {
switch (facet.get('orderByDirection')) {
case 'desc':
var tmp = keyA;
keyA = keyB;
keyB = tmp;
break;
}
return comparator.call(this, sortKeyExtractor(a), sortKeyExtractor(b));
return comparator.call(facet, keyA, keyB);
};
})(comparator);
this.set('items', new ItemCollection([], {comparator: comparator}));
var items = this.get('items');
items.comparator = comparator;
items.sort();
}, this);
this.set('items', new ItemCollection([]));
this.set('comparator', options.comparator, {silent: true});
this.trigger('change:comparator');
this.get('items').on('item-change', function() {
this.trigger('item-change');
}, this);
this.facetType = options.facetType;
this.facetStats = options.facetStats;
this.other = options.other;
......@@ -243,7 +256,7 @@ define(function(require) {
checked: false,
hidden: false,
},
_initialize: function(data, options) {
initialize: function(data, options) {
this.on('add', function(model, parent, options) {
// added to a collection
this.on('change:checked', function() {
......