From e88a1ce0292200f34d8e22b1507dd766e61e7a1a Mon Sep 17 00:00:00 2001 From: Gabriel Calegari Date: Fri, 20 Dec 2013 08:32:59 +0100 Subject: [PATCH] A Multiple Selection Widget --- core/AbstractFacetWidget.js | 139 ++++++-- core/AbstractFacetWidget2.js | 333 ++++++++++++++++++++ core/ParameterStore.js | 3 +- examples/reuters/index.html | 18 +- examples/reuters/js/reuters.js | 34 +- examples/reuters/widgets/ResultWidget.js | 24 +- examples/reuters/widgets/TagcloudWidget.js | 91 ++++-- examples/reuters/widgets/TagcloudWidget2.js | 90 ++++++ 8 files changed, 634 insertions(+), 98 deletions(-) create mode 100644 core/AbstractFacetWidget2.js create mode 100644 examples/reuters/widgets/TagcloudWidget2.js diff --git a/core/AbstractFacetWidget.js b/core/AbstractFacetWidget.js index 9e5ca337..178827f5 100644 --- a/core/AbstractFacetWidget.js +++ b/core/AbstractFacetWidget.js @@ -29,15 +29,22 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( AjaxSolr.extend(this, { start: 0, field: null, - multivalue: true + multivalue: false }, attributes); }, - init: function () { - this.initStore(); - }, - - /** +init: function () { + this.manager.store.add('facet.field', new AjaxSolr.Parameter({ + name: 'facet.field', + value: this.field, + locals: { + ex: this.field + } + })); + this.initStore(); + }, + + /** * Add facet parameters to the parameter store. */ initStore: function () { @@ -58,7 +65,7 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( // Set facet.field, facet.date or facet.range to truthy values to add // related per-field parameters to the parameter store. if (this['facet.field'] !== undefined) { - this.manager.store.addByValue('facet.field', this.field); + this.manager.store.add('facet.field', new AjaxSolr.Parameter({ name: 'facet.field', value: this.field, locals: { ex: this.field } })); } else if (this['facet.date'] !== undefined) { this.manager.store.addByValue('facet.date', this.field); @@ -103,41 +110,101 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( * * @returns {Boolean} Whether the selection changed. */ - set: function (value) { - return this.changeSelection(function () { - var a = this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':')), - b = this.manager.store.addByValue('fq', this.fq(value)); - return a || b; - }); - }, + +set: function (value) { + + //checkedValue = value; + return this.changeSelection(function () { + + var indices = this.manager.store.find('fq', new RegExp('^-?' + + this.field + ':')); + if (indices) { + this.manager.store.params['fq'][indices[0]] = new + AjaxSolr.Parameter({ + name: 'fq', + value: this.manager.store.params['fq'] + [indices[0]].val() + ' OR ' + this.fq(value), + locals: { + tag: this.field + } + }); + return true; + } else { + return this.manager.store.add('fq', new AjaxSolr.Parameter({ + name: 'fq', + value: this.fq(value), + locals: { + tag: this.field + } + })); + } + }); + }, /** * Adds a filter query. * * @returns {Boolean} Whether a filter query was added. */ - add: function (value) { - return this.changeSelection(function () { - return this.manager.store.addByValue('fq', this.fq(value)); - }); - }, + +add: function (value) { + + return this.changeSelection(function () { + return this.manager.store.add('fq', new AjaxSolr.Parameter({ + name: 'fq', + value: this.fq(value), + locals: { + tag: this.field + } + })); + }); + }, + /** * Removes a filter query. * * @returns {Boolean} Whether a filter query was removed. */ - remove: function (value) { - return this.changeSelection(function () { - return this.manager.store.removeByValue('fq', this.fq(value)); - }); - }, + +remove: function (value, field) { + var self = this; + return this.changeSelection(function () { + + for (var i = 0, l = this.manager.store.params['fq'].length; i < l; i++) { + var mySplitResult = this.manager.store.params['fq'] + [i].value.split(" OR "); + var count = mySplitResult.length; + for (var j = 0; j < mySplitResult.length; j++) { + var v = field + ":" + value; + if (value.match(" ") != null && + mySplitResult[j].localeCompare(v) != 0 && mySplitResult[j].split(":")[0].localeCompare(field) === 0) { + value = '"' + value + '"'; + } + v = field + ":" + value; + if (mySplitResult[j].localeCompare(v) == 0) { + mySplitResult.splice(j, 1); + var str = mySplitResult.join(" OR "); + if (count > 1) { + this.manager.store.params['fq'][i].value = str; + } else { + this.manager.store.params['fq'].splice(i, 1); + } + return true; + } + } + } + return false; + }); + }, + /** * Removes all filter queries using the widget's facet field. * * @returns {Boolean} Whether a filter query was removed. */ + clear: function () { return this.changeSelection(function () { return this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':')); @@ -258,8 +325,8 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( clickHandler: function (value) { var self = this, meth = this.multivalue ? 'add' : 'set'; return function () { - if (self[meth].call(self, value)) { - self.doRequest(); + if (self[meth].call(self,value)) { + self.doRequest(0); } return false; } @@ -270,24 +337,26 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( * @returns {Function} Sends a request to Solr if it successfully removes a * filter query with the given value. */ - unclickHandler: function (value) { - var self = this; - return function () { - if (self.remove(value)) { - self.doRequest(); - } - return false; - } - }, + unclickHandler: function (value,field) { + var self = this; + return function () { + if (self.remove(value,field)) { + self.manager.doRequest(0); + } + return false; + } + }, /** * @param {String} value The facet value. * @param {Boolean} exclude Whether to exclude this fq parameter value. * @returns {String} An fq parameter value. */ + fq: function (value, exclude) { return (exclude ? '-' : '') + this.field + ':' + AjaxSolr.Parameter.escapeValue(value); } + }); })); diff --git a/core/AbstractFacetWidget2.js b/core/AbstractFacetWidget2.js new file mode 100644 index 00000000..eee9b0d5 --- /dev/null +++ b/core/AbstractFacetWidget2.js @@ -0,0 +1,333 @@ +(function (callback) { + if (typeof define === 'function' && define.amd) { + define(['core/AbstractWidget', 'core/Parameter'], callback); + } + else { + callback(); + } +}(function () { + +/** + * Baseclass for all facet widgets. + * + * @class AbstractFacetWidget + * @augments AjaxSolr.AbstractWidget + */ +AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend( + /** @lends AjaxSolr.AbstractFacetWidget.prototype */ + { + /** + * @param {Object} attributes + * @param {String} attributes.field The field to facet on. + * @param {Number} [attributes.start] This widget will by default set the + * offset parameter to 0 on each request. + * @param {Boolean} [attributes.multivalue] Set to false to force a + * single "fq" parameter for this widget. Defaults to true. + */ + constructor: function (attributes) { + AjaxSolr.AbstractFacetWidget.__super__.constructor.apply(this, arguments); + AjaxSolr.extend(this, { + start: 0, + field: null, + multivalue: false + }, attributes); + }, + + init: function () { + this.manager.store.add('facet.field', new AjaxSolr.Parameter({ name:'facet.field', value: this.field, locals: { ex:this.field } })); + }, + + /** + * Add facet parameters to the parameter store. + */ + initStore: function () { + /* http://wiki.apache.org/solr/SimpleFacetParameters */ + var parameters = [ + 'facet.prefix', + 'facet.sort', + 'facet.limit', + 'facet.offset', + 'facet.mincount', + 'facet.missing', + 'facet.method', + 'facet.enum.cache.minDf' + ]; + + this.manager.store.addByValue('facet', true); + + // Set facet.field, facet.date or facet.range to truthy values to add + // related per-field parameters to the parameter store. + if (this['facet.field'] !== undefined) { + this.manager.store.add('facet.field', new AjaxSolr.Parameter({ name: 'facet.field', value: this.field, locals: { ex: this.field } })); + } + else if (this['facet.date'] !== undefined) { + this.manager.store.addByValue('facet.date', this.field); + parameters = parameters.concat([ + 'facet.date.start', + 'facet.date.end', + 'facet.date.gap', + 'facet.date.hardend', + 'facet.date.other', + 'facet.date.include' + ]); + } + else if (this['facet.range'] !== undefined) { + this.manager.store.addByValue('facet.range', this.field); + parameters = parameters.concat([ + 'facet.range.start', + 'facet.range.end', + 'facet.range.gap', + 'facet.range.hardend', + 'facet.range.other', + 'facet.range.include' + ]); + } + + for (var i = 0, l = parameters.length; i < l; i++) { + if (this[parameters[i]] !== undefined) { + this.manager.store.addByValue('f.' + this.field + '.' + parameters[i], this[parameters[i]]); + } + } + }, + + /** + * @returns {Boolean} Whether any filter queries have been set using this + * widget's facet field. + */ + isEmpty: function () { + return !this.manager.store.find('fq', new RegExp('^-?' + this.field + ':')); + }, + + /** + * Sets the filter query. + * + * @returns {Boolean} Whether the selection changed. + */ + set: function (value) { + return this.changeSelection(function () { + var indices = this.manager.store.find('fq', new RegExp('^-?' + this.field + ':')); + if (indices) { + this.manager.store.params['fq'][indices[0]] = new AjaxSolr.Parameter({ name: 'fq', value: this.manager.store.params['fq'][indices[0]].val() + ' OR ' + this.fq(value), locals: { tag:this.field } }); + return true; + } + else { + return this.manager.store.add('fq', new AjaxSolr.Parameter({ name: 'fq', value: this.fq(value), locals: { tag: this.field } })); + } +}); + /*return this.changeSelection(function () { + var a = this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':')), + b = this.manager.store.add('fq', new AjaxSolr.Parameter({ name: 'fq', value: this.fq(value), locals: { tag: this.field } })); + return a || b; + });*/ + }, + + /** + * Adds a filter query. + * + * @returns {Boolean} Whether a filter query was added. + */ + add: function (value) { + return this.changeSelection(function () { + return this.manager.store.add('fq', new AjaxSolr.Parameter({ name: 'fq', value: this.fq(value), locals: { tag: this.field } })); + }); + }, + + /** + * Removes a filter query. + * + * @returns {Boolean} Whether a filter query was removed. + */ + +remove: function (value, field) { + var self = this; + return this.changeSelection(function () { + for (var i = 0, l = this.manager.store.params['fq'].length; i< l; i++) { + var mySplitResult = this.manager.store.params['fq'][i].value.split(" OR "); + var count = mySplitResult.length; + for(var j = 0; j < mySplitResult.length; j++){ + var v = field + ":" + value; + if (value.match(" ") != null &&mySplitResult[j].localeCompare(v) != 0 && mySplitResult[j].split(":")[0].localeCompare(field)===0) { + value = '"' + value + '"'; + } + v = field + ":" + value; + if (mySplitResult[j].localeCompare(v) == 0) { + mySplitResult.splice(j,1); + var str = mySplitResult.join(" OR "); + if (count > 1) { + this.manager.store.params['fq'][i].value = str; + } else { + this.manager.store.params['fq'].splice(i,1); + } + return true; + } + } + } + return false; +}); +}, + + + /*remove: function (value) { + return this.changeSelection(function () { + return this.manager.store.removeByValue('fq', this.fq(value)); + }); + }, */ + + /** + * Removes all filter queries using the widget's facet field. + * + * @returns {Boolean} Whether a filter query was removed. + */ + clear: function () { + return this.changeSelection(function () { + return this.manager.store.removeByValue('fq', new RegExp('^-?' + this.field + ':')); + }); + }, + + /** + * Helper for selection functions. + * + * @param {Function} Selection function to call. + * @returns {Boolean} Whether the selection changed. + */ + changeSelection: function (func) { + changed = func.apply(this); + if (changed) { + this.afterChangeSelection(); + } + return changed; + }, + + /** + * An abstract hook for child implementations. + * + *

This method is executed after the filter queries change.

+ */ + afterChangeSelection: function () {}, + + /** + * One of "facet.field", "facet.date" or "facet.range" must be set on the + * widget in order to determine where the facet counts are stored. + * + * @returns {Array} An array of objects with the properties facet and + * count, e.g { facet: 'facet', count: 1 }. + */ + getFacetCounts: function () { + var property; + if (this['facet.field'] !== undefined) { + property = 'facet_fields'; + } + else if (this['facet.date'] !== undefined) { + property = 'facet_dates'; + } + else if (this['facet.range'] !== undefined) { + property = 'facet_ranges'; + } + if (property !== undefined) { + switch (this.manager.store.get('json.nl').val()) { + case 'map': + return this.getFacetCountsMap(property); + case 'arrarr': + return this.getFacetCountsArrarr(property); + default: + return this.getFacetCountsFlat(property); + } + } + throw 'Cannot get facet counts unless one of the following properties is set to "true" on widget "' + this.id + '": "facet.field", "facet.date", or "facet.range".'; + }, + + /** + * Used if the facet counts are represented as a JSON object. + * + * @param {String} property "facet_fields", "facet_dates", or "facet_ranges". + * @returns {Array} An array of objects with the properties facet and + * count, e.g { facet: 'facet', count: 1 }. + */ + getFacetCountsMap: function (property) { + var counts = []; + for (var facet in this.manager.response.facet_counts[property][this.field]) { + counts.push({ + facet: facet, + count: parseInt(this.manager.response.facet_counts[property][this.field][facet]) + }); + } + return counts; + }, + + /** + * Used if the facet counts are represented as an array of two-element arrays. + * + * @param {String} property "facet_fields", "facet_dates", or "facet_ranges". + * @returns {Array} An array of objects with the properties facet and + * count, e.g { facet: 'facet', count: 1 }. + */ + getFacetCountsArrarr: function (property) { + var counts = []; + for (var i = 0, l = this.manager.response.facet_counts[property][this.field].length; i < l; i++) { + counts.push({ + facet: this.manager.response.facet_counts[property][this.field][i][0], + count: parseInt(this.manager.response.facet_counts[property][this.field][i][1]) + }); + } + return counts; + }, + + /** + * Used if the facet counts are represented as a flat array. + * + * @param {String} property "facet_fields", "facet_dates", or "facet_ranges". + * @returns {Array} An array of objects with the properties facet and + * count, e.g { facet: 'facet', count: 1 }. + */ + getFacetCountsFlat: function (property) { + var counts = []; + for (var i = 0, l = this.manager.response.facet_counts[property][this.field].length; i < l; i += 2) { + counts.push({ + facet: this.manager.response.facet_counts[property][this.field][i], + count: parseInt(this.manager.response.facet_counts[property][this.field][i+1]) + }); + } + return counts; + }, + + /** + * @param {String} value The value. + * @returns {Function} Sends a request to Solr if it successfully adds a + * filter query with the given value. + */ + clickHandler: function (value) { + var self = this, meth = this.multivalue ? 'add' : 'set'; + return function () { + if (self[meth].call(self, value)) { + self.doRequest(0); + } + return false; + } + }, + + /** + * @param {String} value The value. + * @returns {Function} Sends a request to Solr if it successfully removes a + * filter query with the given value. + */ + unclickHandler: function (value) { + var self = this; + return function () { + if (self.remove(value)) { + self.doRequest(0); + } + return false; + } + }, + + /** + * @param {String} value The facet value. + * @param {Boolean} exclude Whether to exclude this fq parameter value. + * @returns {String} An fq parameter value. + */ + fq: function (value, exclude) { + return (exclude ? '-' : '') + this.field + ':' + AjaxSolr.Parameter.escapeValue(value); + } +}); + +})); diff --git a/core/ParameterStore.js b/core/ParameterStore.js index 19c44a6f..1b1b5b84 100644 --- a/core/ParameterStore.js +++ b/core/ParameterStore.js @@ -5,7 +5,8 @@ else { callback(); } -}(function () { +} +(function () { /** * The ParameterStore, as its name suggests, stores Solr parameters. Widgets diff --git a/examples/reuters/index.html b/examples/reuters/index.html index 1088e3fa..7794f3d7 100644 --- a/examples/reuters/index.html +++ b/examples/reuters/index.html @@ -14,6 +14,7 @@ + @@ -21,7 +22,6 @@ - @@ -51,18 +51,16 @@

Search

-

Top Topics

-
+

Top Karmas

+
-

Top Organisations

-
+

Age

+
+ +

Sex

+
-

Top Exchanges

-
-

By Country

-
-

By Date

diff --git a/examples/reuters/js/reuters.js b/examples/reuters/js/reuters.js index 99a0cf3d..e534e253 100644 --- a/examples/reuters/js/reuters.js +++ b/examples/reuters/js/reuters.js @@ -4,7 +4,7 @@ var Manager; $(function () { Manager = new AjaxSolr.Manager({ - solrUrl: 'http://evolvingweb.ca/solr/reuters/' + solrUrl: 'http://localhost:8080/solr/collection1/' }); Manager.addWidget(new AjaxSolr.ResultWidget({ id: 'result', @@ -20,12 +20,14 @@ var Manager; $('#pager-header').html($('').text('displaying ' + Math.min(total, offset + 1) + ' to ' + Math.min(total, offset + perPage) + ' of ' + total)); } })); - var fields = [ 'topics', 'organisations', 'exchanges' ]; + + var fields = [ 'comment_id', 'comment_karma' ,'comment_content', 'comment_date', 'comment_randkey', 'comment_user_id', 'comment_link_id', 'user_age', 'user_sex' ]; for (var i = 0, l = fields.length; i < l; i++) { Manager.addWidget(new AjaxSolr.TagcloudWidget({ id: fields[i], target: '#' + fields[i], - field: fields[i] + field: fields[i], + multivalue: false })); } Manager.addWidget(new AjaxSolr.CurrentSearchWidget({ @@ -35,32 +37,24 @@ var Manager; Manager.addWidget(new AjaxSolr.AutocompleteWidget({ id: 'text', target: '#search', - fields: [ 'topics', 'organisations', 'exchanges' ] - })); - Manager.addWidget(new AjaxSolr.CountryCodeWidget({ - id: 'countries', - target: '#countries', - field: 'countryCodes' + fields: [ 'comment_content' ] })); Manager.addWidget(new AjaxSolr.CalendarWidget({ id: 'calendar', target: '#calendar', - field: 'date' + field: 'comment_date' })); Manager.init(); Manager.store.addByValue('q', '*:*'); var params = { facet: true, - 'facet.field': [ 'topics', 'organisations', 'exchanges', 'countryCodes' ], - 'facet.limit': 20, - 'facet.mincount': 1, - 'f.topics.facet.limit': 50, - 'f.countryCodes.facet.limit': -1, - 'facet.date': 'date', - 'facet.date.start': '1987-02-26T00:00:00.000Z/DAY', - 'facet.date.end': '1987-10-20T00:00:00.000Z/DAY+1DAY', - 'facet.date.gap': '+1DAY', - 'json.nl': 'map' + 'facet.field': [ 'comment_id', 'comment_karma' ,'comment_content', 'comment_date', 'comment_randkey', 'comment_user_id', 'comment_link_id', 'user_age', 'user_sex' ], + 'facet.limit': 10, + 'facet.mincount': 20, + 'facet.date': 'comment_date', + 'facet.date.start': '2005-12-01T00:00:00.000Z/DAY', + 'facet.date.end': '2006-04-01T00:00:00.000Z/DAY+1DAY', + 'facet.date.gap': '+1DAY' }; for (var name in params) { Manager.store.addByValue(name, params[name]); diff --git a/examples/reuters/widgets/ResultWidget.js b/examples/reuters/widgets/ResultWidget.js index 51b16ff0..4e408a8b 100644 --- a/examples/reuters/widgets/ResultWidget.js +++ b/examples/reuters/widgets/ResultWidget.js @@ -43,11 +43,12 @@ AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({ $(this.target).append(this.template(doc)); var items = []; - items = items.concat(this.facetLinks('topics', doc.topics)); - items = items.concat(this.facetLinks('organisations', doc.organisations)); - items = items.concat(this.facetLinks('exchanges', doc.exchanges)); - var $links = $('#links_' + doc.id); + items = items.concat(this.facetLinks('comment_id', doc.comment_id)); + items = items.concat(this.facetLinks('comment_date', doc.comment_date)); + items = items.concat(this.facetLinks('comment_content', doc.comment_content)); + + var $links = $('#links_' +doc.comment_id); $links.empty(); for (var j = 0, m = items.length; j < m; j++) { $links.append($('
  • ').append(items[j])); @@ -57,17 +58,17 @@ AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({ template: function (doc) { var snippet = ''; - if (doc.text.length > 300) { - snippet += doc.dateline + ' ' + doc.text.substring(0, 300); - snippet += '' + doc.text.substring(300); + if (doc.comment_content.length > 300) { + snippet += 'DATE: '+doc.comment_date +' USER:'+doc.comment_user_id +' '+ doc.comment_content.substring(0, 300); + snippet += '' + doc.comment_content.substring(300); snippet += ' more'; } else { - snippet += doc.dateline + ' ' + doc.text; + snippet += 'DATE: '+doc.comment_date +' USER:'+doc.comment_user_id +' '+ doc.comment_content; } - var output = '

    ' + doc.title + '

    '; - output += ''; + var output = '

    ' + 'ID: '+doc.comment_id + ' -- AGE: ' + doc.user_age + ' -- KARMA: ' + doc.comment_karma + ' -- SEX: ' + doc.user_sex + '

    '; + output += ''; output += '

    ' + snippet + '

    '; return output; }, @@ -91,4 +92,5 @@ AjaxSolr.ResultWidget = AjaxSolr.AbstractWidget.extend({ } }); -})(jQuery); \ No newline at end of file +})(jQuery); + diff --git a/examples/reuters/widgets/TagcloudWidget.js b/examples/reuters/widgets/TagcloudWidget.js index 71a5d46e..f9beda0f 100644 --- a/examples/reuters/widgets/TagcloudWidget.js +++ b/examples/reuters/widgets/TagcloudWidget.js @@ -1,36 +1,85 @@ (function ($) { AjaxSolr.TagcloudWidget = AjaxSolr.AbstractFacetWidget.extend({ - afterRequest: function () { - if (this.manager.response.facet_counts.facet_fields[this.field] === undefined) { + + +afterRequest: function () { + var links = [], + selected = [], + unselected = [] + + if (this.manager.response.facet_counts.facet_fields[this.field] === undefined) { $(this.target).html('no items found in current selection'); + return; } + var maxCount = 0; var objectedItems = []; for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) { - var count = parseInt(this.manager.response.facet_counts.facet_fields[this.field][facet]); - if (count > maxCount) { - maxCount = count; + var count = this.manager.response.facet_counts.facet_fields[this.field][facet]; + if (this.manager.store.find('fq', new RegExp (this.fq(facet)))) { + selected.push({ + facet: facet, + field: this.field, + count: count + }); + } else if(facet) { + unselected.push({ + facet: facet, + field: this.field, + count: count + }); } - objectedItems.push({ facet: facet, count: count }); - } - objectedItems.sort(function (a, b) { - return a.facet < b.facet ? -1 : 1; - }); - - $(this.target).empty(); - for (var i = 0, l = objectedItems.length; i < l; i++) { - var facet = objectedItems[i].facet; - $(this.target).append( - $('') - .text(facet) - .addClass('tagcloud_size_' + parseInt(objectedItems[i].count / maxCount * 10)) - .click(this.clickHandler(facet)) - ); + + objectedItems.push( + { + facet: facet, + count: count + }); + } + + $(this.target).empty(); + var output = []; + var i = 0; + for (facet in this.manager.response.facet_counts.facet_fields[this.field]) { + output[i] = this.manager.response.facet_counts.facet_fields[this.field][facet]; + i++; + } + + unselected = output; + + //Selected items. It will be shown in checkbox + for (var i = 0, l = selected.length; i < l; i++) { + var facet = selected[i].facet; + var count = selected[i].count; + + $(this.target).append( + $(''+facet+ '
    ') + .text(facet).click(this.unclickHandler(facet, this.field)) + ); + } + + + for (var i = 0, l = unselected.length-1; i < l; i++) { + var facet = unselected[i].facet; + var value = unselected[i]; + var count = unselected[i+1]; + + if(i % 2 == 0) { + $(this.target).append( + $(''+value+ ' (' +count+ ') ' + '
    ') + .text(value+'('+ count +')') + .click(this.clickHandler(value)) + ); + } + } } -}); + + +} +); })(jQuery); diff --git a/examples/reuters/widgets/TagcloudWidget2.js b/examples/reuters/widgets/TagcloudWidget2.js new file mode 100644 index 00000000..c4fe23db --- /dev/null +++ b/examples/reuters/widgets/TagcloudWidget2.js @@ -0,0 +1,90 @@ +(function ($) { + +AjaxSolr.TagcloudWidget = AjaxSolr.AbstractFacetWidget.extend({ + + +afterRequest: function () { + var links = [], + selected = [], + unselected = [] + + if (this.manager.response.facet_counts.facet_fields[this.field] === undefined) { + $(this.target).html('no items found in current selection'); + + return; + } + + + var maxCount = 0; + var objectedItems = []; + for (var facet in this.manager.response.facet_counts.facet_fields[this.field]) { + var count = this.manager.response.facet_counts.facet_fields[this.field][facet]; + if (this.manager.store.find('fq', new RegExp (this.fq(facet)))) { + selected.push({ + facet: facet, + field: this.field, + count: count + }); + } else if(facet) { + unselected.push({ + //facet: facet, + field: this.field, + count: count + }); + } + + objectedItems.push( + { + facet: facet, + count: count + }); + + } + + $(this.target).empty(); + var output = ''; + for (facet in this.manager.response.facet_counts.facet_fields[this.field]) { + output += facet + ' ('+ this.manager.response.facet_counts.facet_fields[this.field][facet]+') '; + } +/* + //Selected items. It will be shown in checkbox UP + for (var i = 0, l = selected.length; i < l; i++) { + + var value = selected[i].count; + var count = selected[i].count; + + $(this.target).append( + $(''+ value + ' (' +count+ ') ' + '
    ') + .text(value+'('+ count +')').click(this.unclickHandler(value, this.field)) + ); + } + */ + + + + // Unchecked + // Example of unselected array + // var = [value1, count1, value2, count2...] + // You want select pair positions. Odd positions are counts + for (var i = 0, l = unselected.length-1; i < l; i++) { + var facet = unselected[i].facet; + var value = unselected[i].count; + var count = unselected[i+1].count; + + if(i % 2 == 0) { + $(this.target).append( + $(''+value+ ' (' +count+ ') ' + '
    ') + .text(value+'('+ count +')') + .click(this.clickHandler(value)) + ); + } + } + + + } + + +} +); + +})(jQuery);