| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
ve.dm.TableMatrix = function VeDmTableMatrix( tableNode ) { |
| 37 |
|
| 38 |
OO.EventEmitter.call( this ); |
| 39 |
|
| 40 |
this.tableNode = tableNode; |
| 41 |
|
| 42 |
|
| 43 |
this.matrix = null; |
| 44 |
this.rowNodes = null; |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
OO.mixinClass( ve.dm.TableMatrix, OO.EventEmitter ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
ve.dm.TableMatrix.prototype.invalidate = function () { |
| 63 |
this.matrix = null; |
| 64 |
this.rowNodes = null; |
| 65 |
this.emit( 'structureChange' ); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
ve.dm.TableMatrix.prototype.update = function () { |
| 72 |
const matrix = [], |
| 73 |
rowNodes = [], |
| 74 |
iterator = this.tableNode.getIterator(); |
| 75 |
let row = -1, |
| 76 |
col = -1; |
| 77 |
|
| 78 |
|
| 79 |
iterator.on( 'newRow', ( rowNode ) => { |
| 80 |
row++; |
| 81 |
col = -1; |
| 82 |
|
| 83 |
matrix[ row ] = matrix[ row ] || []; |
| 84 |
|
| 85 |
rowNodes.push( rowNode ); |
| 86 |
} ); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
let cellNode; |
| 91 |
while ( ( cellNode = iterator.next() ) !== undefined ) { |
| 92 |
col++; |
| 93 |
|
| 94 |
while ( matrix[ row ][ col ] ) { |
| 95 |
col++; |
| 96 |
} |
| 97 |
if ( !cellNode ) { |
| 98 |
matrix[ row ][ col ] = null; |
| 99 |
continue; |
| 100 |
} |
| 101 |
const cell = new ve.dm.TableMatrixCell( cellNode, row, col ); |
| 102 |
|
| 103 |
matrix[ row ][ col ] = cell; |
| 104 |
|
| 105 |
const rowSpan = cellNode.getRowspan(); |
| 106 |
const colSpan = cellNode.getColspan(); |
| 107 |
|
| 108 |
if ( rowSpan === 1 && colSpan === 1 ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
for ( let i = 0; i < rowSpan; i++ ) { |
| 113 |
for ( let j = 0; j < colSpan; j++ ) { |
| 114 |
if ( i === 0 && j === 0 ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
const r = row + i; |
| 118 |
const c = col + j; |
| 119 |
|
| 120 |
matrix[ r ] = matrix[ r ] || []; |
| 121 |
matrix[ r ][ c ] = new ve.dm.TableMatrixCell( cellNode, r, c, cell ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
this.matrix = matrix; |
| 126 |
this.rowNodes = rowNodes; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ve.dm.TableMatrix.prototype.getCell = function ( row, col ) { |
| 137 |
const matrix = this.getMatrix(); |
| 138 |
return matrix[ row ] ? matrix[ row ][ col ] : undefined; |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
ve.dm.TableMatrix.prototype.getColumn = function ( col ) { |
| 148 |
const matrix = this.getMatrix(); |
| 149 |
const cells = []; |
| 150 |
for ( let row = 0; row < matrix.length; row++ ) { |
| 151 |
cells.push( matrix[ row ][ col ] ); |
| 152 |
} |
| 153 |
return cells; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
ve.dm.TableMatrix.prototype.getRow = function ( row ) { |
| 163 |
const matrix = this.getMatrix(); |
| 164 |
return matrix[ row ]; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
ve.dm.TableMatrix.prototype.getRowNode = function ( row ) { |
| 180 |
const rowNodes = this.getRowNodes(); |
| 181 |
return rowNodes[ row ]; |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
ve.dm.TableMatrix.prototype.getMatrix = function () { |
| 193 |
if ( !this.matrix ) { |
| 194 |
this.update(); |
| 195 |
} |
| 196 |
return this.matrix; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
ve.dm.TableMatrix.prototype.getRowNodes = function () { |
| 208 |
if ( !this.rowNodes ) { |
| 209 |
this.update(); |
| 210 |
} |
| 211 |
return this.rowNodes; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
ve.dm.TableMatrix.prototype.getRowCount = function () { |
| 220 |
return this.getMatrix().length; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
ve.dm.TableMatrix.prototype.getColCount = function ( row ) { |
| 232 |
const matrix = this.getMatrix(); |
| 233 |
return matrix.length ? matrix[ row ].length : 0; |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.dm.TableMatrix.prototype.getMaxColCount = function () { |
| 244 |
let colCount = 0; |
| 245 |
|
| 246 |
for ( let row = this.getRowCount() - 1; row >= 0; row-- ) { |
| 247 |
colCount = Math.max( colCount, this.getColCount( row ) ); |
| 248 |
} |
| 249 |
return colCount; |
| 250 |
}; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
ve.dm.TableMatrix.prototype.lookupCell = function ( cellNode ) { |
| 259 |
const matrix = this.getMatrix(), |
| 260 |
rowNodes = this.getRowNodes(); |
| 261 |
|
| 262 |
const row = rowNodes.indexOf( cellNode.getParent() ); |
| 263 |
if ( row < 0 ) { |
| 264 |
return null; |
| 265 |
} |
| 266 |
const rowCells = matrix[ row ]; |
| 267 |
for ( let col = 0, cols = rowCells.length; col < cols; col++ ) { |
| 268 |
if ( rowCells[ col ] && rowCells[ col ].node === cellNode ) { |
| 269 |
return rowCells[ col ]; |
| 270 |
} |
| 271 |
} |
| 272 |
return null; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ve.dm.TableMatrix.prototype.findClosestCell = function ( cell ) { |
| 282 |
const matrix = this.getMatrix(); |
| 283 |
|
| 284 |
const rowCells = matrix[ cell.row ]; |
| 285 |
let col; |
| 286 |
for ( col = cell.col; col >= 0; col-- ) { |
| 287 |
if ( !rowCells[ col ].isPlaceholder() ) { |
| 288 |
return rowCells[ col ]; |
| 289 |
} |
| 290 |
} |
| 291 |
let cols; |
| 292 |
for ( col = cell.col + 1, cols = rowCells.length; col < cols; col++ ) { |
| 293 |
if ( !rowCells[ col ].isPlaceholder() ) { |
| 294 |
return rowCells[ col ]; |
| 295 |
} |
| 296 |
} |
| 297 |
return null; |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
ve.dm.TableMatrixCell = function VeDmTableMatrixCell( node, row, col, owner ) { |
| 315 |
this.node = node; |
| 316 |
this.row = row; |
| 317 |
this.col = col; |
| 318 |
this.key = row + '_' + col; |
| 319 |
this.owner = owner || this; |
| 320 |
|
| 321 |
this.data = null; |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
OO.initClass( ve.dm.TableMatrixCell ); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
ve.dm.TableMatrixCell.static.sortDescending = function ( a, b ) { |
| 338 |
if ( a.row !== b.row ) { |
| 339 |
return b.row - a.row; |
| 340 |
} |
| 341 |
return b.col - a.col; |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
ve.dm.TableMatrixCell.prototype.isPlaceholder = function () { |
| 352 |
return this.owner !== this; |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
ve.dm.TableMatrixCell.prototype.getOwner = function () { |
| 361 |
return this.owner; |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
ve.dm.TableMatrixCell.prototype.equals = function ( other ) { |
| 373 |
return this.getOwner().key === other.getOwner().key; |
| 374 |
}; |