1/** 2 * @file 3 * Reattaches Drupal behaviors to lazy-loaded views with exposed forms. 4 */ 5 6(function ($, Drupal, once) { 7 'use strict'; 8 9 const processedViews = new WeakSet(); 10 11 /** 12 * Process a view element and reattach behaviors if needed. 13 */ 14 function processView(viewElement) { 15 if (processedViews.has(viewElement)) { 16 return; 17 } 18 19 const $view = $(viewElement); 20 const $exposedForm = $view.find('.views-exposed-form'); 21 22 if ($exposedForm.length > 0) { 23 processedViews.add(viewElement); 24 25 setTimeout(function () { 26 Drupal.attachBehaviors(viewElement); 27 }, 150); 28 } 29 } 30 31 Drupal.behaviors.lazyViewsAjaxFix = { 32 attach: function (context, settings) { 33 // Watch for new views being added to the DOM 34 $(once('lazy-views-observer', 'body', context)).each(function() { 35 const observer = new MutationObserver(function (mutations) { 36 mutations.forEach(function (mutation) { 37 mutation.addedNodes.forEach(function (node) { 38 if (node.nodeType === Node.ELEMENT_NODE) {
39 // Check if the node or its descendants contain views 40 $(node).find('.view').addBack('.view').each(function() { 41 processView(this); 42 }); 43 } 44 }); 45 }); 46 }); 47 48 observer.observe(this, { 49 childList: true, 50 subtree: true 51 }); 52 }); 53 54 // Process any existing views in the context 55 $('.view', context).each(function() { 56 processView(this); 57 }); 58 } 59 }; 60 61})(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.