Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/contexts/ve.ui.DesktopContext.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface DesktopContext class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Context menu and inspectors.
9 *
10 * @class
11 * @extends ve.ui.LinearContext
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface
15 * @param {Object} [config] Configuration options
16 * @param {jQuery} [config.$popupContainer] Clipping container for context popup
17 * @param {number} [config.popupPadding=10] Padding between popup and $popupContainer, can be negative
18 */
19 ve.ui.DesktopContext = function VeUiDesktopContext( surface, config ) {
20 config = config || {};
21
22 // Parent constructor
23 ve.ui.DesktopContext.super.apply( this, arguments );
24
25 // Properties
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 // Events
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 // Initialization
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 /* Inheritance */
65
66 OO.inheritClass( ve.ui.DesktopContext, ve.ui.LinearContext );
67
68 /* Methods */
69
70 /**
71 * @inheritdoc
72 */
73 ve.ui.DesktopContext.prototype.afterContextChange = function () {
74 // Parent method
75 ve.ui.DesktopContext.super.prototype.afterContextChange.call( this );
76
77 // Bypass while dragging
78 if ( this.suppressed ) {
79 return;
80 }
81 };
82
83 /**
84 * Handle context suppression event.
85 */
86 ve.ui.DesktopContext.prototype.onSuppress = function () {
87 this.suppressed = true;
88 if ( this.isVisible() ) {
89 if ( !this.isEmpty() ) {
90 // Change state: menu -> closed
91 this.toggleMenu( false );
92 this.toggle( false );
93 } else if ( this.inspector ) {
94 // Change state: inspector -> closed
95 this.inspector.close();
96 }
97 }
98 };
99
100 /**
101 * Handle context unsuppression event.
102 */
103 ve.ui.DesktopContext.prototype.onUnsuppress = function () {
104 this.suppressed = false;
105
106 if ( this.isInspectable() ) {
107 // Change state: closed -> menu
108 this.toggleMenu( true );
109 this.toggle( true );
110 }
111 };
112
113 /**
114 * Handle cursor position change event.
115 */
116 ve.ui.DesktopContext.prototype.onPosition = function () {
117 if ( this.isVisible() ) {
118 this.updateDimensionsDebounced();
119 }
120 };
121
122 /**
123 * @inheritdoc
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 * @inheritdoc
135 */
136 ve.ui.DesktopContext.prototype.onInspectorOpening = function () {
137 ve.ui.DesktopContext.super.prototype.onInspectorOpening.apply( this, arguments );
138 // Resize the popup before opening so the body height of the window is measured correctly
139 this.setPopupSizeAndPosition();
140 };
141
142 /**
143 * Handle inspector resize events
144 */
145 ve.ui.DesktopContext.prototype.onInspectorResize = function () {
146 this.updateDimensionsDebounced();
147 };
148
149 /**
150 * @inheritdoc
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 // Parent method
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 // updateDimensionsDebounced is not necessary here and causes a movement flicker
177 this.updateDimensions();
178 } else if ( this.inspector ) {
179 this.inspector.close();
180 }
181
182 return promise;
183 };
184
185 /**
186 * @inheritdoc
187 */
188 ve.ui.DesktopContext.prototype.updateDimensions = function () {
189 const $container = this.inspector ? this.inspector.$frame : this.$group;
190
191 // Parent method
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 // Selection when the inspector was opened. Used to stop the context from
202 // jumping when an inline selection expands, e.g. to cover a long word
203 let startingSelection;
204 if (
205 !focusedNode && this.inspector && this.inspector.initialFragment &&
206 // Don't use initial selection if it comes from another document,
207 // e.g. the fake document used in source mode.
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 // If !boundingRect, the surface apparently isn't selected.
227 // This shouldn't happen because the context is only supposed to be
228 // displayed in response to a selection, but it sometimes does happen due
229 // to browser weirdness.
230 // Skip updating the cursor position, but still update the width and height.
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 // Embedded context position depends on directionality
241 position = {
242 x: rtl ? boundingRect.left : boundingRect.right,
243 y: boundingRect.top
244 };
245 this.popup.setAlignment( 'backwards' );
246 } else {
247 // Position the context underneath the center of the node
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 // The selection is text or an inline focused node
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 // If the middle position is within the end rect, use it
265 position = {
266 x: middle,
267 y: boundingRect.bottom
268 };
269 } else {
270 // ..otherwise use the side of the end rect
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 * Handle window scroll events
301 *
302 * @param {jQuery.Event} e Scroll event
303 */
304 ve.ui.DesktopContext.prototype.onWindowScroll = function () {
305 this.setPopupSizeAndPosition( true );
306 };
307
308 /**
309 * Check if the context menu for current content is embeddable.
310 *
311 * @return {boolean} Context menu is embeddable
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 * Apply the popup's size and position, within the bounds of the viewport
327 *
328 * @param {boolean} [repositionOnly] Reposition the popup only
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 // viewport can be null if the surface is not attached
340 return;
341 }
342
343 const margin = 10,
344 minimumVisibleHeight = 100;
345
346 if ( this.popup.hasAnchor() ) {
347 // Reserve space for the anchor and one line of text
348 // ('40' is arbitrary and has been picked by experimentation)
349 viewport.top += 40;
350 viewport.height -= 40;
351 }
352
353 if ( this.position ) {
354 // Float the content if it's bigger than the viewport. Exactly how /
355 // whether it should be floated is situational, so this is a
356 // preliminary determination. Checks below might cancel the float.
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 // PopupWidget normally is clippable, suppress that to be able to resize and scroll it into view.
407 // Needs to be repeated before every call, as it resets itself when the popup is shown or hidden.
408 this.popup.toggleClipping( false );
409
410 // We want to stop the popup from possibly being bigger than the viewport (T114614),
411 // as that can result in situations where it's impossible to reach parts
412 // of the popup. Limiting it to the window height would ignore toolbars
413 // and the find-replace dialog and suchlike. We can't use getViewportDimensions
414 // as that doesn't account for the surface height "growing" when we scroll (T304847).
415 const maxSurfaceHeight = this.surface.$scrollContainer.height() -
416 this.surface.getPadding().top -
417 // Allow room for callout and cursor above the context
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 * @inheritdoc
427 */
428 ve.ui.DesktopContext.prototype.destroy = function () {
429 // Hide, so a debounced updateDimensions does nothing
430 this.toggle( false );
431 // Disconnect
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 // Popups bind scroll events if they're in positioning mode, so make sure that's disabled
440 this.popup.togglePositioning( false );
441
442 // Parent method
443 return ve.ui.DesktopContext.super.prototype.destroy.call( this );
444 };