| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.dm.TableNode = function VeDmTableNode() { |
| 17 |
|
| 18 |
ve.dm.TableNode.super.apply( this, arguments ); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
this.matrix = new ve.dm.TableMatrix( this ); |
| 23 |
|
| 24 |
|
| 25 |
this.connect( this, { splice: 'onSplice' } ); |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
OO.inheritClass( ve.dm.TableNode, ve.dm.BranchNode ); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
ve.dm.TableNode.prototype.onCellAttributeChange = function ( cell ) { |
| 67 |
this.emit( 'cellAttributeChange', cell ); |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
ve.dm.TableNode.prototype.getMatrix = function () { |
| 76 |
return this.matrix; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
ve.dm.TableNode.prototype.getIterator = function () { |
| 100 |
return new ve.dm.TableNodeCellIterator( this ); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ve.dm.modelRegistry.register( ve.dm.TableNode ); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ve.dm.TableNodeCellIterator = function VeDmTableNodeCellIterator( tableNode ) { |
| 120 |
|
| 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 |
|
| 141 |
|
| 142 |
OO.mixinClass( ve.dm.TableNodeCellIterator, OO.EventEmitter ); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
ve.dm.TableNodeCellIterator.prototype.isFinished = function () { |
| 164 |
return this.finished; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
ve.dm.TableNodeCellIterator.prototype.nextSection = function () { |
| 187 |
|
| 188 |
if ( this.sectionIndex >= this.sectionCount ) { |
| 189 |
this.finished = true; |
| 190 |
this.sectionNode = undefined; |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 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 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.dm.TableNodeCellIterator.prototype.nextRow = function () { |
| 213 |
|
| 214 |
if ( this.rowIndex >= this.rowCount ) { |
| 215 |
this.nextSection(); |
| 216 |
if ( this.isFinished() ) { |
| 217 |
this.rowNode = undefined; |
| 218 |
return; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 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 |
|
| 237 |
|
| 238 |
ve.dm.TableNodeCellIterator.prototype.nextCell = function () { |
| 239 |
|
| 240 |
if ( !this.sectionNode ) { |
| 241 |
this.nextSection(); |
| 242 |
} |
| 243 |
if ( !this.rowNode ) { |
| 244 |
this.nextRow(); |
| 245 |
} |
| 246 |
|
| 247 |
if ( this.cellIndex >= this.cellCount ) { |
| 248 |
this.nextRow(); |
| 249 |
|
| 250 |
if ( this.isFinished() ) { |
| 251 |
this.cellNode = undefined; |
| 252 |
return; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
const cellNode = this.rowNode.children[ this.cellIndex ]; |
| 257 |
this.cellNode = cellNode && cellNode.isCellable() ? cellNode : null; |
| 258 |
this.cellIndex++; |
| 259 |
}; |