-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacetapi-1804418-facet_api-allow_multiple_clicks.patch
149 lines (145 loc) · 5.68 KB
/
facetapi-1804418-facet_api-allow_multiple_clicks.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
facetapi.js | 96 +++++++++++++++++++++++++++++++++++++++
plugins/facetapi/widget_links.inc | 13 ++++++
2 files changed, 109 insertions(+)
diff --git a/facetapi.js b/facetapi.js
index 1fed227..59c06f8 100644
--- a/facetapi.js
+++ b/facetapi.js
@@ -72,6 +72,96 @@ Drupal.facetapi.Redirect.prototype.gotoHref = function() {
}
/**
+ * Constructor for the facetapi facet class.
+ */
+Drupal.facetapi.Facet = function(filter, link_id, ul_id) {
+ this.filter = filter;
+ this.link_id = link_id;
+ this.ul_id = ul_id;
+}
+
+Drupal.facetapi.Facet.prototype.toggleCheckbox = function() {
+ checkbox = jQuery('#'+this.link_id).prev('input');
+ if (checkbox.attr('checked')) {
+ checkbox.attr('checked', false);
+ }
+ else {
+ checkbox.attr('checked', 'checked');
+ }
+}
+
+/**
+ * Method to add/remove the filter to all the other items in the list
+ */
+Drupal.facetapi.Facet.prototype.toggleFilter = function() {
+ var current_filter = this.filter;
+ var tick_added;
+
+ // Work out whether we are responding to an option being ticked or unticked
+ jQuery.each($('ul#'+this.ul_id+' li'), function(index, value) {
+ if (jQuery(value).children('a').attr('data-facet') == current_filter)
+ {
+ tick_added = jQuery(value).children('input').attr('checked');
+ }
+ });
+
+ // Add (remove) the relevant option from all filters within the same facet
+ jQuery.each($('ul#'+this.ul_id+' li'), function(index, value) {
+ // Invert the add/remove behaviour if we're on the actual checkbox that has
+ // been changed, because the link should be ready for when it is NEXT clicked
+ // (we are already processing this click).
+ if (jQuery(value).children('a').attr('data-facet') == current_filter)
+ {
+ var add_filter = !tick_added;
+ }
+ else {
+ var add_filter = tick_added;
+ }
+
+ // Calculate the new destination URLs with this filter added (removed)
+ if (add_filter) {
+ var new_href = jQuery(this).children('a').attr('href') + '&f[]=' + encodeURIComponent(current_filter);
+ }
+ else {
+ var new_href = jQuery(this).children('a').attr('href');
+ new_href = new_href.replace(encodeURIComponent(current_filter).replace('*', '%2A'),'');
+ }
+
+
+ /* Replace the existing destinations, both href and javascript */
+
+ // First get variables for our link and checkbox, and remove our old handlers
+ var $link = jQuery(this).children('a').unbind('click');
+ var checkbox = jQuery(this).children('input').unbind('click');
+
+ // Replace the URL on the checkbox and link
+ redirect = new Drupal.facetapi.Redirect(new_href);
+ checkbox.click($.proxy(redirect, 'gotoHref'));
+ // Use a tiny timeout so that the browser starts navigating to the link
+ // before we change where we're going to navigate to
+ setTimeout(function() {$link.attr('href', new_href);}, 50);
+
+
+ // We had to remove and re-bind the toggleFilter call, so it gets called
+ // AFTER the redirect in all cases
+ facet = new Drupal.facetapi.Facet($link.attr('data-facet'), $link.attr('id'), $link.parent().parent().attr('id'));
+ $link.click($.proxy(facet, 'toggleCheckbox'));
+ $link.click($.proxy(facet, 'toggleFilter'));
+ checkbox.click($.proxy(facet, 'toggleFilter'));
+ });
+
+ // Hide the other facets while we're processing this one
+ jQuery('ul.facetapi-facetapi-checkbox-links').css('visibility', 'hidden');
+ jQuery('#block-iexplore-departure-solr-price-range-filter').css('visibility', 'hidden');
+ jQuery('ul.facetapi-facetapi-checkbox-links').parent().addClass('ajax-loading');
+ jQuery('#iexplore-price-range-filter-wrapper').addClass('ajax-loading');
+
+ // But leave the one we're interacting with alone
+ $('ul#'+this.ul_id).css('visibility', 'visible');
+ $('ul#'+this.ul_id).parent().removeClass('ajax-loading');
+}
+
+/**
* Replace an unclick link with a checked checkbox.
*/
Drupal.facetapi.makeCheckbox = function() {
@@ -93,6 +183,12 @@ Drupal.facetapi.makeCheckbox = function() {
var href = $link.attr('href');
redirect = new Drupal.facetapi.Redirect(href);
checkbox.click($.proxy(redirect, 'gotoHref'));
+
+ facet = new Drupal.facetapi.Facet($link.attr('data-facet'), $link.attr('id'), $link.parent().parent().attr('id'));
+ $link.click($.proxy(facet, 'toggleCheckbox'));
+ $link.click($.proxy(facet, 'toggleFilter'));
+ checkbox.click($.proxy(facet, 'toggleFilter'));
+
if (active) {
// Add the checkbox, hide the link.
$link.before(checkbox).hide();
diff --git a/plugins/facetapi/widget_links.inc b/plugins/facetapi/widget_links.inc
index fa4862c..5ff0d3f 100644
--- a/plugins/facetapi/widget_links.inc
+++ b/plugins/facetapi/widget_links.inc
@@ -38,6 +38,9 @@ class FacetapiWidgetLinks extends FacetapiWidget {
*/
public function execute() {
$element = &$this->build[$this->facet['field alias']];
+ foreach ($element as $index => $item) {
+ $element[$index]['#facetapi_facet_id'] = $this->facet['name'];
+ }
// Sets each item's theme hook, builds item list.
$this->setThemeHooks($element);
@@ -114,6 +117,16 @@ class FacetapiWidgetLinks extends FacetapiWidget {
$variables['options']['attributes']['class'][] = 'facetapi-zero-results';
}
+ if (isset($item['#facetapi_facet_id'])) {
+ $variables['options']['attributes']['data-facet'] = t('@facet:@value', array(
+ '@facet' => $item['#facetapi_facet_id'],
+ '@value' => $value
+ ));
+ }
+
+ // Add an ID to identify this link.
+ $variables['options']['attributes']['id'] = drupal_html_id('facetapi-link');
+
// If the item has no children, it is a leaf.
if (empty($item['#item_children'])) {
$row['class'][] = 'leaf';