@webcreate/infinite-ajax-scroll
Advanced tools
Comparing version 3.0.0 to 3.1.0-beta.1
/** | ||
* Infinite Ajax Scroll v3.0.0 | ||
* Infinite Ajax Scroll v3.1.0-beta.1 | ||
* Turn your existing pagination into infinite scrolling pages with ease | ||
@@ -8,3 +8,3 @@ * | ||
* | ||
* Copyright 2014-2021 Webcreate (Jeroen Fiege) | ||
* Copyright 2014-2022 Webcreate (Jeroen Fiege) | ||
* https://infiniteajaxscroll.com | ||
@@ -20,2 +20,3 @@ */ | ||
next: undefined, | ||
prev: undefined, | ||
pagination: undefined, | ||
@@ -127,11 +128,17 @@ responseType: 'document', | ||
var APPENDED = 'appended'; | ||
var PREPEND = 'prepend'; | ||
var PREPENDED = 'prepended'; | ||
var BINDED = 'binded'; | ||
var UNBINDED = 'unbinded'; | ||
var HIT = 'hit'; | ||
var TOP = 'top'; | ||
var LOAD = 'load'; | ||
var LOADED = 'loaded'; | ||
var ERROR = 'error'; | ||
var FIRST = 'first'; | ||
var LAST = 'last'; | ||
var NEXT = 'next'; | ||
var NEXTED = 'nexted'; | ||
var PREV = 'prev'; | ||
var PREVED = 'preved'; | ||
var READY = 'ready'; | ||
@@ -147,11 +154,17 @@ var SCROLLED = 'scrolled'; | ||
APPENDED: APPENDED, | ||
PREPEND: PREPEND, | ||
PREPENDED: PREPENDED, | ||
BINDED: BINDED, | ||
UNBINDED: UNBINDED, | ||
HIT: HIT, | ||
TOP: TOP, | ||
LOAD: LOAD, | ||
LOADED: LOADED, | ||
ERROR: ERROR, | ||
FIRST: FIRST, | ||
LAST: LAST, | ||
NEXT: NEXT, | ||
NEXTED: NEXTED, | ||
PREV: PREV, | ||
PREVED: PREVED, | ||
READY: READY, | ||
@@ -201,3 +214,3 @@ SCROLLED: SCROLLED, | ||
var ias = this; | ||
var lastResponse = ias._lastResponse || document; | ||
var lastResponse = ias._lastResponse || document.body; | ||
@@ -235,2 +248,27 @@ var nextEl = $(ias.options.next, lastResponse)[0]; | ||
function prevHandler(pageIndex) { | ||
var ias = this; | ||
var prevEl = ias._prevEl || $(ias.options.prev, document.body)[0]; | ||
if (ias.options.prev === undefined) { | ||
return; | ||
} | ||
if (!prevEl) { | ||
Assert.warn(Assert.singleElement, ias.options.prev, 'options.prev'); | ||
return; | ||
} | ||
var prevUrl = prevEl.href; | ||
return ias.load(prevUrl) | ||
.then(function (data) { | ||
var prevEl = ias._prevEl = $(ias.options.prev, data.xhr.response)[0]; | ||
return ias.prepend(data.items) | ||
.then(function () { return !!prevEl; }) // TODO: evaluate if this makes sense | ||
}); | ||
} | ||
var defaults$2 = { | ||
@@ -384,2 +422,5 @@ element: undefined, | ||
}, | ||
top: function () { | ||
console.log("Hit top scroll threshold"); | ||
}, | ||
binded: function () { | ||
@@ -403,2 +444,8 @@ console.log("Binded event handlers"); | ||
}, | ||
prev: function (event) { | ||
console.log(("Previous page triggered [pageIndex=" + (event.pageIndex) + "]")); | ||
}, | ||
preved: function (event) { | ||
console.log(("Previous page completed [pageIndex=" + (event.pageIndex) + "]")); | ||
}, | ||
load: function (event) { | ||
@@ -416,5 +463,14 @@ console.log(("Start loading " + (event.url))); | ||
}, | ||
prepend: function () { | ||
console.log("Start prepending items"); | ||
}, | ||
prepended: function (event) { | ||
console.log(("Finished prepending " + (event.items.length) + " item(s)")); | ||
}, | ||
last: function () { | ||
console.log("No more pages left to load"); | ||
}, | ||
first: function () { | ||
console.log("Reached first page"); | ||
}, | ||
page: function (event) { | ||
@@ -477,2 +533,3 @@ console.log(("Page changed [pageIndex=" + (event.pageIndex) + "]")); | ||
ias.on(NEXT, this.next.bind(this)); | ||
ias.on(PREV, this.prev.bind(this)); | ||
ias.on(SCROLLED, this.scrolled.bind(this)); | ||
@@ -526,2 +583,32 @@ ias.on(RESIZED, this.scrolled.bind(this)); | ||
Paging.prototype.prev = function prev () { | ||
var this$1$1 = this; | ||
var url = document.location.toString(); | ||
var title = document.title; | ||
var loaded = function (event) { | ||
url = event.url; | ||
if (event.xhr.response) { | ||
title = event.xhr.response.title; | ||
} | ||
}; | ||
this.ias.once(LOADED, loaded); | ||
this.ias.once(PREVED, function (event) { | ||
this$1$1.pageBreaks.unshift({ | ||
pageIndex: event.pageIndex, | ||
url: url, | ||
title: title, | ||
sentinel: this$1$1.ias.first() | ||
}); | ||
this$1$1.update(); | ||
this$1$1.ias.off(LOADED, loaded); | ||
}); | ||
}; | ||
Paging.prototype.scrolled = function scrolled (event) { | ||
@@ -669,2 +756,12 @@ this.update(event.scroll.y); | ||
function prependFn(items, parent, first) { | ||
var insert = document.createDocumentFragment(); | ||
items.forEach(function (item) { | ||
insert.appendChild(item); | ||
}); | ||
parent.insertBefore(insert, first); | ||
} | ||
/* eslint no-console: "off" */ | ||
@@ -756,11 +853,5 @@ | ||
var distance = this.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
this.ias.emitter.emit(events.PREFILL); | ||
return this._prefill().then(function () { | ||
return Promise.all([this._prefillNext(), this._prefillPrev()]).then(function () { | ||
this$1$1.ias.emitter.emit(events.PREFILLED); | ||
@@ -773,18 +864,34 @@ | ||
Prefill.prototype._prefill = function _prefill () { | ||
Prefill.prototype._prefillNext = function _prefillNext () { | ||
var this$1$1 = this; | ||
return this.ias.next().then(function (hasNextUrl) { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
var distance = this.ias.distance(); | ||
var distance = this$1$1.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
if (distance < 0) { | ||
return this$1$1._prefill(); | ||
} | ||
}); | ||
return this.ias.next() | ||
.then(function (hasNextUrl) { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
var distance = this$1$1.ias.distance(); | ||
if (distance < 0) { | ||
return this$1$1._prefillNext(); | ||
} | ||
}) | ||
; | ||
}; | ||
Prefill.prototype._prefillPrev = function _prefillPrev () { | ||
if (!this.ias.options.prev) { | ||
return; | ||
} | ||
return this.ias.prev(); | ||
}; | ||
var InfiniteAjaxScroll = function InfiniteAjaxScroll(container, options) { | ||
@@ -811,2 +918,3 @@ var this$1$1 = this; | ||
this.nextHandler = nextHandler; | ||
this.prevHandler = prevHandler; | ||
@@ -819,2 +927,8 @@ if (this.options.next === false) { | ||
if (this.options.prev === false) { | ||
this.prevHandler = function() {}; | ||
} else if (typeof this.options.prev === 'function') { | ||
this.prevHandler = this.options.prev; | ||
} | ||
this.resizeObserver = ResizeObserverFactory(this, this.scrollContainer); | ||
@@ -827,3 +941,4 @@ this._scrollListener = throttle(scrollHandler, 200).bind(this); | ||
this.paused = false; | ||
this.pageIndex = this.sentinel() ? 0 : -1; | ||
this.pageIndexPrev = 0; | ||
this.pageIndex = this.pageIndexNext = this.sentinel() ? 0 : -1; | ||
@@ -838,2 +953,10 @@ this.on(HIT, function () { | ||
this.on(TOP, function () { | ||
if (!this$1$1.loadOnScroll) { | ||
return; | ||
} | ||
this$1$1.prev(); | ||
}); | ||
this.on(SCROLLED, this.measure); | ||
@@ -853,2 +976,7 @@ this.on(RESIZED, this.measure); | ||
this.hitFirst = this.hitLast = false; | ||
this.on(LAST, function () { return this$1$1.hitLast = true; }); | ||
this.on(FIRST, function () { return this$1$1.hitFirst = true; }); | ||
var ready = function () { | ||
@@ -914,2 +1042,6 @@ if (this$1$1.ready) { | ||
if (this.hitLast) { | ||
return; | ||
} | ||
if (!this.binded) { | ||
@@ -925,14 +1057,12 @@ if (!this.ready) { | ||
var pageIndex = this.pageIndex + 1; | ||
var pageIndex = this.pageIndexNext + 1; | ||
this.emitter.emit(NEXT, {pageIndex: this.pageIndex + 1}); | ||
this.emitter.emit(NEXT, {pageIndex: this.pageIndexNext + 1}); | ||
return Promise.resolve(this.nextHandler(pageIndex)) | ||
.then(function (hasNextUrl) { | ||
this$1$1.pageIndex = pageIndex; | ||
this$1$1.pageIndexNext = pageIndex; | ||
if (!hasNextUrl) { | ||
this$1$1.emitter.emit(LAST); | ||
return; | ||
} | ||
@@ -942,6 +1072,33 @@ | ||
}).then(function () { | ||
this$1$1.emitter.emit(NEXTED, {pageIndex: this$1$1.pageIndex}); | ||
this$1$1.emitter.emit(NEXTED, {pageIndex: this$1$1.pageIndexNext}); | ||
}); | ||
}; | ||
InfiniteAjaxScroll.prototype.prev = function prev () { | ||
var this$1$1 = this; | ||
if (!this.binded || this.hitFirst) { | ||
return; | ||
} | ||
this.pause(); | ||
var pageIndex = this.pageIndexPrev - 1; | ||
this.emitter.emit(PREV, {pageIndex: this.pageIndexPrev - 1}); | ||
return Promise.resolve(this.prevHandler(pageIndex)) | ||
.then(function (hasPrevUrl) { | ||
this$1$1.pageIndexPrev = pageIndex; | ||
this$1$1.resume(); | ||
if (!hasPrevUrl) { | ||
this$1$1.emitter.emit(FIRST); | ||
} | ||
}).then(function () { | ||
this$1$1.emitter.emit(PREVED, {pageIndex: this$1$1.pageIndexPrev}); | ||
}); | ||
}; | ||
/** | ||
@@ -1055,2 +1212,46 @@ * @param {string} url | ||
/** | ||
* @param {array<Element>} items | ||
* @param {Element|null} parent | ||
*/ | ||
InfiniteAjaxScroll.prototype.prepend = function prepend (items, parent) { | ||
var this$1$1 = this; | ||
var ias = this; | ||
parent = parent || ias.container; | ||
var event = { | ||
items: items, | ||
parent: parent, | ||
prependFn: prependFn | ||
}; | ||
ias.emitter.emit(PREPEND, event); | ||
var executor = function (resolve) { | ||
window.requestAnimationFrame(function () { | ||
var first = ias.first(); | ||
var scrollPositionStart = getScrollPosition(this$1$1.scrollContainer); | ||
var topStart = first.getBoundingClientRect().top + scrollPositionStart.y; | ||
Promise.resolve(event.prependFn(event.items, event.parent, ias.first())) | ||
.then(function () { | ||
var scrollPositionEnd = getScrollPosition(this$1$1.scrollContainer); | ||
var topEnd = first.getBoundingClientRect().top + scrollPositionEnd.y; | ||
var deltaY = topEnd - topStart; | ||
this$1$1.scrollContainer.scrollTo(scrollPositionEnd.x, deltaY); | ||
}) | ||
.then(function () { | ||
resolve({items: items, parent: parent}); | ||
}); | ||
}); | ||
}; | ||
return (new Promise(executor)).then(function (event) { | ||
ias.emitter.emit(PREPENDED, event); | ||
}); | ||
}; | ||
InfiniteAjaxScroll.prototype.sentinel = function sentinel () { | ||
@@ -1066,2 +1267,12 @@ var items = $(this.options.item, this.container); | ||
InfiniteAjaxScroll.prototype.first = function first () { | ||
var items = $(this.options.item, this.container); | ||
if (!items.length) { | ||
return null; | ||
} | ||
return items[0]; | ||
}; | ||
InfiniteAjaxScroll.prototype.pause = function pause () { | ||
@@ -1083,5 +1294,11 @@ this.paused = true; | ||
/** | ||
* @deprecated replaced by distanceBottom | ||
*/ | ||
InfiniteAjaxScroll.prototype.distance = function distance (rootRect, sentinel) { | ||
return this.distanceBottom(rootRect, sentinel); | ||
}; | ||
InfiniteAjaxScroll.prototype.distanceBottom = function distanceBottom (rootRect, sentinel) { | ||
var _rootRect = rootRect || getRootRect(this.scrollContainer); | ||
var _sentinel = sentinel || this.sentinel(); | ||
@@ -1099,4 +1316,10 @@ | ||
InfiniteAjaxScroll.prototype.distanceTop = function distanceTop () { | ||
var scrollPosition = getScrollPosition(this.scrollContainer); | ||
return scrollPosition.y - this.negativeMargin; | ||
}; | ||
InfiniteAjaxScroll.prototype.measure = function measure () { | ||
if (this.paused) { | ||
if (this.paused || (this.hitFirst && this.hitLast)) { | ||
return; | ||
@@ -1117,8 +1340,16 @@ } | ||
var sentinel = this.sentinel(); | ||
if (!this.hitFirst) { | ||
var distanceTop = this.distanceTop(); | ||
var distance = this.distance(rootRect, sentinel); | ||
if (distanceTop <= 0) { | ||
this.emitter.emit(TOP, {distance: distanceTop}); | ||
} | ||
} | ||
if (distance <= 0) { | ||
this.emitter.emit(HIT, {distance: distance}); | ||
if (!this.hitLast) { | ||
var distanceBottom = this.distanceBottom(rootRect, this.sentinel()); | ||
if (distanceBottom <= 0) { | ||
this.emitter.emit(HIT, {distance: distanceBottom}); | ||
} | ||
} | ||
@@ -1125,0 +1356,0 @@ }; |
/** | ||
* Infinite Ajax Scroll v3.0.0 | ||
* Infinite Ajax Scroll v3.1.0-beta.1 | ||
* Turn your existing pagination into infinite scrolling pages with ease | ||
@@ -8,3 +8,3 @@ * | ||
* | ||
* Copyright 2014-2021 Webcreate (Jeroen Fiege) | ||
* Copyright 2014-2022 Webcreate (Jeroen Fiege) | ||
* https://infiniteajaxscroll.com | ||
@@ -691,2 +691,3 @@ */ | ||
next: undefined, | ||
prev: undefined, | ||
pagination: undefined, | ||
@@ -798,11 +799,17 @@ responseType: 'document', | ||
var APPENDED = 'appended'; | ||
var PREPEND = 'prepend'; | ||
var PREPENDED = 'prepended'; | ||
var BINDED = 'binded'; | ||
var UNBINDED = 'unbinded'; | ||
var HIT = 'hit'; | ||
var TOP = 'top'; | ||
var LOAD = 'load'; | ||
var LOADED = 'loaded'; | ||
var ERROR = 'error'; | ||
var FIRST = 'first'; | ||
var LAST = 'last'; | ||
var NEXT = 'next'; | ||
var NEXTED = 'nexted'; | ||
var PREV = 'prev'; | ||
var PREVED = 'preved'; | ||
var READY = 'ready'; | ||
@@ -818,11 +825,17 @@ var SCROLLED = 'scrolled'; | ||
APPENDED: APPENDED, | ||
PREPEND: PREPEND, | ||
PREPENDED: PREPENDED, | ||
BINDED: BINDED, | ||
UNBINDED: UNBINDED, | ||
HIT: HIT, | ||
TOP: TOP, | ||
LOAD: LOAD, | ||
LOADED: LOADED, | ||
ERROR: ERROR, | ||
FIRST: FIRST, | ||
LAST: LAST, | ||
NEXT: NEXT, | ||
NEXTED: NEXTED, | ||
PREV: PREV, | ||
PREVED: PREVED, | ||
READY: READY, | ||
@@ -940,3 +953,3 @@ SCROLLED: SCROLLED, | ||
var ias = this; | ||
var lastResponse = ias._lastResponse || document; | ||
var lastResponse = ias._lastResponse || document.body; | ||
@@ -974,2 +987,27 @@ var nextEl = tealight(ias.options.next, lastResponse)[0]; | ||
function prevHandler(pageIndex) { | ||
var ias = this; | ||
var prevEl = ias._prevEl || tealight(ias.options.prev, document.body)[0]; | ||
if (ias.options.prev === undefined) { | ||
return; | ||
} | ||
if (!prevEl) { | ||
Assert.warn(Assert.singleElement, ias.options.prev, 'options.prev'); | ||
return; | ||
} | ||
var prevUrl = prevEl.href; | ||
return ias.load(prevUrl) | ||
.then(function (data) { | ||
var prevEl = ias._prevEl = tealight(ias.options.prev, data.xhr.response)[0]; | ||
return ias.prepend(data.items) | ||
.then(function () { return !!prevEl; }) // TODO: evaluate if this makes sense | ||
}); | ||
} | ||
var defaults$2 = { | ||
@@ -1123,2 +1161,5 @@ element: undefined, | ||
}, | ||
top: function () { | ||
console.log("Hit top scroll threshold"); | ||
}, | ||
binded: function () { | ||
@@ -1142,2 +1183,8 @@ console.log("Binded event handlers"); | ||
}, | ||
prev: function (event) { | ||
console.log(("Previous page triggered [pageIndex=" + (event.pageIndex) + "]")); | ||
}, | ||
preved: function (event) { | ||
console.log(("Previous page completed [pageIndex=" + (event.pageIndex) + "]")); | ||
}, | ||
load: function (event) { | ||
@@ -1155,5 +1202,14 @@ console.log(("Start loading " + (event.url))); | ||
}, | ||
prepend: function () { | ||
console.log("Start prepending items"); | ||
}, | ||
prepended: function (event) { | ||
console.log(("Finished prepending " + (event.items.length) + " item(s)")); | ||
}, | ||
last: function () { | ||
console.log("No more pages left to load"); | ||
}, | ||
first: function () { | ||
console.log("Reached first page"); | ||
}, | ||
page: function (event) { | ||
@@ -1216,2 +1272,3 @@ console.log(("Page changed [pageIndex=" + (event.pageIndex) + "]")); | ||
ias.on(NEXT, this.next.bind(this)); | ||
ias.on(PREV, this.prev.bind(this)); | ||
ias.on(SCROLLED, this.scrolled.bind(this)); | ||
@@ -1265,2 +1322,32 @@ ias.on(RESIZED, this.scrolled.bind(this)); | ||
Paging.prototype.prev = function prev () { | ||
var this$1$1 = this; | ||
var url = document.location.toString(); | ||
var title = document.title; | ||
var loaded = function (event) { | ||
url = event.url; | ||
if (event.xhr.response) { | ||
title = event.xhr.response.title; | ||
} | ||
}; | ||
this.ias.once(LOADED, loaded); | ||
this.ias.once(PREVED, function (event) { | ||
this$1$1.pageBreaks.unshift({ | ||
pageIndex: event.pageIndex, | ||
url: url, | ||
title: title, | ||
sentinel: this$1$1.ias.first() | ||
}); | ||
this$1$1.update(); | ||
this$1$1.ias.off(LOADED, loaded); | ||
}); | ||
}; | ||
Paging.prototype.scrolled = function scrolled (event) { | ||
@@ -1408,2 +1495,12 @@ this.update(event.scroll.y); | ||
function prependFn(items, parent, first) { | ||
var insert = document.createDocumentFragment(); | ||
items.forEach(function (item) { | ||
insert.appendChild(item); | ||
}); | ||
parent.insertBefore(insert, first); | ||
} | ||
/* eslint no-console: "off" */ | ||
@@ -1495,11 +1592,5 @@ | ||
var distance = this.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
this.ias.emitter.emit(events.PREFILL); | ||
return this._prefill().then(function () { | ||
return Promise.all([this._prefillNext(), this._prefillPrev()]).then(function () { | ||
this$1$1.ias.emitter.emit(events.PREFILLED); | ||
@@ -1512,18 +1603,34 @@ | ||
Prefill.prototype._prefill = function _prefill () { | ||
Prefill.prototype._prefillNext = function _prefillNext () { | ||
var this$1$1 = this; | ||
return this.ias.next().then(function (hasNextUrl) { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
var distance = this.ias.distance(); | ||
var distance = this$1$1.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
if (distance < 0) { | ||
return this$1$1._prefill(); | ||
} | ||
}); | ||
return this.ias.next() | ||
.then(function (hasNextUrl) { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
var distance = this$1$1.ias.distance(); | ||
if (distance < 0) { | ||
return this$1$1._prefillNext(); | ||
} | ||
}) | ||
; | ||
}; | ||
Prefill.prototype._prefillPrev = function _prefillPrev () { | ||
if (!this.ias.options.prev) { | ||
return; | ||
} | ||
return this.ias.prev(); | ||
}; | ||
var InfiniteAjaxScroll = function InfiniteAjaxScroll(container, options) { | ||
@@ -1550,2 +1657,3 @@ var this$1$1 = this; | ||
this.nextHandler = nextHandler; | ||
this.prevHandler = prevHandler; | ||
@@ -1558,2 +1666,8 @@ if (this.options.next === false) { | ||
if (this.options.prev === false) { | ||
this.prevHandler = function() {}; | ||
} else if (typeof this.options.prev === 'function') { | ||
this.prevHandler = this.options.prev; | ||
} | ||
this.resizeObserver = ResizeObserverFactory(this, this.scrollContainer); | ||
@@ -1566,3 +1680,4 @@ this._scrollListener = lodash_throttle(scrollHandler, 200).bind(this); | ||
this.paused = false; | ||
this.pageIndex = this.sentinel() ? 0 : -1; | ||
this.pageIndexPrev = 0; | ||
this.pageIndex = this.pageIndexNext = this.sentinel() ? 0 : -1; | ||
@@ -1577,2 +1692,10 @@ this.on(HIT, function () { | ||
this.on(TOP, function () { | ||
if (!this$1$1.loadOnScroll) { | ||
return; | ||
} | ||
this$1$1.prev(); | ||
}); | ||
this.on(SCROLLED, this.measure); | ||
@@ -1592,2 +1715,7 @@ this.on(RESIZED, this.measure); | ||
this.hitFirst = this.hitLast = false; | ||
this.on(LAST, function () { return this$1$1.hitLast = true; }); | ||
this.on(FIRST, function () { return this$1$1.hitFirst = true; }); | ||
var ready = function () { | ||
@@ -1653,2 +1781,6 @@ if (this$1$1.ready) { | ||
if (this.hitLast) { | ||
return; | ||
} | ||
if (!this.binded) { | ||
@@ -1664,14 +1796,12 @@ if (!this.ready) { | ||
var pageIndex = this.pageIndex + 1; | ||
var pageIndex = this.pageIndexNext + 1; | ||
this.emitter.emit(NEXT, {pageIndex: this.pageIndex + 1}); | ||
this.emitter.emit(NEXT, {pageIndex: this.pageIndexNext + 1}); | ||
return Promise.resolve(this.nextHandler(pageIndex)) | ||
.then(function (hasNextUrl) { | ||
this$1$1.pageIndex = pageIndex; | ||
this$1$1.pageIndexNext = pageIndex; | ||
if (!hasNextUrl) { | ||
this$1$1.emitter.emit(LAST); | ||
return; | ||
} | ||
@@ -1681,6 +1811,33 @@ | ||
}).then(function () { | ||
this$1$1.emitter.emit(NEXTED, {pageIndex: this$1$1.pageIndex}); | ||
this$1$1.emitter.emit(NEXTED, {pageIndex: this$1$1.pageIndexNext}); | ||
}); | ||
}; | ||
InfiniteAjaxScroll.prototype.prev = function prev () { | ||
var this$1$1 = this; | ||
if (!this.binded || this.hitFirst) { | ||
return; | ||
} | ||
this.pause(); | ||
var pageIndex = this.pageIndexPrev - 1; | ||
this.emitter.emit(PREV, {pageIndex: this.pageIndexPrev - 1}); | ||
return Promise.resolve(this.prevHandler(pageIndex)) | ||
.then(function (hasPrevUrl) { | ||
this$1$1.pageIndexPrev = pageIndex; | ||
this$1$1.resume(); | ||
if (!hasPrevUrl) { | ||
this$1$1.emitter.emit(FIRST); | ||
} | ||
}).then(function () { | ||
this$1$1.emitter.emit(PREVED, {pageIndex: this$1$1.pageIndexPrev}); | ||
}); | ||
}; | ||
/** | ||
@@ -1794,2 +1951,46 @@ * @param {string} url | ||
/** | ||
* @param {array<Element>} items | ||
* @param {Element|null} parent | ||
*/ | ||
InfiniteAjaxScroll.prototype.prepend = function prepend (items, parent) { | ||
var this$1$1 = this; | ||
var ias = this; | ||
parent = parent || ias.container; | ||
var event = { | ||
items: items, | ||
parent: parent, | ||
prependFn: prependFn | ||
}; | ||
ias.emitter.emit(PREPEND, event); | ||
var executor = function (resolve) { | ||
window.requestAnimationFrame(function () { | ||
var first = ias.first(); | ||
var scrollPositionStart = getScrollPosition(this$1$1.scrollContainer); | ||
var topStart = first.getBoundingClientRect().top + scrollPositionStart.y; | ||
Promise.resolve(event.prependFn(event.items, event.parent, ias.first())) | ||
.then(function () { | ||
var scrollPositionEnd = getScrollPosition(this$1$1.scrollContainer); | ||
var topEnd = first.getBoundingClientRect().top + scrollPositionEnd.y; | ||
var deltaY = topEnd - topStart; | ||
this$1$1.scrollContainer.scrollTo(scrollPositionEnd.x, deltaY); | ||
}) | ||
.then(function () { | ||
resolve({items: items, parent: parent}); | ||
}); | ||
}); | ||
}; | ||
return (new Promise(executor)).then(function (event) { | ||
ias.emitter.emit(PREPENDED, event); | ||
}); | ||
}; | ||
InfiniteAjaxScroll.prototype.sentinel = function sentinel () { | ||
@@ -1805,2 +2006,12 @@ var items = tealight(this.options.item, this.container); | ||
InfiniteAjaxScroll.prototype.first = function first () { | ||
var items = tealight(this.options.item, this.container); | ||
if (!items.length) { | ||
return null; | ||
} | ||
return items[0]; | ||
}; | ||
InfiniteAjaxScroll.prototype.pause = function pause () { | ||
@@ -1822,5 +2033,11 @@ this.paused = true; | ||
/** | ||
* @deprecated replaced by distanceBottom | ||
*/ | ||
InfiniteAjaxScroll.prototype.distance = function distance (rootRect, sentinel) { | ||
return this.distanceBottom(rootRect, sentinel); | ||
}; | ||
InfiniteAjaxScroll.prototype.distanceBottom = function distanceBottom (rootRect, sentinel) { | ||
var _rootRect = rootRect || getRootRect(this.scrollContainer); | ||
var _sentinel = sentinel || this.sentinel(); | ||
@@ -1838,4 +2055,10 @@ | ||
InfiniteAjaxScroll.prototype.distanceTop = function distanceTop () { | ||
var scrollPosition = getScrollPosition(this.scrollContainer); | ||
return scrollPosition.y - this.negativeMargin; | ||
}; | ||
InfiniteAjaxScroll.prototype.measure = function measure () { | ||
if (this.paused) { | ||
if (this.paused || (this.hitFirst && this.hitLast)) { | ||
return; | ||
@@ -1856,8 +2079,16 @@ } | ||
var sentinel = this.sentinel(); | ||
if (!this.hitFirst) { | ||
var distanceTop = this.distanceTop(); | ||
var distance = this.distance(rootRect, sentinel); | ||
if (distanceTop <= 0) { | ||
this.emitter.emit(TOP, {distance: distanceTop}); | ||
} | ||
} | ||
if (distance <= 0) { | ||
this.emitter.emit(HIT, {distance: distance}); | ||
if (!this.hitLast) { | ||
var distanceBottom = this.distanceBottom(rootRect, this.sentinel()); | ||
if (distanceBottom <= 0) { | ||
this.emitter.emit(HIT, {distance: distanceBottom}); | ||
} | ||
} | ||
@@ -1864,0 +2095,0 @@ }; |
/** | ||
* Infinite Ajax Scroll v3.0.0 | ||
* Infinite Ajax Scroll v3.1.0-beta.1 | ||
* Turn your existing pagination into infinite scrolling pages with ease | ||
@@ -8,5 +8,5 @@ * | ||
* | ||
* Copyright 2014-2021 Webcreate (Jeroen Fiege) | ||
* Copyright 2014-2022 Webcreate (Jeroen Fiege) | ||
* https://infiniteajaxscroll.com | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).InfiniteAjaxScroll=e()}(this,(function(){"use strict";function t(t){return"object"==typeof window.Node?t instanceof window.Node:null!==t&&"object"==typeof t&&"number"==typeof t.nodeType&&"string"==typeof t.nodeName}function e(e,n){if(void 0===n&&(n=document),e instanceof Array)return e.filter(t);if(t(e))return[e];if(o=Object.prototype.toString.call(i=e),"object"==typeof window.NodeList?i instanceof window.NodeList:null!==i&&"object"==typeof i&&"number"==typeof i.length&&/^\[object (HTMLCollection|NodeList|Object)\]$/.test(o)&&(0===i.length||t(i[0])))return Array.prototype.slice.call(e);var i,o;if("string"==typeof e)try{var r=n.querySelectorAll(e);return Array.prototype.slice.call(r)}catch(t){return[]}return[]}var n=Object.prototype.hasOwnProperty,i=Object.prototype.toString,o=Object.defineProperty,r=Object.getOwnPropertyDescriptor,s=function(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===i.call(t)},l=function(t){if(!t||"[object Object]"!==i.call(t))return!1;var e,o=n.call(t,"constructor"),r=t.constructor&&t.constructor.prototype&&n.call(t.constructor.prototype,"isPrototypeOf");if(t.constructor&&!o&&!r)return!1;for(e in t);return void 0===e||n.call(t,e)},a=function(t,e){o&&"__proto__"===e.name?o(t,e.name,{enumerable:!0,configurable:!0,value:e.newValue,writable:!0}):t[e.name]=e.newValue},h=function(t,e){if("__proto__"===e){if(!n.call(t,e))return;if(r)return r(t,e).value}return t[e]},c=function t(){var e,n,i,o,r,c,u=arguments,d=arguments[0],p=1,f=arguments.length,m=!1;for("boolean"==typeof d&&(m=d,d=arguments[1]||{},p=2),(null==d||"object"!=typeof d&&"function"!=typeof d)&&(d={});f>p;++p)if(null!=(e=u[p]))for(n in e)i=h(d,n),d!==(o=h(e,n))&&(m&&o&&(l(o)||(r=s(o)))?(r?(r=!1,c=i&&s(i)?i:[]):c=i&&l(i)?i:{},a(d,{name:n,newValue:t(m,c,o)})):void 0!==o&&a(d,{name:n,newValue:o}));return d},u="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},d="Expected a function",p=/^\s+|\s+$/g,f=/^[-+]0x[0-9a-f]+$/i,m=/^0b[01]+$/i,y=/^0o[0-7]+$/i,g=parseInt,v="object"==typeof self&&self&&self.Object===Object&&self,b="object"==typeof u&&u&&u.Object===Object&&u||v||Function("return this")(),w=Object.prototype.toString,x=Math.max,O=Math.min,E=function(){return b.Date.now()};function S(t,e,n){var i,o,r,s,l,a,h=0,c=!1,u=!1,p=!0;if("function"!=typeof t)throw new TypeError(d);function f(e){var n=i,r=o;return i=o=void 0,h=e,s=t.apply(r,n)}function m(t){return h=t,l=setTimeout(g,e),c?f(t):s}function y(t){var n=t-a;return void 0===a||n>=e||0>n||u&&t-h>=r}function g(){var t=E();if(y(t))return v(t);l=setTimeout(g,function(t){var n=e-(t-a);return u?O(n,r-(t-h)):n}(t))}function v(t){return l=void 0,p&&i?f(t):(i=o=void 0,s)}function b(){var t=E(),n=y(t);if(i=arguments,o=this,a=t,n){if(void 0===l)return m(a);if(u)return l=setTimeout(g,e),f(a)}return void 0===l&&(l=setTimeout(g,e)),s}return e=j(e)||0,T(n)&&(c=!!n.leading,r=(u="maxWait"in n)?x(j(n.maxWait)||0,e):r,p="trailing"in n?!!n.trailing:p),b.cancel=function(){void 0!==l&&clearTimeout(l),h=0,i=a=o=l=void 0},b.flush=function(){return void 0===l?s:v(E())},b}function T(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function j(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==w.call(t)}(t))return NaN;if(T(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=T(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(p,"");var n=m.test(t);return n||y.test(t)?g(t.slice(2),n?2:8):f.test(t)?NaN:+t}var L=function(t,e,n){var i=!0,o=!0;if("function"!=typeof t)throw new TypeError(d);return T(n)&&(i="leading"in n?!!n.leading:i,o="trailing"in n?!!n.trailing:o),S(t,e,{leading:i,maxWait:e,trailing:o})},I={item:void 0,next:void 0,pagination:void 0,responseType:"document",bind:!0,scrollContainer:window,spinner:!1,logger:!0,loadOnScroll:!0,negativeMargin:0,trigger:!1,prefill:!0},C=function(t,n){var i=e(t);if(i.length>1)throw Error('Expected single element for "'+n+'"');if(0===i.length)throw Error('Element "'+t+'" not found for "'+n+'"')},_=function(t,n){if(0===e(t).length)throw Error('Element "'+t+'" not found for "'+n+'"')},H=function(t){for(var e=[],n=arguments.length-1;n-- >0;)e[n]=arguments[n+1];try{t.apply(void 0,e)}catch(t){console&&console.warn&&console.warn(t.message)}};function N(t){if(t!==window)return{x:t.scrollLeft,y:t.scrollTop};var e=void 0!==window.pageXOffset,n="CSS1Compat"===(document.compatMode||"");return{x:e?window.pageXOffset:n?document.documentElement.scrollLeft:document.body.scrollLeft,y:e?window.pageYOffset:n?document.documentElement.scrollTop:document.body.scrollTop}}function M(t){var e;if(t!==window)e=t.getBoundingClientRect();else{var n=document.documentElement,i=document.body;e={top:0,left:0,right:n.clientWidth||i.clientWidth,width:n.clientWidth||i.clientWidth,bottom:n.clientHeight||i.clientHeight,height:n.clientHeight||i.clientHeight}}return e}var P="append",F="appended",R="binded",D="unbinded",k="hit",A="load",B="loaded",W="error",z="last",X="next",q="nexted",$="ready",V="scrolled",Y="resized",G="page",U="prefill",J="prefilled",K={y:0,x:0,deltaY:0,deltaX:0};function Q(t,e){var n=N(t);return n.deltaY=n.y-(e?e.y:n.y),n.deltaX=n.x-(e?e.x:n.x),n}function Z(){var t=this,e=t._lastScroll=Q(t.scrollContainer,t._lastScroll||K);this.emitter.emit(V,{scroll:e})}function tt(){var t=this,e=t._lastScroll=Q(t.scrollContainer,t._lastScroll||K);this.emitter.emit(Y,{scroll:e})}function et(){}et.prototype={on:function(t,e,n){var i=this.e||(this.e={});return(i[t]||(i[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){var i=this;function o(){i.off(t,o),e.apply(n,arguments)}return o._=e,this.on(t,o,n)},emit:function(t){for(var e=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),i=0,o=n.length;o>i;i++)n[i].fn.apply(n[i].ctx,e);return this},off:function(t,e){var n=this.e||(this.e={}),i=n[t],o=[];if(i&&e)for(var r=0,s=i.length;s>r;r++)i[r].fn!==e&&i[r].fn._!==e&&o.push(i[r]);return o.length?n[t]=o:delete n[t],this}};var nt=et;function it(t){var n=this,i=n._lastResponse||document,o=e(n.options.next,i)[0];if(o)return n.load(o.href).then((function(o){i=n._lastResponse=o.xhr.response;var r=e(n.options.next,i)[0];return n.append(o.items).then((function(){return!!r})).then((function(e){return!e&&1>=t&&console&&console.warn&&console.warn('Element "'+n.options.next+'" not found for "options.next" on "'+o.url+'"'),e}))}));H(C,n.options.next,"options.next")}nt.TinyEmitter=et;var ot={element:void 0,hide:!1};var rt=function(t,e){this.options=c({},ot,function(t){return"string"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE?t={element:t,hide:!0}:"boolean"==typeof t&&(t={element:void 0,hide:t}),t}(e)),this.originalDisplayStyles=new WeakMap,this.options.hide&&(H(_,this.options.element,"pagination.element"),t.on(R,this.hide.bind(this)),t.on(D,this.restore.bind(this)))};rt.prototype.hide=function(){var t=this;e(this.options.element).forEach((function(e){t.originalDisplayStyles.set(e,window.getComputedStyle(e).display),e.style.display="none"}))},rt.prototype.restore=function(){var t=this;e(this.options.element).forEach((function(e){e.style.display=t.originalDisplayStyles.get(e)||"block"}))};var st={element:void 0,delay:600,show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var lt=function(t,n){!1!==n&&(this.ias=t,this.options=c({},st,function(t){return("string"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE)&&(t={element:t}),t}(n)),void 0!==this.options.element&&C(this.options.element,"spinner.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,t.on(R,this.bind.bind(this)),t.on(R,this.hide.bind(this)))};lt.prototype.bind=function(){var t,e,n=this,i=this.ias;i.on(X,(function(){t=+new Date,n.show()})),i.on(z,(function(){n.hide()})),i.on(P,(function(i){e=Math.max(0,n.options.delay-(+new Date-t));var o=i.appendFn.bind({});i.appendFn=function(t,i,r){return new Promise((function(s){setTimeout((function(){n.hide().then((function(){o(t,i,r),s()}))}),e)}))}}))},lt.prototype.show=function(){return Promise.resolve(this.showFn(this.element))},lt.prototype.hide=function(){return Promise.resolve(this.hideFn(this.element))};var at={hit:function(){console.log("Hit scroll threshold")},binded:function(){console.log("Binded event handlers")},unbinded:function(){console.log("Unbinded event handlers")},next:function(t){console.log("Next page triggered [pageIndex="+t.pageIndex+"]")},nexted:function(t){console.log("Next page completed [pageIndex="+t.pageIndex+"]")},load:function(t){console.log("Start loading "+t.url)},loaded:function(){console.log("Finished loading")},append:function(){console.log("Start appending items")},appended:function(t){console.log("Finished appending "+t.items.length+" item(s)")},last:function(){console.log("No more pages left to load")},page:function(t){console.log("Page changed [pageIndex="+t.pageIndex+"]")},prefill:function(t){console.log("Start prefilling")},prefilled:function(t){console.log("Finished prefilling")}};var ht=function(t,e){if(!1!==e){var n=function(t){return!0===t&&(t=at),t}(e);Object.keys(n).forEach((function(e){t.on(e,n[e])}))}};var ct=function(t){this.ias=t,this.pageBreaks=[],this.currentPageIndex=t.pageIndex,this.currentScrollTop=0,t.on(R,this.binded.bind(this)),t.on(X,this.next.bind(this)),t.on(V,this.scrolled.bind(this)),t.on(Y,this.scrolled.bind(this))};ct.prototype.binded=function(){this.ias.sentinel()&&this.pageBreaks.push({pageIndex:this.currentPageIndex,url:""+document.location,title:document.title,sentinel:this.ias.sentinel()})},ct.prototype.next=function(){var t=this,e=""+document.location,n=document.title,i=function(t){e=t.url,t.xhr.response&&(n=t.xhr.response.title)};this.ias.once(B,i),this.ias.once(q,(function(o){t.pageBreaks.push({pageIndex:o.pageIndex,url:e,title:n,sentinel:t.ias.sentinel()}),t.update(),t.ias.off(B,i)}))},ct.prototype.scrolled=function(t){this.update(t.scroll.y)},ct.prototype.update=function(t){this.currentScrollTop=t||this.currentScrollTop;var e=function(t,e,n){for(var i=e+M(n).height,o=t.length-1;o>=0;o--)if(i>t[o].sentinel.getBoundingClientRect().bottom+e)return t[Math.min(o+1,t.length-1)];return t[0]}(this.pageBreaks,this.currentScrollTop,this.ias.scrollContainer);e&&e.pageIndex!==this.currentPageIndex&&(this.ias.emitter.emit(G,e),this.currentPageIndex=e.pageIndex)};var ut={element:void 0,when:function(t){return!0},show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var dt=function(t,n){var i=this;!1!==n&&(this.ias=t,this.options=c({},ut,function(t){if(("string"==typeof t||"function"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE)&&(t={element:t}),"function"==typeof t.element&&(t.element=t.element()),t.when&&Array.isArray(t.when)){var e=t.when;t.when=function(t){return-1!==e.indexOf(t)}}return t}(n)),void 0!==this.options.element&&C(this.options.element,"trigger.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,this.voter=this.options.when,this.showing=void 0,this.enabled=void 0,t.on(R,this.bind.bind(this)),t.on(D,this.unbind.bind(this)),t.on(k,this.hit.bind(this)),t.on(X,(function(t){return i.ias.once(F,(function(){return i.update(t.pageIndex)}))})))};function pt(t,e,n){var i=n?n.nextSibling:null,o=document.createDocumentFragment();t.forEach((function(t){o.appendChild(t)})),e.insertBefore(o,i)}dt.prototype.bind=function(){this.hide(),this.update(this.ias.pageIndex),this.element.addEventListener("click",this.clickHandler.bind(this))},dt.prototype.unbind=function(){this.element.removeEventListener("click",this.clickHandler.bind(this))},dt.prototype.clickHandler=function(){this.hide().then(this.ias.next.bind(this.ias))},dt.prototype.update=function(t){this.enabled=this.voter(t),this.enabled?this.ias.disableLoadOnScroll():this.ias.enableLoadOnScroll()},dt.prototype.hit=function(){this.enabled&&this.show()},dt.prototype.show=function(){if(!this.showing)return this.showing=!0,Promise.resolve(this.showFn(this.element))},dt.prototype.hide=function(){if(this.showing||void 0===this.showing)return this.showing=!1,Promise.resolve(this.hideFn(this.element))};var ft=window.ResizeObserver,mt=function(t,e){this.el=t,this.listener=e};mt.prototype.observe=function(){this.el.addEventListener("resize",this.listener)},mt.prototype.unobserve=function(){this.el.removeEventListener("resize",this.listener)};var yt=function(t,e){this.el=t,this.listener=e,this.ro=new ft(this.listener)};yt.prototype.observe=function(){this.ro.observe(this.el)},yt.prototype.unobserve=function(){this.ro.unobserve()};var gt=function(t,e){this.el=t,this.listener=e,this.interval=null,this.lastHeight=null};gt.prototype.pollHeight=function(){var t=Math.trunc(M(this.el).height);null!==this.lastHeight&&this.lastHeight!==t&&this.listener(),this.lastHeight=t},gt.prototype.observe=function(){this.interval=setInterval(this.pollHeight.bind(this),200)},gt.prototype.unobserve=function(){clearInterval(this.interval)};var vt=function(t,e){this.ias=t,this.enabled=e};vt.prototype.prefill=function(){var t=this;if(this.enabled&&0>=this.ias.distance())return this.ias.emitter.emit(U),this._prefill().then((function(){t.ias.emitter.emit(J),t.ias.measure()}))},vt.prototype._prefill=function(){var t=this;return this.ias.next().then((function(e){if(e)return 0>t.ias.distance()?t._prefill():void 0}))};var bt=function(t,n){var i,o,r,s=this;void 0===n&&(n={}),C(t,"container"),this.container=e(t)[0],this.options=c({},I,n),this.emitter=new nt,this.options.loadOnScroll?this.enableLoadOnScroll():this.disableLoadOnScroll(),this.negativeMargin=Math.abs(this.options.negativeMargin),this.scrollContainer=this.options.scrollContainer,this.options.scrollContainer!==window&&(C(this.options.scrollContainer,"options.scrollContainer"),this.scrollContainer=e(this.options.scrollContainer)[0]),this.nextHandler=it,!1===this.options.next?this.nextHandler=function(){}:"function"==typeof this.options.next&&(this.nextHandler=this.options.next),this.resizeObserver=(i=this,o=this.scrollContainer,r=L(tt,200).bind(i),o===window?new mt(o,r):ft?new yt(o,r):(console&&console.warn&&console.warn("ResizeObserver not supported. Falling back on polling."),new gt(o,r))),this._scrollListener=L(Z,200).bind(this),this.ready=!1,this.bindOnReady=!0,this.binded=!1,this.paused=!1,this.pageIndex=this.sentinel()?0:-1,this.on(k,(function(){s.loadOnScroll&&s.next()})),this.on(V,this.measure),this.on(Y,this.measure),this.pagination=new rt(this,this.options.pagination),this.spinner=new lt(this,this.options.spinner),this.logger=new ht(this,this.options.logger),this.paging=new ct(this),this.trigger=new dt(this,this.options.trigger),this.prefill=new vt(this,this.options.prefill),this.on(R,this.prefill.prefill.bind(this.prefill));var l=function(){s.ready||(s.ready=!0,s.emitter.emit($),s.bindOnReady&&s.options.bind&&s.bind())};"complete"===document.readyState||"interactive"===document.readyState?setTimeout(l,1):window.addEventListener("DOMContentLoaded",l)};return bt.prototype.bind=function(){this.binded||(this.ready||(this.bindOnReady=!1),this.scrollContainer.addEventListener("scroll",this._scrollListener),this.resizeObserver.observe(),this.binded=!0,this.emitter.emit(R))},bt.prototype.unbind=function(){this.binded?(this.resizeObserver.unobserve(),this.scrollContainer.removeEventListener("scroll",this._scrollListener),this.binded=!1,this.emitter.emit(D)):this.ready||this.once(R,this.unbind)},bt.prototype.next=function(){var t=this;if(!this.binded)return this.ready?void 0:this.once(R,this.next);this.pause();var e=this.pageIndex+1;return this.emitter.emit(X,{pageIndex:this.pageIndex+1}),Promise.resolve(this.nextHandler(e)).then((function(n){t.pageIndex=e,n?t.resume():t.emitter.emit(z)})).then((function(){t.emitter.emit(q,{pageIndex:t.pageIndex})}))},bt.prototype.load=function(t){var n=this;return new Promise((function(i,o){var r=new XMLHttpRequest,s={url:t,xhr:r,method:"GET",body:null,nocache:!1,responseType:n.options.responseType,headers:{"X-Requested-With":"XMLHttpRequest"}};n.emitter.emit(A,s);var l=s.url,a=s.method,h=s.responseType,c=s.headers,u=s.body;for(var d in s.nocache||(l=l+(/\?/.test(l)?"&":"?")+(new Date).getTime()),r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE)if(0===r.status);else if(200===r.status){var t=r.response;"document"===h&&(t=e(n.options.item,r.response)),n.emitter.emit(B,{items:t,url:l,xhr:r}),i({items:t,url:l,xhr:r})}else n.emitter.emit(W,{url:l,method:a,xhr:r}),o(r)},r.onerror=function(){n.emitter.emit(W,{url:l,method:a,xhr:r}),o(r)},r.open(a,l,!0),r.responseType=h,c)r.setRequestHeader(d,c[d]);r.send(u)}))},bt.prototype.append=function(t,e){var n=this,i={items:t,parent:e=e||n.container,appendFn:pt};n.emitter.emit(P,i);return new Promise((function(o){window.requestAnimationFrame((function(){Promise.resolve(i.appendFn(i.items,i.parent,n.sentinel())).then((function(){o({items:t,parent:e})}))}))})).then((function(t){n.emitter.emit(F,t)}))},bt.prototype.sentinel=function(){var t=e(this.options.item,this.container);return t.length?t[t.length-1]:null},bt.prototype.pause=function(){this.paused=!0},bt.prototype.resume=function(){this.paused=!1},bt.prototype.enableLoadOnScroll=function(){this.loadOnScroll=!0},bt.prototype.disableLoadOnScroll=function(){this.loadOnScroll=!1},bt.prototype.distance=function(t,e){var n=t||M(this.scrollContainer),i=function(t,e,n){var i=n;if(!t)return-1*i.height;var o=e.y,r=t.getBoundingClientRect();return Math.trunc(o+r.bottom-i.top-(o+i.height))}(e||this.sentinel(),N(this.scrollContainer),n);return i-=this.negativeMargin,i},bt.prototype.measure=function(){if(!this.paused){var t=M(this.scrollContainer);if(0!==t.height){var e=this.sentinel(),n=this.distance(t,e);n>0||this.emitter.emit(k,{distance:n})}}},bt.prototype.on=function(t,e){this.emitter.on(t,e,this),t===R&&this.binded&&e.bind(this)()},bt.prototype.off=function(t,e){this.emitter.off(t,e,this)},bt.prototype.once=function(t,e){var n=this;return new Promise((function(i){n.emitter.once(t,(function(){Promise.resolve(e.apply(this,arguments)).then(i)}),n),t===R&&n.binded&&(e.bind(n)(),i())}))},bt})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).InfiniteAjaxScroll=e()}(this,(function(){"use strict";function t(t){return"object"==typeof window.Node?t instanceof window.Node:null!==t&&"object"==typeof t&&"number"==typeof t.nodeType&&"string"==typeof t.nodeName}function e(e,n){if(void 0===n&&(n=document),e instanceof Array)return e.filter(t);if(t(e))return[e];if(o=Object.prototype.toString.call(i=e),"object"==typeof window.NodeList?i instanceof window.NodeList:null!==i&&"object"==typeof i&&"number"==typeof i.length&&/^\[object (HTMLCollection|NodeList|Object)\]$/.test(o)&&(0===i.length||t(i[0])))return Array.prototype.slice.call(e);var i,o;if("string"==typeof e)try{var r=n.querySelectorAll(e);return Array.prototype.slice.call(r)}catch(t){return[]}return[]}var n=Object.prototype.hasOwnProperty,i=Object.prototype.toString,o=Object.defineProperty,r=Object.getOwnPropertyDescriptor,s=function(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===i.call(t)},l=function(t){if(!t||"[object Object]"!==i.call(t))return!1;var e,o=n.call(t,"constructor"),r=t.constructor&&t.constructor.prototype&&n.call(t.constructor.prototype,"isPrototypeOf");if(t.constructor&&!o&&!r)return!1;for(e in t);return void 0===e||n.call(t,e)},a=function(t,e){o&&"__proto__"===e.name?o(t,e.name,{enumerable:!0,configurable:!0,value:e.newValue,writable:!0}):t[e.name]=e.newValue},h=function(t,e){if("__proto__"===e){if(!n.call(t,e))return;if(r)return r(t,e).value}return t[e]},c=function t(){var e,n,i,o,r,c,p=arguments,u=arguments[0],d=1,f=arguments.length,m=!1;for("boolean"==typeof u&&(m=u,u=arguments[1]||{},d=2),(null==u||"object"!=typeof u&&"function"!=typeof u)&&(u={});f>d;++d)if(null!=(e=p[d]))for(n in e)i=h(u,n),u!==(o=h(e,n))&&(m&&o&&(l(o)||(r=s(o)))?(r?(r=!1,c=i&&s(i)?i:[]):c=i&&l(i)?i:{},a(u,{name:n,newValue:t(m,c,o)})):void 0!==o&&a(u,{name:n,newValue:o}));return u},p="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},u="Expected a function",d=/^\s+|\s+$/g,f=/^[-+]0x[0-9a-f]+$/i,m=/^0b[01]+$/i,g=/^0o[0-7]+$/i,v=parseInt,y="object"==typeof self&&self&&self.Object===Object&&self,b="object"==typeof p&&p&&p.Object===Object&&p||y||Function("return this")(),x=Object.prototype.toString,w=Math.max,O=Math.min,I=function(){return b.Date.now()};function E(t,e,n){var i,o,r,s,l,a,h=0,c=!1,p=!1,d=!0;if("function"!=typeof t)throw new TypeError(u);function f(e){var n=i,r=o;return i=o=void 0,h=e,s=t.apply(r,n)}function m(t){return h=t,l=setTimeout(v,e),c?f(t):s}function g(t){var n=t-a;return void 0===a||n>=e||0>n||p&&t-h>=r}function v(){var t=I();if(g(t))return y(t);l=setTimeout(v,function(t){var n=e-(t-a);return p?O(n,r-(t-h)):n}(t))}function y(t){return l=void 0,d&&i?f(t):(i=o=void 0,s)}function b(){var t=I(),n=g(t);if(i=arguments,o=this,a=t,n){if(void 0===l)return m(a);if(p)return l=setTimeout(v,e),f(a)}return void 0===l&&(l=setTimeout(v,e)),s}return e=T(e)||0,S(n)&&(c=!!n.leading,r=(p="maxWait"in n)?w(T(n.maxWait)||0,e):r,d="trailing"in n?!!n.trailing:d),b.cancel=function(){void 0!==l&&clearTimeout(l),h=0,i=a=o=l=void 0},b.flush=function(){return void 0===l?s:y(I())},b}function S(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function T(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==x.call(t)}(t))return NaN;if(S(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=S(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(d,"");var n=m.test(t);return n||g.test(t)?v(t.slice(2),n?2:8):f.test(t)?NaN:+t}var L=function(t,e,n){var i=!0,o=!0;if("function"!=typeof t)throw new TypeError(u);return S(n)&&(i="leading"in n?!!n.leading:i,o="trailing"in n?!!n.trailing:o),E(t,e,{leading:i,maxWait:e,trailing:o})},C={item:void 0,next:void 0,prev:void 0,pagination:void 0,responseType:"document",bind:!0,scrollContainer:window,spinner:!1,logger:!0,loadOnScroll:!0,negativeMargin:0,trigger:!1,prefill:!0},P=function(t,n){var i=e(t);if(i.length>1)throw Error('Expected single element for "'+n+'"');if(0===i.length)throw Error('Element "'+t+'" not found for "'+n+'"')},j=function(t,n){if(0===e(t).length)throw Error('Element "'+t+'" not found for "'+n+'"')},N=function(t){for(var e=[],n=arguments.length-1;n-- >0;)e[n]=arguments[n+1];try{t.apply(void 0,e)}catch(t){console&&console.warn&&console.warn(t.message)}};function F(t){if(t!==window)return{x:t.scrollLeft,y:t.scrollTop};var e=void 0!==window.pageXOffset,n="CSS1Compat"===(document.compatMode||"");return{x:e?window.pageXOffset:n?document.documentElement.scrollLeft:document.body.scrollLeft,y:e?window.pageYOffset:n?document.documentElement.scrollTop:document.body.scrollTop}}function _(t){var e;if(t!==window)e=t.getBoundingClientRect();else{var n=document.documentElement,i=document.body;e={top:0,left:0,right:n.clientWidth||i.clientWidth,width:n.clientWidth||i.clientWidth,bottom:n.clientHeight||i.clientHeight,height:n.clientHeight||i.clientHeight}}return e}var H="append",M="appended",R="prepend",B="prepended",D="binded",k="unbinded",A="hit",W="top",q="load",z="loaded",X="error",$="first",V="last",Y="next",G="nexted",U="prev",J="preved",K="ready",Q="scrolled",Z="resized",tt="page",et="prefill",nt="prefilled",it={y:0,x:0,deltaY:0,deltaX:0};function ot(t,e){var n=F(t);return n.deltaY=n.y-(e?e.y:n.y),n.deltaX=n.x-(e?e.x:n.x),n}function rt(){var t=this,e=t._lastScroll=ot(t.scrollContainer,t._lastScroll||it);this.emitter.emit(Q,{scroll:e})}function st(){var t=this,e=t._lastScroll=ot(t.scrollContainer,t._lastScroll||it);this.emitter.emit(Z,{scroll:e})}function lt(){}lt.prototype={on:function(t,e,n){var i=this.e||(this.e={});return(i[t]||(i[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){var i=this;function o(){i.off(t,o),e.apply(n,arguments)}return o._=e,this.on(t,o,n)},emit:function(t){for(var e=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),i=0,o=n.length;o>i;i++)n[i].fn.apply(n[i].ctx,e);return this},off:function(t,e){var n=this.e||(this.e={}),i=n[t],o=[];if(i&&e)for(var r=0,s=i.length;s>r;r++)i[r].fn!==e&&i[r].fn._!==e&&o.push(i[r]);return o.length?n[t]=o:delete n[t],this}};var at=lt;function ht(t){var n=this,i=n._lastResponse||document.body,o=e(n.options.next,i)[0];if(o)return n.load(o.href).then((function(o){i=n._lastResponse=o.xhr.response;var r=e(n.options.next,i)[0];return n.append(o.items).then((function(){return!!r})).then((function(e){return!e&&1>=t&&console&&console.warn&&console.warn('Element "'+n.options.next+'" not found for "options.next" on "'+o.url+'"'),e}))}));N(P,n.options.next,"options.next")}function ct(t){var n=this,i=n._prevEl||e(n.options.prev,document.body)[0];if(void 0!==n.options.prev){if(i)return n.load(i.href).then((function(t){var i=n._prevEl=e(n.options.prev,t.xhr.response)[0];return n.prepend(t.items).then((function(){return!!i}))}));N(P,n.options.prev,"options.prev")}}at.TinyEmitter=lt;var pt={element:void 0,hide:!1};var ut=function(t,e){this.options=c({},pt,function(t){return"string"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE?t={element:t,hide:!0}:"boolean"==typeof t&&(t={element:void 0,hide:t}),t}(e)),this.originalDisplayStyles=new WeakMap,this.options.hide&&(N(j,this.options.element,"pagination.element"),t.on(D,this.hide.bind(this)),t.on(k,this.restore.bind(this)))};ut.prototype.hide=function(){var t=this;e(this.options.element).forEach((function(e){t.originalDisplayStyles.set(e,window.getComputedStyle(e).display),e.style.display="none"}))},ut.prototype.restore=function(){var t=this;e(this.options.element).forEach((function(e){e.style.display=t.originalDisplayStyles.get(e)||"block"}))};var dt={element:void 0,delay:600,show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var ft=function(t,n){!1!==n&&(this.ias=t,this.options=c({},dt,function(t){return("string"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE)&&(t={element:t}),t}(n)),void 0!==this.options.element&&P(this.options.element,"spinner.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,t.on(D,this.bind.bind(this)),t.on(D,this.hide.bind(this)))};ft.prototype.bind=function(){var t,e,n=this,i=this.ias;i.on(Y,(function(){t=+new Date,n.show()})),i.on(V,(function(){n.hide()})),i.on(H,(function(i){e=Math.max(0,n.options.delay-(+new Date-t));var o=i.appendFn.bind({});i.appendFn=function(t,i,r){return new Promise((function(s){setTimeout((function(){n.hide().then((function(){o(t,i,r),s()}))}),e)}))}}))},ft.prototype.show=function(){return Promise.resolve(this.showFn(this.element))},ft.prototype.hide=function(){return Promise.resolve(this.hideFn(this.element))};var mt={hit:function(){console.log("Hit scroll threshold")},top:function(){console.log("Hit top scroll threshold")},binded:function(){console.log("Binded event handlers")},unbinded:function(){console.log("Unbinded event handlers")},next:function(t){console.log("Next page triggered [pageIndex="+t.pageIndex+"]")},nexted:function(t){console.log("Next page completed [pageIndex="+t.pageIndex+"]")},prev:function(t){console.log("Previous page triggered [pageIndex="+t.pageIndex+"]")},preved:function(t){console.log("Previous page completed [pageIndex="+t.pageIndex+"]")},load:function(t){console.log("Start loading "+t.url)},loaded:function(){console.log("Finished loading")},append:function(){console.log("Start appending items")},appended:function(t){console.log("Finished appending "+t.items.length+" item(s)")},prepend:function(){console.log("Start prepending items")},prepended:function(t){console.log("Finished prepending "+t.items.length+" item(s)")},last:function(){console.log("No more pages left to load")},first:function(){console.log("Reached first page")},page:function(t){console.log("Page changed [pageIndex="+t.pageIndex+"]")},prefill:function(t){console.log("Start prefilling")},prefilled:function(t){console.log("Finished prefilling")}};var gt=function(t,e){if(!1!==e){var n=function(t){return!0===t&&(t=mt),t}(e);Object.keys(n).forEach((function(e){t.on(e,n[e])}))}};var vt=function(t){this.ias=t,this.pageBreaks=[],this.currentPageIndex=t.pageIndex,this.currentScrollTop=0,t.on(D,this.binded.bind(this)),t.on(Y,this.next.bind(this)),t.on(U,this.prev.bind(this)),t.on(Q,this.scrolled.bind(this)),t.on(Z,this.scrolled.bind(this))};vt.prototype.binded=function(){this.ias.sentinel()&&this.pageBreaks.push({pageIndex:this.currentPageIndex,url:""+document.location,title:document.title,sentinel:this.ias.sentinel()})},vt.prototype.next=function(){var t=this,e=""+document.location,n=document.title,i=function(t){e=t.url,t.xhr.response&&(n=t.xhr.response.title)};this.ias.once(z,i),this.ias.once(G,(function(o){t.pageBreaks.push({pageIndex:o.pageIndex,url:e,title:n,sentinel:t.ias.sentinel()}),t.update(),t.ias.off(z,i)}))},vt.prototype.prev=function(){var t=this,e=""+document.location,n=document.title,i=function(t){e=t.url,t.xhr.response&&(n=t.xhr.response.title)};this.ias.once(z,i),this.ias.once(J,(function(o){t.pageBreaks.unshift({pageIndex:o.pageIndex,url:e,title:n,sentinel:t.ias.first()}),t.update(),t.ias.off(z,i)}))},vt.prototype.scrolled=function(t){this.update(t.scroll.y)},vt.prototype.update=function(t){this.currentScrollTop=t||this.currentScrollTop;var e=function(t,e,n){for(var i=e+_(n).height,o=t.length-1;o>=0;o--)if(i>t[o].sentinel.getBoundingClientRect().bottom+e)return t[Math.min(o+1,t.length-1)];return t[0]}(this.pageBreaks,this.currentScrollTop,this.ias.scrollContainer);e&&e.pageIndex!==this.currentPageIndex&&(this.ias.emitter.emit(tt,e),this.currentPageIndex=e.pageIndex)};var yt={element:void 0,when:function(t){return!0},show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var bt=function(t,n){var i=this;!1!==n&&(this.ias=t,this.options=c({},yt,function(t){if(("string"==typeof t||"function"==typeof t||"object"==typeof t&&t.nodeType===Node.ELEMENT_NODE)&&(t={element:t}),"function"==typeof t.element&&(t.element=t.element()),t.when&&Array.isArray(t.when)){var e=t.when;t.when=function(t){return-1!==e.indexOf(t)}}return t}(n)),void 0!==this.options.element&&P(this.options.element,"trigger.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,this.voter=this.options.when,this.showing=void 0,this.enabled=void 0,t.on(D,this.bind.bind(this)),t.on(k,this.unbind.bind(this)),t.on(A,this.hit.bind(this)),t.on(Y,(function(t){return i.ias.once(M,(function(){return i.update(t.pageIndex)}))})))};function xt(t,e,n){var i=n?n.nextSibling:null,o=document.createDocumentFragment();t.forEach((function(t){o.appendChild(t)})),e.insertBefore(o,i)}function wt(t,e,n){var i=document.createDocumentFragment();t.forEach((function(t){i.appendChild(t)})),e.insertBefore(i,n)}bt.prototype.bind=function(){this.hide(),this.update(this.ias.pageIndex),this.element.addEventListener("click",this.clickHandler.bind(this))},bt.prototype.unbind=function(){this.element.removeEventListener("click",this.clickHandler.bind(this))},bt.prototype.clickHandler=function(){this.hide().then(this.ias.next.bind(this.ias))},bt.prototype.update=function(t){this.enabled=this.voter(t),this.enabled?this.ias.disableLoadOnScroll():this.ias.enableLoadOnScroll()},bt.prototype.hit=function(){this.enabled&&this.show()},bt.prototype.show=function(){if(!this.showing)return this.showing=!0,Promise.resolve(this.showFn(this.element))},bt.prototype.hide=function(){if(this.showing||void 0===this.showing)return this.showing=!1,Promise.resolve(this.hideFn(this.element))};var Ot=window.ResizeObserver,It=function(t,e){this.el=t,this.listener=e};It.prototype.observe=function(){this.el.addEventListener("resize",this.listener)},It.prototype.unobserve=function(){this.el.removeEventListener("resize",this.listener)};var Et=function(t,e){this.el=t,this.listener=e,this.ro=new Ot(this.listener)};Et.prototype.observe=function(){this.ro.observe(this.el)},Et.prototype.unobserve=function(){this.ro.unobserve()};var St=function(t,e){this.el=t,this.listener=e,this.interval=null,this.lastHeight=null};St.prototype.pollHeight=function(){var t=Math.trunc(_(this.el).height);null!==this.lastHeight&&this.lastHeight!==t&&this.listener(),this.lastHeight=t},St.prototype.observe=function(){this.interval=setInterval(this.pollHeight.bind(this),200)},St.prototype.unobserve=function(){clearInterval(this.interval)};var Tt=function(t,e){this.ias=t,this.enabled=e};Tt.prototype.prefill=function(){var t=this;if(this.enabled)return this.ias.emitter.emit(et),Promise.all([this._prefillNext(),this._prefillPrev()]).then((function(){t.ias.emitter.emit(nt),t.ias.measure()}))},Tt.prototype._prefillNext=function(){var t=this;if(0>=this.ias.distance())return this.ias.next().then((function(e){if(e)return 0>t.ias.distance()?t._prefillNext():void 0}))},Tt.prototype._prefillPrev=function(){if(this.ias.options.prev)return this.ias.prev()};var Lt=function(t,n){var i,o,r,s=this;void 0===n&&(n={}),P(t,"container"),this.container=e(t)[0],this.options=c({},C,n),this.emitter=new at,this.options.loadOnScroll?this.enableLoadOnScroll():this.disableLoadOnScroll(),this.negativeMargin=Math.abs(this.options.negativeMargin),this.scrollContainer=this.options.scrollContainer,this.options.scrollContainer!==window&&(P(this.options.scrollContainer,"options.scrollContainer"),this.scrollContainer=e(this.options.scrollContainer)[0]),this.nextHandler=ht,this.prevHandler=ct,!1===this.options.next?this.nextHandler=function(){}:"function"==typeof this.options.next&&(this.nextHandler=this.options.next),!1===this.options.prev?this.prevHandler=function(){}:"function"==typeof this.options.prev&&(this.prevHandler=this.options.prev),this.resizeObserver=(i=this,o=this.scrollContainer,r=L(st,200).bind(i),o===window?new It(o,r):Ot?new Et(o,r):(console&&console.warn&&console.warn("ResizeObserver not supported. Falling back on polling."),new St(o,r))),this._scrollListener=L(rt,200).bind(this),this.ready=!1,this.bindOnReady=!0,this.binded=!1,this.paused=!1,this.pageIndexPrev=0,this.pageIndex=this.pageIndexNext=this.sentinel()?0:-1,this.on(A,(function(){s.loadOnScroll&&s.next()})),this.on(W,(function(){s.loadOnScroll&&s.prev()})),this.on(Q,this.measure),this.on(Z,this.measure),this.pagination=new ut(this,this.options.pagination),this.spinner=new ft(this,this.options.spinner),this.logger=new gt(this,this.options.logger),this.paging=new vt(this),this.trigger=new bt(this,this.options.trigger),this.prefill=new Tt(this,this.options.prefill),this.on(D,this.prefill.prefill.bind(this.prefill)),this.hitFirst=this.hitLast=!1,this.on(V,(function(){return s.hitLast=!0})),this.on($,(function(){return s.hitFirst=!0}));var l=function(){s.ready||(s.ready=!0,s.emitter.emit(K),s.bindOnReady&&s.options.bind&&s.bind())};"complete"===document.readyState||"interactive"===document.readyState?setTimeout(l,1):window.addEventListener("DOMContentLoaded",l)};return Lt.prototype.bind=function(){this.binded||(this.ready||(this.bindOnReady=!1),this.scrollContainer.addEventListener("scroll",this._scrollListener),this.resizeObserver.observe(),this.binded=!0,this.emitter.emit(D))},Lt.prototype.unbind=function(){this.binded?(this.resizeObserver.unobserve(),this.scrollContainer.removeEventListener("scroll",this._scrollListener),this.binded=!1,this.emitter.emit(k)):this.ready||this.once(D,this.unbind)},Lt.prototype.next=function(){var t=this;if(!this.hitLast){if(!this.binded)return this.ready?void 0:this.once(D,this.next);this.pause();var e=this.pageIndexNext+1;return this.emitter.emit(Y,{pageIndex:this.pageIndexNext+1}),Promise.resolve(this.nextHandler(e)).then((function(n){t.pageIndexNext=e,n||t.emitter.emit(V),t.resume()})).then((function(){t.emitter.emit(G,{pageIndex:t.pageIndexNext})}))}},Lt.prototype.prev=function(){var t=this;if(this.binded&&!this.hitFirst){this.pause();var e=this.pageIndexPrev-1;return this.emitter.emit(U,{pageIndex:this.pageIndexPrev-1}),Promise.resolve(this.prevHandler(e)).then((function(n){t.pageIndexPrev=e,t.resume(),n||t.emitter.emit($)})).then((function(){t.emitter.emit(J,{pageIndex:t.pageIndexPrev})}))}},Lt.prototype.load=function(t){var n=this;return new Promise((function(i,o){var r=new XMLHttpRequest,s={url:t,xhr:r,method:"GET",body:null,nocache:!1,responseType:n.options.responseType,headers:{"X-Requested-With":"XMLHttpRequest"}};n.emitter.emit(q,s);var l=s.url,a=s.method,h=s.responseType,c=s.headers,p=s.body;for(var u in s.nocache||(l=l+(/\?/.test(l)?"&":"?")+(new Date).getTime()),r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE)if(0===r.status);else if(200===r.status){var t=r.response;"document"===h&&(t=e(n.options.item,r.response)),n.emitter.emit(z,{items:t,url:l,xhr:r}),i({items:t,url:l,xhr:r})}else n.emitter.emit(X,{url:l,method:a,xhr:r}),o(r)},r.onerror=function(){n.emitter.emit(X,{url:l,method:a,xhr:r}),o(r)},r.open(a,l,!0),r.responseType=h,c)r.setRequestHeader(u,c[u]);r.send(p)}))},Lt.prototype.append=function(t,e){var n=this,i={items:t,parent:e=e||n.container,appendFn:xt};n.emitter.emit(H,i);return new Promise((function(o){window.requestAnimationFrame((function(){Promise.resolve(i.appendFn(i.items,i.parent,n.sentinel())).then((function(){o({items:t,parent:e})}))}))})).then((function(t){n.emitter.emit(M,t)}))},Lt.prototype.prepend=function(t,e){var n=this,i=this,o={items:t,parent:e=e||i.container,prependFn:wt};i.emitter.emit(R,o);return new Promise((function(r){window.requestAnimationFrame((function(){var s=i.first(),l=F(n.scrollContainer),a=s.getBoundingClientRect().top+l.y;Promise.resolve(o.prependFn(o.items,o.parent,i.first())).then((function(){var t=F(n.scrollContainer),e=s.getBoundingClientRect().top+t.y;n.scrollContainer.scrollTo(t.x,e-a)})).then((function(){r({items:t,parent:e})}))}))})).then((function(t){i.emitter.emit(B,t)}))},Lt.prototype.sentinel=function(){var t=e(this.options.item,this.container);return t.length?t[t.length-1]:null},Lt.prototype.first=function(){var t=e(this.options.item,this.container);return t.length?t[0]:null},Lt.prototype.pause=function(){this.paused=!0},Lt.prototype.resume=function(){this.paused=!1},Lt.prototype.enableLoadOnScroll=function(){this.loadOnScroll=!0},Lt.prototype.disableLoadOnScroll=function(){this.loadOnScroll=!1},Lt.prototype.distance=function(t,e){return this.distanceBottom(t,e)},Lt.prototype.distanceBottom=function(t,e){var n=t||_(this.scrollContainer),i=function(t,e,n){var i=n;if(!t)return-1*i.height;var o=e.y,r=t.getBoundingClientRect();return Math.trunc(o+r.bottom-i.top-(o+i.height))}(e||this.sentinel(),F(this.scrollContainer),n);return i-=this.negativeMargin},Lt.prototype.distanceTop=function(){return F(this.scrollContainer).y-this.negativeMargin},Lt.prototype.measure=function(){if(!(this.paused||this.hitFirst&&this.hitLast)){var t=_(this.scrollContainer);if(0!==t.height){if(!this.hitFirst){var e=this.distanceTop();e>0||this.emitter.emit(W,{distance:e})}if(!this.hitLast){var n=this.distanceBottom(t,this.sentinel());n>0||this.emitter.emit(A,{distance:n})}}}},Lt.prototype.on=function(t,e){this.emitter.on(t,e,this),t===D&&this.binded&&e.bind(this)()},Lt.prototype.off=function(t,e){this.emitter.off(t,e,this)},Lt.prototype.once=function(t,e){var n=this;return new Promise((function(i){n.emitter.once(t,(function(){Promise.resolve(e.apply(this,arguments)).then(i)}),n),t===D&&n.binded&&(e.bind(n)(),i())}))},Lt})); |
{ | ||
"name": "@webcreate/infinite-ajax-scroll", | ||
"version": "3.0.0", | ||
"version": "3.1.0-beta.1", | ||
"title": "Infinite Ajax Scroll", | ||
@@ -61,4 +61,4 @@ "description": "Turn your existing pagination into infinite scrolling pages with ease", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"serve": "^13.0.2" | ||
"serve": "^14.0.1" | ||
} | ||
} |
@@ -17,3 +17,3 @@ <p align="center"> | ||
[![Build Status](https://travis-ci.org/webcreate/infinite-ajax-scroll.svg?branch=3.x)](https://travis-ci.org/webcreate/infinite-ajax-scroll) [![npm](https://img.shields.io/npm/v/@webcreate/infinite-ajax-scroll)](https://www.npmjs.com/package/@webcreate/infinite-ajax-scroll) | ||
[![npm](https://img.shields.io/npm/v/@webcreate/infinite-ajax-scroll)](https://www.npmjs.com/package/@webcreate/infinite-ajax-scroll) | ||
@@ -27,3 +27,3 @@ ## Installation | ||
```markup | ||
<script src="https://unpkg.com/@webcreate/infinite-ajax-scroll@^3.0.0-beta.6/dist/infinite-ajax-scroll.min.js"></script> | ||
<script src="https://unpkg.com/@webcreate/infinite-ajax-scroll@^3.0.0/dist/infinite-ajax-scroll.min.js"></script> | ||
``` | ||
@@ -54,3 +54,5 @@ | ||
<div class="pagination"> | ||
<a href="page2.html" class="next">Next</a> | ||
<a href="page1.html" class="prev">Prev</a> | ||
<span class="current">1</span> | ||
<a href="page3.html" class="next">Next</a> | ||
</div> | ||
@@ -68,2 +70,3 @@ ``` | ||
next: '.next', | ||
prev: '.prev', | ||
pagination: '.pagination' | ||
@@ -70,0 +73,0 @@ }); |
export default { | ||
item: undefined, | ||
next: undefined, | ||
prev: undefined, | ||
pagination: undefined, | ||
@@ -5,0 +6,0 @@ responseType: 'document', |
export const APPEND = 'append'; | ||
export const APPENDED = 'appended'; | ||
export const PREPEND = 'prepend'; | ||
export const PREPENDED = 'prepended'; | ||
export const BINDED = 'binded'; | ||
export const UNBINDED = 'unbinded'; | ||
export const HIT = 'hit'; | ||
export const TOP = 'top'; | ||
export const LOAD = 'load'; | ||
export const LOADED = 'loaded'; | ||
export const ERROR = 'error'; | ||
export const FIRST = 'first'; | ||
export const LAST = 'last'; | ||
export const NEXT = 'next'; | ||
export const NEXTED = 'nexted'; | ||
export const PREV = 'prev'; | ||
export const PREVED = 'preved'; | ||
export const READY = 'ready'; | ||
@@ -22,11 +28,17 @@ export const SCROLLED = 'scrolled'; | ||
APPENDED, | ||
PREPEND, | ||
PREPENDED, | ||
BINDED, | ||
UNBINDED, | ||
HIT, | ||
TOP, | ||
LOAD, | ||
LOADED, | ||
ERROR, | ||
FIRST, | ||
LAST, | ||
NEXT, | ||
NEXTED, | ||
PREV, | ||
PREVED, | ||
READY, | ||
@@ -33,0 +45,0 @@ SCROLLED, |
@@ -10,2 +10,3 @@ import $ from 'tealight'; | ||
import {nextHandler} from './next-handler'; | ||
import {prevHandler} from './prev-handler'; | ||
import Pagination from './pagination'; | ||
@@ -17,2 +18,3 @@ import Spinner from './spinner'; | ||
import {appendFn} from './append'; | ||
import {prependFn} from './prepend'; | ||
import * as Events from './events'; | ||
@@ -41,2 +43,3 @@ import ResizeObserverFactory from './resize-observer'; | ||
this.nextHandler = nextHandler; | ||
this.prevHandler = prevHandler; | ||
@@ -49,2 +52,8 @@ if (this.options.next === false) { | ||
if (this.options.prev === false) { | ||
this.prevHandler = function() {} | ||
} else if (typeof this.options.prev === 'function') { | ||
this.prevHandler = this.options.prev; | ||
} | ||
this.resizeObserver = ResizeObserverFactory(this, this.scrollContainer); | ||
@@ -57,3 +66,4 @@ this._scrollListener = throttle(scrollHandler, 200).bind(this); | ||
this.paused = false; | ||
this.pageIndex = this.sentinel() ? 0 : -1; | ||
this.pageIndexPrev = 0; | ||
this.pageIndex = this.pageIndexNext = this.sentinel() ? 0 : -1; | ||
@@ -68,2 +78,10 @@ this.on(Events.HIT, () => { | ||
this.on(Events.TOP, () => { | ||
if (!this.loadOnScroll) { | ||
return; | ||
} | ||
this.prev(); | ||
}); | ||
this.on(Events.SCROLLED, this.measure); | ||
@@ -83,2 +101,7 @@ this.on(Events.RESIZED, this.measure); | ||
this.hitFirst = this.hitLast = false; | ||
this.on(Events.LAST, () => this.hitLast = true); | ||
this.on(Events.FIRST, () => this.hitFirst = true); | ||
let ready = () => { | ||
@@ -142,2 +165,6 @@ if (this.ready) { | ||
next() { | ||
if (this.hitLast) { | ||
return; | ||
} | ||
if (!this.binded) { | ||
@@ -153,14 +180,12 @@ if (!this.ready) { | ||
const pageIndex = this.pageIndex + 1; | ||
const pageIndex = this.pageIndexNext + 1; | ||
this.emitter.emit(Events.NEXT, {pageIndex: this.pageIndex + 1}); | ||
this.emitter.emit(Events.NEXT, {pageIndex: this.pageIndexNext + 1}); | ||
return Promise.resolve(this.nextHandler(pageIndex)) | ||
.then((hasNextUrl) => { | ||
this.pageIndex = pageIndex; | ||
this.pageIndexNext = pageIndex; | ||
if (!hasNextUrl) { | ||
this.emitter.emit(Events.LAST); | ||
return; | ||
} | ||
@@ -170,6 +195,31 @@ | ||
}).then(() => { | ||
this.emitter.emit(Events.NEXTED, {pageIndex: this.pageIndex}); | ||
this.emitter.emit(Events.NEXTED, {pageIndex: this.pageIndexNext}); | ||
}); | ||
} | ||
prev() { | ||
if (!this.binded || this.hitFirst) { | ||
return; | ||
} | ||
this.pause(); | ||
const pageIndex = this.pageIndexPrev - 1; | ||
this.emitter.emit(Events.PREV, {pageIndex: this.pageIndexPrev - 1}); | ||
return Promise.resolve(this.prevHandler(pageIndex)) | ||
.then((hasPrevUrl) => { | ||
this.pageIndexPrev = pageIndex; | ||
this.resume(); | ||
if (!hasPrevUrl) { | ||
this.emitter.emit(Events.FIRST); | ||
} | ||
}).then(() => { | ||
this.emitter.emit(Events.PREVED, {pageIndex: this.pageIndexPrev}); | ||
}); | ||
} | ||
/** | ||
@@ -285,2 +335,44 @@ * @param {string} url | ||
/** | ||
* @param {array<Element>} items | ||
* @param {Element|null} parent | ||
*/ | ||
prepend(items, parent) { | ||
let ias = this; | ||
parent = parent || ias.container; | ||
let event = { | ||
items, | ||
parent, | ||
prependFn | ||
}; | ||
ias.emitter.emit(Events.PREPEND, event); | ||
let executor = (resolve) => { | ||
window.requestAnimationFrame(() => { | ||
const first = ias.first(); | ||
const scrollPositionStart = getScrollPosition(this.scrollContainer); | ||
const topStart = first.getBoundingClientRect().top + scrollPositionStart.y; | ||
Promise.resolve(event.prependFn(event.items, event.parent, ias.first())) | ||
.then(() => { | ||
const scrollPositionEnd = getScrollPosition(this.scrollContainer); | ||
const topEnd = first.getBoundingClientRect().top + scrollPositionEnd.y; | ||
let deltaY = topEnd - topStart; | ||
this.scrollContainer.scrollTo(scrollPositionEnd.x, deltaY); | ||
}) | ||
.then(() => { | ||
resolve({items, parent}); | ||
}); | ||
}); | ||
}; | ||
return (new Promise(executor)).then((event) => { | ||
ias.emitter.emit(Events.PREPENDED, event); | ||
}); | ||
} | ||
sentinel() { | ||
@@ -296,2 +388,12 @@ const items = $(this.options.item, this.container); | ||
first() { | ||
const items = $(this.options.item, this.container); | ||
if (!items.length) { | ||
return null; | ||
} | ||
return items[0]; | ||
} | ||
pause() { | ||
@@ -313,5 +415,11 @@ this.paused = true; | ||
/** | ||
* @deprecated replaced by distanceBottom | ||
*/ | ||
distance(rootRect, sentinel) { | ||
return this.distanceBottom(rootRect, sentinel); | ||
} | ||
distanceBottom(rootRect, sentinel) { | ||
const _rootRect = rootRect || getRootRect(this.scrollContainer); | ||
const _sentinel = sentinel || this.sentinel(); | ||
@@ -329,4 +437,10 @@ | ||
distanceTop() { | ||
const scrollPosition = getScrollPosition(this.scrollContainer); | ||
return scrollPosition.y - this.negativeMargin; | ||
} | ||
measure() { | ||
if (this.paused) { | ||
if (this.paused || (this.hitFirst && this.hitLast)) { | ||
return; | ||
@@ -347,8 +461,16 @@ } | ||
const sentinel = this.sentinel(); | ||
if (!this.hitFirst) { | ||
let distanceTop = this.distanceTop(); | ||
let distance = this.distance(rootRect, sentinel); | ||
if (distanceTop <= 0) { | ||
this.emitter.emit(Events.TOP, {distance: distanceTop}); | ||
} | ||
} | ||
if (distance <= 0) { | ||
this.emitter.emit(Events.HIT, {distance}); | ||
if (!this.hitLast) { | ||
let distanceBottom = this.distanceBottom(rootRect, this.sentinel()); | ||
if (distanceBottom <= 0) { | ||
this.emitter.emit(Events.HIT, {distance: distanceBottom}); | ||
} | ||
} | ||
@@ -355,0 +477,0 @@ } |
@@ -7,2 +7,5 @@ /* eslint no-console: "off" */ | ||
}, | ||
top: () => { | ||
console.log(`Hit top scroll threshold`); | ||
}, | ||
binded: () => { | ||
@@ -26,2 +29,8 @@ console.log(`Binded event handlers`); | ||
}, | ||
prev: (event) => { | ||
console.log(`Previous page triggered [pageIndex=${event.pageIndex}]`); | ||
}, | ||
preved: (event) => { | ||
console.log(`Previous page completed [pageIndex=${event.pageIndex}]`); | ||
}, | ||
load: (event) => { | ||
@@ -39,5 +48,14 @@ console.log(`Start loading ${event.url}`); | ||
}, | ||
prepend: () => { | ||
console.log(`Start prepending items`); | ||
}, | ||
prepended: (event) => { | ||
console.log(`Finished prepending ${event.items.length} item(s)`); | ||
}, | ||
last: () => { | ||
console.log(`No more pages left to load`); | ||
}, | ||
first: () => { | ||
console.log(`Reached first page`); | ||
}, | ||
page: (event) => { | ||
@@ -44,0 +62,0 @@ console.log(`Page changed [pageIndex=${event.pageIndex}]`); |
@@ -6,3 +6,3 @@ import $ from 'tealight'; | ||
let ias = this; | ||
let lastResponse = ias._lastResponse || document; | ||
let lastResponse = ias._lastResponse || document.body; | ||
@@ -9,0 +9,0 @@ let nextEl = $(ias.options.next, lastResponse)[0]; |
@@ -30,2 +30,3 @@ import {getRootRect} from "./dimensions"; | ||
ias.on(Events.NEXT, this.next.bind(this)); | ||
ias.on(Events.PREV, this.prev.bind(this)); | ||
ias.on(Events.SCROLLED, this.scrolled.bind(this)); | ||
@@ -77,2 +78,30 @@ ias.on(Events.RESIZED, this.scrolled.bind(this)); | ||
prev() { | ||
let url = document.location.toString(); | ||
let title = document.title; | ||
let loaded = (event) => { | ||
url = event.url; | ||
if (event.xhr.response) { | ||
title = event.xhr.response.title | ||
} | ||
}; | ||
this.ias.once(Events.LOADED, loaded); | ||
this.ias.once(Events.PREVED, (event) => { | ||
this.pageBreaks.unshift({ | ||
pageIndex: event.pageIndex, | ||
url, | ||
title, | ||
sentinel: this.ias.first() | ||
}); | ||
this.update(); | ||
this.ias.off(Events.LOADED, loaded); | ||
}); | ||
} | ||
scrolled(event) { | ||
@@ -79,0 +108,0 @@ this.update(event.scroll.y); |
@@ -14,11 +14,5 @@ import Events from "./events"; | ||
let distance = this.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
this.ias.emitter.emit(Events.PREFILL); | ||
return this._prefill().then(() => { | ||
return Promise.all([this._prefillNext(), this._prefillPrev()]).then(() => { | ||
this.ias.emitter.emit(Events.PREFILLED); | ||
@@ -31,15 +25,31 @@ | ||
_prefill() { | ||
return this.ias.next().then((hasNextUrl) => { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
_prefillNext() { | ||
let distance = this.ias.distance(); | ||
let distance = this.ias.distance(); | ||
if (distance > 0) { | ||
return; | ||
} | ||
if (distance < 0) { | ||
return this._prefill(); | ||
} | ||
}); | ||
return this.ias.next() | ||
.then((hasNextUrl) => { | ||
if (!hasNextUrl) { | ||
return; | ||
} | ||
let distance = this.ias.distance(); | ||
if (distance < 0) { | ||
return this._prefillNext(); | ||
} | ||
}) | ||
; | ||
} | ||
_prefillPrev() { | ||
if (!this.ias.options.prev) { | ||
return; | ||
} | ||
return this.ias.prev(); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151179
23
3953
84
1