1/** 2 * @file 3 * Infinite Scroll JS. 4 */ 5 6(function ($, Drupal, debounce) { 7 "use strict"; 8 9 // Cached reference to $(window). 10 var $window = $(window); 11 12 // The threshold for how far to the bottom you should reach before reloading. 13 var scrollThreshold = 200; 14 15 // The selector for the automatic pager. 16 var automaticPagerSelector = '[data-drupal-views-infinite-scroll-pager="automatic"]'; 17 18 // The selector for both manual load and automatic pager. 19 var pagerSelector = '[data-drupal-views-infinite-scroll-pager]'; 20 21 // The selector for the automatic pager. 22 var contentWrapperSelector = '[data-drupal-views-infinite-scroll-content-wrapper]'; 23 24 // The event and namespace that is bound to window for automatic scrolling. 25 var scrollEvent = 'scroll.views_infinite_scroll'; 26 27 /** 28 * Insert a views infinite scroll view into the document. 29 * 30 * @param {jQuery} $newView 31 * New content detached from the DOM. 32 */ 33 $.fn.infiniteScrollInsertView = function ($newView) { 34 // Extract the view DOM ID from the view classes. 35 var matches = /(js-view-dom-id-\w+)/.exec(this.attr('class')); 36 37 if (!matches) { 38 return; 39 } 40 41 var currentViewId = matches[1].replace('js-view-dom-id-', 'views_dom_id:'); 42 43 // Get the existing ajaxViews object. 44 var view = Drupal.views.instances[currentViewId]; 45 // Remove once so that the exposed form and pager are processed on 46 // behavior attach. 47 once.remove('ajax-pager', view.$view); 48 once.remove('exposed-form', view.$exposed_form); 49 // Make sure infinite scroll can be reinitialized. 50 var $existingPager = view.$view.find(pagerSelector); 51 once.remove('infinite-scroll', $existingPager); 52 53 var $newRows = $newView.find(contentWrapperSelector).children(); 54 var $newPager = $newView.find(pagerSelector); 55 56 view.$view.find(contentWrapperSelector) 57 // Trigger a jQuery event on the wrapper to inform that new content was 58 // loaded and allow other scripts to respond to the event. 59 .trigger('views_infinite_scroll.new_content', $newRows.clone()) 60 // Add the new rows to existing view. 61 .append($newRows); 62 63 // Replace the pager link with the new link and ajaxPageState values. 64 $existingPager.replaceWith($newPager); 65 66 // Run views and VIS behaviors. 67 Drupal.attachBehaviors(view.$view[0]); 68 }; 69 70 /** 71 * Handle the automatic paging based on the scroll amount. 72 * 73 * @type {Drupal~behavior} 74 * 75 * @prop {Drupal~behaviorAttach} attach 76 * Initialize infinite scroll pagers and bind the scroll event. 77 * @prop {Drupal~behaviorDetach} detach 78 * During `unload` remove the scroll event binding. 79 */ 80 Drupal.behaviors.views_infinite_scroll_automatic = { 81 attach : function (context, settings) { 82 once('infinite-scroll', automaticPagerSelector, context).forEach(function (elem) { 83 var $pager = $(elem); 84 $pager.addClass('visually-hidden'); 85 var isLoadNeeded = function () { 86 return window.innerHeight + window.pageYOffset > $pager.offset().top - scrollThreshold; 87 }; 88 $window.on(scrollEvent, debounce(function () { 89 if (isLoadNeeded()) { 90 $pager.find('[rel=next]').click(); 91 $window.off(scrollEvent); 92 } 93 }, 200)); 94 if (isLoadNeeded()) { 95 $window.trigger(scrollEvent); 96 } 97 }); 98 }, 99 detach: function (context, settings, trigger) { 100 // In the case where the view is removed from the document, remove it's 101 // events. This is important in the case a view being refreshed for a reason 102 // other than a scroll. AJAX filters are a good example of the event needing 103 // to be destroyed earlier than above. 104 if (trigger === 'unload') { 105 if (once.remove('infinite-scroll', automaticPagerSelector, context).length) { 106 $window.off(scrollEvent); 107 } 108 } 109 } 110 }; 111 112 /** 113 * Views AJAX pagination filter. 114 * 115 * In case the Entity View Attachment is rendered in a view context, 116 * the default filter function prevents the required 'Use AJAX' setting 117 * to work. 118 * 119 * @return {Boolean} 120 * Whether to apply the Views AJAX paginator. VIS requires this setting 121 * for pagination. 122 */ 123 Drupal.views.ajaxView.prototype.filterNestedViews = function () { 124 return this.$view.hasClass('view-eva') || !this.$view.parents('.view').length; 125 }; 126 127})(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.