1/** 2 * @file 3 * SBD Search Facets views AJAX handling. 4 */ 5 6 7(function ($, Drupal, once) { 8 'use strict'; 9 10 /** 11 * Keep the original beforeSend method to use it later. 12 */ 13 var beforeSend = Drupal.Ajax.prototype.beforeSend; 14 15 /** 16 * Trigger views AJAX refresh on click. 17 */ 18 Drupal.behaviors.sbdFacetsViewsAjax = { 19 attach: function (context, settings) { 20 21 // Loop through all facets. 22 $.each(settings.facets_views_ajax, function (facetId, facetSettings) { 23 if (facetId === "categories_category" || facetId === "new_coming_soon") { 24 return false; 25 } 26 // Get the View for the current facet. 27 var view, current_dom_id, view_path; 28 if (settings.views && settings.views.ajaxViews) { 29 $.each(settings.views.ajaxViews, function (domId, viewSettings) { 30 // Check if we have facet for this view. 31 if (facetSettings.view_id == viewSettings.view_name && facetSettings.current_display_id == viewSettings.view_display_id) { 32 view = $('.js-view-dom-id-' + viewSettings.view_dom_id); 33 current_dom_id = viewSettings.view_dom_id; 34 view_path = facetSettings.ajax_path; 35 } 36 }); 37 } 38 39 if (!view || view.length != 1) { 40 return; 41 } 42 43 // Update view on summary block click. 44 if (updateFacetsSummaryBlock() && (facetId === 'facets_summary_ajax')) { 45 $(once(facetId, '[data-drupal-facets-summary-id=' + facetSettings.facets_summary_id + '] ul li')).click(function (e) { 46 e.preventDefault(); 47 var facetLink = $(this).find('a'); 48 updateFacetsView(facetLink.attr('href'), current_dom_id, view_path); 49 }); 50 } 51 // Update view on facet item click. 52 else { 53 $('[data-drupal-facet-id=' + facetId + ']').each(function (index, facet_item) { 54 if ($(facet_item).hasClass('js-facets-widget')) { 55 $(facet_item).unbind('facets_filter.facets'); 56 $(facet_item).on('facets_filter.facets', function (event, url) { 57 $('.js-facets-widget').trigger('facets_filtering'); 58 59 updateFacetsView(url, current_dom_id, view_path); 60 }); 61 } 62 }); 63 64 } 65 }); 66 } 67 }; 68 69 // Helper function to update views output & Ajax facets. 70 var updateFacetsView = function (href, current_dom_id, view_path) { 71 // Event handler for Views's own AJAX completion, so we can run Facets's 72 // AJAX afterward. 73 var afterViewsAjax = function (e, xmlhttprequest, ajaxObject) { 74 if (ajaxObject.url.indexOf('/views/ajax') === 0) { 75 updateFacetsBlocks(href); 76 $(document).off('.updateFacetsView', afterViewsAjax); 77 } 78 }; 79 80 // Refresh view. 81 var views_parameters = Drupal.Views.parseQueryString(href); 82 var views_arguments = Drupal.Views.parseViewArgs(href, 'search'); 83 var views_settings = $.extend( 84 {}, 85 Drupal.views.instances['views_dom_id:' + current_dom_id].settings, 86 views_arguments, 87 views_parameters 88 ); 89 90 // Update View. 91 var views_ajax_settings = Drupal.views.instances['views_dom_id:' + current_dom_id].element_settings; 92 views_ajax_settings.submit = views_settings; 93 views_ajax_settings.url = view_path; 94 // views_ajax_settings.url = view_path + '?q=' + href; 95 96 // Set up a temporary event handler to replace the facets after the Views AJAX sends incorrect versions. 97 $(document).on('ajaxComplete.updateFacetsView', afterViewsAjax); 98 99 Drupal.ajax(views_ajax_settings).execute(); 100 101 // Update url. 102 window.historyInitiated = true; 103 window.history.pushState(null, document.title, href); 104 105 // ToDo: Update views+facets with ajax on history back. 106 // For now we will reload the full page. 107 window.addEventListener("popstate", function (e) { 108 if (window.historyInitiated) { 109 window.location.reload(); 110 } 111 }); 112 113 // Refresh facets blocks. 114 updateFacetsBlocks(href); 115 } 116 117 // Helper function, updates facet blocks. 118 var updateFacetsBlocks = function (href) { 119 var settings = drupalSettings; 120 var facets_blocks = facetsBlocks(); 121 122 // Remove All Range Input Form Facet Blocks from being updated. 123 if(settings.facets && settings.facets.rangeInput) { 124 $.each(settings.facets.rangeInput, function (index, value) { 125 delete facets_blocks[value.facetId]; 126 }); 127 } 128 129 // Update facet blocks. 130 var facet_settings = { 131 url: Drupal.url('facets-block-ajax'), 132 submit: { 133 facet_link: href, 134 facets_blocks: facets_blocks 135 } 136 }; 137 138 // Update facets summary block. 139 if (updateFacetsSummaryBlock()) { 140 var facet_summary_wrapper_id = $('[data-drupal-facets-summary-id=' + settings.facets_views_ajax.facets_summary_ajax.facets_summary_id + ']').attr('id'); 141 var facet_summary_block_id = ''; 142 if (facet_summary_wrapper_id.indexOf('--') !== -1) { 143 facet_summary_block_id = facet_summary_wrapper_id.substring(0, facet_summary_wrapper_id.indexOf('--')).replace('block-', ''); 144 } 145 else { 146 facet_summary_block_id = facet_summary_wrapper_id.replace('block-', ''); 147 } 148 facet_settings.submit.update_summary_block = true; 149 facet_settings.submit.facet_summary_block_id = facet_summary_block_id; 150 facet_settings.submit.facet_summary_wrapper_id = settings.facets_views_ajax.facets_summary_ajax.facets_summary_id; 151 } 152 153 // Drupal.ajax(facet_settings).execute(); 154 }; 155 156 // Helper function to determine if we should update the summary block. 157 // Returns true or false. 158 var updateFacetsSummaryBlock = function () { 159 var settings = drupalSettings; 160 var update_summary = false;
161 162 if (settings.facets_views_ajax.facets_summary_ajax) { 163 update_summary = true; 164 } 165 166 return update_summary; 167 }; 168 169 // Helper function, return facet blocks. 170 var facetsBlocks = function () { 171 // Get all ajax facets blocks from the current page. 172 var facets_blocks = {}; 173 174 $('.block-facets-ajax').each(function (index) { 175 var block_id_start = 'js-facet-block-id-'; 176 var block_id = $.map($(this).attr('class').split(' '), function (v, i) { 177 if (v.indexOf(block_id_start) > -1) { 178 return v.slice(block_id_start.length, v.length); 179 } 180 }).join(); 181 var block_selector = '#' + $(this).attr('id'); 182 facets_blocks[block_id] = block_selector; 183 }); 184 185 return facets_blocks; 186 }; 187 188 /** 189 * Overrides beforeSend to trigger facetblocks update on exposed filter change. 190 * 191 * @param {XMLHttpRequest} xmlhttprequest 192 * Native Ajax object. 193 * @param {object} options 194 * jQuery.ajax options. 195 */ 196 Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) { 197 198 // Update facet blocks as well. 199 // Get view from options. 200 if (typeof options.extraData !== 'undefined' && typeof options.extraData.view_name !== 'undefined') { 201 var href = window.location.href; 202 var settings = drupalSettings; 203 204 // TODO: Maybe we should limit facet block reloads by view? 205 var reload = false; 206 $.each(settings.facets_views_ajax, function (facetId, facetSettings) { 207 if (facetSettings.view_id == options.extraData.view_name && facetSettings.current_display_id == options.extraData.view_display_id) { 208 reload = true; 209 } 210 }); 211 212 if (reload) { 213 href = addExposedFiltersToFacetsUrl(href, options.extraData.view_name, options.extraData.view_display_id); 214 updateFacetsBlocks(href); 215 } 216 } 217 218 // Call the original Drupal method with the right context. 219 beforeSend.apply(this, arguments); 220 } 221 222 // Helper function to add exposed form data to facets url 223 var addExposedFiltersToFacetsUrl = function (href, view_name, view_display_id) { 224 var $exposed_form = $('form#views-exposed-form-' + view_name.replace(/_/g, '-') + '-' + view_display_id.replace(/_/g, '-')); 225 226 var params = Drupal.Views.parseQueryString(href); 227 228 $.each($exposed_form.serializeArray(), function () { 229 params[this.name] = this.value; 230 }); 231 232 return href.split('?')[0] + '?' + $.param(params); 233 }; 234 235})(jQuery, Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.