| 1 | |
| 2 | _initInfinite: function () { |
| 3 | var el = this.options.infiniteElements; |
| 4 | |
| 5 | this.infiniteElements = typeof el == 'string' ? document.querySelectorAll(el) : el; |
| 6 | this.infiniteLength = this.infiniteElements.length; |
| 7 | this.infiniteMaster = this.infiniteElements[0]; |
| 8 | this.infiniteElementHeight = this.infiniteMaster.offsetHeight; |
| 9 | this.infiniteHeight = this.infiniteLength * this.infiniteElementHeight; |
| 10 | |
| 11 | this.options.cacheSize = this.options.cacheSize || 1000; |
| 12 | this.infiniteCacheBuffer = Math.round(this.options.cacheSize / 4); |
| 13 | |
| 14 | //this.infiniteCache = {}; |
| 15 | this.options.dataset.call(this, 0, this.options.cacheSize); |
| 16 | |
| 17 | this.on('refresh', function () { |
| 18 | var elementsPerPage = Math.ceil(this.wrapperHeight / this.infiniteElementHeight); |
| 19 | this.infiniteUpperBufferSize = Math.floor((this.infiniteLength - elementsPerPage) / 2); |
| 20 | this.reorderInfinite(); |
| 21 | }); |
| 22 | |
| 23 | this.on('scroll', this.reorderInfinite); |
| 24 | }, |
| 25 | |
| 26 | // TO-DO: clean up the mess |
| 27 | reorderInfinite: function () { |
| 28 | var center = -this.y + this.wrapperHeight / 2; |
| 29 | |
| 30 | var minorPhase = Math.max(Math.floor(-this.y / this.infiniteElementHeight) - this.infiniteUpperBufferSize, 0), |
| 31 | majorPhase = Math.floor(minorPhase / this.infiniteLength), |
| 32 | phase = minorPhase - majorPhase * this.infiniteLength; |
| 33 | |
| 34 | var top = 0; |
| 35 | var i = 0; |
| 36 | var update = []; |
| 37 | |
| 38 | //var cachePhase = Math.floor((minorPhase + this.infiniteLength / 2) / this.infiniteCacheBuffer); |
| 39 | var cachePhase = Math.floor(minorPhase / this.infiniteCacheBuffer); |
| 40 | |
| 41 | while ( i < this.infiniteLength ) { |
| 42 | top = i * this.infiniteElementHeight + majorPhase * this.infiniteHeight; |
| 43 | |
| 44 | if ( phase > i ) { |
| 45 | top += this.infiniteElementHeight * this.infiniteLength; |
| 46 | } |
| 47 | |
| 48 | if ( this.infiniteElements[i]._top !== top ) { |
| 49 | this.infiniteElements[i]._phase = top / this.infiniteElementHeight; |
| 50 | |
| 51 | if ( this.infiniteElements[i]._phase < this.options.infiniteLimit ) { |
| 52 | this.infiniteElements[i]._top = top; |
| 53 | if ( this.options.infiniteUseTransform ) { |
| 54 | this.infiniteElements[i].style[utils.style.transform] = 'translate(0, ' + top + 'px)' + this.translateZ; |
| 55 | } else { |
| 56 | this.infiniteElements[i].style.top = top + 'px'; |
| 57 | } |
| 58 | update.push(this.infiniteElements[i]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | i++; |
| 63 | } |
| 64 | |
| 65 | if ( this.cachePhase != cachePhase && (cachePhase === 0 || minorPhase - this.infiniteCacheBuffer > 0) ) { |
| 66 | this.options.dataset.call(this, Math.max(cachePhase * this.infiniteCacheBuffer - this.infiniteCacheBuffer, 0), this.options.cacheSize); |
| 67 | } |
| 68 | |
| 69 | this.cachePhase = cachePhase; |
| 70 | |
| 71 | this.updateContent(update); |
| 72 | }, |
| 73 | |
| 74 | updateContent: function (els) { |
| 75 | if ( this.infiniteCache === undefined ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | for ( var i = 0, l = els.length; i < l; i++ ) { |
| 80 | this.options.dataFiller.call(this, els[i], this.infiniteCache[els[i]._phase]); |
| 81 | } |
| 82 | }, |
| 83 | |
| 84 | updateCache: function (start, data) { |
| 85 | var firstRun = this.infiniteCache === undefined; |
| 86 | |
| 87 | this.infiniteCache = {}; |
| 88 | |
| 89 | for ( var i = 0, l = data.length; i < l; i++ ) { |
| 90 | this.infiniteCache[start++] = data[i]; |
| 91 | } |
| 92 | |
| 93 | if ( firstRun ) { |
| 94 | this.updateContent(this.infiniteElements); |
| 95 | } |
| 96 | |
| 97 | }, |
| 98 | |