Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TableCellNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel table cell node.
9 *
10 * @class
11 * @extends ve.dm.BranchNode
12 * @mixes ve.dm.TableCellableNode
13 *
14 * @constructor
15 * @param {Object} [element] Reference to element in linear model
16 * @param {ve.dm.Node[]} [children]
17 */
18 ve.dm.TableCellNode = function VeDmTableCellNode() {
19 // Parent constructor
20 ve.dm.TableCellNode.super.apply( this, arguments );
21
22 // Mixin constructor
23 ve.dm.TableCellableNode.call( this );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.dm.TableCellNode, ve.dm.BranchNode );
29
30 OO.mixinClass( ve.dm.TableCellNode, ve.dm.TableCellableNode );
31
32 /* Static Properties */
33
34 ve.dm.TableCellNode.static.name = 'tableCell';
35
36 ve.dm.TableCellNode.static.isUnwrappable = false;
37
38 ve.dm.TableCellNode.static.parentNodeTypes = [ 'tableRow' ];
39
40 ve.dm.TableCellNode.static.defaultAttributes = { style: 'data' };
41
42 ve.dm.TableCellNode.static.matchTagNames = [ 'td', 'th' ];
43
44 ve.dm.TableCellNode.static.isCellEditable = true;
45
46 // Exclude 'colspan' and 'rowspan' as they are managed explicitly
47 ve.dm.TableCellNode.static.preserveHtmlAttributes = function ( attribute ) {
48 return attribute !== 'colspan' && attribute !== 'rowspan';
49 };
50
51 /* Static Methods */
52
53 ve.dm.TableCellNode.static.toDataElement = function ( domElements ) {
54 const attributes = {};
55
56 ve.dm.TableCellableNode.static.setAttributes( attributes, domElements );
57
58 return {
59 type: this.name,
60 attributes: attributes
61 };
62 };
63
64 ve.dm.TableCellNode.static.toDomElements = function ( dataElement, doc ) {
65 const tag = dataElement.attributes && dataElement.attributes.style === 'header' ? 'th' : 'td',
66 domElement = doc.createElement( tag ),
67 attributes = dataElement.attributes;
68
69 ve.dm.TableCellableNode.static.applyAttributes( attributes, domElement );
70
71 return [ domElement ];
72 };
73
74 /**
75 * Creates data that can be inserted into the model to create a new table cell.
76 *
77 * @param {Object} [options]
78 * @param {string} [options.style='data'] Either 'header' or 'data'
79 * @param {number} [options.rowspan=1] Number of rows the cell spans
80 * @param {number} [options.colspan=1] Number of columns the cell spans
81 * @param {Array} [options.content] Linear model data, defaults to empty wrapper paragraph
82 * @return {Array} Model data for a new table cell
83 */
84 ve.dm.TableCellNode.static.createData = function ( options ) {
85 options = options || {};
86 const opening = {
87 type: 'tableCell',
88 attributes: {
89 style: options.style || 'data',
90 rowspan: options.rowspan || 1,
91 colspan: options.colspan || 1
92 }
93 };
94 const content = options.content || [
95 { type: 'paragraph', internal: { generated: 'wrapper' } },
96 { type: '/paragraph' }
97 ];
98 return [ opening ].concat( content, { type: '/tableCell' } );
99 };
100
101 ve.dm.TableCellNode.static.describeChange = function ( key, change ) {
102 if ( key === 'style' ) {
103 return ve.htmlMsg( 'visualeditor-changedesc-no-key',
104 // The following messages are used here:
105 // * visualeditor-table-format-data
106 // * visualeditor-table-format-header
107 this.wrapText( 'del', ve.msg( 'visualeditor-table-format-' + change.from ) ),
108 this.wrapText( 'ins', ve.msg( 'visualeditor-table-format-' + change.to ) )
109 );
110 } else if ( key === 'colspan' || key === 'rowspan' ) {
111 // colspan/rowspan of '1' is the same as not setting it
112 if ( change.from === 1 ) {
113 change.from = undefined;
114 }
115 if ( change.to === 1 ) {
116 change.to = undefined;
117 }
118 // These might be the same now
119 if ( change.from === change.to ) {
120 return null;
121 }
122 } else if ( key === 'originalColspan' || key === 'originalRowspan' ) {
123 return null;
124 }
125
126 // Parent method
127 return ve.dm.TableCellNode.super.static.describeChange.call( this, key, change );
128 };
129
130 /* Registration */
131
132 ve.dm.modelRegistry.register( ve.dm.TableCellNode );