Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TableCellableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel node which can behave as a table cell
9 *
10 * @class
11 *
12 * @abstract
13 * @constructor
14 */
15 ve.dm.TableCellableNode = function VeDmTableCellableNode() {
16 // Events
17 this.connect( this, {
18 attributeChange: 'onCellableAttributeChange'
19 } );
20 };
21
22 /* Inheritance */
23
24 OO.initClass( ve.dm.TableCellableNode );
25
26 /* Static Properties */
27
28 ve.dm.TableCellableNode.static.isCellable = true;
29
30 /* Static Methods */
31
32 ve.dm.TableCellableNode.static.areNodesCellable = function ( domNodes ) {
33 // We can handle a DM node consisting of multiple table cell DOM elements with identical rowspan
34 // as a table element. Give up on anything else.
35 const cellElems = new Set( [ 'td', 'th' ] );
36 return domNodes.every(
37 ( node ) => cellElems.has( node.nodeName.toLowerCase() ) &&
38 node.rowspan === domNodes[ 0 ].rowspan
39 );
40 };
41
42 ve.dm.TableCellableNode.static.setAttributes = function ( attributes, domElements, isAlien ) {
43 if ( isAlien ) {
44 // For alienTableCells, we only need the colspan and rowspan, which
45 // may need to be summed over an about-group (T366984).
46 let colspan = 0, rowspan = 0;
47 Array.prototype.forEach.call( domElements, ( node ) => {
48 const attrs = {};
49 this.setAttributes( attrs, [ node ], false );
50 colspan += attrs.colspan || 1;
51 // TODO: Support a non-rectangular alien group
52 rowspan = Math.max( rowspan, attrs.rowspan || 1 );
53 } );
54 if ( colspan !== 1 ) {
55 attributes.colspan = colspan;
56 }
57 if ( rowspan !== 1 ) {
58 attributes.rowspan = rowspan;
59 }
60 } else {
61 const style = domElements[ 0 ].nodeName.toLowerCase() === 'th' ? 'header' : 'data';
62 const colspan = domElements[ 0 ].getAttribute( 'colspan' );
63 const rowspan = domElements[ 0 ].getAttribute( 'rowspan' );
64
65 attributes.style = style;
66
67 if ( colspan !== null ) {
68 attributes.originalColspan = colspan;
69 if ( colspan !== '' && !isNaN( Number( colspan ) ) ) {
70 attributes.colspan = Number( colspan );
71 }
72 }
73
74 if ( rowspan !== null ) {
75 attributes.originalRowspan = rowspan;
76 if ( rowspan !== '' && !isNaN( Number( rowspan ) ) ) {
77 attributes.rowspan = Number( rowspan );
78 }
79 }
80 }
81 };
82
83 ve.dm.TableCellableNode.static.applyAttributes = function ( attributes, domElement ) {
84 const spans = {
85 colspan: attributes.colspan,
86 rowspan: attributes.rowspan
87 };
88
89 // Ignore spans of 1 unless they were in the original HTML
90 if ( attributes.colspan === 1 && Number( attributes.originalColspan ) !== 1 ) {
91 spans.colspan = null;
92 }
93
94 if ( attributes.rowspan === 1 && Number( attributes.originalRowspan ) !== 1 ) {
95 spans.rowspan = null;
96 }
97
98 // Use original value if the numerical value didn't change, or if we didn't set one
99 if ( attributes.colspan === undefined || attributes.colspan === Number( attributes.originalColspan ) ) {
100 spans.colspan = attributes.originalColspan;
101 }
102
103 if ( attributes.rowspan === undefined || attributes.rowspan === Number( attributes.originalRowspan ) ) {
104 spans.rowspan = attributes.originalRowspan;
105 }
106
107 ve.setDomAttributes( domElement, spans );
108 };
109
110 /* Methods */
111
112 /**
113 * Get the number of rows the cell spans
114 *
115 * @return {number} Rows spanned
116 */
117 ve.dm.TableCellableNode.prototype.getRowspan = function () {
118 return this.getAttribute( 'rowspan' ) || 1;
119 };
120
121 /**
122 * Get the number of columns the cell spans
123 *
124 * @return {number} Columns spanned
125 */
126 ve.dm.TableCellableNode.prototype.getColspan = function () {
127 return this.getAttribute( 'colspan' ) || 1;
128 };
129
130 /**
131 * Get number of columns and rows the cell spans
132 *
133 * @return {Object} Object containing 'col' and 'row'
134 */
135 ve.dm.TableCellableNode.prototype.getSpans = function () {
136 return {
137 col: this.getColspan(),
138 row: this.getRowspan()
139 };
140 };
141
142 /**
143 * Get the style of the cell
144 *
145 * @return {string} Style, 'header' or 'data'
146 */
147 ve.dm.TableCellableNode.prototype.getStyle = function () {
148 return this.getAttribute( 'style' ) || 'data';
149 };
150
151 /**
152 * Handle attributes changes
153 *
154 * @param {string} key Attribute key
155 * @param {string} from Old value
156 * @param {string} to New value
157 */
158 ve.dm.TableCellableNode.prototype.onCellableAttributeChange = function ( key ) {
159 if ( this.getParent() && ( key === 'colspan' || key === 'rowspan' ) ) {
160 // In practice the matrix should already be invalidated as you
161 // shouldn't change a span without adding/removing other cells,
162 // but it is possible to just change spans if you don't mind a
163 // non-rectangular table.
164 this.findParent( ve.dm.TableNode ).getMatrix().invalidate();
165 }
166 };