Skip to content

Commit

Permalink
Restructure the collection query
Browse files Browse the repository at this point in the history
Closes #1143
  • Loading branch information
laurenwalker committed Oct 22, 2019
1 parent 41fb340 commit f0b18f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/js/collections/Filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ define(["jquery", "underscore", "backbone", "models/filters/Filter", "models/fil
var allGroupsQueryFragments = [],
//The complete query string that eventually gets returned
completeQuery = "",
//Get the list of filters that use the 'id' field, since these are used differently
idFilters = this.filter(function(filter){
return filter.get("fields").includes("id");
}),
//Separate the filter models in this collection by their query group.
groupedFilters = this.groupBy(function(m){
groupedFilters = _.groupBy(this.without(idFilters), function(m){
return m.get("queryGroup");
});

Expand Down Expand Up @@ -128,17 +132,33 @@ define(["jquery", "underscore", "backbone", "models/filters/Filter", "models/fil
completeQuery += this.getGroupQuery(catalogFilters);
}

//Create the grouped query for the id filters
var idFilterQuery = this.getGroupQuery(idFilters, "OR");

//Add the grouped query for the id filters
if( completeQuery.length && idFilterQuery.length ){
completeQuery = "(" + completeQuery + ")%20OR%20" + idFilterQuery;
}

//Return the completed query
return completeQuery;

},

/**
* Get a query string for a group of Filters. The Filters will be ANDed together.
* Get a query string for a group of Filters.
* The Filters will be ANDed together, unless a different operator is given.
* @param {Filter[]} filterModels - The Filters to turn into a query string
* @param {string} [operator] - The oeprator to use between filter models
* @return {string} The query string
*/
getGroupQuery: function(filterModels){
getGroupQuery: function(filterModels, operator){

//Default to the AND operator
if(typeof operator != "string"){
var operator = "AND";
}

//Start an array to contian the query fragments
var groupQueryFragments = [];

Expand All @@ -156,7 +176,7 @@ define(["jquery", "underscore", "backbone", "models/filters/Filter", "models/fil

//Join this group's query fragments with an OR operator
if( groupQueryFragments.length ){
return "(" + groupQueryFragments.join("%20AND%20") + ")"
return "(" + groupQueryFragments.join("%20" + operator + "%20") + ")"
}
//Otherwise, return an empty string
else{
Expand Down
5 changes: 5 additions & 0 deletions src/js/models/filters/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ define(['jquery', 'underscore', 'backbone'],
if( $(xml).find("operator").length ){
modelJSON.operator = this.parseTextNode(xml, "operator");
}
else{
if( modelJSON.fields.includes('id') ){
modelJSON.operator = "OR";
}
}

//Parse the exclude, if it exists
if( $(xml).find("exclude").length ){
Expand Down

0 comments on commit f0b18f9

Please sign in to comment.