Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.TableMatrix.js

raw symbols SCIP
1 /**
2 * A helper class that allows random access to the table cells
3 * and introduces place-holders for fields occupied by spanning cells,
4 * making it a non-sparse representation of the sparse HTML model.
5 * This is essential for the implementation of table manipulations, such as row insertions or deletions.
6 *
7 * Example:
8 *
9 * <table>
10 * <tr><td rowspan=2>1</td><td colspan=2>2</td><td rowspan=2 colspan=2>3</td></tr>
11 * <tr><td>4</td><td>5</td></tr>
12 * </table>
13 *
14 * Visually this table would look like:
15 *
16 * -------------------
17 * | 1 | 2 | 3 |
18 * | |-------| |
19 * | | 4 | 5 | |
20 * -------------------
21 *
22 * The HTML model is sparse which makes it hard to read but also difficult to work with programmatically.
23 * The corresponding TableCellMatrix would look like:
24 *
25 * | C[1] | C[2] | P[2] | C[3] | P[3] |
26 * | P[1] | C[4] | C[5] | P[3] | P[3] |
27 *
28 * Where C[1] represents a Cell instance wrapping cell 1,
29 * and P[1] a PlaceHolder instance owned by that cell.
30 *
31 * @class
32 * @mixes OO.EventEmitter
33 * @constructor
34 * @param {ve.dm.TableNode} tableNode Reference to a table instance
35 */
36 ve.dm.TableMatrix = function VeDmTableMatrix( tableNode ) {
37 // Mixin constructors
38 OO.EventEmitter.call( this );
39
40 this.tableNode = tableNode;
41 // Do not access these directly as they get invalidated on structural changes
42 // Use the accessor methods instead.
43 this.matrix = null;
44 this.rowNodes = null;
45 };
46
47 /* Inheritance */
48
49 OO.mixinClass( ve.dm.TableMatrix, OO.EventEmitter );
50
51 /**
52 * @event ve.dm.TableMatrix#structureChange
53 */
54
55 /**
56 * Invalidates the matrix structure.
57 *
58 * This is called by ve.dm.TableNode on structural changes.
59 *
60 * @fires ve.dm.TableMatrix#structureChange
61 */
62 ve.dm.TableMatrix.prototype.invalidate = function () {
63 this.matrix = null;
64 this.rowNodes = null;
65 this.emit( 'structureChange' );
66 };
67
68 /**
69 * Recreates the matrix structure.
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 // Handle row transitions
79 iterator.on( 'newRow', ( rowNode ) => {
80 row++;
81 col = -1;
82 // Initialize a matrix row
83 matrix[ row ] = matrix[ row ] || [];
84 // Store the row node
85 rowNodes.push( rowNode );
86 } );
87
88 // Iterates through all cells and stores the cells as well as
89 // so called placeholders into the matrix.
90 let cellNode;
91 while ( ( cellNode = iterator.next() ) !== undefined ) {
92 col++;
93 // Skip placeholders
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 // Store the cell in the matrix
103 matrix[ row ][ col ] = cell;
104 // Add place holders for spanned cells
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 // Initialize the cell matrix row if not yet present
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 * Retrieves a single cell.
131 *
132 * @param {number} row Row index
133 * @param {number} col Column index
134 * @return {ve.dm.TableMatrixCell|undefined} Cell, or undefined if out of bounds
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 * Retrieves all cells of a column with given index.
143 *
144 * @param {number} col Column index
145 * @return {ve.dm.TableMatrixCell[]} The cells of a column
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 * Retrieves all cells of a row with given index.
158 *
159 * @param {number} row Row index
160 * @return {ve.dm.TableMatrixCell[]} The cells of a row
161 */
162 ve.dm.TableMatrix.prototype.getRow = function ( row ) {
163 const matrix = this.getMatrix();
164 return matrix[ row ];
165 };
166
167 /**
168 * Retrieves the row node of a row with given index.
169 *
170 * It is possible for bad tables to be constructed with rowspans
171 * that exceed the last actual row node. In these cases, the number
172 * of rows in the table matrix will exceed the number of row
173 * nodes, so users should expected getRowNode to sometimes
174 * return undefined. (T191858)
175 *
176 * @param {number} row Row index
177 * @return {ve.dm.TableRowNode|undefined} Node at given index, if found
178 */
179 ve.dm.TableMatrix.prototype.getRowNode = function ( row ) {
180 const rowNodes = this.getRowNodes();
181 return rowNodes[ row ];
182 };
183
184 /**
185 * Provides a reference to the internal cell matrix.
186 *
187 * Note: this is primarily for internal use. Do not change the delivered matrix
188 * and do not store as it may be invalidated.
189 *
190 * @return {ve.dm.TableMatrixCell[][]} Table matrix
191 */
192 ve.dm.TableMatrix.prototype.getMatrix = function () {
193 if ( !this.matrix ) {
194 this.update();
195 }
196 return this.matrix;
197 };
198
199 /**
200 * Provides a reference to the internal array of row nodes.
201 *
202 * Note: this is primarily for internal use. Do not change the delivered array
203 * and do not store it as it may be invalidated.
204 *
205 * @return {ve.dm.TableRowNode[]} Table row nodes
206 */
207 ve.dm.TableMatrix.prototype.getRowNodes = function () {
208 if ( !this.rowNodes ) {
209 this.update();
210 }
211 return this.rowNodes;
212 };
213
214 /**
215 * Get number of rows in the table
216 *
217 * @return {number} Number of rows
218 */
219 ve.dm.TableMatrix.prototype.getRowCount = function () {
220 return this.getMatrix().length;
221 };
222
223 /**
224 * Get number of columns in a row
225 *
226 * To get the number of columns in a table use #getMaxColCount
227 *
228 * @param {number} row Row to count columns in
229 * @return {number} Number of columns
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 * Get the maximum number of columns in a table
238 *
239 * This is required because in sparse tables the column count is variable.
240 *
241 * @return {number} Number of columns
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 * Look up the matrix cell for a given cell node.
254 *
255 * @param {ve.dm.TableCellNode} cellNode Cell node
256 * @return {ve.dm.TableMatrixCell|null} The cell or null if not found
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 * Finds the closest cell not being a placeholder for a given cell.
277 *
278 * @param {ve.dm.TableMatrixCell} cell Table cell
279 * @return {ve.dm.TableMatrixCell|null} Closest cell
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 * An object wrapping a table cell node, augmenting it with row and column indexes.
302 *
303 * Cells which are occupied by another cell's with 'rowspan' or 'colspan' attributes are
304 * placeholders and have an owner property other than themselves.
305 * Placeholders are used to create a dense representation of the sparse HTML table model.
306 *
307 * @class
308 * @constructor
309 * @param {ve.dm.TableCellNode} node DM Node
310 * @param {number} row Row index
311 * @param {number} col Column index
312 * @param {ve.dm.TableMatrixCell} [owner] Owner cell if this is a placeholder
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 // Used when moving cells
321 this.data = null;
322 };
323
324 /* Inheritance */
325
326 OO.initClass( ve.dm.TableMatrixCell );
327
328 /* Static Methods */
329
330 /**
331 * Comparison function for sorting cells in text flow order
332 *
333 * @param {ve.dm.TableMatrixCell} a First cell
334 * @param {ve.dm.TableMatrixCell} b Second cell
335 * @return {number} Positive, negative or zero, depending on relative position
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 /* Methods */
345
346 /**
347 * Check if this cell is a placeholder
348 *
349 * @return {boolean} This cell is a placeholder
350 */
351 ve.dm.TableMatrixCell.prototype.isPlaceholder = function () {
352 return this.owner !== this;
353 };
354
355 /**
356 * Get owner matrix cell
357 *
358 * @return {ve.dm.TableMatrixCell} Owner cell
359 */
360 ve.dm.TableMatrixCell.prototype.getOwner = function () {
361 return this.owner;
362 };
363
364 /**
365 * Compare to another cell
366 *
367 * Cells are considered equal to their placeholders
368 *
369 * @param {ve.dm.TableMatrixCell} other Cell to compare
370 * @return {boolean} Cells are equal
371 */
372 ve.dm.TableMatrixCell.prototype.equals = function ( other ) {
373 return this.getOwner().key === other.getOwner().key;
374 };