| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
ve.dm.TableCellableNode = function VeDmTableCellableNode() { |
| 16 |
|
| 17 |
this.connect( this, { |
| 18 |
attributeChange: 'onCellableAttributeChange' |
| 19 |
} ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.initClass( ve.dm.TableCellableNode ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.dm.TableCellableNode.static.isCellable = true; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ve.dm.TableCellableNode.static.areNodesCellable = function ( domNodes ) { |
| 33 |
|
| 34 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
ve.dm.TableCellableNode.prototype.getRowspan = function () { |
| 118 |
return this.getAttribute( 'rowspan' ) || 1; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.dm.TableCellableNode.prototype.getColspan = function () { |
| 127 |
return this.getAttribute( 'colspan' ) || 1; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
ve.dm.TableCellableNode.prototype.getSpans = function () { |
| 136 |
return { |
| 137 |
col: this.getColspan(), |
| 138 |
row: this.getRowspan() |
| 139 |
}; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
ve.dm.TableCellableNode.prototype.getStyle = function () { |
| 148 |
return this.getAttribute( 'style' ) || 'data'; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
ve.dm.TableCellableNode.prototype.onCellableAttributeChange = function ( key ) { |
| 159 |
if ( this.getParent() && ( key === 'colspan' || key === 'rowspan' ) ) { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
this.findParent( ve.dm.TableNode ).getMatrix().invalidate(); |
| 165 |
} |
| 166 |
}; |