|
| 1 | +import { HorizontalLayoutMatrixBrain } from '../VirtualBrain/HorizontalLayoutMatrixBrain'; |
| 2 | +import { |
| 3 | + columnOffsetAtIndex, |
| 4 | + columnOffsetAtIndexWhileReordering, |
| 5 | + currentTransformY, |
| 6 | + ReactHeadlessTableRenderer, |
| 7 | + TableRenderCellFn, |
| 8 | +} from './ReactHeadlessTableRenderer'; |
| 9 | + |
| 10 | +export class HorizontalLayoutTableRenderer extends ReactHeadlessTableRenderer { |
| 11 | + protected brain: HorizontalLayoutMatrixBrain; |
| 12 | + constructor(brain: HorizontalLayoutMatrixBrain) { |
| 13 | + super(brain); |
| 14 | + this.brain = brain; |
| 15 | + } |
| 16 | + |
| 17 | + protected old_renderCellAtElement( |
| 18 | + rowIndex: number, |
| 19 | + colIndex: number, |
| 20 | + elementIndex: number, |
| 21 | + renderCell: TableRenderCellFn, |
| 22 | + ) { |
| 23 | + if (this.destroyed) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const covered = this.isCellCovered(rowIndex, colIndex); |
| 28 | + |
| 29 | + const height = this.brain.getRowHeight(rowIndex); |
| 30 | + const width = this.brain.getColWidth(colIndex); |
| 31 | + |
| 32 | + const rowspan = this.brain.getRowspan(rowIndex, colIndex); |
| 33 | + const colspan = this.brain.getColspan(rowIndex, colIndex); |
| 34 | + |
| 35 | + const heightWithRowspan = |
| 36 | + rowspan === 1 |
| 37 | + ? height |
| 38 | + : this.brain.getRowHeightWithSpan(rowIndex, colIndex, rowspan); |
| 39 | + |
| 40 | + const widthWithColspan = |
| 41 | + colspan === 1 |
| 42 | + ? width |
| 43 | + : this.brain.getColWidthWithSpan(rowIndex, colIndex, colspan); |
| 44 | + |
| 45 | + const { row: rowFixed, col: colFixed } = this.isCellFixed( |
| 46 | + rowIndex, |
| 47 | + colIndex, |
| 48 | + ); |
| 49 | + |
| 50 | + const hidden = !!covered; |
| 51 | + |
| 52 | + const renderedNode = renderCell({ |
| 53 | + rowIndex, |
| 54 | + colIndex: colIndex % this.brain.initialCols, |
| 55 | + height, |
| 56 | + width, |
| 57 | + rowspan, |
| 58 | + colspan, |
| 59 | + rowFixed, |
| 60 | + colFixed, |
| 61 | + hidden, |
| 62 | + heightWithRowspan, |
| 63 | + widthWithColspan, |
| 64 | + onMouseEnter: this.onMouseEnter.bind(null, rowIndex), |
| 65 | + onMouseLeave: this.onMouseLeave.bind(null, rowIndex), |
| 66 | + domRef: this.itemDOMRefs[elementIndex], |
| 67 | + }); |
| 68 | + |
| 69 | + const itemUpdater = this.updaters[elementIndex]; |
| 70 | + |
| 71 | + if (!itemUpdater) { |
| 72 | + this.error( |
| 73 | + `Cannot find item updater for item ${rowIndex},${colIndex} at this time... sorry.`, |
| 74 | + ); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // console.log('render row', rowIndex); |
| 79 | + |
| 80 | + this.mappedCells.renderCellAtElement( |
| 81 | + rowIndex, |
| 82 | + colIndex, |
| 83 | + elementIndex, |
| 84 | + renderedNode, |
| 85 | + ); |
| 86 | + |
| 87 | + if (__DEV__) { |
| 88 | + this.debug( |
| 89 | + `Render cell ${rowIndex},${colIndex} at element ${elementIndex}`, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // console.log('update', rowIndex, colIndex, renderedNode); |
| 94 | + itemUpdater(renderedNode); |
| 95 | + |
| 96 | + this.updateElementPosition(elementIndex, { hidden, rowspan, colspan }); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + protected getCellRealCoordinates(rowIndex: number, colIndex: number) { |
| 101 | + const coords = this.brain.getHorizontalLayoutPositionFromMatrixCoordinates({ |
| 102 | + rowIndex, |
| 103 | + colIndex, |
| 104 | + }); |
| 105 | + // console.log([rowIndex, colIndex], ' -> ', [ |
| 106 | + // coords.rowIndex, |
| 107 | + // coords.colIndex, |
| 108 | + // ]); |
| 109 | + return coords; |
| 110 | + } |
| 111 | + |
| 112 | + setTransform = ( |
| 113 | + element: HTMLElement, |
| 114 | + rowIndex: number, |
| 115 | + colIndex: number, |
| 116 | + |
| 117 | + options: { |
| 118 | + x: number; |
| 119 | + y: number; |
| 120 | + scrollLeft?: boolean; |
| 121 | + scrollTop?: boolean; |
| 122 | + }, |
| 123 | + zIndex: number | 'auto' | undefined | null, |
| 124 | + ) => { |
| 125 | + const horizontalLayoutCoords = this.getCellRealCoordinates( |
| 126 | + rowIndex, |
| 127 | + colIndex, |
| 128 | + ); |
| 129 | + |
| 130 | + const { y } = options; |
| 131 | + const pageIndex = Math.floor( |
| 132 | + horizontalLayoutCoords.rowIndex / this.brain.rowsPerPage, |
| 133 | + ); |
| 134 | + const pageOffset = pageIndex ? pageIndex * this.brain.pageWidth : 0; |
| 135 | + |
| 136 | + const columnOffsetX = `${columnOffsetAtIndex}-${horizontalLayoutCoords.colIndex}`; |
| 137 | + const columnOffsetXWhileReordering = `${columnOffsetAtIndexWhileReordering}-${horizontalLayoutCoords.colIndex}`; |
| 138 | + |
| 139 | + const currentTransformYValue = `${y}px`; |
| 140 | + |
| 141 | + //@ts-ignore |
| 142 | + if (element.__currentTransformY !== currentTransformYValue) { |
| 143 | + //@ts-ignore |
| 144 | + element.__currentTransformY = currentTransformYValue; |
| 145 | + element.style.setProperty(currentTransformY, currentTransformYValue); |
| 146 | + } |
| 147 | + |
| 148 | + const xOffset = `calc(var(${columnOffsetX}) + ${pageOffset}px)`; |
| 149 | + const transformX = `var(${columnOffsetXWhileReordering}, ${xOffset})`; |
| 150 | + const transformY = `var(${currentTransformY})`; |
| 151 | + |
| 152 | + const transformValue = `translate3d(${transformX}, ${transformY}, 0px)`; |
| 153 | + |
| 154 | + //@ts-ignore |
| 155 | + if (element.__transformValue !== transformValue) { |
| 156 | + //@ts-ignore |
| 157 | + element.__transformValue = transformValue; |
| 158 | + element.style.transform = transformValue; |
| 159 | + } |
| 160 | + }; |
| 161 | + setTransform_old = ( |
| 162 | + element: HTMLElement, |
| 163 | + rowIndex: number, |
| 164 | + colIndex: number, |
| 165 | + |
| 166 | + options: { |
| 167 | + x: number; |
| 168 | + y: number; |
| 169 | + scrollLeft?: boolean; |
| 170 | + scrollTop?: boolean; |
| 171 | + }, |
| 172 | + zIndex: number | 'auto' | undefined | null, |
| 173 | + ) => { |
| 174 | + const horizontalLayoutCoords = this.getCellRealCoordinates( |
| 175 | + rowIndex, |
| 176 | + colIndex, |
| 177 | + ); |
| 178 | + console.log(horizontalLayoutCoords); |
| 179 | + colIndex = colIndex % this.brain.initialCols; |
| 180 | + const { y } = options; |
| 181 | + const pageIndex = Math.floor(rowIndex / this.brain.rowsPerPage); |
| 182 | + const pageOffset = pageIndex ? pageIndex * this.brain.pageWidth : 0; |
| 183 | + |
| 184 | + const columnOffsetX = `${columnOffsetAtIndex}-${colIndex}`; |
| 185 | + const columnOffsetXWhileReordering = `${columnOffsetAtIndexWhileReordering}-${colIndex}`; |
| 186 | + |
| 187 | + const currentTransformYValue = `${y}px`; |
| 188 | + |
| 189 | + //@ts-ignore |
| 190 | + if (element.__currentTransformY !== currentTransformYValue) { |
| 191 | + //@ts-ignore |
| 192 | + element.__currentTransformY = currentTransformYValue; |
| 193 | + element.style.setProperty(currentTransformY, currentTransformYValue); |
| 194 | + } |
| 195 | + |
| 196 | + const xOffset = `calc(var(${columnOffsetX}) + ${pageOffset}px)`; |
| 197 | + const transformX = `var(${columnOffsetXWhileReordering}, ${xOffset})`; |
| 198 | + const transformY = `var(${currentTransformY})`; |
| 199 | + |
| 200 | + const transformValue = `translate3d(${transformX}, ${transformY}, 0px)`; |
| 201 | + |
| 202 | + //@ts-ignore |
| 203 | + if (element.__transformValue !== transformValue) { |
| 204 | + //@ts-ignore |
| 205 | + element.__transformValue = transformValue; |
| 206 | + element.style.transform = transformValue; |
| 207 | + } |
| 208 | + }; |
| 209 | +} |
0 commit comments