Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TableRowNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel table row node.
9 *
10 * @class
11 * @extends ve.dm.BranchNode
12 *
13 * @constructor
14 * @param {Object} [element] Reference to element in linear model
15 * @param {ve.dm.Node[]} [children]
16 */
17 ve.dm.TableRowNode = function VeDmTableRowNode() {
18 // Parent constructor
19 ve.dm.TableRowNode.super.apply( this, arguments );
20
21 // Events
22 this.connect( this, { splice: 'onSplice' } );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.dm.TableRowNode, ve.dm.BranchNode );
28
29 /* Static Properties */
30
31 ve.dm.TableRowNode.static.name = 'tableRow';
32
33 ve.dm.TableRowNode.static.childNodeTypes = [ 'tableCell', 'alienTableCell' ];
34
35 ve.dm.TableRowNode.static.parentNodeTypes = [ 'tableSection' ];
36
37 ve.dm.TableRowNode.static.matchTagNames = [ 'tr' ];
38
39 /* Static Methods */
40
41 /**
42 * Creates data that can be inserted into the model to create a new table row.
43 *
44 * @param {Object} [options] Creation options
45 * @param {string|string[]} [options.style='data'] Cell style; 'data' or 'header', or array of styles
46 * @param {number} [options.cellCount=1] Number of cells to create
47 * @return {Array} Model data for a new table row
48 */
49 ve.dm.TableRowNode.static.createData = function ( options ) {
50 options = options || {};
51
52 const cellCount = options.cellCount || 1;
53
54 const data = [];
55 data.push( { type: 'tableRow' } );
56 for ( let i = 0; i < cellCount; i++ ) {
57 ve.batchPush( data, ve.dm.TableCellNode.static.createData( {
58 style: Array.isArray( options.style ) ? options.style[ i ] : options.style
59 } ) );
60 }
61 data.push( { type: '/tableRow' } );
62 return data;
63 };
64
65 /* Methods */
66
67 /**
68 * Handle splicing of child nodes
69 */
70 ve.dm.TableRowNode.prototype.onSplice = function () {
71 if ( this.getRoot() ) {
72 this.getParent().getParent().getMatrix().invalidate();
73 }
74 const nodes = Array.prototype.slice.call( arguments, 2 );
75 for ( let i = 0; i < nodes.length; i++ ) {
76 nodes[ i ].connect( this, {
77 attributeChange: [ 'onCellAttributeChange', nodes[ i ] ]
78 } );
79 }
80 };
81
82 /**
83 * Handle cell attribute changes
84 *
85 * @param {ve.dm.TableCellableNode} cell
86 * @fires ve.dm.TableNode#cellAttributeChange
87 */
88 ve.dm.TableRowNode.prototype.onCellAttributeChange = function ( cell ) {
89 this.emit( 'cellAttributeChange', cell );
90 };
91
92 /* Registration */
93
94 ve.dm.modelRegistry.register( ve.dm.TableRowNode );