@@ -26,6 +26,16 @@ var PivotView = function (controller, container) {
2626 messageElement : undefined
2727 } ;
2828
29+ /**
30+ * Pagination object.
31+ * @see pushTable
32+ * @type {{on: boolean, page: number, pages: number, rows: number} }
33+ */
34+ this . pagination = null ;
35+
36+ this . PAGINATION_BLOCK_HEIGHT = 20 ;
37+ this . ANIMATION_TIMEOUT = 500 ;
38+
2939 this . controller = controller ;
3040
3141 this . SCROLLBAR_WIDTH = ( function ( ) {
@@ -116,18 +126,27 @@ PivotView.prototype.getCurrentTableData = function () {
116126PivotView . prototype . pushTable = function ( opts ) {
117127
118128 var _ = this ,
129+ pg ,
119130 tableElement = document . createElement ( "div" ) ;
120131
121132 tableElement . className = "tableContainer" ;
122133 if ( this . tablesStack . length ) tableElement . style . left = "100%" ;
123134
124135 this . tablesStack . push ( {
125136 element : tableElement ,
126- opts : opts || { }
137+ opts : opts || { } ,
138+ pagination : pg = { // defaults copied to pushTable
139+ on : false ,
140+ rows : Infinity , // rows by page including (headers + summary + rows from config)
141+ page : 0 , // current page,
142+ pages : 0 // available pages
143+ }
127144 } ) ;
128145
129146 this . elements . base . appendChild ( tableElement ) ;
130147 this . elements . tableContainer = tableElement ;
148+ console . log ( "pagination changed to " , pg ) ;
149+ this . pagination = pg ;
131150
132151 setTimeout ( function ( ) {
133152 _ . _updateTablesPosition ( ) ;
@@ -141,14 +160,38 @@ PivotView.prototype.popTable = function () {
141160
142161 this . _updateTablesPosition ( 1 ) ;
143162 var garbage = this . tablesStack . pop ( ) ;
163+ this . pagination = this . tablesStack [ this . tablesStack . length - 1 ] . pagination ;
144164
145165 setTimeout ( function ( ) {
146166 garbage . element . parentNode . removeChild ( garbage . element ) ;
147- } , 500 ) ;
167+ } , this . ANIMATION_TIMEOUT ) ;
148168 this . elements . tableContainer = this . tablesStack [ this . tablesStack . length - 1 ] . element ;
149169
150170} ;
151171
172+ /**
173+ * Data change handler.
174+ *
175+ * @param data
176+ */
177+ PivotView . prototype . dataChanged = function ( data ) {
178+
179+ var dataRows =
180+ data . rawData . length - data . info . topHeaderRowsNumber ; // - (data.info.SUMMARY_SHOWN ? 1 : 0);
181+
182+ if ( this . controller . CONFIG . pagination ) this . pagination . on = true ;
183+ this . pagination . rows = this . controller . CONFIG . pagination || Infinity ;
184+ //? this.controller.CONFIG.pagination
185+ //+ data.info.topHeaderRowsNumber + (data.info.SUMMARY_SHOWN ? 1 : 0)
186+ //: Infinity;
187+ this . pagination . page = 0 ;
188+ this . pagination . pages = Math . ceil ( dataRows / this . pagination . rows ) ;
189+ if ( this . pagination . pages < 2 ) this . pagination . on = false ;
190+
191+ this . renderRawData ( data ) ;
192+
193+ } ;
194+
152195PivotView . prototype . _columnClickHandler = function ( columnIndex ) {
153196
154197 this . controller . dataController . sortByColumn ( columnIndex ) ;
@@ -161,6 +204,13 @@ PivotView.prototype._rowClickHandler = function (rowIndex, cellData) {
161204
162205} ;
163206
207+ PivotView . prototype . _pageSwitcherHandler = function ( pageIndex ) {
208+
209+ this . pagination . page = pageIndex ;
210+ this . renderRawData ( this . controller . dataController . getData ( ) ) ;
211+
212+ } ;
213+
164214PivotView . prototype . _backClickHandler = function ( event ) {
165215
166216 if ( event ) {
@@ -383,12 +433,13 @@ PivotView.prototype.recalculateSizes = function (container) {
383433 lTableHead . removeChild ( lTableHead . lastChild ) ;
384434 }
385435
386- var headerW = leftHeader . offsetWidth ,
436+ var pagedHeight = this . pagination . on ? this . PAGINATION_BLOCK_HEIGHT : 0 ,
437+ headerW = leftHeader . offsetWidth ,
387438 headerH = topHeader . offsetHeight ,
388439 containerHeight = container . offsetHeight ,
389440 mainHeaderWidth = headerContainer . offsetWidth ,
390- hasVerticalScrollBar = tableBlock . scrollHeight > containerHeight - headerH ,
391- addExtraLeftHeaderCell = lTableHead . offsetHeight > containerHeight - headerH
441+ hasVerticalScrollBar = tableBlock . scrollHeight > containerHeight - headerH - pagedHeight ,
442+ addExtraLeftHeaderCell = lTableHead . offsetHeight > containerHeight - headerH - pagedHeight
392443 && this . SCROLLBAR_WIDTH > 0 ,
393444 cell , tr , cellWidths = [ ] , columnHeights = [ ] , i ;
394445
@@ -416,10 +467,10 @@ PivotView.prototype.recalculateSizes = function (container) {
416467
417468 topHeader . style . marginLeft = headerW + "px" ;
418469 tableBlock . style . marginLeft = headerW + "px" ;
419- leftHeader . style . height = containerHeight - headerH + "px" ;
470+ leftHeader . style . height = containerHeight - headerH - pagedHeight + "px" ;
420471 leftHeader . style . width = headerW + "px" ;
421472 if ( mainHeaderWidth > headerW ) leftHeader . style . width = mainHeaderWidth + "px" ;
422- tableBlock . style . height = containerHeight - headerH + "px" ;
473+ tableBlock . style . height = containerHeight - headerH - pagedHeight + "px" ;
423474 headerContainer . style . height = headerH + "px" ;
424475
425476 if ( addExtraLeftHeaderCell ) {
@@ -436,7 +487,7 @@ PivotView.prototype.recalculateSizes = function (container) {
436487 leftHeader . className = leftHeader . className . replace ( / \s b o r d e r e d / , "" )
437488 + " bordered" ;
438489 cell . colSpan = lTableHead . childNodes . length ;
439- cell . style . height = this . SCROLLBAR_WIDTH + "px" ;
490+ cell . style . height = ( this . SCROLLBAR_WIDTH ? this . SCROLLBAR_WIDTH + 1 : 0 ) + "px" ;
440491 }
441492
442493 for ( i in tableTr . childNodes ) {
@@ -518,7 +569,11 @@ PivotView.prototype.renderRawData = function (data) {
518569 LHTHead = document . createElement ( "thead" ) ,
519570 mainTable = document . createElement ( "table" ) ,
520571 mainTBody = document . createElement ( "tbody" ) ,
521- x , y , tr = null , th , td , primaryColumns = [ ] , primaryRows = [ ] , ratio , cellStyle ;
572+ pageSwitcher = this . pagination . on ? document . createElement ( "div" ) : null ,
573+ pageNumbers = this . pagination . on ? [ ] : null ,
574+ pageSwitcherContainer = pageSwitcher ? document . createElement ( "div" ) : null ,
575+ i , x , y , tr = null , th , td , primaryColumns = [ ] , primaryRows = [ ] , ratio , cellStyle ,
576+ tempI , tempJ ;
522577
523578 // clean previous content
524579 this . removeMessage ( ) ;
@@ -617,13 +672,15 @@ PivotView.prototype.renderRawData = function (data) {
617672 renderHeader (
618673 0 ,
619674 info . leftHeaderColumnsNumber ,
620- info . topHeaderRowsNumber ,
621- rawData . length ,
675+ tempI = info . topHeaderRowsNumber + ( this . pagination . page * this . pagination . rows || 0 ) ,
676+ tempJ = this . pagination . on
677+ ? Math . min ( tempI + this . pagination . rows , rawData . length )
678+ : rawData . length ,
622679 LHTHead
623680 ) ;
624681
625682 // render table
626- for ( y = info . topHeaderRowsNumber ; y < rawData . length ; y ++ ) {
683+ for ( y = tempI /* info.topHeaderRowsNumber*/ ; y < tempJ /* rawData.length*/ ; y ++ ) {
627684 tr = document . createElement ( "tr" ) ;
628685 for ( x = info . leftHeaderColumnsNumber ; x < rawData [ 0 ] . length ; x ++ ) {
629686
@@ -727,6 +784,39 @@ PivotView.prototype.renderRawData = function (data) {
727784 pivotBottomSection . appendChild ( tableBlock ) ;
728785 container . appendChild ( pivotTopSection ) ;
729786 container . appendChild ( pivotBottomSection ) ;
787+ if ( pageSwitcher ) {
788+ pageSwitcher . className = "lpt-pageSwitcher" ;
789+ pageNumbers = ( function getPageNumbersArray ( currentPage , pages ) { // minPage = 1
790+
791+ var step = 0 ,
792+ pagesArr = [ currentPage ] ;
793+ while ( currentPage > 1 ) {
794+ currentPage = Math . max ( 1 , currentPage - ( step || 1 ) ) ;
795+ pagesArr . unshift ( currentPage ) ;
796+ step = step * step + 1 ;
797+ }
798+ step = 0 ;
799+ currentPage = pagesArr [ pagesArr . length - 1 ] ;
800+ while ( currentPage < pages ) {
801+ currentPage = Math . min ( pages , currentPage + ( step || 1 ) ) ;
802+ pagesArr . push ( currentPage ) ;
803+ step = step * step + 1 ;
804+ }
805+ return pagesArr ;
806+
807+ } ) ( this . pagination . page + 1 , this . pagination . pages ) ;
808+ for ( i in pageNumbers ) {
809+ td = document . createElement ( "span" ) ;
810+ if ( pageNumbers [ i ] === this . pagination . page + 1 ) { td . className = "lpt-active" ; }
811+ td . textContent = pageNumbers [ i ] ;
812+ ( function ( page ) { td . addEventListener ( CLICK_EVENT , function ( ) { // add handler
813+ _ . _pageSwitcherHandler . call ( _ , page - 1 ) ;
814+ } ) } ) ( pageNumbers [ i ] ) ;
815+ pageSwitcherContainer . appendChild ( td ) ;
816+ }
817+ pageSwitcher . appendChild ( pageSwitcherContainer ) ;
818+ container . appendChild ( pageSwitcher ) ;
819+ }
730820 container [ "_primaryColumns" ] = primaryColumns ;
731821 container [ "_primaryRows" ] = primaryRows ;
732822
0 commit comments