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) { ...@@ -159,6 +159,11 @@ define(function(require) {
159 return function facetItemValue(a, b) { 159 return function facetItemValue(a, b) {
160 var keyA = sortKeyExtractor(a); 160 var keyA = sortKeyExtractor(a);
161 var keyB = sortKeyExtractor(b); 161 var keyB = sortKeyExtractor(b);
162 if (keyA === null) {
163 return -1;
164 } else if (keyB === null) {
165 return 1;
166 }
162 return comparator.call(facet, keyA, keyB); 167 return comparator.call(facet, keyA, keyB);
163 }; 168 };
164 })(comparator); 169 })(comparator);
...@@ -282,12 +287,8 @@ define(function(require) { ...@@ -282,12 +287,8 @@ define(function(require) {
282 } else if (excludeValues[key]) { 287 } else if (excludeValues[key]) {
283 return; 288 return;
284 } 289 }
285 var item = items.get({key: key}); 290 var item = this.getOrCreateItem(key);
286 if (item) { 291 item.set({hidden: value === 0, value: value});
287 item.set({hidden: value === 0, value: value});
288 } else {
289 item = new Facet.Item({key: key, value: value});
290 }
291 newItems.push(item); 292 newItems.push(item);
292 }, this); 293 }, this);
293 294
...@@ -320,6 +321,20 @@ define(function(require) { ...@@ -320,6 +321,20 @@ define(function(require) {
320 this.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max}); 321 this.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max});
321 } 322 }
322 }, 323 },
324 getOrCreateItem: function addOrGetItem(key) {
325 var items = this.get('items');
326 key = Facet.Item.checkNull(key);
327 var item = items.get(key);
328 if (!item) {
329 item = new Facet.Item({key: key, value: 0});
330 }
331 return item;
332 },
333 addOrGetItem: function addOrGetItem(key) {
334 var item = this.getOrCreateItem(key);
335 this.get('items').add(item);
336 return item;
337 },
323 getFacetFormData: function(facetName, options) { 338 getFacetFormData: function(facetName, options) {
324 options = options || {}; 339 options = options || {};
325 var result = { 340 var result = {
...@@ -338,7 +353,10 @@ define(function(require) { ...@@ -338,7 +353,10 @@ define(function(require) {
338 var valueFormatter; 353 var valueFormatter;
339 var facetOptions = {}; 354 var facetOptions = {};
340 var quoteFormatter = function(value) { 355 var quoteFormatter = function(value) {
341 return value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value; 356 if (value === 'null' || value === undefined || value === null) {
357 return '(*:* -[\'\' TO *])';
358 }
359 return value && value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
342 }; 360 };
343 var rangeFormatter = function(from, to) { 361 var rangeFormatter = function(from, to) {
344 return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']'; 362 return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']';
...@@ -512,9 +530,35 @@ define(function(require) { ...@@ -512,9 +530,35 @@ define(function(require) {
512 }, this); 530 }, this);
513 return Item.__super__.initialize.apply(this, arguments); 531 return Item.__super__.initialize.apply(this, arguments);
514 } 532 }
533 }, {
534 checkNull: function checkNull(value) {
535 return (value === 'null' || value === undefined) ? null : value;
536 },
515 }); 537 });
516 var ItemCollection = Backbone.Collection.extend({ 538 var ItemCollection = Backbone.Collection.extend({
517 model: Item, 539 model: Item,
540 initialize: function initialize(models, options) {
541 ItemCollection.__super__.initialize.apply(this, arguments);
542 this.on('update', function(model, options) {
543 var items = this;
544 _.each(options.changes.added, function(item) {
545 if (!item.get('key')) {
546 items._nullItem = item;
547 }
548 });
549 _.each(options.changes.removed, function(item) {
550 if (!item.get('key')) {
551 delete items._nullItem;
552 }
553 });
554 }, this);
555 },
556 get: function(obj) {
557 if (Item.checkNull(obj) === null) {
558 return this._nullItem;
559 }
560 return ItemCollection.__super__.get.apply(this, arguments);
561 },
518 remove: function() { 562 remove: function() {
519 } 563 }
520 }); 564 });
......
...@@ -88,16 +88,7 @@ define(function(require) { ...@@ -88,16 +88,7 @@ define(function(require) {
88 } else if (field === 'max') { 88 } else if (field === 'max') {
89 impl.set('queryMax', parseInt(value), skipOptions); 89 impl.set('queryMax', parseInt(value), skipOptions);
90 } else if (field === 'items') { 90 } else if (field === 'items') {
91 var items = impl.get('items'); 91 var item = impl.addOrGetItem(value);
92 var item = items.get(value);
93 if (!item) {
94 item = new Facets.Facet.Item({key: value, value: 0});
95 items.add(item);
96 item.on('change:checked', function(model, value, options) {
97 this.trigger('item-change', null, null, options);
98 impl.trigger('item-change', null, null, options);
99 }, facets);
100 }
101 item.set('checked', true, skipOptions); 92 item.set('checked', true, skipOptions);
102 } 93 }
103 } 94 }
......