Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/nodes/ve.dm.TableNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel table node.
9 *
10 * @class
11 * @extends ve.dm.BranchNode
12 *
13 * @constructor
14 * @param {ve.dm.Node[]} [children]
15 */
16 ve.dm.TableNode = function VeDmTableNode() {
17 // Parent constructor
18 ve.dm.TableNode.super.apply( this, arguments );
19
20 // A dense representation of the sparse model to make manipulations
21 // in presence of spanning cells feasible.
22 this.matrix = new ve.dm.TableMatrix( this );
23
24 // Events
25 this.connect( this, { splice: 'onSplice' } );
26 };
27
28 /* Inheritance */
29
30 OO.inheritClass( ve.dm.TableNode, ve.dm.BranchNode );
31
32 /**
33 * @event ve.dm.TableNode#cellAttributeChange
34 * @param {ve.dm.TableCellableNode} cell
35 */
36
37 /* Static Properties */
38
39 ve.dm.TableNode.static.name = 'table';
40
41 ve.dm.TableNode.static.childNodeTypes = [ 'tableSection', 'tableCaption' ];
42
43 ve.dm.TableNode.static.matchTagNames = [ 'table' ];
44
45 /* Methods */
46
47 /**
48 * Handle splicing of child nodes
49 */
50 ve.dm.TableNode.prototype.onSplice = function () {
51 const nodes = Array.prototype.slice.call( arguments, 2 );
52 this.getMatrix().invalidate();
53 for ( let i = 0; i < nodes.length; i++ ) {
54 nodes[ i ].connect( this, {
55 cellAttributeChange: 'onCellAttributeChange'
56 } );
57 }
58 };
59
60 /**
61 * Handle cell attribute changes
62 *
63 * @param {ve.dm.TableCellableNode} cell
64 * @fires ve.dm.TableNode#cellAttributeChange
65 */
66 ve.dm.TableNode.prototype.onCellAttributeChange = function ( cell ) {
67 this.emit( 'cellAttributeChange', cell );
68 };
69
70 /**
71 * Get table matrix for this table node
72 *
73 * @return {ve.dm.TableMatrix} Table matrix
74 */
75 ve.dm.TableNode.prototype.getMatrix = function () {
76 return this.matrix;
77 };
78
79 /**
80 * Get the table's caption node, if it exists
81 *
82 * @return {ve.dm.TableCaptionNode|null} The table's caption node, or null if not found
83 */
84 ve.dm.TableNode.prototype.getCaptionNode = function () {
85 for ( let i = 0, l = this.children.length; i < l; i++ ) {
86 if ( this.children[ i ] instanceof ve.dm.TableCaptionNode ) {
87 return this.children[ i ];
88 }
89 }
90 return null;
91 };
92
93 /**
94 * Provides a cell iterator that allows convenient traversal regardless of
95 * the structure with respect to sections.
96 *
97 * @return {ve.dm.TableNodeCellIterator}
98 */
99 ve.dm.TableNode.prototype.getIterator = function () {
100 return new ve.dm.TableNodeCellIterator( this );
101 };
102
103 /* Registration */
104
105 ve.dm.modelRegistry.register( ve.dm.TableNode );
106
107 /**
108 * A helper class to iterate over the cells of a table node.
109 *
110 * It provides a unified interface to iterate cells in presence of table sections,
111 * e.g., providing consecutive row indexes.
112 *
113 * @class
114 * @mixes OO.EventEmitter
115 *
116 * @constructor
117 * @param {ve.dm.TableNode} tableNode Table node to iterate through
118 */
119 ve.dm.TableNodeCellIterator = function VeDmTableNodeCellIterator( tableNode ) {
120 // Mixin constructors
121 OO.EventEmitter.call( this );
122
123 this.table = tableNode;
124
125 this.sectionIndex = 0;
126 this.rowIndex = 0;
127 this.cellIndex = 0;
128
129 this.sectionCount = this.table.children.length;
130 this.rowCount = 0;
131 this.cellCount = 0;
132
133 this.sectionNode = null;
134 this.rowNode = null;
135 this.cellNode = null;
136
137 this.finished = false;
138 };
139
140 /* Inheritance */
141
142 OO.mixinClass( ve.dm.TableNodeCellIterator, OO.EventEmitter );
143
144 /* Events */
145
146 /**
147 * @event ve.dm.TableNodeCellIterator#newSection
148 * @param {ve.dm.TableSectionNode} node Table section node
149 */
150
151 /**
152 * @event ve.dm.TableNodeCellIterator#newRow
153 * @param {ve.dm.TableRowNode} node Table row node
154 */
155
156 /* Methods */
157
158 /**
159 * Check if the iterator has finished iterating over the cells of a table node.
160 *
161 * @return {boolean} Iterator is finished
162 */
163 ve.dm.TableNodeCellIterator.prototype.isFinished = function () {
164 return this.finished;
165 };
166
167 /**
168 * Get the next cell node
169 *
170 * @return {ve.dm.TableCellNode|null|undefined} Next cell node, null if a not a table cell, or undefined if at the end
171 * @throws {Error} TableNodeCellIterator has no more cells left.
172 */
173 ve.dm.TableNodeCellIterator.prototype.next = function () {
174 if ( this.isFinished() ) {
175 throw new Error( 'TableNodeCellIterator has no more cells left.' );
176 }
177 this.nextCell( this );
178 return this.cellNode;
179 };
180
181 /**
182 * Move to the next table section
183 *
184 * @fires ve.dm.TableNodeCellIterator#newSection
185 */
186 ve.dm.TableNodeCellIterator.prototype.nextSection = function () {
187 // If there are no sections left, finish
188 if ( this.sectionIndex >= this.sectionCount ) {
189 this.finished = true;
190 this.sectionNode = undefined;
191 return;
192 }
193 // Get the next node and make sure it's a section node (and not an alien node)
194 const sectionNode = this.table.children[ this.sectionIndex ];
195 this.sectionIndex++;
196 this.rowIndex = 0;
197 if ( sectionNode instanceof ve.dm.TableSectionNode ) {
198 this.sectionNode = sectionNode;
199 this.rowCount = this.sectionNode.children.length;
200 this.emit( 'newSection', this.sectionNode );
201 } else {
202 this.nextSection();
203 return;
204 }
205 };
206
207 /**
208 * Move to the next table row
209 *
210 * @fires ve.dm.TableNodeCellIterator#newRow
211 */
212 ve.dm.TableNodeCellIterator.prototype.nextRow = function () {
213 // If there are no rows left, go to the next section
214 if ( this.rowIndex >= this.rowCount ) {
215 this.nextSection();
216 if ( this.isFinished() ) {
217 this.rowNode = undefined;
218 return;
219 }
220 }
221 // Get the next node and make sure it's a row node (and not an alien node)
222 const rowNode = this.sectionNode.children[ this.rowIndex ];
223 this.rowIndex++;
224 this.cellIndex = 0;
225 if ( rowNode instanceof ve.dm.TableRowNode ) {
226 this.rowNode = rowNode;
227 this.cellCount = this.rowNode.children.length;
228 this.emit( 'newRow', this.rowNode );
229 } else {
230 this.nextRow();
231 return;
232 }
233 };
234
235 /**
236 * Move to the next table cell
237 */
238 ve.dm.TableNodeCellIterator.prototype.nextCell = function () {
239 // For the first read, sectionNode and rowNode will be empty
240 if ( !this.sectionNode ) {
241 this.nextSection();
242 }
243 if ( !this.rowNode ) {
244 this.nextRow();
245 }
246 // If there are no cells left, go to the next row
247 if ( this.cellIndex >= this.cellCount ) {
248 this.nextRow();
249 // If calling next row finished the iterator, clear and return
250 if ( this.isFinished() ) {
251 this.cellNode = undefined;
252 return;
253 }
254 }
255 // Get the next node and make sure it's a cell node (and not an alien node)
256 const cellNode = this.rowNode.children[ this.cellIndex ];
257 this.cellNode = cellNode && cellNode.isCellable() ? cellNode : null;
258 this.cellIndex++;
259 };