You need to sign in or sign up before continuing.
Newer
Older

Joris Vercammen
committed
/**
* @file
* Facets views AJAX handling.
*/

Markus Kalkbrenner
committed
(function ($, Drupal, once) {

Joris Vercammen
committed
'use strict';
/**
* Keep the original beforeSend method to use it later.
*/
var beforeSend = Drupal.Ajax.prototype.beforeSend;
/**
* Trigger views AJAX refresh on click.
*/
Drupal.behaviors.facetsViewsAjax = {
attach: function (context, settings) {
// Loop through all facets.
$.each(settings.facets_views_ajax, function (facetId, facetSettings) {
// Get the View for the current facet.
var view, current_dom_id, view_path;
if (settings.views && settings.views.ajaxViews) {
$.each(settings.views.ajaxViews, function (domId, viewSettings) {
// Check if we have facet for this view.
if (facetSettings.view_id == viewSettings.view_name && facetSettings.current_display_id == viewSettings.view_display_id) {
view = $('.js-view-dom-id-' + viewSettings.view_dom_id);
current_dom_id = viewSettings.view_dom_id;
view_path = facetSettings.ajax_path;
}
});
}
if (!view || view.length != 1) {
return;
}
// Update view on summary block click.
if (updateFacetsSummaryBlock() && (facetId === 'facets_summary_ajax')) {

Markus Kalkbrenner
committed
$(once(facetId, '[data-drupal-facets-summary-id=' + facetSettings.facets_summary_id + '] ul li')).click(function (e) {

Joris Vercammen
committed
e.preventDefault();
var facetLink = $(this).find('a');
updateFacetsView(facetLink.attr('href'), current_dom_id, view_path);
});
}
// Update view on facet item click.
else {
$('[data-drupal-facet-id=' + facetId + ']').each(function (index, facet_item) {
if ($(facet_item).hasClass('js-facets-widget')) {
$(facet_item).unbind('facets_filter.facets');
$(facet_item).on('facets_filter.facets', function (event, url) {
$('.js-facets-widget').trigger('facets_filtering');
updateFacetsView(url, current_dom_id, view_path);

Joris Vercammen
committed
});
}
});
}
});
}
};
// Helper function to update views output & Ajax facets.
var updateFacetsView = function (href, current_dom_id, view_path) {

Bram Driesen
committed
// Update url.
window.historyInitiated = true;
window.history.pushState(null, document.title, href);

Joris Vercammen
committed
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
// Refresh view.
var views_parameters = Drupal.Views.parseQueryString(href);
var views_arguments = Drupal.Views.parseViewArgs(href, 'search');
var views_settings = $.extend(
{},
Drupal.views.instances['views_dom_id:' + current_dom_id].settings,
views_arguments,
views_parameters
);
// Update View.
var views_ajax_settings = Drupal.views.instances['views_dom_id:' + current_dom_id].element_settings;
views_ajax_settings.submit = views_settings;
views_ajax_settings.url = view_path + '?q=' + href;
Drupal.ajax(views_ajax_settings).execute();
// ToDo: Update views+facets with ajax on history back.
// For now we will reload the full page.
window.addEventListener("popstate", function (e) {
if (window.historyInitiated) {
window.location.reload();
}
});
// Refresh facets blocks.
updateFacetsBlocks(href);
}
// Helper function, updates facet blocks.
var updateFacetsBlocks = function (href) {
var settings = drupalSettings;
var facets_blocks = facetsBlocks();

Alexander Sluiter
committed
// Remove All Range Input Form Facet Blocks from being updated.

Denis K****
committed
if(settings.facets && settings.facets.rangeInput) {
$.each(settings.facets.rangeInput, function (index, value) {

Alexander Sluiter
committed
delete facets_blocks[value.facetId];
});
}

Joris Vercammen
committed
// Update facet blocks.

Joris Vercammen
committed
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
url: Drupal.url('facets-block-ajax'),
submit: {
facet_link: href,
facets_blocks: facets_blocks
}
};
// Update facets summary block.
if (updateFacetsSummaryBlock()) {
var facet_summary_wrapper_id = $('[data-drupal-facets-summary-id=' + settings.facets_views_ajax.facets_summary_ajax.facets_summary_id + ']').attr('id');
var facet_summary_block_id = '';
if (facet_summary_wrapper_id.indexOf('--') !== -1) {
facet_summary_block_id = facet_summary_wrapper_id.substring(0, facet_summary_wrapper_id.indexOf('--')).replace('block-', '');
}
else {
facet_summary_block_id = facet_summary_wrapper_id.replace('block-', '');
}
facet_settings.submit.update_summary_block = true;
facet_settings.submit.facet_summary_block_id = facet_summary_block_id;
facet_settings.submit.facet_summary_wrapper_id = settings.facets_views_ajax.facets_summary_ajax.facets_summary_id;
}
Drupal.ajax(facet_settings).execute();
};
// Helper function to determine if we should update the summary block.
// Returns true or false.
var updateFacetsSummaryBlock = function () {
var settings = drupalSettings;
var update_summary = false;
if (settings.facets_views_ajax.facets_summary_ajax) {
update_summary = true;
}
return update_summary;
};
// Helper function, return facet blocks.
var facetsBlocks = function () {
// Get all ajax facets blocks from the current page.
var facets_blocks = {};
$('.block-facets-ajax').each(function (index) {
var block_id_start = 'js-facet-block-id-';
var block_id = $.map($(this).attr('class').split(' '), function (v, i) {
if (v.indexOf(block_id_start) > -1) {
return v.slice(block_id_start.length, v.length);
}
}).join();
var block_selector = '#' + $(this).attr('id');
facets_blocks[block_id] = block_selector;
});
return facets_blocks;
};
/**
* Overrides beforeSend to trigger facetblocks update on exposed filter change.
*
* @param {XMLHttpRequest} xmlhttprequest
* Native Ajax object.
* @param {object} options
* jQuery.ajax options.
*/
Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) {
// Update facet blocks as well.
// Get view from options.
if (typeof options.extraData !== 'undefined' && typeof options.extraData.view_name !== 'undefined') {
var href = window.location.href;
var settings = drupalSettings;
// TODO: Maybe we should limit facet block reloads by view?
var reload = false;
$.each(settings.facets_views_ajax, function (facetId, facetSettings) {
if (facetSettings.view_id == options.extraData.view_name && facetSettings.current_display_id == options.extraData.view_display_id) {
reload = true;
}
});
if (reload) {

Joris Vercammen
committed
href = addExposedFiltersToFacetsUrl(href, options.extraData.view_name, options.extraData.view_display_id);

Joris Vercammen
committed
updateFacetsBlocks(href);
}
}
// Call the original Drupal method with the right context.
beforeSend.apply(this, arguments);
}

Joris Vercammen
committed
// Helper function to add exposed form data to facets url
var addExposedFiltersToFacetsUrl = function (href, view_name, view_display_id) {

Joris Vercammen
committed
var $exposed_form = $('form#views-exposed-form-' + view_name.replace(/_/g, '-') + '-' + view_display_id.replace(/_/g, '-'));
var params = Drupal.Views.parseQueryString(href);
$.each($exposed_form.serializeArray(), function () {

Joris Vercammen
committed
params[this.name] = this.value;
});
return href.split('?')[0] + '?' + $.param(params);
};

Markus Kalkbrenner
committed
})(jQuery, Drupal, once);