| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ce.TableNode = function VeCeTableNode() { |
| 17 |
|
| 18 |
ve.ce.TableNode.super.apply( this, arguments ); |
| 19 |
|
| 20 |
|
| 21 |
this.surface = null; |
| 22 |
this.active = false; |
| 23 |
this.startCell = null; |
| 24 |
this.endCell = null; |
| 25 |
|
| 26 |
|
| 27 |
this.editingFragment = null; |
| 28 |
|
| 29 |
|
| 30 |
this.$element |
| 31 |
.addClass( 've-ce-tableNode' ) |
| 32 |
.prop( 'contentEditable', 'false' ); |
| 33 |
}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
OO.inheritClass( ve.ce.TableNode, ve.ce.BranchNode ); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ve.ce.TableNode.static.autoFocus = false; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ce.TableNode.prototype.onSetup = function () { |
| 49 |
|
| 50 |
ve.ce.TableNode.super.prototype.onSetup.call( this ); |
| 51 |
|
| 52 |
|
| 53 |
if ( this.surface || !this.root ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
this.surface = this.getRoot().getSurface(); |
| 57 |
|
| 58 |
|
| 59 |
this.$selectionBox = $( '<div>' ).addClass( 've-ce-tableNodeOverlay-selection-box' ); |
| 60 |
this.$selectionBoxAnchor = $( '<div>' ).addClass( 've-ce-tableNodeOverlay-selection-box-anchor' ); |
| 61 |
if ( OO.ui.isMobile() ) { |
| 62 |
this.nodeContext = new ve.ui.TableLineContext( this, 'table' ); |
| 63 |
} else { |
| 64 |
this.nodeContext = null; |
| 65 |
} |
| 66 |
this.colContext = new ve.ui.TableLineContext( this, 'col' ); |
| 67 |
this.rowContext = new ve.ui.TableLineContext( this, 'row' ); |
| 68 |
|
| 69 |
this.$overlay = $( '<div>' ) |
| 70 |
.addClass( 've-ce-tableNodeOverlay oo-ui-element-hidden' ) |
| 71 |
.append( |
| 72 |
this.$selectionBox, |
| 73 |
this.$selectionBoxAnchor, |
| 74 |
this.nodeContext ? this.nodeContext.$element : undefined, |
| 75 |
this.colContext.$element, |
| 76 |
this.rowContext.$element, |
| 77 |
this.$rowBracket, |
| 78 |
this.$colBracket |
| 79 |
); |
| 80 |
this.surface.surface.$blockers.append( this.$overlay ); |
| 81 |
|
| 82 |
|
| 83 |
this.$element.on( { |
| 84 |
'mousedown.ve-ce-tableNode': this.onTableMouseDown.bind( this ), |
| 85 |
'dblclick.ve-ce-tableNode': this.onTableDblClick.bind( this ) |
| 86 |
} ); |
| 87 |
this.$overlay.on( { |
| 88 |
'mousedown.ve-ce-tableNode': this.onTableMouseDown.bind( this ), |
| 89 |
'dblclick.ve-ce-tableNode': this.onTableDblClick.bind( this ) |
| 90 |
} ); |
| 91 |
this.onTableMouseUpHandler = this.onTableMouseUp.bind( this ); |
| 92 |
this.onTableMouseMoveHandler = this.onTableMouseMove.bind( this ); |
| 93 |
|
| 94 |
|
| 95 |
this.updateOverlayDebounced = ve.debounce( this.updateOverlay.bind( this ) ); |
| 96 |
this.surface.getModel().connect( this, { select: 'onSurfaceModelSelect' } ); |
| 97 |
this.surface.connect( this, { |
| 98 |
position: this.updateOverlayDebounced, |
| 99 |
activation: 'onSurfaceActivation' |
| 100 |
} ); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
ve.ce.TableNode.prototype.onTeardown = function () { |
| 107 |
|
| 108 |
ve.ce.TableNode.super.prototype.onTeardown.call( this ); |
| 109 |
|
| 110 |
|
| 111 |
if ( !this.surface ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
this.$element.off( '.ve-ce-tableNode' ); |
| 117 |
this.$overlay.off( '.ve-ce-tableNode' ); |
| 118 |
this.surface.getModel().disconnect( this ); |
| 119 |
this.surface.disconnect( this ); |
| 120 |
this.$overlay.remove(); |
| 121 |
|
| 122 |
this.surface = null; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ve.ce.TableNode.prototype.onTableDblClick = function ( e ) { |
| 131 |
if ( !this.getCellNodeFromEvent( e ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
if ( this.surface.getModel().getSelection() instanceof ve.dm.TableSelection ) { |
| 135 |
|
| 136 |
this.setEditing( true, true ); |
| 137 |
|
| 138 |
|
| 139 |
const offset = this.surface.getOffsetFromEventCoords( e.originalEvent ); |
| 140 |
if ( offset !== -1 ) { |
| 141 |
|
| 142 |
this.surface.getModel().setLinearSelection( new ve.Range( offset ) ); |
| 143 |
} else { |
| 144 |
this.setEditing( true ); |
| 145 |
} |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
ve.ce.TableNode.prototype.onTableMouseDown = function ( e ) { |
| 155 |
const cellNode = this.getCellNodeFromEvent( e ); |
| 156 |
if ( !cellNode ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
const endCell = this.getModel().getMatrix().lookupCell( cellNode.getModel() ); |
| 161 |
if ( !endCell ) { |
| 162 |
e.preventDefault(); |
| 163 |
return; |
| 164 |
} |
| 165 |
const selection = this.surface.getModel().getSelection(); |
| 166 |
|
| 167 |
let startCell; |
| 168 |
let newSelection; |
| 169 |
if ( e.shiftKey && this.active ) { |
| 170 |
|
| 171 |
if ( selection instanceof ve.dm.TableSelection ) { |
| 172 |
startCell = { col: selection.fromCol, row: selection.fromRow }; |
| 173 |
} else { |
| 174 |
startCell = this.getModel().getMatrix().lookupCell( this.getActiveCellNode().getModel() ); |
| 175 |
} |
| 176 |
} else if ( |
| 177 |
( e.which === OO.ui.MouseButtons.RIGHT || this.surface.isDeactivated() ) && |
| 178 |
selection instanceof ve.dm.TableSelection && |
| 179 |
selection.containsCell( endCell ) |
| 180 |
) { |
| 181 |
|
| 182 |
|
| 183 |
newSelection = selection; |
| 184 |
|
| 185 |
startCell = this.startCell || endCell; |
| 186 |
} else { |
| 187 |
|
| 188 |
startCell = endCell; |
| 189 |
} |
| 190 |
|
| 191 |
if ( !newSelection ) { |
| 192 |
newSelection = new ve.dm.TableSelection( |
| 193 |
this.getModel().getOuterRange(), |
| 194 |
startCell.col, |
| 195 |
startCell.row, |
| 196 |
endCell.col, |
| 197 |
endCell.row |
| 198 |
); |
| 199 |
newSelection = newSelection.expand( this.getModel().getDocument() ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( this.editingFragment ) { |
| 203 |
if ( newSelection.equals( this.editingFragment.getSelection() ) ) { |
| 204 |
|
| 205 |
return; |
| 206 |
} else { |
| 207 |
this.setEditing( false, true ); |
| 208 |
} |
| 209 |
} |
| 210 |
this.surface.getModel().setSelection( newSelection ); |
| 211 |
|
| 212 |
this.surface.activate(); |
| 213 |
|
| 214 |
|
| 215 |
if ( e.which === OO.ui.MouseButtons.RIGHT && !this.getActiveCellNode() ) { |
| 216 |
|
| 217 |
|
| 218 |
cellNode.$element.prop( 'contentEditable', true ); |
| 219 |
|
| 220 |
ve.selectElement( cellNode.$element[ 0 ] ); |
| 221 |
setTimeout( () => { |
| 222 |
|
| 223 |
cellNode.$element.prop( 'contentEditable', 'false' ); |
| 224 |
|
| 225 |
this.surface.onModelSelect(); |
| 226 |
} ); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
this.startCell = startCell; |
| 231 |
this.endCell = endCell; |
| 232 |
if ( !( selection instanceof ve.dm.TableSelection ) && OO.ui.isMobile() ) { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
setTimeout( () => { |
| 240 |
this.onTableDblClick( e ); |
| 241 |
} ); |
| 242 |
} else { |
| 243 |
this.surface.$document.on( { |
| 244 |
'mouseup touchend': this.onTableMouseUpHandler, |
| 245 |
'mousemove touchmove': this.onTableMouseMoveHandler |
| 246 |
} ); |
| 247 |
} |
| 248 |
e.preventDefault(); |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
ve.ce.TableNode.prototype.getCellNodeFromEvent = function ( e ) { |
| 260 |
|
| 261 |
if ( e.type === 'touchstart' && e.originalEvent.touches.length > 1 ) { |
| 262 |
|
| 263 |
return null; |
| 264 |
} else if ( e.type === 'touchmove' ) { |
| 265 |
if ( e.originalEvent.touches.length > 1 ) { |
| 266 |
|
| 267 |
return null; |
| 268 |
} |
| 269 |
const touch = e.originalEvent.touches[ 0 ]; |
| 270 |
return this.getCellNodeFromPoint( touch.clientX, touch.clientY ); |
| 271 |
} else { |
| 272 |
return this.getNearestCellNode( e.target ); |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
ve.ce.TableNode.prototype.getCellNodeFromPoint = function ( x, y ) { |
| 284 |
return this.getNearestCellNode( |
| 285 |
this.surface.getElementDocument().elementFromPoint( x, y ) |
| 286 |
); |
| 287 |
}; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
ve.ce.TableNode.prototype.getNearestCellNode = function ( element ) { |
| 298 |
const $element = $( element ), |
| 299 |
$table = $element.closest( 'table' ); |
| 300 |
|
| 301 |
|
| 302 |
if ( !this.$element.is( $table ) ) { |
| 303 |
return null; |
| 304 |
} |
| 305 |
|
| 306 |
return $element.closest( 'td, th' ).data( 'view' ); |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
ve.ce.TableNode.prototype.onTableMouseMove = function ( e ) { |
| 315 |
const endCellNode = this.getCellNodeFromEvent( e ); |
| 316 |
if ( !endCellNode ) { |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
const endCell = this.getModel().matrix.lookupCell( endCellNode.getModel() ); |
| 321 |
if ( !endCell || endCell === this.endCell ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
this.endCell = endCell; |
| 326 |
|
| 327 |
let selection = new ve.dm.TableSelection( |
| 328 |
this.getModel().getOuterRange(), |
| 329 |
this.startCell.col, this.startCell.row, endCell.col, endCell.row |
| 330 |
); |
| 331 |
selection = selection.expand( this.getModel().getDocument() ); |
| 332 |
this.surface.getModel().setSelection( selection ); |
| 333 |
}; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
ve.ce.TableNode.prototype.onTableMouseUp = function () { |
| 341 |
this.startCell = null; |
| 342 |
this.endCell = null; |
| 343 |
this.surface.$document.off( { |
| 344 |
'mouseup touchend': this.onTableMouseUpHandler, |
| 345 |
'mousemove touchmove': this.onTableMouseMoveHandler |
| 346 |
} ); |
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
ve.ce.TableNode.prototype.setEditing = function ( isEditing, noSelect ) { |
| 356 |
const surfaceModel = this.surface.getModel(), |
| 357 |
documentModel = surfaceModel.getDocument(); |
| 358 |
|
| 359 |
let selection = surfaceModel.getSelection(); |
| 360 |
if ( isEditing ) { |
| 361 |
if ( !selection.isSingleCell( documentModel ) ) { |
| 362 |
selection = selection.collapseToFrom(); |
| 363 |
this.surface.getModel().setSelection( selection ); |
| 364 |
} |
| 365 |
const cell = this.getCellNodesFromSelection( selection )[ 0 ]; |
| 366 |
if ( !cell.isCellEditable() ) { |
| 367 |
return; |
| 368 |
} |
| 369 |
this.editingFragment = this.surface.getModel().getFragment( selection ); |
| 370 |
cell.setEditing( true ); |
| 371 |
if ( !noSelect ) { |
| 372 |
const cellRange = cell.getModel().getRange(); |
| 373 |
const offset = surfaceModel.getDocument().data.getNearestContentOffset( cellRange.end, -1 ); |
| 374 |
if ( offset > cellRange.start ) { |
| 375 |
surfaceModel.setLinearSelection( new ve.Range( offset ) ); |
| 376 |
} |
| 377 |
} |
| 378 |
} else { |
| 379 |
let activeCellNode; |
| 380 |
if ( ( activeCellNode = this.getActiveCellNode() ) ) { |
| 381 |
activeCellNode.setEditing( false ); |
| 382 |
if ( !noSelect ) { |
| 383 |
surfaceModel.setSelection( this.editingFragment.getSelection() ); |
| 384 |
} |
| 385 |
} |
| 386 |
this.editingFragment = null; |
| 387 |
} |
| 388 |
|
| 389 |
this.$element.toggleClass( 've-ce-tableNode-editing', isEditing ); |
| 390 |
this.$overlay.toggleClass( 've-ce-tableNodeOverlay-editing', isEditing ); |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
ve.ce.TableNode.prototype.onSurfaceModelSelect = function ( selection ) { |
| 399 |
|
| 400 |
|
| 401 |
const active = |
| 402 |
( |
| 403 |
this.editingFragment !== null && |
| 404 |
selection instanceof ve.dm.LinearSelection && |
| 405 |
this.editingFragment.getSelection().getRanges( |
| 406 |
this.editingFragment.getDocument() |
| 407 |
)[ 0 ].containsRange( selection.getRange() ) |
| 408 |
) || |
| 409 |
( |
| 410 |
selection instanceof ve.dm.TableSelection && |
| 411 |
selection.tableRange.equalsSelection( this.getModel().getOuterRange() ) |
| 412 |
); |
| 413 |
|
| 414 |
if ( active ) { |
| 415 |
if ( !this.active ) { |
| 416 |
this.$overlay.removeClass( 'oo-ui-element-hidden' ); |
| 417 |
|
| 418 |
|
| 419 |
this.$element.on( 'touchstart.ve-ce-tableNode', this.onTableMouseDown.bind( this ) ); |
| 420 |
} |
| 421 |
|
| 422 |
if ( selection instanceof ve.dm.TableSelection ) { |
| 423 |
if ( this.editingFragment ) { |
| 424 |
this.setEditing( false, true ); |
| 425 |
} |
| 426 |
this.updateOverlayDebounced(); |
| 427 |
} |
| 428 |
} else if ( !active && this.active ) { |
| 429 |
this.$overlay.addClass( 'oo-ui-element-hidden' ); |
| 430 |
if ( this.editingFragment ) { |
| 431 |
this.setEditing( false, true ); |
| 432 |
} |
| 433 |
|
| 434 |
if ( this.getActiveCellNode() ) { |
| 435 |
this.surface.setActiveNode( null ); |
| 436 |
} |
| 437 |
this.$element.off( 'touchstart.ve-ce-tableNode' ); |
| 438 |
} |
| 439 |
this.$element.toggleClass( 've-ce-tableNode-active', active ); |
| 440 |
this.active = active; |
| 441 |
}; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
ve.ce.TableNode.prototype.getActiveCellNode = function () { |
| 449 |
const activeNode = this.surface.getActiveNode(), |
| 450 |
tableNodeOfActiveCellNode = activeNode && activeNode instanceof ve.ce.TableCellNode && activeNode.findParent( ve.ce.TableNode ); |
| 451 |
|
| 452 |
return tableNodeOfActiveCellNode === this ? activeNode : null; |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
ve.ce.TableNode.prototype.onSurfaceActivation = function () { |
| 459 |
this.$overlay.toggleClass( 've-ce-tableNodeOverlay-deactivated', !!this.surface.isShownAsDeactivated() ); |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
ve.ce.TableNode.prototype.updateOverlay = function () { |
| 466 |
if ( |
| 467 |
!this.active || !this.root || |
| 468 |
!this.surface || |
| 469 |
|
| 470 |
!this.surface.surface.$blockers[ 0 ].parentNode |
| 471 |
) { |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
const selection = this.editingFragment ? |
| 476 |
this.editingFragment.getSelection() : |
| 477 |
this.surface.getModel().getSelection(); |
| 478 |
const documentModel = this.editingFragment ? |
| 479 |
this.editingFragment.getDocument() : |
| 480 |
this.surface.getModel().getDocument(); |
| 481 |
|
| 482 |
|
| 483 |
const tableOffset = this.getFirstSectionNode().$element[ 0 ].getBoundingClientRect(); |
| 484 |
const surfaceOffset = this.surface.getSurface().$element[ 0 ].getBoundingClientRect(); |
| 485 |
|
| 486 |
if ( !tableOffset ) { |
| 487 |
return; |
| 488 |
} |
| 489 |
|
| 490 |
const selectionRect = this.surface.getSelection( selection ).getSelectionBoundingRect(); |
| 491 |
|
| 492 |
if ( !selectionRect ) { |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
const selectionOffset = ve.translateRect( |
| 498 |
selectionRect, |
| 499 |
surfaceOffset.left - tableOffset.left, surfaceOffset.top - tableOffset.top |
| 500 |
); |
| 501 |
|
| 502 |
let anchorOffset; |
| 503 |
if ( selection.isSingleCell( documentModel ) ) { |
| 504 |
|
| 505 |
anchorOffset = selectionOffset; |
| 506 |
} else { |
| 507 |
anchorOffset = ve.translateRect( |
| 508 |
this.surface.getSelection( selection.collapseToFrom() ).getSelectionBoundingRect(), |
| 509 |
surfaceOffset.left - tableOffset.left, surfaceOffset.top - tableOffset.top |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
this.$selectionBox.css( { |
| 515 |
top: selectionOffset.top, |
| 516 |
left: selectionOffset.left, |
| 517 |
width: selectionOffset.width, |
| 518 |
height: selectionOffset.height |
| 519 |
} ); |
| 520 |
this.$selectionBoxAnchor.css( { |
| 521 |
top: anchorOffset.top, |
| 522 |
left: anchorOffset.left, |
| 523 |
width: anchorOffset.width, |
| 524 |
height: anchorOffset.height |
| 525 |
} ); |
| 526 |
|
| 527 |
|
| 528 |
this.$overlay.css( { |
| 529 |
top: tableOffset.top - surfaceOffset.top, |
| 530 |
left: tableOffset.left - surfaceOffset.left, |
| 531 |
width: tableOffset.width |
| 532 |
} ); |
| 533 |
|
| 534 |
this.colContext.icon.$element.css( { |
| 535 |
left: selectionOffset.left, |
| 536 |
width: selectionOffset.width |
| 537 |
} ); |
| 538 |
this.rowContext.icon.$element.css( { |
| 539 |
top: selectionOffset.top, |
| 540 |
height: selectionOffset.height |
| 541 |
} ); |
| 542 |
|
| 543 |
if ( this.nodeContext ) { |
| 544 |
this.nodeContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() ); |
| 545 |
} |
| 546 |
this.colContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() ); |
| 547 |
this.rowContext.$element.toggleClass( 'oo-ui-element-hidden', this.surface.isReadOnly() ); |
| 548 |
|
| 549 |
|
| 550 |
this.$selectionBox.toggleClass( 've-ce-tableNodeOverlay-selection-box-notEditable', !selection.isEditable( documentModel ) ); |
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
ve.ce.TableNode.prototype.getFirstSectionNode = function () { |
| 559 |
let i = 0; |
| 560 |
while ( !( this.children[ i ] instanceof ve.ce.TableSectionNode ) ) { |
| 561 |
i++; |
| 562 |
} |
| 563 |
return this.children[ i ]; |
| 564 |
}; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
ve.ce.TableNode.prototype.getCellNodesFromSelection = function ( selection ) { |
| 573 |
const cells = selection.getMatrixCells( this.getModel().getDocument() ), |
| 574 |
nodes = []; |
| 575 |
|
| 576 |
for ( let i = 0, l = cells.length; i < l; i++ ) { |
| 577 |
const cellModel = cells[ i ].node; |
| 578 |
const cellView = this.getNodeFromOffset( cellModel.getOffset() - this.model.getOffset() ); |
| 579 |
nodes.push( cellView ); |
| 580 |
} |
| 581 |
return nodes; |
| 582 |
}; |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
ve.ce.TableNode.static.name = 'table'; |
| 587 |
|
| 588 |
ve.ce.TableNode.static.tagName = 'table'; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
ve.ce.nodeFactory.register( ve.ce.TableNode ); |