1/* 2* Infinite Scroll v1.0 3* Woocommerce and themco pro doesn't have default infinite scroll options 4* Using following script we can enable inifite scroll into woocommerce products 5* jQuery required to execute this features wordpress has inbuild jquery so this will work 6* 7* Developed by Hajakutbudeen [ DEC 07 2023 ] 8*/ 9(function(root, factory) { 10 "use strict"; 11 if (typeof define === "function" && define.amd) { 12 define(["jquery"], factory); 13 } else if (typeof exports === "object") { 14 factory(require("jquery")); 15 } else { 16 factory(root.jQuery); 17 } 18})(this, function(jQuery) { 19 "use strict"; 20 21 var InfiniteScroll = { 22 init: function(options) { 23 /** 24 * Settings 25 */ 26 this.windowHeight = (typeof window.outerHeight !== "undefined") ? Math.max(window.outerHeight, jQuery(window).height()) : jQuery(window).height(); 27 this.defaults = { 28 contentsWrapperSelector: "", 29 contentSelector: "", 30 nextSelector: "", 31 loadImage: "", 32 offset: this.windowHeight, 33 }; 34 this.settings = jQuery.extend(this.defaults, options); 35 36 // Initialize 37 this.initialize(); 38 }, 39 40 initialize: function() { 41 // Get the current page's URL. 42 this.path = jQuery(location).attr("href"); 43 this.documentHeight = jQuery(document).height(); 44 this.threshold = this.settings.offset; 45 this.contents = jQuery(this.settings.contentSelector); 46 47 // Set hidden span elements and history 48 this.contents.last().append(this.generateHiddenSpans(this.path)); 49 this.contents.addClass("active"); 50 this.setHistory(this.path); 51 52 // Bind scroll event 53 this.bindScrollEvent(); 54 }, 55 56 generateHiddenSpans: function(_path) { 57 return "<span class='hidden-url' style='display:none'>" + _path + "</span>"; 58 }, 59 60 setHistory: function(_path) { 61 // Set history 62 history.replaceState(null, "",_path); 63 }, 64 65 changeURL: function(_value) { 66 // value is an element of content user is seeing 67 // Get path of the article page from hidden span elements 68 var path = jQuery(_value).children(".hidden-url:first").text(); 69 70 if (window.location.href !== path) { 71 // If it has changed 72 this.contents.removeClass("active"); 73 jQuery(_value).addClass("active"); 74 this.setHistory(path); 75 jQuery(document).trigger('rr-infinite-scroll-url-change', ["", path]); 76 } 77 }, 78 79 bindScrollEvent: function() { 80 var self = this; 81 var lastScroll = 0, 82 currentScroll; 83 84 jQuery(window).scroll(function() { 85 // Detect where you are 86 window.clearTimeout(jQuery.data("this", "scrollTimer")); 87 jQuery.data(this, "scrollTimer", window.setTimeout(function() { 88 // Get the current scroll position 89 currentScroll = jQuery(window).scrollTop(); 90 91 // Detect whether it's scrolling up or down by comparing the current scroll location and the last scroll location 92 if (currentScroll > lastScroll) { 93 // If it's scrolling down 94 self.contents.each(function(key, value) { 95 if (jQuery(value).offset().top + jQuery(value).height() > currentScroll) { 96 // Change URL 97 self.changeURL(value); 98 // Quit the each loop 99 return false; 100 } 101 }); 102 } else if (currentScroll < lastScroll) { 103 // If it's scrolling up 104 self.contents.each(function(key, value) { 105 if (jQuery(value).offset().top + jQuery(value).height() - self.windowHeight / 2 > currentScroll) { 106 // Change URL 107 self.changeURL(value); 108 // Quit the each loop 109 return false;
110 } 111 }); 112 } else { 113 // When currentScroll == lastScroll, it does not do anything because it has not been scrolled. 114 } 115 116 // Check if scrolled back to the top 117 if (currentScroll === 0) { 118 var originalUrl = self.removePageFromUrl(window.location.href); 119 self.setHistory(originalUrl); 120 } 121 122 // Renew the last scroll position 123 lastScroll = currentScroll; 124 }, 200)); 125 126 if (jQuery(window).scrollTop() + self.windowHeight + self.threshold >= self.documentHeight) { 127 // If scrolling close to the bottom 128 129 // Getting URL from settings.nextSelector 130 var url = [jQuery(self.settings.nextSelector).attr("href")]; 131 jQuery(self.settings.nextSelector).remove(); 132 133 if (url[0] !== undefined) { 134 // If the page has a link, call AJAX 135 if (self.settings.loadImage !== "") { 136 jQuery(self.settings.contentsWrapperSelector).append("<div id='rr-loader'><img src='" + self.settings.loadImage + "' id='rr-load-img' class='rr-isc-loadimage'></div>"); 137 } 138 jQuery.ajax({ 139 url: url[0], 140 dataType: "html", 141 success: function(res) { 142 // Get URL 143 self.path = url[0]; 144 145 // Set hidden span elements and history 146 // jQuery(self.settings.contentsWrapperSelector).append(jQuery(res).find(self.settings.contentSelector).append(self.generateHiddenSpans(self.path))); 147 jQuery(self.settings.contentsWrapperSelector) 148 .append(jQuery(res).find(self.settings.contentSelector) 149 .append(self.generateHiddenSpans(self.path)) 150 .find('a') // Find all <a> tags inside the content 151 .attr('target', '_blank') // Set target="_blank" 152 .end() // Go back to the content selector context 153 ); 154 155 // Check for the existence of the next selector 156 var nextSelector = jQuery(res).find(self.settings.nextSelector); 157 jQuery(self.settings.contentsWrapperSelector).append(nextSelector); 158 self.documentHeight = jQuery(document).height(); 159 160 // Update the contents variable 161 self.contents = jQuery(self.settings.contentSelector); 162 163 // Remove the loading image 164 jQuery("#rr-loader").remove(); 165 166 // Trigger the 'rr-infinite-scroll-content-loaded' event 167 jQuery(document).trigger('rr-infinite-scroll-content-loaded'); 168 }, 169 error: function(jqXHR, textStatus, errorThrown) { 170 console.error("AJAX Error:", textStatus, errorThrown); 171 } 172 }); 173 } 174 } 175 }); 176 }, 177 178 removePageFromUrl: function(url) { 179 // Remove everything after the last '/' 180 // return url.replace(/\/page\/\d+\/?$/, '/'); 181 // return url.replace(/\/page\/\d+\/?(\?.*)?$|\/?$/, '/$1'); 182 return url.replace(/\/page\/\d+\/?(\?.*)?$|\/$/, function(match, query) { 183 return query ? '/' + query : '/'; 184 }); 185 }, 186 }; 187 188 // Expose the InfiniteScroll object 189 jQuery.fn.InfiniteScroll = function(options) { 190 InfiniteScroll.init(options); 191 return this; 192 }; 193});
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.