1(function ($, Drupal, debounce) { 2 "use strict"; 3 4 // Cached reference to $(window). 5 var $window = $(window); 6 7 // The threshold for how far to the bottom you should reach before reloading. 8 var scrollThreshold = 200; 9 10 // The selector for the automatic pager. 11 var automaticPagerSelector = '[data-drupal-views-infinite-scroll-pager="automatic"]'; 12 13 // The selector for both manual load and automatic pager. 14 var pagerSelector = '[data-drupal-views-infinite-scroll-pager]'; 15 16 // The selector for the automatic pager. 17 var contentWrapperSelector = '[data-drupal-views-infinite-scroll-content-wrapper]'; 18 19 // The event and namespace that is bound to window for automatic scrolling. 20 var scrollEvent = 'scroll.views_infinite_scroll'; 21 22 /** 23 * Insert a views infinite scroll view into the document. 24 * 25 * @param {jQuery} $newView 26 * New content detached from the DOM. 27 */ 28 $.fn.infiniteScrollInsertView = function ($newView) { 29 // Extract the view DOM ID from the view classes. 30 var matches = /(js-view-dom-id-\w+)/.exec(this.attr('class')); 31 var currentViewId = matches[1].replace('js-view-dom-id-', 'views_dom_id:'); 32 33 // Get the existing ajaxViews object. 34 var view = Drupal.views.instances[currentViewId]; 35 // Remove once so that the exposed form and pager are processed on 36 // behavior attach. 37 view.$view.removeOnce('ajax-pager'); 38 view.$exposed_form.removeOnce('exposed-form'); 39 // Make sure infinite scroll can be reinitialized. 40 var $existingPager = view.$view.find(pagerSelector); 41 $existingPager.removeOnce('infinite-scroll'); 42 43 var $newRows = $newView.find(contentWrapperSelector).children(); 44 var $newPager = $newView.find(pagerSelector); 45 46 // Add the new rows to existing view. 47 view.$view.find(contentWrapperSelector).append($newRows); 48 // Replace the pager link with the new link and ajaxPageState values. 49 $existingPager.replaceWith($newPager); 50 51 // Run views and VIS behaviors. 52 Drupal.attachBehaviors(view.$view[0]); 53 }; 54 55 /** 56 * Handle the automatic paging based on the scroll amount. 57 * 58 * @type {Drupal~behavior} 59 * 60 * @prop {Drupal~behaviorAttach} attach 61 * Initialize infinite scroll pagers and bind the scroll event. 62 * @prop {Drupal~behaviorDetach} detach 63 * During `unload` remove the scroll event binding. 64 */ 65 Drupal.behaviors.views_infinite_scroll_automatic = { 66 attach : function(context, settings) { 67 $(context).find(automaticPagerSelector).once('infinite-scroll').each(function() { 68 var $pager = $(this); 69 $pager.addClass('visually-hidden'); 70 $window.on(scrollEvent, debounce(function() { 71 if (window.innerHeight + window.pageYOffset > $pager.offset().top - scrollThreshold) { 72 $pager.find('[rel=next]').click(); 73 $window.off(scrollEvent); 74 } 75 }, 200)); 76 }); 77 }, 78 detach: function (context, settings, trigger) { 79 // In the case where the view is removed from the document, remove it's 80 // events. This is important in the case a view being refreshed for a reason 81 // other than a scroll. AJAX filters are a good example of the event needing 82 // to be destroyed earlier than above. 83 if (trigger === 'unload') { 84 if ($(context).find(automaticPagerSelector).removeOnce('infinite-scroll').length) { 85 $window.off(scrollEvent); 86 } 87 } 88 } 89 }; 90 91})(jQuery, Drupal, Drupal.debounce);
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.