| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.ce.ResizableNode = function VeCeResizableNode( $resizable, config ) { |
| 22 |
config = config || {}; |
| 23 |
|
| 24 |
|
| 25 |
this.$resizable = $resizable || this.$element; |
| 26 |
this.resizing = false; |
| 27 |
this.$resizeHandles = $( '<div>' ); |
| 28 |
this.snapToGrid = config.snapToGrid !== undefined ? config.snapToGrid : 10; |
| 29 |
this.outline = !!config.outline; |
| 30 |
this.showSizeLabel = config.showSizeLabel !== false; |
| 31 |
this.showScaleLabel = config.showScaleLabel !== false; |
| 32 |
|
| 33 |
this.canShowScaleLabel = false; |
| 34 |
if ( this.showSizeLabel || this.showScaleLabel ) { |
| 35 |
this.$sizeText = $( '<span>' ).addClass( 've-ce-resizableNode-sizeText' ); |
| 36 |
this.$sizeLabel = $( '<div>' ).addClass( 've-ce-resizableNode-sizeLabel' ).append( this.$sizeText ); |
| 37 |
} |
| 38 |
this.resizableOffset = null; |
| 39 |
this.resizableSurface = null; |
| 40 |
|
| 41 |
|
| 42 |
this.connect( this, { |
| 43 |
focus: 'onResizableFocus', |
| 44 |
blur: 'onResizableBlur', |
| 45 |
setup: 'onResizableSetup', |
| 46 |
teardown: 'onResizableTeardown', |
| 47 |
resizing: 'onResizableResizing', |
| 48 |
resizeEnd: 'onResizableFocus', |
| 49 |
rerender: 'onResizableFocus', |
| 50 |
align: 'onResizableAlign' |
| 51 |
} ); |
| 52 |
this.model.connect( this, { |
| 53 |
attributeChange: 'onResizableAttributeChange' |
| 54 |
} ); |
| 55 |
|
| 56 |
|
| 57 |
this.$resizeHandles |
| 58 |
.addClass( 've-ce-resizableNode-handles' ) |
| 59 |
.append( $( '<div>' ) |
| 60 |
.addClass( 've-ce-resizableNode-nwHandle' ) |
| 61 |
.data( 'handle', 'nw' ) ) |
| 62 |
.append( $( '<div>' ) |
| 63 |
.addClass( 've-ce-resizableNode-neHandle' ) |
| 64 |
.data( 'handle', 'ne' ) ) |
| 65 |
.append( $( '<div>' ) |
| 66 |
.addClass( 've-ce-resizableNode-seHandle' ) |
| 67 |
.data( 'handle', 'se' ) ) |
| 68 |
.append( $( '<div>' ) |
| 69 |
.addClass( 've-ce-resizableNode-swHandle' ) |
| 70 |
.data( 'handle', 'sw' ) ); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
OO.initClass( ve.ce.ResizableNode ); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
ve.ce.ResizableNode.prototype.isResizable = function () { |
| 100 |
return this.$resizable && !!this.$resizable.length && !OO.ui.isMobile() && |
| 101 |
!( this.root && this.root.getSurface() && this.root.getSurface().isReadOnly() ); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ve.ce.ResizableNode.prototype.getResizableOffset = function () { |
| 110 |
if ( !this.resizableOffset ) { |
| 111 |
this.resizableOffset = OO.ui.Element.static.getRelativePosition( |
| 112 |
this.$resizable, this.resizableSurface.getSurface().$element |
| 113 |
); |
| 114 |
} |
| 115 |
return this.resizableOffset; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
ve.ce.ResizableNode.prototype.setOriginalDimensions = function ( dimensions ) { |
| 124 |
if ( !this.isResizable() ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
const scalable = this.model.getScalable(); |
| 129 |
|
| 130 |
scalable.setOriginalDimensions( dimensions ); |
| 131 |
|
| 132 |
|
| 133 |
this.canShowScaleLabel = this.showScaleLabel && |
| 134 |
scalable.getOriginalDimensions().width && |
| 135 |
scalable.getOriginalDimensions().height; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
ve.ce.ResizableNode.prototype.hideSizeLabel = function () { |
| 142 |
if ( !this.isResizable() ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
setTimeout( () => { |
| 149 |
this.$sizeLabel.removeClass( 've-ce-resizableNode-sizeLabel-resizing' ); |
| 150 |
} ); |
| 151 |
|
| 152 |
setTimeout( () => { |
| 153 |
this.$sizeLabel.addClass( 'oo-ui-element-hidden' ); |
| 154 |
}, 200 ); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
ve.ce.ResizableNode.prototype.updateSizeLabel = function () { |
| 161 |
if ( !this.isResizable() ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
if ( !this.showSizeLabel && !this.canShowScaleLabel ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
const scalable = this.model.getScalable(); |
| 169 |
const dimensions = scalable.getCurrentDimensions(); |
| 170 |
const offset = this.getResizableOffset(); |
| 171 |
const minWidth = ( this.showSizeLabel ? 100 : 0 ) + ( this.showScaleLabel ? 30 : 0 ); |
| 172 |
|
| 173 |
let top, height; |
| 174 |
|
| 175 |
if ( dimensions.width < minWidth ) { |
| 176 |
top = offset.top + dimensions.height; |
| 177 |
height = 30; |
| 178 |
} else { |
| 179 |
top = offset.top; |
| 180 |
height = dimensions.height; |
| 181 |
} |
| 182 |
this.$sizeLabel |
| 183 |
.removeClass( 'oo-ui-element-hidden' ) |
| 184 |
.addClass( 've-ce-resizableNode-sizeLabel-resizing' ) |
| 185 |
.css( { |
| 186 |
top: top, |
| 187 |
left: offset.left, |
| 188 |
width: dimensions.width, |
| 189 |
height: height, |
| 190 |
lineHeight: height + 'px' |
| 191 |
} ); |
| 192 |
this.$sizeText.empty(); |
| 193 |
if ( this.showSizeLabel ) { |
| 194 |
this.$sizeText.append( $( '<span>' ) |
| 195 |
.addClass( 've-ce-resizableNode-sizeText-size' ) |
| 196 |
|
| 197 |
.text( Math.round( dimensions.width ) + ' × ' + Math.round( dimensions.height ) ) |
| 198 |
); |
| 199 |
} |
| 200 |
if ( this.canShowScaleLabel ) { |
| 201 |
this.$sizeText.append( $( '<span>' ) |
| 202 |
.addClass( 've-ce-resizableNode-sizeText-scale' ) |
| 203 |
.text( Math.round( 100 * scalable.getCurrentScale() ) + '%' ) |
| 204 |
); |
| 205 |
} |
| 206 |
this.$sizeText.toggleClass( 've-ce-resizableNode-sizeText-warning', scalable.isTooSmall() || scalable.isTooLarge() ); |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
ve.ce.ResizableNode.prototype.showHandles = function ( handles ) { |
| 215 |
if ( !this.isResizable() ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
const add = [], |
| 220 |
remove = [], |
| 221 |
allDirections = [ 'nw', 'ne', 'sw', 'se' ]; |
| 222 |
|
| 223 |
for ( let i = 0, len = allDirections.length; i < len; i++ ) { |
| 224 |
if ( handles === undefined || handles.indexOf( allDirections[ i ] ) !== -1 ) { |
| 225 |
remove.push( 've-ce-resizableNode-hide-' + allDirections[ i ] ); |
| 226 |
} else { |
| 227 |
add.push( 've-ce-resizableNode-hide-' + allDirections[ i ] ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
this.$resizeHandles |
| 237 |
.addClass( add ) |
| 238 |
.removeClass( remove ); |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
ve.ce.ResizableNode.prototype.onResizableFocus = function () { |
| 245 |
|
| 246 |
if ( !this.isResizable() || !this.isFocused() ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
this.$resizeHandles.appendTo( this.resizableSurface.getSurface().$controls ); |
| 250 |
if ( this.$sizeLabel ) { |
| 251 |
this.$sizeLabel.appendTo( this.resizableSurface.getSurface().$controls ); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
this.model.getScalable(); |
| 256 |
|
| 257 |
this.setResizableHandlesSizeAndPosition(); |
| 258 |
|
| 259 |
this.$resizeHandles |
| 260 |
.find( '.ve-ce-resizableNode-neHandle' ) |
| 261 |
.css( { marginRight: -this.$resizable.outerWidth() } ); |
| 262 |
this.$resizeHandles |
| 263 |
.find( '.ve-ce-resizableNode-swHandle' ) |
| 264 |
.css( { marginBottom: -this.$resizable.outerHeight() } ); |
| 265 |
this.$resizeHandles |
| 266 |
.find( '.ve-ce-resizableNode-seHandle' ) |
| 267 |
.css( { |
| 268 |
marginRight: -this.$resizable.outerWidth(), |
| 269 |
marginBottom: -this.$resizable.outerHeight() |
| 270 |
} ); |
| 271 |
|
| 272 |
this.$resizeHandles.children() |
| 273 |
.off( '.ve-ce-resizableNode' ) |
| 274 |
.on( |
| 275 |
'mousedown.ve-ce-resizableNode', |
| 276 |
this.onResizeHandlesCornerMouseDown.bind( this ) |
| 277 |
); |
| 278 |
|
| 279 |
this.resizableSurface.connect( this, { position: 'setResizableHandlesSizeAndPosition' } ); |
| 280 |
|
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
ve.ce.ResizableNode.prototype.onResizableBlur = function () { |
| 287 |
|
| 288 |
if ( !this.isResizableSetup || !this.root ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
this.$resizeHandles.detach(); |
| 293 |
if ( this.$sizeLabel ) { |
| 294 |
this.$sizeLabel.detach(); |
| 295 |
} |
| 296 |
|
| 297 |
this.resizableSurface.disconnect( this, { position: 'setResizableHandlesSizeAndPosition' } ); |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
ve.ce.ResizableNode.prototype.onResizableAlign = function ( align ) { |
| 306 |
if ( !this.isResizable() ) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
this.showHandles( { |
| 311 |
right: [ 'sw' ], |
| 312 |
left: [ 'se' ], |
| 313 |
center: [ 'sw', 'se' ] |
| 314 |
|
| 315 |
}[ align ] ); |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
ve.ce.ResizableNode.prototype.onResizableSetup = function () { |
| 322 |
|
| 323 |
if ( this.isResizableSetup || !this.root ) { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
this.resizableSurface = this.root.getSurface(); |
| 328 |
this.isResizableSetup = true; |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
ve.ce.ResizableNode.prototype.onResizableTeardown = function () { |
| 335 |
|
| 336 |
if ( !this.isResizableSetup || !this.root ) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
this.onResizableBlur(); |
| 341 |
this.resizableSurface = null; |
| 342 |
this.isResizableSetup = false; |
| 343 |
}; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
ve.ce.ResizableNode.prototype.onResizableResizing = function ( dimensions ) { |
| 351 |
if ( !this.isResizable() ) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
this.resizableOffset = null; |
| 356 |
this.model.getScalable().setCurrentDimensions( dimensions ); |
| 357 |
if ( !this.outline ) { |
| 358 |
this.$resizable.css( this.model.getScalable().getCurrentDimensions() ); |
| 359 |
this.setResizableHandlesPosition(); |
| 360 |
} |
| 361 |
this.updateSizeLabel(); |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
ve.ce.ResizableNode.prototype.onResizableAttributeChange = function () { |
| 372 |
if ( !this.isResizable() ) { |
| 373 |
return; |
| 374 |
} |
| 375 |
this.$resizable.css( this.model.getCurrentDimensions() ); |
| 376 |
}; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
ve.ce.ResizableNode.prototype.onResizeHandlesCornerMouseDown = function ( e ) { |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
this.root.getSurface().getSurface().getContext().toggle( false ); |
| 389 |
|
| 390 |
|
| 391 |
this.$resizeHandles |
| 392 |
.addClass( 've-ce-resizableNode-handles-resizing' ) |
| 393 |
.css( { |
| 394 |
width: this.$resizable.outerWidth(), |
| 395 |
height: this.$resizable.outerHeight() |
| 396 |
} ); |
| 397 |
|
| 398 |
this.$resizeHandles.children().css( 'margin', 0 ); |
| 399 |
|
| 400 |
|
| 401 |
this.resizeInfo = { |
| 402 |
mouseX: e.screenX, |
| 403 |
mouseY: e.screenY, |
| 404 |
top: this.$resizeHandles.position().top, |
| 405 |
left: this.$resizeHandles.position().left, |
| 406 |
height: this.$resizeHandles.height(), |
| 407 |
width: this.$resizeHandles.width(), |
| 408 |
handle: $( e.target ).data( 'handle' ) |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
this.resizing = true; |
| 413 |
this.root.getSurface().resizing = true; |
| 414 |
|
| 415 |
this.model.getScalable().setCurrentDimensions( { |
| 416 |
width: this.resizeInfo.width, |
| 417 |
height: this.resizeInfo.height |
| 418 |
} ); |
| 419 |
this.updateSizeLabel(); |
| 420 |
$( this.getElementDocument() ).on( { |
| 421 |
'mousemove.ve-ce-resizableNode': this.onDocumentMouseMove.bind( this ), |
| 422 |
'mouseup.ve-ce-resizableNode': this.onDocumentMouseUp.bind( this ) |
| 423 |
} ); |
| 424 |
this.emit( 'resizeStart' ); |
| 425 |
|
| 426 |
e.preventDefault(); |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
ve.ce.ResizableNode.prototype.setResizableHandlesSizeAndPosition = function () { |
| 433 |
if ( !this.isResizable() ) { |
| 434 |
return; |
| 435 |
} |
| 436 |
|
| 437 |
const width = this.$resizable.outerWidth(); |
| 438 |
const height = this.$resizable.outerHeight(); |
| 439 |
|
| 440 |
|
| 441 |
this.resizableOffset = null; |
| 442 |
|
| 443 |
this.setResizableHandlesPosition(); |
| 444 |
|
| 445 |
this.$resizeHandles |
| 446 |
.css( { |
| 447 |
width: 0, |
| 448 |
height: 0 |
| 449 |
} ) |
| 450 |
.find( '.ve-ce-resizableNode-neHandle' ) |
| 451 |
.css( { marginRight: -width } ); |
| 452 |
this.$resizeHandles |
| 453 |
.find( '.ve-ce-resizableNode-swHandle' ) |
| 454 |
.css( { marginBottom: -height } ); |
| 455 |
this.$resizeHandles |
| 456 |
.find( '.ve-ce-resizableNode-seHandle' ) |
| 457 |
.css( { |
| 458 |
marginRight: -width, |
| 459 |
marginBottom: -height |
| 460 |
} ); |
| 461 |
}; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
ve.ce.ResizableNode.prototype.setResizableHandlesPosition = function () { |
| 467 |
if ( !this.isResizable() ) { |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
const offset = this.getResizableOffset(); |
| 472 |
|
| 473 |
this.$resizeHandles.css( { |
| 474 |
top: offset.top, |
| 475 |
left: offset.left |
| 476 |
} ); |
| 477 |
}; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
ve.ce.ResizableNode.prototype.onDocumentMouseMove = function ( e ) { |
| 486 |
const diff = {}; |
| 487 |
let dimensions = { |
| 488 |
width: 0, |
| 489 |
height: 0, |
| 490 |
top: this.resizeInfo.top, |
| 491 |
left: this.resizeInfo.left |
| 492 |
}; |
| 493 |
|
| 494 |
if ( this.resizing ) { |
| 495 |
|
| 496 |
switch ( this.resizeInfo.handle ) { |
| 497 |
case 'se': |
| 498 |
diff.x = e.screenX - this.resizeInfo.mouseX; |
| 499 |
diff.y = e.screenY - this.resizeInfo.mouseY; |
| 500 |
break; |
| 501 |
case 'nw': |
| 502 |
diff.x = this.resizeInfo.mouseX - e.screenX; |
| 503 |
diff.y = this.resizeInfo.mouseY - e.screenY; |
| 504 |
break; |
| 505 |
case 'ne': |
| 506 |
diff.x = e.screenX - this.resizeInfo.mouseX; |
| 507 |
diff.y = this.resizeInfo.mouseY - e.screenY; |
| 508 |
break; |
| 509 |
case 'sw': |
| 510 |
diff.x = this.resizeInfo.mouseX - e.screenX; |
| 511 |
diff.y = e.screenY - this.resizeInfo.mouseY; |
| 512 |
break; |
| 513 |
} |
| 514 |
|
| 515 |
dimensions = this.model.getScalable().getBoundedDimensions( { |
| 516 |
width: this.resizeInfo.width + diff.x, |
| 517 |
height: this.resizeInfo.height + diff.y |
| 518 |
}, e.shiftKey && this.snapToGrid ); |
| 519 |
|
| 520 |
|
| 521 |
switch ( this.resizeInfo.handle ) { |
| 522 |
case 'ne': |
| 523 |
dimensions.top = this.resizeInfo.top + |
| 524 |
( this.resizeInfo.height - dimensions.height ); |
| 525 |
break; |
| 526 |
case 'sw': |
| 527 |
dimensions.left = this.resizeInfo.left + |
| 528 |
( this.resizeInfo.width - dimensions.width ); |
| 529 |
break; |
| 530 |
case 'nw': |
| 531 |
dimensions.top = this.resizeInfo.top + |
| 532 |
( this.resizeInfo.height - dimensions.height ); |
| 533 |
dimensions.left = this.resizeInfo.left + |
| 534 |
( this.resizeInfo.width - dimensions.width ); |
| 535 |
break; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
this.$resizeHandles.css( dimensions ); |
| 540 |
this.emit( 'resizing', { |
| 541 |
width: dimensions.width, |
| 542 |
height: dimensions.height |
| 543 |
} ); |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
ve.ce.ResizableNode.prototype.onDocumentMouseUp = function () { |
| 553 |
const width = this.$resizeHandles.outerWidth(), |
| 554 |
height = this.$resizeHandles.outerHeight(); |
| 555 |
|
| 556 |
this.$resizeHandles.removeClass( 've-ce-resizableNode-handles-resizing' ); |
| 557 |
$( this.getElementDocument() ).off( '.ve-ce-resizableNode' ); |
| 558 |
this.resizing = false; |
| 559 |
this.root.getSurface().resizing = false; |
| 560 |
this.hideSizeLabel(); |
| 561 |
|
| 562 |
|
| 563 |
const attrChanges = this.getAttributeChanges( width, height ); |
| 564 |
if ( !ve.isEmptyObject( attrChanges ) ) { |
| 565 |
this.resizableSurface.getModel().getFragment().changeAttributes( attrChanges ); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
this.root.getSurface().getSurface().getContext().updateDimensions(); |
| 571 |
|
| 572 |
this.emit( 'resizeEnd' ); |
| 573 |
}; |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
ve.ce.ResizableNode.prototype.getAttributeChanges = function ( width, height ) { |
| 583 |
const attrChanges = {}, |
| 584 |
currentDimensions = this.model.getCurrentDimensions(); |
| 585 |
|
| 586 |
if ( currentDimensions.width !== width ) { |
| 587 |
attrChanges.width = width; |
| 588 |
} |
| 589 |
if ( currentDimensions.height !== height ) { |
| 590 |
attrChanges.height = height; |
| 591 |
} |
| 592 |
return attrChanges; |
| 593 |
}; |