Skip to content

A Multiple Selection Widget #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 104 additions & 35 deletions core/AbstractFacetWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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);
Expand Down Expand Up @@ -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 + ':'));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

});

}));
Loading