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 if($pager.offset().top === 0) return false; 87 return window.innerHeight + window.pageYOffset > $pager.offset().top - scrollThreshold; 88 }; 89 $window.on(scrollEvent, debounce(function () { 90 if (isLoadNeeded()) { 91 $pager.find('[rel=next]').click(); 92 $window.off(scrollEvent); 93 } 94 }, 200)); 95 if (isLoadNeeded()) { 96 $window.trigger(scrollEvent); 97 } 98 }); 99 }, 100 detach: function (context, settings, trigger) { 101 // In the case where the view is removed from the document, remove it's 102 // events. This is important in the case a view being refreshed for a reason 103 // other than a scroll. AJAX filters are a good example of the event needing 104 // to be destroyed earlier than above. 105 if (trigger === 'unload') { 106 if (once.remove('infinite-scroll', automaticPagerSelector, context).length) { 107 $window.off(scrollEvent); 108 } 109 } 110 } 111 }; 112 113 /** 114 * Views AJAX pagination filter. 115 * 116 * In case the Entity View Attachment is rendered in a view context, 117 * the default filter function prevents the required 'Use AJAX' setting 118 * to work. 119 * 120 * @return {Boolean} 121 * Whether to apply the Views AJAX paginator. VIS requires this setting 122 * for pagination. 123 */ 124 Drupal.views.ajaxView.prototype.filterNestedViews = function () { 125 return this.$view.hasClass('view-eva') || !this.$view.parents('.view').length; 126 }; 127 128})(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.