87d470c0 by Adam Heath

Fix broken implementation of facet.missing support; basically, null keys

returned from solr weren't handled well at all.
1 parent 1cbd9db0
......@@ -159,6 +159,11 @@ define(function(require) {
return function facetItemValue(a, b) {
var keyA = sortKeyExtractor(a);
var keyB = sortKeyExtractor(b);
if (keyA === null) {
return -1;
} else if (keyB === null) {
return 1;
}
return comparator.call(facet, keyA, keyB);
};
})(comparator);
......@@ -282,12 +287,8 @@ define(function(require) {
} else if (excludeValues[key]) {
return;
}
var item = items.get({key: key});
if (item) {
item.set({hidden: value === 0, value: value});
} else {
item = new Facet.Item({key: key, value: value});
}
var item = this.getOrCreateItem(key);
item.set({hidden: value === 0, value: value});
newItems.push(item);
}, this);
......@@ -320,6 +321,20 @@ define(function(require) {
this.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max});
}
},
getOrCreateItem: function addOrGetItem(key) {
var items = this.get('items');
key = Facet.Item.checkNull(key);
var item = items.get(key);
if (!item) {
item = new Facet.Item({key: key, value: 0});
}
return item;
},
addOrGetItem: function addOrGetItem(key) {
var item = this.getOrCreateItem(key);
this.get('items').add(item);
return item;
},
getFacetFormData: function(facetName, options) {
options = options || {};
var result = {
......@@ -338,7 +353,10 @@ define(function(require) {
var valueFormatter;
var facetOptions = {};
var quoteFormatter = function(value) {
return value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
if (value === 'null' || value === undefined || value === null) {
return '(*:* -[\'\' TO *])';
}
return value && value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
};
var rangeFormatter = function(from, to) {
return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']';
......@@ -512,9 +530,35 @@ define(function(require) {
}, this);
return Item.__super__.initialize.apply(this, arguments);
}
}, {
checkNull: function checkNull(value) {
return (value === 'null' || value === undefined) ? null : value;
},
});
var ItemCollection = Backbone.Collection.extend({
model: Item,
initialize: function initialize(models, options) {
ItemCollection.__super__.initialize.apply(this, arguments);
this.on('update', function(model, options) {
var items = this;
_.each(options.changes.added, function(item) {
if (!item.get('key')) {
items._nullItem = item;
}
});
_.each(options.changes.removed, function(item) {
if (!item.get('key')) {
delete items._nullItem;
}
});
}, this);
},
get: function(obj) {
if (Item.checkNull(obj) === null) {
return this._nullItem;
}
return ItemCollection.__super__.get.apply(this, arguments);
},
remove: function() {
}
});
......
......@@ -88,16 +88,7 @@ define(function(require) {
} else if (field === 'max') {
impl.set('queryMax', parseInt(value), skipOptions);
} else if (field === 'items') {
var items = impl.get('items');
var item = items.get(value);
if (!item) {
item = new Facets.Facet.Item({key: value, value: 0});
items.add(item);
item.on('change:checked', function(model, value, options) {
this.trigger('item-change', null, null, options);
impl.trigger('item-change', null, null, options);
}, facets);
}
var item = impl.addOrGetItem(value);
item.set('checked', true, skipOptions);
}
}
......