1"use strict"; 2/* ===================================================================================== @preserve = 3 ___ _ _ _ 4/ || | | | | | 5\__ | | | | | | __ 6/ |/ |/_) |/ / \_/\/ 7\___/|__/| \_/|__/\__/ /\_/ 8 |\ 9 |/ 10Ajaxinate 11version v2.0.4 12https://github.com/Elkfox/Ajaxinate 13Copyright (c) 2017 Elkfox Co Pty Ltd 14https://elkfox.com 15MIT License 16================================================================================================= */ 17 18var Ajaxinate = function ajaxinateConstructor(config) { 19 var settings = config || {}; 20 /* 21 pagination: Selector of pagination container 22 container: Selector of repeating content 23 offset: 0, offset the number of pixels before the bottom to start loading more on scroll 24 loadingText: 'Loading', The text changed during loading 25 callback: null, function to callback after a new page is loaded 26 */ 27 28 var defaultSettings = { 29 method: "scroll", 30 container: "#AjaxinateContainer", 31 pagination: "#AjaxinatePagination", 32 offset: 0, 33 loadingText: "Loading", 34 callback: null, 35 }; // Merge configs 36 37 this.settings = Object.assign(defaultSettings, settings); 38 this.addScrollListeners = this.addScrollListeners.bind(this); 39 this.addClickListener = this.addClickListener.bind(this); 40 this.checkIfPaginationInView = this.checkIfPaginationInView.bind(this); 41 this.stopMultipleClicks = this.stopMultipleClicks.bind(this); 42 this.removeClickListener = this.removeClickListener.bind(this); 43 this.removeScrollListener = this.removeScrollListener.bind(this); 44 this.destroy = this.destroy.bind(this); 45 this.containerElement = document.querySelector(this.settings.container); 46 this.paginationElement = document.querySelector(this.settings.pagination); 47 this.initialize(); 48}; 49 50Ajaxinate.prototype.initialize = 51 function initializeTheCorrectFunctionsBasedOnTheMethod() { 52 if (this.containerElement) { 53 var initializers = { 54 click: this.addClickListener, 55 scroll: this.addScrollListeners, 56 }; 57 initializers[this.settings.method](); 58 } 59 }; 60 61Ajaxinate.prototype.addScrollListeners = 62 function addEventListenersForScrolling() { 63 if (this.paginationElement) { 64 document.addEventListener("scroll", this.checkIfPaginationInView); 65 window.addEventListener("resize", this.checkIfPaginationInView); 66 window.addEventListener( 67 "orientationchange", 68 this.checkIfPaginationInView 69 ); 70 } 71 }; 72 73Ajaxinate.prototype.addClickListener = function addEventListenerForClicking() { 74 if (this.paginationElement) { 75 this.nextPageLinkElement = this.paginationElement.querySelector("a"); 76 this.clickActive = true; 77 78 if ( 79 typeof this.nextPageLinkElement !== "undefined" && 80 this.nextPageLinkElement !== null 81 ) { 82 this.nextPageLinkElement.addEventListener( 83 "click", 84 this.stopMultipleClicks 85 ); 86 } 87 } 88}; 89 90Ajaxinate.prototype.stopMultipleClicks = function handleClickEvent(event) { 91 event.preventDefault(); 92 93 if (this.clickActive) { 94 this.nextPageLinkElement.innerText = this.settings.loadingText; 95 this.nextPageUrl = this.nextPageLinkElement.href; 96 this.clickActive = false; 97 this.loadMore(); 98 } 99}; 100 101Ajaxinate.prototype.checkIfPaginationInView = function handleScrollEvent() { 102 var top = 103 this.paginationElement.getBoundingClientRect().top - this.settings.offset; 104 var bottom = 105 this.paginationElement.getBoundingClientRect().bottom + 106 this.settings.offset; 107 108 if (top <= window.innerHeight && bottom >= 0) { 109 this.nextPageLinkElement = this.paginationElement.querySelector("a"); 110 this.removeScrollListener(); 111 112 if (this.nextPageLinkElement) { 113 this.nextPageLinkElement.innerText = this.settings.loadingText; 114 this.nextPageUrl = this.nextPageLinkElement.href; 115 this.loadMore(); 116 } 117 } 118}; 119 120Ajaxinate.prototype.loadMore = 121 function getTheHtmlOfTheNextPageWithAnAjaxRequest() { 122 this.request = new XMLHttpRequest(); 123 124 this.request.onreadystatechange = function success() { 125 if (this.request.readyState === 4 && this.request.status === 200) { 126 var newContainer = this.request.responseXML.querySelectorAll( 127 this.settings.container 128 )[0]; 129 var newPagination = this.request.responseXML.querySelectorAll( 130 this.settings.pagination 131 )[0]; 132 this.containerElement.insertAdjacentHTML( 133 "beforeend", 134 newContainer.innerHTML 135 ); 136 this.paginationElement.innerHTML = newPagination.innerHTML; 137 138 if ( 139 this.settings.callback && 140 typeof this.settings.callback === "function" 141 ) { 142 this.settings.callback(this.request.responseXML); 143 } 144 145 this.initialize(); 146 } 147 }.bind(this); 148 149 this.request.open("GET", this.nextPageUrl); 150 this.request.responseType = "document"; 151 this.request.send(); 152 }; 153 154Ajaxinate.prototype.removeClickListener = function removeClickEventListener() { 155 this.nextPageLinkElement.removeEventListener( 156 "click", 157 this.stopMultipleClicks 158 ); 159}; 160 161Ajaxinate.prototype.removeScrollListener = 162 function removeScrollEventListener() { 163 document.removeEventListener("scroll", this.checkIfPaginationInView); 164 window.removeEventListener("resize", this.checkIfPaginationInView); 165 window.removeEventListener( 166 "orientationchange", 167 this.checkIfPaginationInView 168 ); 169 }; 170 171Ajaxinate.prototype.destroy = function removeEventListenersAndRemoveThis() { 172 var destroyers = { 173 click: this.removeClickListener, 174 scroll: this.removeScrollListener, 175 }; 176 destroyers[this.settings.method](); 177 return this; 178};
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.