Socket
Socket
Sign inDemoInstall

@webcreate/infinite-ajax-scroll

Package Overview
Dependencies
6
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.0-beta.1 to 3.0.0-beta.2

src/append.js

71

dist/infinite-ajax-scroll.es.js
/**
* Infinite Ajax Scroll v3.0.0-beta.1
* Infinite Ajax Scroll v3.0.0-beta.2
* Turn your existing pagination into infinite scrolling pages with ease

@@ -263,13 +263,17 @@ *

// original executor
var executor = event.executor;
// copy original append function
var appendFn = event.appendFn;
// wrap executor with delay
event.executor = function (resolve) {
setTimeout(function () {
// turn hide function into promise
Promise.resolve(self.hide()).then(function () {
executor(resolve);
});
}, delay);
// wrap append function with delay
event.appendFn = function (items, parent, last) {
return new Promise(function (resolve) {
setTimeout(function () {
// turn hide function into promise
Promise.resolve(self.hide()).then(function () {
appendFn(items, parent, last);
resolve();
});
}, delay);
});
};

@@ -438,2 +442,13 @@ });

function appendFn(items, parent, last) {
var sibling = last ? last.nextSibling : null;
var insert = document.createDocumentFragment();
items.forEach(function (item) {
insert.appendChild(item);
});
parent.insertBefore(insert, sibling);
}
var scrollListener;

@@ -591,31 +606,21 @@ var resizeListener;

// @todo move fragment creation into executor?
var insert = document.createDocumentFragment();
var event = {
items: items,
parent: parent,
appendFn: appendFn
};
items.forEach(function (item) {
insert.appendChild(item);
});
ias.emitter.emit('append', event);
var executor = function (resolve) {
var last = ias.sentinel();
var sibling = last ? last.nextSibling : null;
window.requestAnimationFrame(function () {
parent.insertBefore(insert, sibling);
resolve({items: items, parent: parent});
ias.emitter.emit('appended', {items: items, parent: parent});
Promise.resolve(event.appendFn(event.items, event.parent, ias.sentinel())).then(function () {
resolve({items: items, parent: parent});
});
});
};
var event = {
items: items,
parent: parent,
executor: executor,
};
ias.emitter.emit('append', event);
return new Promise(event.executor);
return (new Promise(executor)).then(function (event) {
ias.emitter.emit('appended', event);
});
};

@@ -622,0 +627,0 @@

/**
* Infinite Ajax Scroll v3.0.0-beta.1
* Infinite Ajax Scroll v3.0.0-beta.2
* Turn your existing pagination into infinite scrolling pages with ease

@@ -1000,13 +1000,17 @@ *

// original executor
var executor = event.executor;
// copy original append function
var appendFn = event.appendFn;
// wrap executor with delay
event.executor = function (resolve) {
setTimeout(function () {
// turn hide function into promise
Promise.resolve(self.hide()).then(function () {
executor(resolve);
});
}, delay);
// wrap append function with delay
event.appendFn = function (items, parent, last) {
return new Promise(function (resolve) {
setTimeout(function () {
// turn hide function into promise
Promise.resolve(self.hide()).then(function () {
appendFn(items, parent, last);
resolve();
});
}, delay);
});
};

@@ -1175,2 +1179,13 @@ });

function appendFn(items, parent, last) {
var sibling = last ? last.nextSibling : null;
var insert = document.createDocumentFragment();
items.forEach(function (item) {
insert.appendChild(item);
});
parent.insertBefore(insert, sibling);
}
var scrollListener;

@@ -1328,31 +1343,21 @@ var resizeListener;

// @todo move fragment creation into executor?
var insert = document.createDocumentFragment();
var event = {
items: items,
parent: parent,
appendFn: appendFn
};
items.forEach(function (item) {
insert.appendChild(item);
});
ias.emitter.emit('append', event);
var executor = function (resolve) {
var last = ias.sentinel();
var sibling = last ? last.nextSibling : null;
window.requestAnimationFrame(function () {
parent.insertBefore(insert, sibling);
resolve({items: items, parent: parent});
ias.emitter.emit('appended', {items: items, parent: parent});
Promise.resolve(event.appendFn(event.items, event.parent, ias.sentinel())).then(function () {
resolve({items: items, parent: parent});
});
});
};
var event = {
items: items,
parent: parent,
executor: executor,
};
ias.emitter.emit('append', event);
return new Promise(event.executor);
return (new Promise(executor)).then(function (event) {
ias.emitter.emit('appended', event);
});
};

@@ -1359,0 +1364,0 @@

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.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},c=function(t,e){if("__proto__"===e){if(!n.call(t,e))return;if(r)return r(t,e).value}return t[e]},u=function t(){var e,n,i,o,r,u,d=arguments,p=arguments[0],h=1,f=arguments.length,m=!1;for("boolean"==typeof p&&(m=p,p=arguments[1]||{},h=2),(null==p||"object"!=typeof p&&"function"!=typeof p)&&(p={});f>h;++h)if(null!=(e=d[h]))for(n in e)i=c(p,n),p!==(o=c(e,n))&&(m&&o&&(l(o)||(r=s(o)))?(r?(r=!1,u=i&&s(i)?i:[]):u=i&&l(i)?i:{},a(p,{name:n,newValue:t(m,u,o)})):void 0!==o&&a(p,{name:n,newValue:o}));return p},d="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},p="Expected a function",h=NaN,f="[object Symbol]",m=/^\s+|\s+$/g,y=/^[-+]0x[0-9a-f]+$/i,g=/^0b[01]+$/i,v=/^0o[0-7]+$/i,b=parseInt,x="object"==typeof self&&self&&self.Object===Object&&self,w="object"==typeof d&&d&&d.Object===Object&&d||x||Function("return this")(),j=Object.prototype.toString,C=Math.max,T=Math.min,I=function(){return w.Date.now()};function O(t,e,n){var i,o,r,s,l,a,c=0,u=!1,d=!1,h=!0;if("function"!=typeof t)throw new TypeError(p);function f(e){var n=i,r=o;return i=o=void 0,c=e,s=t.apply(r,n)}function m(t){var n=t-a;return void 0===a||n>=e||0>n||d&&t-c>=r}function y(){var t=I();if(m(t))return g(t);l=setTimeout(y,function(t){var n=e-(t-a);return d?T(n,r-(t-c)):n}(t))}function g(t){return l=void 0,h&&i?f(t):(i=o=void 0,s)}function v(){var t=I(),n=m(t);if(i=arguments,o=this,a=t,n){if(void 0===l)return function(t){return c=t,l=setTimeout(y,e),u?f(t):s}(a);if(d)return l=setTimeout(y,e),f(a)}return void 0===l&&(l=setTimeout(y,e)),s}return e=E(e)||0,S(n)&&(u=!!n.leading,r=(d="maxWait"in n)?C(E(n.maxWait)||0,e):r,h="trailing"in n?!!n.trailing:h),v.cancel=function(){void 0!==l&&clearTimeout(l),c=0,i=a=o=l=void 0},v.flush=function(){return void 0===l?s:g(I())},v}function S(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function E(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&j.call(t)==f}(t))return h;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(m,"");var n=g.test(t);return n||v.test(t)?b(t.slice(2),n?2:8):y.test(t)?h:+t}var P=function(t,e,n){var i=!0,o=!0;if("function"!=typeof t)throw new TypeError(p);return S(n)&&(i="leading"in n?!!n.leading:i,o="trailing"in n?!!n.trailing:o),O(t,e,{leading:i,maxWait:e,trailing:o})},L={item:void 0,next:void 0,pagination:void 0,responseType:"document",bind:!0,scrollContainer:window,spinner:!1,logger:!0},H=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 A(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 N(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 _={y:0,x:0,deltaY:0,deltaX:0};function B(t){var e=A(t);return e.deltaY=e.y-(_?_.y:e.y),e.deltaX=e.x-(_?_.x:e.x),_=e,e}function D(){var t=B(this.scrollContainer);this.emitter.emit("scrolled",{scroll:t}),this.measure()}function F(){var t=B(this.scrollContainer);this.emitter.emit("resized",{scroll:t}),this.measure()}function M(){}M.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 R=M,q=document;function W(t){var n=this,i=e(n.options.next,q)[0];if(i)return n.load(i.href).then(function(t){var i=e(n.options.next,q=t.xhr.response)[0];return n.append(t.items).then(function(){return!!i})})}var X={element:void 0,hide:!1};var k=function(t,e){this.options=u({},X,function(t){return"string"==typeof t?t={element:t,hide:!0}:"boolean"==typeof t&&(t={element:void 0,hide:t}),t}(e)),this.options.hide&&(H(this.options.element,"pagination.element"),t.on("binded",this.hide.bind(this)),t.on("unbinded",this.restore.bind(this)))};k.prototype.hide=function(){var t=e(this.options.element)[0];this.originalDisplayStyle=window.getComputedStyle(t).display,t.style.display="none"},k.prototype.restore=function(){e(this.options.element)[0].style.display=this.originalDisplayStyle};var $={element:void 0,delay:600,show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var z=function(t,n){!1!==n&&(this.ias=t,this.options=u({},$,function(t){return"string"==typeof t&&(t={element:t}),t}(n)),H(this.options.element,"spinner.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,t.on("binded",this.bind.bind(this)),t.on("binded",this.hide.bind(this)))};z.prototype.bind=function(){var t,e,n=this,i=this.ias;i.on("next",function(){t=+new Date,n.show()}),i.on("last",function(){n.hide()}),i.on("append",function(i){e=Math.max(0,n.options.delay-(+new Date-t));var o=i.executor;i.executor=function(t){setTimeout(function(){Promise.resolve(n.hide()).then(function(){o(t)})},e)}})},z.prototype.show=function(){return Promise.resolve(this.showFn(this.element))},z.prototype.hide=function(){return Promise.resolve(this.hideFn(this.element))};var V={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+"]")},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+"]")}};var Y=function(t,e){if(!1!==e){var n=function(t){return!0===t&&(t=V),t}(e);Object.keys(n).forEach(function(e){t.on(e,n[e])})}};var G,U,J=function(t){this.ias=t,this.pageBreaks=[],this.currentPageIndex=t.pageIndex,this.currentScrollTop=0,t.on("binded",this.binded.bind(this)),t.on("next",this.next.bind(this)),t.on("scrolled",this.scrolled.bind(this)),t.on("resized",this.scrolled.bind(this))};J.prototype.binded=function(){this.ias.sentinel()&&this.pageBreaks.push({pageIndex:this.currentPageIndex,url:""+document.location,title:document.title,sentinel:this.ias.sentinel()})},J.prototype.next=function(t){var e,n,i=this,o=function(t){e=t.url,n=t.xhr.response.title};this.ias.once("loaded",o),this.ias.once("appended",function(){i.pageBreaks.push({pageIndex:t.pageIndex,url:e,title:n,sentinel:i.ias.sentinel()}),i.update(),i.ias.off("loaded",o)})},J.prototype.scrolled=function(t){this.update(t.scroll.y)},J.prototype.update=function(t){this.currentScrollTop=t||this.currentScrollTop;var e=function(t,e,n){for(var i=e+N(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("page",e),this.currentPageIndex=e.pageIndex)};var K=function(t,n){void 0===n&&(n={}),H(t,"container"),this.container=e(t)[0],this.options=u({},L,n),this.emitter=new R,this.scrollContainer=this.options.scrollContainer,this.options.scrollContainer!==window&&(H(this.options.scrollContainer,"options.scrollContainer"),this.scrollContainer=e(this.options.scrollContainer)[0]),this.nextHandler=W,"function"==typeof this.options.next&&(this.nextHandler=this.options.next),this.binded=!1,this.paused=!1,this.pageIndex=this.sentinel()?0:-1,this.on("hit",this.next),this.pagination=new k(this,this.options.pagination),this.spinner=new z(this,this.options.spinner),this.logger=new Y(this,this.options.logger),this.paging=new J(this),this.on("binded",this.measure),this.options.bind&&this.bind()};return K.prototype.bind=function(){this.binded||(G=P(D,200).bind(this),U=P(F,200).bind(this),this.scrollContainer.addEventListener("scroll",G),this.scrollContainer.addEventListener("resize",U),this.binded=!0,this.emitter.emit("binded"))},K.prototype.unbind=function(){this.binded&&(this.scrollContainer.removeEventListener("resize",U),this.scrollContainer.removeEventListener("scroll",G),this.binded=!1,this.emitter.emit("unbinded"))},K.prototype.next=function(){var t=this;this.pause();var e={pageIndex:this.pageIndex+1};this.emitter.emit("next",e),Promise.resolve(this.nextHandler(e.pageIndex)).then(function(n){n?(t.pageIndex=e.pageIndex,t.resume()):t.emitter.emit("last")})},K.prototype.load=function(t){var n=this;return new Promise(function(i,o){var r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE)if(200===r.status){var s=r.response;"document"===n.options.responseType&&(s=e(n.options.item,r.response)),n.emitter.emit("loaded",{items:s,url:t,xhr:r}),i({items:s,url:t,xhr:r})}else console.error("Request failed"),o(r)};var s=t+(/\?/.test(t)?"&":"?")+(new Date).getTime();r.open("GET",s,!0),r.responseType=n.options.responseType,r.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.emitter.emit("load",{url:t,xhr:r}),r.send()})},K.prototype.append=function(t,e){var n=this;e=e||n.container;var i=document.createDocumentFragment();t.forEach(function(t){i.appendChild(t)});var o={items:t,parent:e,executor:function(o){var r=n.sentinel(),s=r?r.nextSibling:null;window.requestAnimationFrame(function(){e.insertBefore(i,s),o({items:t,parent:e}),n.emitter.emit("appended",{items:t,parent:e})})}};return n.emitter.emit("append",o),new Promise(o.executor)},K.prototype.sentinel=function(){var t=e(this.options.item,this.container);return t.length?t[t.length-1]:null},K.prototype.pause=function(){this.paused=!0},K.prototype.resume=function(){this.paused=!1,this.measure()},K.prototype.measure=function(){if(!this.paused){var t,e,n,i,o,r=0,s=this.sentinel();s&&(t=s,n=A(e=this.scrollContainer),i=N(e),o=t.getBoundingClientRect(),r=n.y+o.bottom-i.top-(n.y+i.height)),r>0||this.emitter.emit("hit",{distance:r})}},K.prototype.on=function(t,e){this.emitter.on(t,e,this),"binded"===t&&this.binded&&e.bind(this)()},K.prototype.off=function(t,e){this.emitter.off(t,e,this)},K.prototype.once=function(t,e){this.emitter.once(t,e,this),"binded"===t&&this.binded&&e.bind(this)()},K});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.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},c=function(t,e){if("__proto__"===e){if(!n.call(t,e))return;if(r)return r(t,e).value}return t[e]},u=function t(){var e,n,i,o,r,u,d=arguments,p=arguments[0],h=1,f=arguments.length,m=!1;for("boolean"==typeof p&&(m=p,p=arguments[1]||{},h=2),(null==p||"object"!=typeof p&&"function"!=typeof p)&&(p={});f>h;++h)if(null!=(e=d[h]))for(n in e)i=c(p,n),p!==(o=c(e,n))&&(m&&o&&(l(o)||(r=s(o)))?(r?(r=!1,u=i&&s(i)?i:[]):u=i&&l(i)?i:{},a(p,{name:n,newValue:t(m,u,o)})):void 0!==o&&a(p,{name:n,newValue:o}));return p},d="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},p="Expected a function",h=NaN,f="[object Symbol]",m=/^\s+|\s+$/g,y=/^[-+]0x[0-9a-f]+$/i,g=/^0b[01]+$/i,v=/^0o[0-7]+$/i,b=parseInt,w="object"==typeof self&&self&&self.Object===Object&&self,x="object"==typeof d&&d&&d.Object===Object&&d||w||Function("return this")(),j=Object.prototype.toString,C=Math.max,T=Math.min,I=function(){return x.Date.now()};function O(t,e,n){var i,o,r,s,l,a,c=0,u=!1,d=!1,h=!0;if("function"!=typeof t)throw new TypeError(p);function f(e){var n=i,r=o;return i=o=void 0,c=e,s=t.apply(r,n)}function m(t){var n=t-a;return void 0===a||n>=e||0>n||d&&t-c>=r}function y(){var t=I();if(m(t))return g(t);l=setTimeout(y,function(t){var n=e-(t-a);return d?T(n,r-(t-c)):n}(t))}function g(t){return l=void 0,h&&i?f(t):(i=o=void 0,s)}function v(){var t=I(),n=m(t);if(i=arguments,o=this,a=t,n){if(void 0===l)return function(t){return c=t,l=setTimeout(y,e),u?f(t):s}(a);if(d)return l=setTimeout(y,e),f(a)}return void 0===l&&(l=setTimeout(y,e)),s}return e=E(e)||0,S(n)&&(u=!!n.leading,r=(d="maxWait"in n)?C(E(n.maxWait)||0,e):r,h="trailing"in n?!!n.trailing:h),v.cancel=function(){void 0!==l&&clearTimeout(l),c=0,i=a=o=l=void 0},v.flush=function(){return void 0===l?s:g(I())},v}function S(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function E(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&j.call(t)==f}(t))return h;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(m,"");var n=g.test(t);return n||v.test(t)?b(t.slice(2),n?2:8):y.test(t)?h:+t}var P=function(t,e,n){var i=!0,o=!0;if("function"!=typeof t)throw new TypeError(p);return S(n)&&(i="leading"in n?!!n.leading:i,o="trailing"in n?!!n.trailing:o),O(t,e,{leading:i,maxWait:e,trailing:o})},L={item:void 0,next:void 0,pagination:void 0,responseType:"document",bind:!0,scrollContainer:window,spinner:!1,logger:!0},F=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 H(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 A(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 N={y:0,x:0,deltaY:0,deltaX:0};function _(t){var e=H(t);return e.deltaY=e.y-(N?N.y:e.y),e.deltaX=e.x-(N?N.x:e.x),N=e,e}function B(){var t=_(this.scrollContainer);this.emitter.emit("scrolled",{scroll:t}),this.measure()}function D(){var t=_(this.scrollContainer);this.emitter.emit("resized",{scroll:t}),this.measure()}function M(){}M.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 R=M,q=document;function W(t){var n=this,i=e(n.options.next,q)[0];if(i)return n.load(i.href).then(function(t){var i=e(n.options.next,q=t.xhr.response)[0];return n.append(t.items).then(function(){return!!i})})}var X={element:void 0,hide:!1};var k=function(t,e){this.options=u({},X,function(t){return"string"==typeof t?t={element:t,hide:!0}:"boolean"==typeof t&&(t={element:void 0,hide:t}),t}(e)),this.options.hide&&(F(this.options.element,"pagination.element"),t.on("binded",this.hide.bind(this)),t.on("unbinded",this.restore.bind(this)))};k.prototype.hide=function(){var t=e(this.options.element)[0];this.originalDisplayStyle=window.getComputedStyle(t).display,t.style.display="none"},k.prototype.restore=function(){e(this.options.element)[0].style.display=this.originalDisplayStyle};var $={element:void 0,delay:600,show:function(t){t.style.opacity="1"},hide:function(t){t.style.opacity="0"}};var z=function(t,n){!1!==n&&(this.ias=t,this.options=u({},$,function(t){return"string"==typeof t&&(t={element:t}),t}(n)),F(this.options.element,"spinner.element"),this.element=e(this.options.element)[0],this.hideFn=this.options.hide,this.showFn=this.options.show,t.on("binded",this.bind.bind(this)),t.on("binded",this.hide.bind(this)))};z.prototype.bind=function(){var t,e,n=this,i=this.ias;i.on("next",function(){t=+new Date,n.show()}),i.on("last",function(){n.hide()}),i.on("append",function(i){e=Math.max(0,n.options.delay-(+new Date-t));var o=i.appendFn;i.appendFn=function(t,i,r){return new Promise(function(s){setTimeout(function(){Promise.resolve(n.hide()).then(function(){o(t,i,r),s()})},e)})}})},z.prototype.show=function(){return Promise.resolve(this.showFn(this.element))},z.prototype.hide=function(){return Promise.resolve(this.hideFn(this.element))};var V={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+"]")},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+"]")}};var Y=function(t,e){if(!1!==e){var n=function(t){return!0===t&&(t=V),t}(e);Object.keys(n).forEach(function(e){t.on(e,n[e])})}};var G,U,J=function(t){this.ias=t,this.pageBreaks=[],this.currentPageIndex=t.pageIndex,this.currentScrollTop=0,t.on("binded",this.binded.bind(this)),t.on("next",this.next.bind(this)),t.on("scrolled",this.scrolled.bind(this)),t.on("resized",this.scrolled.bind(this))};function K(t,e,n){var i=n?n.nextSibling:null,o=document.createDocumentFragment();t.forEach(function(t){o.appendChild(t)}),e.insertBefore(o,i)}J.prototype.binded=function(){this.ias.sentinel()&&this.pageBreaks.push({pageIndex:this.currentPageIndex,url:""+document.location,title:document.title,sentinel:this.ias.sentinel()})},J.prototype.next=function(t){var e,n,i=this,o=function(t){e=t.url,n=t.xhr.response.title};this.ias.once("loaded",o),this.ias.once("appended",function(){i.pageBreaks.push({pageIndex:t.pageIndex,url:e,title:n,sentinel:i.ias.sentinel()}),i.update(),i.ias.off("loaded",o)})},J.prototype.scrolled=function(t){this.update(t.scroll.y)},J.prototype.update=function(t){this.currentScrollTop=t||this.currentScrollTop;var e=function(t,e,n){for(var i=e+A(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("page",e),this.currentPageIndex=e.pageIndex)};var Q=function(t,n){void 0===n&&(n={}),F(t,"container"),this.container=e(t)[0],this.options=u({},L,n),this.emitter=new R,this.scrollContainer=this.options.scrollContainer,this.options.scrollContainer!==window&&(F(this.options.scrollContainer,"options.scrollContainer"),this.scrollContainer=e(this.options.scrollContainer)[0]),this.nextHandler=W,"function"==typeof this.options.next&&(this.nextHandler=this.options.next),this.binded=!1,this.paused=!1,this.pageIndex=this.sentinel()?0:-1,this.on("hit",this.next),this.pagination=new k(this,this.options.pagination),this.spinner=new z(this,this.options.spinner),this.logger=new Y(this,this.options.logger),this.paging=new J(this),this.on("binded",this.measure),this.options.bind&&this.bind()};return Q.prototype.bind=function(){this.binded||(G=P(B,200).bind(this),U=P(D,200).bind(this),this.scrollContainer.addEventListener("scroll",G),this.scrollContainer.addEventListener("resize",U),this.binded=!0,this.emitter.emit("binded"))},Q.prototype.unbind=function(){this.binded&&(this.scrollContainer.removeEventListener("resize",U),this.scrollContainer.removeEventListener("scroll",G),this.binded=!1,this.emitter.emit("unbinded"))},Q.prototype.next=function(){var t=this;this.pause();var e={pageIndex:this.pageIndex+1};this.emitter.emit("next",e),Promise.resolve(this.nextHandler(e.pageIndex)).then(function(n){n?(t.pageIndex=e.pageIndex,t.resume()):t.emitter.emit("last")})},Q.prototype.load=function(t){var n=this;return new Promise(function(i,o){var r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE)if(200===r.status){var s=r.response;"document"===n.options.responseType&&(s=e(n.options.item,r.response)),n.emitter.emit("loaded",{items:s,url:t,xhr:r}),i({items:s,url:t,xhr:r})}else console.error("Request failed"),o(r)};var s=t+(/\?/.test(t)?"&":"?")+(new Date).getTime();r.open("GET",s,!0),r.responseType=n.options.responseType,r.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.emitter.emit("load",{url:t,xhr:r}),r.send()})},Q.prototype.append=function(t,e){var n=this,i={items:t,parent:e=e||n.container,appendFn:K};n.emitter.emit("append",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("appended",t)})},Q.prototype.sentinel=function(){var t=e(this.options.item,this.container);return t.length?t[t.length-1]:null},Q.prototype.pause=function(){this.paused=!0},Q.prototype.resume=function(){this.paused=!1,this.measure()},Q.prototype.measure=function(){if(!this.paused){var t,e,n,i,o,r=0,s=this.sentinel();s&&(t=s,n=H(e=this.scrollContainer),i=A(e),o=t.getBoundingClientRect(),r=n.y+o.bottom-i.top-(n.y+i.height)),r>0||this.emitter.emit("hit",{distance:r})}},Q.prototype.on=function(t,e){this.emitter.on(t,e,this),"binded"===t&&this.binded&&e.bind(this)()},Q.prototype.off=function(t,e){this.emitter.off(t,e,this)},Q.prototype.once=function(t,e){this.emitter.once(t,e,this),"binded"===t&&this.binded&&e.bind(this)()},Q});
{
"name": "@webcreate/infinite-ajax-scroll",
"version": "3.0.0-beta.1",
"version": "3.0.0-beta.2",
"title": "Infinite Ajax Scroll",

@@ -5,0 +5,0 @@ "description": "Turn your existing pagination into infinite scrolling pages with ease",

@@ -14,2 +14,3 @@ import $ from 'tealight';

import Paging from './paging';
import {appendFn} from './append';

@@ -165,31 +166,21 @@ let scrollListener;

// @todo move fragment creation into executor?
let insert = document.createDocumentFragment();
let event = {
items,
parent,
appendFn
};
items.forEach((item) => {
insert.appendChild(item);
});
ias.emitter.emit('append', event);
let executor = (resolve) => {
let last = ias.sentinel();
let sibling = last ? last.nextSibling : null;
window.requestAnimationFrame(() => {
parent.insertBefore(insert, sibling);
resolve({items, parent});
ias.emitter.emit('appended', {items, parent});
Promise.resolve(event.appendFn(event.items, event.parent, ias.sentinel())).then(() => {
resolve({items, parent});
});
});
};
let event = {
items,
parent,
executor,
};
ias.emitter.emit('append', event);
return new Promise(event.executor);
return (new Promise(executor)).then((event) => {
ias.emitter.emit('appended', event);
});
}

@@ -196,0 +187,0 @@

@@ -66,13 +66,17 @@ import $ from 'tealight';

// original executor
let executor = event.executor;
// copy original append function
let appendFn = event.appendFn;
// wrap executor with delay
event.executor = (resolve) => {
setTimeout(() => {
// turn hide function into promise
Promise.resolve(self.hide()).then(() => {
executor(resolve);
});
}, delay);
// wrap append function with delay
event.appendFn = (items, parent, last) => {
return new Promise((resolve) => {
setTimeout(() => {
// turn hide function into promise
Promise.resolve(self.hide()).then(() => {
appendFn(items, parent, last);
resolve();
});
}, delay);
});
};

@@ -79,0 +83,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc