| 1 |
- define(function(e,i,t){"use strict";var n=e("../util"),s=e("../base"),r=n.prefixStyle("transform"),l=n.prefixStyle("transition"),o=function(e){o.superclass.constructor.call(this,e),this.userConfig=n.mix({transition:"all 0.5s ease"},e)};if(n.extend(o,s,{pluginId:"infinite",visibleElements:{},sections:{},pluginInitializer:function(e){var i=this;return i.xscroll=e,i.isY=!("y"!=e.userConfig.zoomType),i._={_top:i.isY?"_top":"_left",_height:i.isY?"_height":"_width",top:i.isY?"top":"left",height:i.isY?"height":"width",width:i.isY?"width":"height",y:i.isY?"y":"x",translate:i.isY?"translateY":"translateX",containerHeight:i.isY?"containerHeight":"containerWidth",scrollTop:i.isY?"scrollTop":"scrollLeft"},i._initInfinite(),e.on("afterrender",function(){i.render(),i._bindEvt()}),i},pluginDestructor:function(){for(var e=this,i=e._,t=0;t<e.infiniteLength;t++)e.infiniteElements[t].style[i.top]="auto",e.infiniteElements[t].style[r]="none",e.infiniteElements[t].style.visibility="hidden";return e.xscroll&&e.xscroll.off("scroll",e._updateByScroll,e),e.xscroll&&e.xscroll.off("tap panstart pan panend",e._cellEventsHandler,e),e},_initInfinite:function(){var e=this,i=e.xscroll,t=e._;return e.sections={},e.infiniteElements=i.renderTo.querySelectorAll(e.userConfig.infiniteElements),e.infiniteLength=e.infiniteElements.length,e.infiniteElementsCache=function(){for(var i=[],s=0;s<e.infiniteLength;s++)i.push({}),e.infiniteElements[s].style.position="absolute",e.infiniteElements[s].style[t.top]=0,e.infiniteElements[s].style.visibility="hidden",e.infiniteElements[s].style.display="block",n.addClass(e.infiniteElements[s],"_xs_infinite_elements_");return i}(),e.elementsPos={},e},_renderUnRecycledEl:function(){var e=this,i=e._,t=e.userConfig.gpuAcceleration?" translateZ(0) ":"";for(var s in e.__serializedData){var l=e.__serializedData[s];if(e.__serializedData[s].recycled===!1){var o=l.id&&document.getElementById(l.id.replace("#",""))||document.createElement("div"),a=n.guid("xs-row-");o.id=l.id||a,l.id=o.id,e.xscroll.content.appendChild(o);for(var d in l.style)d!=i.height&&"display"!=d&&"position"!=d&&(o.style[d]=l.style[d]);o.style[i.top]=0,o.style.position="absolute",o.style.display="block",o.style[i.height]=l[i._height]+"px",o.style[r]=i.translate+"("+l[i._top]+"px) "+t,n.addClass(o,l.className),e.userConfig.renderHook.call(e,o,l)}}},render:function(){var e=this,i=e._,t=e.xscroll,n=e.isY?t.getScrollTop():t.getScrollLeft();e.visibleElements=e.getVisibleElements(n),e.__serializedData=e._computeDomPositions(),t.sticky&&t.sticky.render(!0),t.fixed&&t.fixed.render();var s=t[i.height],r=e._containerSize;return r<s&&(r=s),t[i.containerHeight]=r,t.container.style[i.height]=r+"px",t.content.style[i.height]=r+"px",e._renderUnRecycledEl(),e._updateByScroll(),e._updateByRender(n),e.xscroll.boundryCheck(),e},_getChangedRows:function(e){var i=this,t={};for(var n in i.elementsPos)e.hasOwnProperty(n)||(t[n]="delete");for(var n in e)e[n].recycled&&!i.elementsPos.hasOwnProperty(n)&&(t[n]="add");return i.elementsPos=e,t},_updateByScroll:function(e){var i=this,t=i.xscroll,n=i._,s=e&&e[n.scrollTop],r=void 0===s?i.isY?t.getScrollTop():t.getScrollLeft():s,l=i.getVisibleElements(r),o=i.changedRows=i._getChangedRows(l);try{for(var a in o)if("delete"==o[a]&&i._pushEl(a),"add"==o[a]){var d=i._popEl(l[a][i.guid]),c=d.index,f=d.el;f&&(i.infiniteElementsCache[c].guid=l[a].guid,i.__serializedData[l[a].guid].__infiniteIndex=c,i._renderData(f,l[a]),i._renderStyle(f,l[a]))}}catch(e){console.warn("Not enough infiniteElements setted!")}return i},_updateByRender:function(e){var i,t,n=this,s=n._,r=n.xscroll,e=void 0===e?n.isY?r.getScrollTop():r.getScrollLeft():e,l=n.visibleElements,o=n.getVisibleElements(e);for(var a in o){t=o[a];for(var d in l)if(i=l[d],i.guid===t.guid)t.style==i.style&&t[s._top]==i[s._top]&&t[s._height]==i[s._height]||n._renderStyle(n.infiniteElements[t.__infiniteIndex],t,!0),JSON.stringify(t.data)!=JSON.stringify(i.data)&&n._renderData(n.infiniteElements[t.__infiniteIndex],t);else if(n.__serializedData[t.guid].recycled&&void 0===n.__serializedData[t.guid].__infiniteIndex){var c=n._popEl();n.__serializedData[t.guid].__infiniteIndex=c.index,n._renderData(c.el,t),n._renderStyle(c.el,t)}}n.visibleElements=o},_computeDomPositions:function(){var e,i=this,t=i._,s=0,r=0,l=i.sections,o=[],a={};for(var d in l)for(var c=0,f=l[d].length;c<f;c++)e=l[d][c],e.sectionId=d,e.index=c,o.push(e);i.userConfig.maxSpeed=3;for(var d=0,u=o.length;d<u;d++){var h=o[d];r=h.style&&h.style[t.height]>=0&&"fixed"!=h.style.position?h.style[t.height]:0,h.guid=h.guid||n.guid(),h[t._top]=s,h[t._height]=r,h.recycled=h.recycled!==!1,s+=r,a[h.guid]=h}return i._containerSize=s,a},getVisibleElements:function(e){var i,t=this,n=t.xscroll,s=t._,e=void 0===e?t.isY?n.getScrollTop():n.getScrollLeft():e,r=t.userConfig.threshold>=0?t.userConfig.threshold:n[s.height]/3,l={},o=t.__serializedData;for(var a in o)i=o[a],i[s._top]>=e-r&&i[s._top]<=e+n[s.height]+r&&(l[i.guid]=i);return JSON.parse(JSON.stringify(l))},_popEl:function(){for(var e=this,i=0;i<e.infiniteLength;i++)if(!e.infiniteElementsCache[i]._visible)return e.infiniteElementsCache[i]._visible=!0,{index:i,el:e.infiniteElements[i]}},_pushEl:function(e){for(var i=this,t=0;t<i.infiniteLength;t++)i.infiniteElementsCache[t].guid==e&&(i.infiniteElementsCache[t]._visible=!1,i.infiniteElements[t].style.visibility="hidden",i.infiniteElementsCache[t].guid=null)},_renderData:function(e,i){var t=this;e&&i&&"fixed"!=i.style.position&&t.userConfig.renderHook.call(t,e,i)},_renderStyle:function(e,i,t){var n=this,s=n._;if(e){var o=n.xscroll.userConfig.gpuAcceleration?" translateZ(0) ":"";for(var a in i.style)a!=s.height&&"display"!=a&&"position"!=a&&(e.style[a]=i.style[a]);e.setAttribute("xs-index",i.index),e.setAttribute("xs-sectionid",i.sectionId),e.setAttribute("xs-guid",i.guid),e.style.visibility="visible",e.style[s.height]=i[s._height]+"px",e.style[r]=s.translate+"("+i[s._top]+"px) "+o,e.style[l]=t?n.userConfig.transition:"none"}},getCell:function(e){var i=this,t=n.findParentEl(e.target,"._xs_infinite_elements_",i.xscroll.renderTo);t||(t=n.findParentEl(e.target,".xs-sticky-handler",i.xscroll.renderTo));var s=t&&t.getAttribute("xs-guid");if(void 0!==s)return{data:i.__serializedData[s],el:t}},_bindEvt:function(){var e=this;if(!e._isEvtBinded)return e._isEvtBinded=!0,e.xscroll.renderTo.addEventListener("webkitTransitionEnd",function(e){e.target.className.match(/xs-row/)&&(e.target.style.webkitTransition="")}),e.xscroll.on("scroll",e._updateByScroll,e),e.xscroll.on("tap panstart pan panend",e._cellEventsHandler,e),e},_cellEventsHandler:function(e){var i=this,t=i.getCell(e);e.cell=t.data,e.cellEl=t.el,e.cell&&i[e.type].call(i,e)},tap:function(e){return this.trigger("tap",e),this},panstart:function(e){return this.trigger("panstart",e),this},pan:function(e){return this.trigger("pan",e),this},panend:function(e){return this.trigger("panend",e),this},insertBefore:function(e,i,t){var n=this;return void 0===e||void 0===i||void 0===t?n:(n.sections[e]||(n.sections[e]=[]),n.sections[e].splice(i,0,t),n)},insertAfter:function(e,i,t){var n=this;return void 0===e||void 0===i||void 0===t?n:(n.sections[e]||(n.sections[e]=[]),n.sections[e].splice(Number(i)+1,0,t),n)},append:function(e,i){var t=this;return t.sections[e]||(t.sections[e]=[]),t.sections[e]=t.sections[e].concat(i),t},remove:function(e,i,t){var n=this,t=t||1;return void 0!==e&&n.sections[e]?void 0===i?(n.sections[e]=null,n):n.sections[e]&&n.sections[e][i]?(n.sections[e].splice(i,t),n):n:n},replace:function(e,i,t){var n=this;return void 0!==e&&n.sections[e]?(n.sections[e][i]=t,n):n},get:function(e,i){if(void 0!==e)return void 0===i?this.sections[e]:this.sections[e][i]}}),"object"==typeof t&&t.exports)t.exports=o;else if(window.XScroll&&window.XScroll.Plugins)return XScroll.Plugins.Infinite=o});
|