diff --git a/js/dropdown-widget.js b/js/dropdown-widget.js index 569e350..03f7f2c 100644 --- a/js/dropdown-widget.js +++ b/js/dropdown-widget.js @@ -37,7 +37,7 @@ // Remove the class which we are using for .once(). $dropdown.removeClass('js-facets-dropdown-links'); - $dropdown.addClass('facets-dropdown'); + $dropdown.addClass('js-facets-dropdown'); var id = $(this).data('drupal-facet-id'); var default_option_label = settings.facets.dropdown_widget[id]['facet-default-option-label']; diff --git a/js/facets-views-ajax.js b/js/facets-views-ajax.js index a0e25eb..5ff034e 100644 --- a/js/facets-views-ajax.js +++ b/js/facets-views-ajax.js @@ -29,34 +29,36 @@ Drupal.behaviors.facetsViewsAjax = { attach: function (context, settings) { - // Loop through all facets + // 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; - $.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.length != 1) { + 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 + // Update view on summary block click. if (updateFacetsSummaryBlock() && (facetId === 'facets_summary_ajax')) { - $('[data-drupal-facets-summary-id=' + facetSettings.facets_summary_id +']').children('ul').children('li').once().click(function (e) { + $('[data-drupal-facets-summary-id=' + facetSettings.facets_summary_id + ']').children('ul').children('li').once().click(function (e) { e.preventDefault(); var facetLink = $(this).find('a'); updateFacetsView(facetLink, current_dom_id, view_path); }); } - // Update view on facet item click + // Update view on facet item click. else { - $('[data-drupal-facet-id=' + facetId + ']').find('.facet-item').each(function (index, facet_item) { + $('[data-drupal-facet-id=' + facetId + ']').find('.facet-item').once().each(function (index, facet_item) { $(facet_item).children('a').click(function (e) { e.preventDefault(); updateFacetsView($(this).attr('href'), current_dom_id, view_path); @@ -64,7 +66,7 @@ }); $('[data-drupal-facet-id=' + facetId + ']').each(function (index, facet_item) { - if ($(facet_item).hasClass('facets-dropdown')) { + if ($(facet_item).hasClass('js-facets-dropdown')) { $(facet_item).unbind('change.facets'); $(facet_item).on('change.facets', function () { updateFacetsView($(this).val(), current_dom_id, view_path); @@ -78,8 +80,8 @@ }; // Helper function to update views output & Ajax facets. - var updateFacetsView = function(href, current_dom_id, view_path) { - // Refresh view + var updateFacetsView = function (href, current_dom_id, view_path) { + // Refresh view. var views_parameters = Drupal.Views.parseQueryString(href); var views_arguments = Drupal.Views.parseViewArgs(href, 'search'); var views_settings = $.extend( @@ -96,24 +98,24 @@ Drupal.ajax(views_ajax_settings).execute(); - // Update url + // Update url. window.historyInitiated = true; window.history.pushState(null, document.title, href); - // ToDo: Update views+facets with ajax on history back - // For now we will reload the full page - window.addEventListener("popstate", function(e) { + // 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 + // Refresh facets blocks. updateFacetsBlocks(href); } - // Helper function, updates facet blocks - var updateFacetsBlocks = function(href) { + // Helper function, updates facet blocks. + var updateFacetsBlocks = function (href) { var settings = drupalSettings; var facets_blocks = facetsBlocks(); @@ -126,7 +128,7 @@ } }; - // Update facets summary block + // 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 = ''; @@ -136,7 +138,7 @@ else { facet_summary_block_id = facet_summary_wrapper_id.replace('block-', ''); } - facet_settings.submit.update_summary_block = true; + 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; } @@ -144,9 +146,9 @@ Drupal.ajax(facet_settings).execute(); }; - // Helper function to determine if we should update the summary block - // Returns true or false - var updateFacetsSummaryBlock = function() { + // 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; @@ -157,8 +159,8 @@ return update_summary; } - // Helper function, return facet blocks - var facetsBlocks = function() { + // Helper function, return facet blocks. + var facetsBlocks = function () { // Get all ajax facets blocks from the current page. var facets_blocks = {}; @@ -176,7 +178,7 @@ return facets_blocks; } - $.fn.replaceFacets = function(data) { + $.fn.replaceFacets = function (data) { if (data.replaceWith === '') { $(data.selector).html(''); } @@ -186,7 +188,7 @@ }; /** - * Override beforeSend to trigger facetblocks update on exposed filter change + * Overrides beforeSend to trigger facetblocks update on exposed filter change. * * @param {XMLHttpRequest} xmlhttprequest * Native Ajax object. @@ -195,8 +197,8 @@ */ Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) { - // Update facet blocks as well - // Get view from 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;