Skip to content

Commit

Permalink
add support for disabled facets - these facets are not displayed or q…
Browse files Browse the repository at this point in the history
…ueried for, and they cannot have filters associated with them. Use them when enabling/disabling facets dynamically during, say, pre-search callback
  • Loading branch information
richard-jones committed Mar 10, 2015
1 parent 74bea47 commit 15b89a1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
3 changes: 3 additions & 0 deletions bootstrap2.facetview.theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ function facetList(options) {
continue;
}

// note that we do render disabled facets, so that they are available for enabling/disabling
// by callbacks

var type = facet.type ? facet.type : "terms"
if (type === "terms") {
thefilters += options.render_terms_facet(facet, options)
Expand Down
3 changes: 3 additions & 0 deletions bootstrap3.facetview.theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ function facetList(options) {
continue;
}

// note that we do render disabled facets, so that they are available for enabling/disabling
// by callbacks

var type = facet.type ? facet.type : "terms"
if (type === "terms") {
thefilters += options.render_terms_facet(facet, options)
Expand Down
8 changes: 8 additions & 0 deletions es.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ function getFilters(params) {
for (var field in filter_definition) {
if (filter_definition.hasOwnProperty(field)) {
var facet = selectFacet(field);

// FIXME: is this the right behaviour?
// ignore any filters from disabled facets
if (facet.disabled) { continue }

var filter_list = filter_definition[field];

if (facet.type === "terms") {
Expand Down Expand Up @@ -320,6 +325,8 @@ function elasticSearchQuery(params) {
qs['facets'] = {};
for (var item = 0; item < options.facets.length; item++) {
var defn = options.facets[item];
if (defn.disabled) { continue }

var size = defn.size;

// add a bunch of extra values to the facets to deal with the shard count issue
Expand Down Expand Up @@ -463,6 +470,7 @@ function elasticSearchSuccess(callback) {
for (var item in data.facets) {
if (data.facets.hasOwnProperty(item)) {
var facet = data.facets[item];

// handle any terms facets
if ("terms" in facet) {
var terms = facet["terms"];
Expand Down
102 changes: 63 additions & 39 deletions jquery.facetview2.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ function getUrlVars() {
"type": "term|range|geo_distance|statistical|date_histogram", // the kind of facet this will be
"open" : true|false, // whether the facet should be open or closed (initially)
"hidden" : true|false // whether the facet should be displayed at all (e.g. you may just want the data for a callback)
"disabled" : true|false // whether the facet should be acted upon in any way. This might be useful if you want to enable/disable facets under different circumstances via a callback
// terms facet only
Expand Down Expand Up @@ -640,6 +641,7 @@ function getUrlVars() {
if (!("interval" in facet)) { facet["interval"] = provided_options.default_date_histogram_interval }
if (!("hide_empty_date_bin" in facet)) { facet["hide_empty_date_bin"] = provided_options.default_hide_empty_date_bin }
if (!("sort" in facet)) { facet["sort"] = provided_options.default_date_histogram_sort }
if (!("disabled" in facet)) { facet["disabled"] = false } // no default setter for this - if you don't specify disabled, they are not disabled
}

return provided_options
Expand Down Expand Up @@ -1071,45 +1073,47 @@ function getUrlVars() {
$('.facetview_filters', obj).each(function() {
var facet = selectFacet(options, $(this).attr('data-href'));
var values = "values" in facet ? facet["values"] : [];
var visible = true;
if (facet.type === "terms") {
// terms facet becomes deactivated if the number of results is less than the deactivate threshold defined
visible = values.length > facet.deactivate_threshold;
} else if (facet.type === "range") {
// range facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i=0; i < values.length; i=i+1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
var visible = !facet.disabled;
if (!facet.disabled) {
if (facet.type === "terms") {
// terms facet becomes deactivated if the number of results is less than the deactivate threshold defined
visible = values.length > facet.deactivate_threshold;
} else if (facet.type === "range") {
// range facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i = 0; i < values.length; i = i + 1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
}
}
}
visible = view
} else if (facet.type === "geo_distance") {
// distance facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i=0; i < values.length; i=i+1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
visible = view
} else if (facet.type === "geo_distance") {
// distance facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i = 0; i < values.length; i = i + 1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
}
}
}
visible = view
} else if (facet.type === "date_histogram") {
// date histogram facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i=0; i < values.length; i=i+1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
visible = view
} else if (facet.type === "date_histogram") {
// date histogram facet becomes deactivated if there is a count of 0 in every value
var view = false;
for (var i = 0; i < values.length; i = i + 1) {
var val = values[i];
if (val.count > 0) {
view = true;
break
}
}
visible = view
}
visible = view
// FIXME: statistical facet?
}
// FIXME: statistical facet?

options.behaviour_facet_visibility(options, obj, facet, visible)
});
Expand Down Expand Up @@ -1202,6 +1206,10 @@ function getUrlVars() {
for (var each = 0; each < options.facets.length; each++) {
// get the facet, the field name and the size
var facet = options.facets[each];

// no need to populate any disabled facets
if (facet.disabled) { continue }

var field = facet['field'];
var size = facet.hasOwnProperty("size") ? facet["size"] : options.default_facet_size;

Expand Down Expand Up @@ -1249,6 +1257,18 @@ function getUrlVars() {
options.behaviour_finished_searching(options, obj);
options.searching = false;
}

function pruneActiveFilters() {
for (var i = 0; i < options.facets.length; i++) {
var facet = options.facets[i];
if (facet.disabled) {
if (facet.field in options.active_filters) {
delete options.active_filters[facet.field];
}
}
}
publishSelectedFilters();
}

function doSearch() {
// FIXME: does this have any weird side effects?
Expand All @@ -1266,7 +1286,11 @@ function getUrlVars() {

// trigger any searching notification behaviour
options.behaviour_show_searching(options, obj);


// remove from the active filters any whose facets are disabled
// (this may have happened during the pre-search callback, for example)
pruneActiveFilters();

// make the search query
var queryobj = elasticSearchQuery({"options" : options});
options.queryobj = queryobj
Expand Down Expand Up @@ -1297,10 +1321,10 @@ function getUrlVars() {
var options = $.fn.facetview.options;

// render the facetview frame which will then be populated
thefacetview = options.render_the_facetview(options);
thesearchopts = options.render_search_options(options);
thefacets = options.render_facet_list(options);
searching = options.render_searching_notification(options);
var thefacetview = options.render_the_facetview(options);
var thesearchopts = options.render_search_options(options);
var thefacets = options.render_facet_list(options);
var searching = options.render_searching_notification(options);

// now create the plugin on the page for each div
var obj = undefined;
Expand Down

0 comments on commit 15b89a1

Please sign in to comment.