| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ui.DesktopContext = function VeUiDesktopContext( surface, config ) { |
| 20 |
config = config || {}; |
| 21 |
|
| 22 |
|
| 23 |
ve.ui.DesktopContext.super.apply( this, arguments ); |
| 24 |
|
| 25 |
|
| 26 |
this.popup = new OO.ui.PopupWidget( { |
| 27 |
hideWhenOutOfView: false, |
| 28 |
autoFlip: false, |
| 29 |
$container: config.$popupContainer || this.surface.$element, |
| 30 |
containerPadding: config.popupPadding |
| 31 |
} ); |
| 32 |
this.position = null; |
| 33 |
this.embeddable = null; |
| 34 |
this.boundingRect = null; |
| 35 |
this.transitioning = null; |
| 36 |
this.dimensions = null; |
| 37 |
this.suppressed = false; |
| 38 |
this.onWindowScrollDebounced = ve.debounce( this.onWindowScroll.bind( this ) ); |
| 39 |
this.onWindowResizeHandler = this.onPosition.bind( this ); |
| 40 |
this.$window = $( this.getElementWindow() ); |
| 41 |
|
| 42 |
|
| 43 |
this.surface.getView().connect( this, { |
| 44 |
relocationStart: 'onSuppress', |
| 45 |
relocationEnd: 'onUnsuppress', |
| 46 |
position: 'onPosition' |
| 47 |
} ); |
| 48 |
this.inspectors.connect( this, { |
| 49 |
resize: 'onInspectorResize' |
| 50 |
} ); |
| 51 |
this.$window.on( { |
| 52 |
resize: this.onWindowResizeHandler |
| 53 |
} ); |
| 54 |
this.surface.$scrollListener[ 0 ].addEventListener( 'scroll', this.onWindowScrollDebounced, { passive: true } ); |
| 55 |
|
| 56 |
|
| 57 |
this.$element |
| 58 |
.addClass( 've-ui-desktopContext' ) |
| 59 |
.append( this.$focusTrapBefore, this.popup.$element, this.$focusTrapAfter ); |
| 60 |
this.$group.addClass( 've-ui-desktopContext-menu' ); |
| 61 |
this.popup.$body.append( this.$group, this.inspectors.$element ); |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
OO.inheritClass( ve.ui.DesktopContext, ve.ui.LinearContext ); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ve.ui.DesktopContext.prototype.afterContextChange = function () { |
| 74 |
|
| 75 |
ve.ui.DesktopContext.super.prototype.afterContextChange.call( this ); |
| 76 |
|
| 77 |
|
| 78 |
if ( this.suppressed ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
ve.ui.DesktopContext.prototype.onSuppress = function () { |
| 87 |
this.suppressed = true; |
| 88 |
if ( this.isVisible() ) { |
| 89 |
if ( !this.isEmpty() ) { |
| 90 |
|
| 91 |
this.toggleMenu( false ); |
| 92 |
this.toggle( false ); |
| 93 |
} else if ( this.inspector ) { |
| 94 |
|
| 95 |
this.inspector.close(); |
| 96 |
} |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ve.ui.DesktopContext.prototype.onUnsuppress = function () { |
| 104 |
this.suppressed = false; |
| 105 |
|
| 106 |
if ( this.isInspectable() ) { |
| 107 |
|
| 108 |
this.toggleMenu( true ); |
| 109 |
this.toggle( true ); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ve.ui.DesktopContext.prototype.onPosition = function () { |
| 117 |
if ( this.isVisible() ) { |
| 118 |
this.updateDimensionsDebounced(); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
ve.ui.DesktopContext.prototype.createInspectorWindowManager = function () { |
| 126 |
return new ve.ui.DesktopInspectorWindowManager( this.surface, { |
| 127 |
factory: ve.ui.windowFactory, |
| 128 |
overlay: this.surface.getLocalOverlay(), |
| 129 |
modal: false |
| 130 |
} ); |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ve.ui.DesktopContext.prototype.onInspectorOpening = function () { |
| 137 |
ve.ui.DesktopContext.super.prototype.onInspectorOpening.apply( this, arguments ); |
| 138 |
|
| 139 |
this.setPopupSizeAndPosition(); |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ve.ui.DesktopContext.prototype.onInspectorResize = function () { |
| 146 |
this.updateDimensionsDebounced(); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ve.ui.DesktopContext.prototype.toggle = function ( show ) { |
| 153 |
if ( this.transitioning ) { |
| 154 |
return this.transitioning; |
| 155 |
} |
| 156 |
show = show === undefined ? !this.visible : !!show; |
| 157 |
if ( show === this.visible ) { |
| 158 |
return ve.createDeferred().resolve().promise(); |
| 159 |
} |
| 160 |
|
| 161 |
this.transitioning = ve.createDeferred(); |
| 162 |
const promise = this.transitioning.promise(); |
| 163 |
|
| 164 |
|
| 165 |
ve.ui.DesktopContext.super.prototype.toggle.call( this, show ); |
| 166 |
this.popup.toggle( show ); |
| 167 |
|
| 168 |
this.transitioning.resolve(); |
| 169 |
this.transitioning = null; |
| 170 |
this.visible = show; |
| 171 |
|
| 172 |
if ( show ) { |
| 173 |
if ( this.inspector ) { |
| 174 |
this.inspector.updateSize(); |
| 175 |
} |
| 176 |
|
| 177 |
this.updateDimensions(); |
| 178 |
} else if ( this.inspector ) { |
| 179 |
this.inspector.close(); |
| 180 |
} |
| 181 |
|
| 182 |
return promise; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
ve.ui.DesktopContext.prototype.updateDimensions = function () { |
| 189 |
const $container = this.inspector ? this.inspector.$frame : this.$group; |
| 190 |
|
| 191 |
|
| 192 |
ve.ui.DesktopContext.super.prototype.updateDimensions.call( this ); |
| 193 |
|
| 194 |
if ( !this.isVisible() ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
const rtl = this.surface.getModel().getDocument().getDir() === 'rtl'; |
| 199 |
const surface = this.surface.getView(); |
| 200 |
const focusedNode = surface.getFocusedNode(); |
| 201 |
|
| 202 |
|
| 203 |
let startingSelection; |
| 204 |
if ( |
| 205 |
!focusedNode && this.inspector && this.inspector.initialFragment && |
| 206 |
|
| 207 |
|
| 208 |
this.inspector.getFragment() && |
| 209 |
this.inspector.getFragment().getDocument() === surface.getModel().getDocument() |
| 210 |
) { |
| 211 |
startingSelection = this.inspector.initialFragment.getSelection(); |
| 212 |
} |
| 213 |
const currentSelection = this.surface.getModel().getSelection(); |
| 214 |
const isTableSelection = ( startingSelection || currentSelection ) instanceof ve.dm.TableSelection; |
| 215 |
|
| 216 |
const boundingRect = isTableSelection ? |
| 217 |
surface.getSelection( startingSelection ).getTableBoundingRect() : |
| 218 |
surface.getSelection( startingSelection ).getSelectionBoundingRect(); |
| 219 |
|
| 220 |
this.$element.removeClass( 've-ui-desktopContext-embedded' ); |
| 221 |
|
| 222 |
let position; |
| 223 |
let middle; |
| 224 |
let embeddable = false; |
| 225 |
if ( !boundingRect ) { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
this.popup.toggleAnchor( true ); |
| 232 |
this.popup.setAlignment( 'center' ); |
| 233 |
} else if ( isTableSelection || ( focusedNode && !focusedNode.isContent() ) ) { |
| 234 |
embeddable = this.isEmbeddable() && |
| 235 |
boundingRect.height > this.$group.outerHeight() + 5 && |
| 236 |
boundingRect.width > this.$group.outerWidth() + 10; |
| 237 |
this.popup.toggleAnchor( !embeddable ); |
| 238 |
this.$element.toggleClass( 've-ui-desktopContext-embedded', !!embeddable ); |
| 239 |
if ( embeddable ) { |
| 240 |
|
| 241 |
position = { |
| 242 |
x: rtl ? boundingRect.left : boundingRect.right, |
| 243 |
y: boundingRect.top |
| 244 |
}; |
| 245 |
this.popup.setAlignment( 'backwards' ); |
| 246 |
} else { |
| 247 |
|
| 248 |
middle = ( boundingRect.left + boundingRect.right ) / 2; |
| 249 |
position = { |
| 250 |
x: middle, |
| 251 |
y: boundingRect.bottom |
| 252 |
}; |
| 253 |
this.popup.setAlignment( 'center' ); |
| 254 |
} |
| 255 |
} else { |
| 256 |
|
| 257 |
const startAndEndRects = surface.getSelection( startingSelection ).getSelectionStartAndEndRects(); |
| 258 |
if ( startAndEndRects ) { |
| 259 |
middle = ( boundingRect.left + boundingRect.right ) / 2; |
| 260 |
if ( |
| 261 |
( !rtl && startAndEndRects.end.right > middle ) || |
| 262 |
( rtl && startAndEndRects.end.left < middle ) |
| 263 |
) { |
| 264 |
|
| 265 |
position = { |
| 266 |
x: middle, |
| 267 |
y: boundingRect.bottom |
| 268 |
}; |
| 269 |
} else { |
| 270 |
|
| 271 |
position = { |
| 272 |
x: rtl ? startAndEndRects.end.left : startAndEndRects.end.right, |
| 273 |
y: startAndEndRects.end.bottom |
| 274 |
}; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
this.popup.toggleAnchor( true ); |
| 279 |
this.popup.setAlignment( 'center' ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( position ) { |
| 283 |
this.position = position; |
| 284 |
} |
| 285 |
if ( boundingRect ) { |
| 286 |
this.boundingRect = boundingRect; |
| 287 |
} |
| 288 |
this.embeddable = embeddable; |
| 289 |
this.dimensions = { |
| 290 |
width: $container.outerWidth( true ), |
| 291 |
height: $container.outerHeight( true ) |
| 292 |
}; |
| 293 |
|
| 294 |
this.setPopupSizeAndPosition(); |
| 295 |
|
| 296 |
return this; |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
ve.ui.DesktopContext.prototype.onWindowScroll = function () { |
| 305 |
this.setPopupSizeAndPosition( true ); |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
ve.ui.DesktopContext.prototype.isEmbeddable = function () { |
| 314 |
const sources = this.getRelatedSources(); |
| 315 |
|
| 316 |
for ( let i = 0, len = sources.length; i < len; i++ ) { |
| 317 |
if ( !sources[ i ].embeddable ) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
return true; |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
ve.ui.DesktopContext.prototype.setPopupSizeAndPosition = function ( repositionOnly ) { |
| 331 |
if ( !this.isVisible() ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
const surface = this.surface; |
| 336 |
const viewport = surface.getViewportDimensions(); |
| 337 |
|
| 338 |
if ( !viewport || !this.dimensions ) { |
| 339 |
|
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
const margin = 10, |
| 344 |
minimumVisibleHeight = 100; |
| 345 |
|
| 346 |
if ( this.popup.hasAnchor() ) { |
| 347 |
|
| 348 |
|
| 349 |
viewport.top += 40; |
| 350 |
viewport.height -= 40; |
| 351 |
} |
| 352 |
|
| 353 |
if ( this.position ) { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
let floating = |
| 358 |
( !this.embeddable && this.position.y + this.dimensions.height > viewport.bottom - margin ) || |
| 359 |
( this.embeddable && this.position.y < viewport.top + margin ); |
| 360 |
|
| 361 |
if ( floating ) { |
| 362 |
if ( this.embeddable ) { |
| 363 |
if ( this.boundingRect.bottom - viewport.top - minimumVisibleHeight < this.dimensions.height + margin ) { |
| 364 |
floating = false; |
| 365 |
this.$element.css( { |
| 366 |
left: this.position.x, |
| 367 |
top: this.position.y + this.boundingRect.height - this.dimensions.height - minimumVisibleHeight, |
| 368 |
bottom: '' |
| 369 |
} ); |
| 370 |
} else { |
| 371 |
this.$element.css( { |
| 372 |
left: this.position.x + viewport.left, |
| 373 |
top: this.surface.getPadding().top + margin, |
| 374 |
bottom: '' |
| 375 |
} ); |
| 376 |
} |
| 377 |
} else { |
| 378 |
if ( viewport.bottom - this.boundingRect.top - minimumVisibleHeight < this.dimensions.height + margin ) { |
| 379 |
floating = false; |
| 380 |
this.$element.css( { |
| 381 |
left: this.position.x, |
| 382 |
top: this.position.y, |
| 383 |
bottom: '' |
| 384 |
} ); |
| 385 |
} else { |
| 386 |
this.$element.css( { |
| 387 |
left: this.position.x + viewport.left, |
| 388 |
top: '', |
| 389 |
bottom: this.dimensions.height + margin |
| 390 |
} ); |
| 391 |
} |
| 392 |
} |
| 393 |
} else { |
| 394 |
this.$element.css( { |
| 395 |
left: this.position.x, |
| 396 |
top: this.position.y, |
| 397 |
bottom: '' |
| 398 |
} ); |
| 399 |
} |
| 400 |
|
| 401 |
this.$element.toggleClass( 've-ui-desktopContext-floating', !!floating ); |
| 402 |
this.popup.toggleAnchor( !floating && !this.embeddable ); |
| 403 |
} |
| 404 |
|
| 405 |
if ( !repositionOnly ) { |
| 406 |
|
| 407 |
|
| 408 |
this.popup.toggleClipping( false ); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
const maxSurfaceHeight = this.surface.$scrollContainer.height() - |
| 416 |
this.surface.getPadding().top - |
| 417 |
|
| 418 |
30; |
| 419 |
this.popup.setSize( this.dimensions.width, Math.min( this.dimensions.height, maxSurfaceHeight ) ); |
| 420 |
|
| 421 |
this.popup.scrollElementIntoView( { animate: false } ); |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
ve.ui.DesktopContext.prototype.destroy = function () { |
| 429 |
|
| 430 |
this.toggle( false ); |
| 431 |
|
| 432 |
this.surface.getView().disconnect( this ); |
| 433 |
this.surface.getModel().disconnect( this ); |
| 434 |
this.inspectors.disconnect( this ); |
| 435 |
this.$window.off( { |
| 436 |
resize: this.onWindowResizeHandler |
| 437 |
} ); |
| 438 |
this.surface.$scrollListener[ 0 ].removeEventListener( 'scroll', this.onWindowScrollDebounced ); |
| 439 |
|
| 440 |
this.popup.togglePositioning( false ); |
| 441 |
|
| 442 |
|
| 443 |
return ve.ui.DesktopContext.super.prototype.destroy.call( this ); |
| 444 |
}; |