UNPKG

react-infinite

Version:

A browser-ready efficient scrolling container based on UITableView

1 lines 10.8 kB
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Infinite=t()}}(function(){return function t(e,i,r){function n(s,l){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!l&&a)return a(s,!0);if(o)return o(s,!0);var p=new Error("Cannot find module '"+s+"'");throw p.code="MODULE_NOT_FOUND",p}var c=i[s]={exports:{}};e[s][0].call(c.exports,function(t){var i=e[s][1][t];return n(i?i:t)},c,c.exports,t,e,i,r)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s<r.length;s++)n(r[s]);return n}({1:[function(t,e){(function(i){var r=i.React||t("react"),n=t("lodash.isarray"),o=t("lodash.isfinite"),s=t("./computers/constant_infinite_computer.js"),l=t("./computers/array_infinite_computer.js"),a=r.createClass({displayName:"Infinite",propTypes:{handleScroll:r.PropTypes.func,preloadBatchSize:r.PropTypes.number,preloadAdditionalHeight:r.PropTypes.number,elementHeight:r.PropTypes.oneOfType([r.PropTypes.number,r.PropTypes.arrayOf(r.PropTypes.number)]).isRequired,containerHeight:r.PropTypes.number.isRequired,infiniteLoadBeginBottomOffset:r.PropTypes.number,onInfiniteLoad:r.PropTypes.func,loadingSpinnerDelegate:r.PropTypes.node,isInfiniteLoading:r.PropTypes.bool,timeScrollStateLastsForAfterUserScrolls:r.PropTypes.number,className:r.PropTypes.string},getDefaultProps:function(){return{handleScroll:function(){},loadingSpinnerDelegate:r.createElement("div",null),onInfiniteLoad:function(){},isInfiniteLoading:!1,timeScrollStateLastsForAfterUserScrolls:150}},getInitialState:function(){var t=this.createInfiniteComputer(this.props.elementHeight,this.props.children),e=this.props.preloadBatchSize?this.props.preloadBatchSize:this.props.containerHeight/2,i=this.props.preloadAdditionalHeight?this.props.preloadAdditionalHeight:this.props.containerHeight;return{infiniteComputer:t,numberOfChildren:this.props.children.length,scrollableHeight:void 0,displayIndexStart:0,displayIndexEnd:t.getDisplayIndexEnd(e+i),isInfiniteLoading:!1,currentScrollTop:void 0,previousScrollTop:void 0,preloadBatchSize:e,preloadAdditionalHeight:i,scrollTimeout:void 0,isScrolling:!1}},createInfiniteComputer:function(t,e){var i,a=r.Children.count(e);if(o(t))i=new s(t,a);else{if(!n(t))throw new Error("You must provide either a number or an array of numbers as the elementHeight prop.");i=new l(t,a)}return i},componentWillReceiveProps:function(t){var e=this,i={};i.infiniteComputer=this.createInfiniteComputer(t.elementHeight,t.children),void 0!==t.isInfiniteLoading&&(i.isInfiniteLoading=t.isInfiniteLoading);var r=t.preloadBatchSize;i.preloadBatchSize=r?r:t.containerHeight/2;var n=t.preloadAdditionalHeight;i.preloadAdditionalHeight=n?n:t.containerHeight,this.setState(i,function(){e.setStateFromScrollTop(e.getScrollTop())})},componentDidUpdate:function(t){r.Children.count(this.props.children)!==t.children.length&&this.setStateFromScrollTop(this.getScrollTop())},componentWillMount:function(){if(n(this.props.elementHeight)&&this.props.children.length!==this.props.elementHeight.length)throw new Error("There must be as many values provided in the elementHeight prop as there are children.")},componentDidMount:function(){this.setState({scrollableHeight:this.refs.scrollable.getDOMNode().clientHeight,currentScrollTop:this.getScrollTop(),previousScrollTop:this.getScrollTop()})},getScrollTop:function(){return this.refs.scrollable.getDOMNode().scrollTop},isScrollingDown:function(){return this.state.previousScrollTop<this.state.currentScrollTop},setStateFromScrollTop:function(t){var e=Math.floor(t/this.state.preloadBatchSize),i=this.state.preloadBatchSize*e,r=i+this.state.preloadBatchSize,n=Math.max(0,i-this.state.preloadAdditionalHeight),o=Math.min(this.state.infiniteComputer.getTotalScrollableHeight(),r+this.state.preloadAdditionalHeight);this.setState({displayIndexStart:this.state.infiniteComputer.getDisplayIndexStart(n),displayIndexEnd:this.state.infiniteComputer.getDisplayIndexEnd(o),currentScrollTop:t,previousScrollTop:this.state.currentScrollTop})},infiniteHandleScroll:function(t){this.props.handleScroll(this.refs.scrollable.getDOMNode()),this.handleScroll(t.target.scrollTop)},manageScrollTimeouts:function(){this.state.scrollTimeout&&clearTimeout(this.state.scrollTimeout);var t=this,e=setTimeout(function(){t.setState({isScrolling:!1,scrollTimeout:void 0})},this.props.timeScrollStateLastsForAfterUserScrolls);this.setState({isScrolling:!0,scrollTimeout:e})},handleScroll:function(t){this.manageScrollTimeouts(),this.setStateFromScrollTop(t);var e=t>this.state.infiniteComputer.getTotalScrollableHeight()-this.props.containerHeight-this.props.infiniteLoadBeginBottomOffset;e&&!this.state.isInfiniteLoading&&(this.setState({isInfiniteLoading:!0}),this.props.onInfiniteLoad())},buildScrollableStyle:function(){return{height:this.props.containerHeight,overflowX:"hidden",overflowY:"scroll"}},buildHeightStyle:function(t){return{width:"100%",height:Math.ceil(t)+"px"}},render:function(){var t=this.props.children.slice(this.state.displayIndexStart,this.state.displayIndexEnd),e={};this.state.isScrolling&&(e.pointerEvents="none");var i=this.state.infiniteComputer.getTopSpacerHeight(this.state.displayIndexStart),n=this.state.infiniteComputer.getBottomSpacerHeight(this.state.displayIndexEnd);return r.createElement("div",{className:this.props.className?this.props.className:"",ref:"scrollable",style:this.buildScrollableStyle(),onScroll:this.infiniteHandleScroll},r.createElement("div",{ref:"smoothScrollingWrapper",style:e},r.createElement("div",{ref:"topSpacer",style:this.buildHeightStyle(i)}),t,r.createElement("div",{ref:"bottomSpacer",style:this.buildHeightStyle(n)}),r.createElement("div",{ref:"loadingSpinner"},this.state.isInfiniteLoading?this.props.loadingSpinnerDelegate:null)))}});e.exports=a,i.Infinite=a}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./computers/array_infinite_computer.js":5,"./computers/constant_infinite_computer.js":6,"lodash.isarray":2,"lodash.isfinite":3,react:void 0}],2:[function(t,e){function i(t){return"string"==typeof t?t:null==t?"":t+""}function r(t){return t&&"object"==typeof t||!1}function n(t){return"number"==typeof t&&t>-1&&t%1==0&&m>=t}function o(t){return null==t?!1:d.call(t)==a?g.test(f.call(t)):r(t)&&p.test(t)||!1}function s(t){return t=i(t),t&&u.test(t)?t.replace(c,"\\$&"):t}var l="[object Array]",a="[object Function]",p=/^\[object .+?Constructor\]$/,c=/[.*+?^${}()|[\]\/\\]/g,u=RegExp(c.source),h=Object.prototype,f=Function.prototype.toString,d=h.toString,g=RegExp("^"+s(d).replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),S=o(S=Array.isArray)&&S,m=Math.pow(2,53)-1,y=S||function(t){return r(t)&&n(t.length)&&d.call(t)==l||!1};e.exports=y},{}],3:[function(t,e){(function(i){var r=t("lodash.isnative"),n=i.isFinite,o=r(o=Number.isFinite)&&o,s=o||function(t){return"number"==typeof t&&n(t)};e.exports=s}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"lodash.isnative":4}],4:[function(t,e){function i(t){return"string"==typeof t?t:null==t?"":t+""}function r(t){return t&&"object"==typeof t||!1}function n(t){return null==t?!1:h.call(t)==s?f.test(u.call(t)):r(t)&&l.test(t)||!1}function o(t){return t=i(t),t&&p.test(t)?t.replace(a,"\\$&"):t}var s="[object Function]",l=/^\[object .+?Constructor\]$/,a=/[.*+?^${}()|[\]\/\\]/g,p=RegExp(a.source),c=Object.prototype,u=Function.prototype.toString,h=c.toString,f=RegExp("^"+o(h).replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");e.exports=n},{}],5:[function(t,e){function i(t,e){"use strict";r.call(this,t,e),this.prefixHeightData=this.heightData.reduce(function(t,e){return 0===t.length?[e]:(t.push(t[t.length-1]+e),t)},[])}var r=t("./infinite_computer.js"),n=t("../utils/binary_index_search.js");for(var o in r)r.hasOwnProperty(o)&&(i[o]=r[o]);var s=null===r?null:r.prototype;i.prototype=Object.create(s),i.prototype.constructor=i,i.__superConstructor__=r,i.prototype.getTotalScrollableHeight=function(){"use strict";var t=this.prefixHeightData.length;return 0===t?0:this.prefixHeightData[t-1]},i.prototype.getDisplayIndexStart=function(t){"use strict";return n.binaryIndexSearch(this.prefixHeightData,t,n.opts.CLOSEST_HIGHER)},i.prototype.getDisplayIndexEnd=function(t){"use strict";return n.binaryIndexSearch(this.prefixHeightData,t,n.opts.CLOSEST_HIGHER)+1},i.prototype.getTopSpacerHeight=function(t){"use strict";var e=t-1;return 0>e?0:this.prefixHeightData[e]},i.prototype.getBottomSpacerHeight=function(t){"use strict";var e=t-1;return 0===t?this.getTotalScrollableHeight():t>=this.prefixHeightData.length?0:this.getTotalScrollableHeight()-this.prefixHeightData[e]},e.exports=i},{"../utils/binary_index_search.js":8,"./infinite_computer.js":7}],6:[function(t,e){function i(){"use strict";null!==r&&r.apply(this,arguments)}var r=t("./infinite_computer.js");for(var n in r)r.hasOwnProperty(n)&&(i[n]=r[n]);var o=null===r?null:r.prototype;i.prototype=Object.create(o),i.prototype.constructor=i,i.__superConstructor__=r,i.prototype.getTotalScrollableHeight=function(){"use strict";return this.heightData*this.numberOfChildren},i.prototype.getDisplayIndexStart=function(t){"use strict";return Math.floor(t/this.heightData)},i.prototype.getDisplayIndexEnd=function(t){"use strict";return Math.ceil(t/this.heightData)},i.prototype.getTopSpacerHeight=function(t){"use strict";return t*this.heightData},i.prototype.getBottomSpacerHeight=function(t){"use strict";return Math.max(0,(this.numberOfChildren-t)*this.heightData)},e.exports=i},{"./infinite_computer.js":7}],7:[function(t,e){function i(t,e){"use strict";this.heightData=t,this.numberOfChildren=e}i.prototype.getTotalScrollableHeight=function(){"use strict";throw new Error("getTotalScrollableHeight not implemented.")},i.prototype.getDisplayIndexStart=function(){"use strict";throw new Error("getDisplayIndexStart not implemented.")},i.prototype.getDisplayIndexEnd=function(){"use strict";throw new Error("getDisplayIndexEnd not implemented.")},i.prototype.getTopSpacerHeight=function(){"use strict";throw new Error("getTopSpacerHeight not implemented.")},i.prototype.getBottomSpacerHeight=function(){"use strict";throw new Error("getBottomSpacerHeight not implemented.")},e.exports=i},{}],8:[function(t,e){var i={CLOSEST_LOWER:1,CLOSEST_HIGHER:2},r=function(t,e,r){for(var n,o,s,l=t.length-1,a=0;l>=a;){if(o=a+Math.floor((l-a)/2),s=t[o],s===e)return o;e>s?a=o+1:s>e&&(l=o-1)}return r===i.CLOSEST_LOWER&&a>0?n=a-1:r===i.CLOSEST_HIGHER&&l<t.length-1&&(n=l+1),n};e.exports={binaryIndexSearch:r,opts:i}},{}]},{},[1])(1)});