Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/mmv.lightboxinterface.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const Canvas = require( './ui/mmv.ui.canvas.js' );
19 const CanvasButtons = require( './ui/mmv.ui.canvasButtons.js' );
20 const MetadataPanel = require( './ui/mmv.ui.metadataPanel.js' );
21 const ThumbnailWidthCalculator = require( './mmv.ThumbnailWidthCalculator.js' );
22 const UiElement = require( './ui/mmv.ui.js' );
23
24 /** Proxy for a Dialog. Initialises and attaches the dialog upon first use. */
25 class DialogProxy extends UiElement {
26 constructor( eventName, initDialog ) {
27 super();
28 this.eventName = eventName;
29 this.initDialog = initDialog;
30 }
31
32 attach() {
33 this.handleEvent( this.eventName, this.handleOpenCloseClick.bind( this ) );
34 }
35
36 set( ...setValues ) {
37 this.setValues = setValues;
38 }
39
40 handleOpenCloseClick() {
41 mw.loader.using( 'mmv.ui.reuse', ( req ) => {
42 this.unattach();
43 const dialog = this.initDialog( req );
44 dialog.attach();
45 dialog.set( ...this.setValues );
46 dialog.handleOpenCloseClick();
47 } );
48 }
49
50 closeDialog() {}
51 }
52
53 /**
54 * Represents the main interface of the lightbox
55 */
56 class LightboxInterface extends UiElement {
57
58 constructor() {
59 const $wrapper = $( '<div>' )
60 .addClass( 'mw-mmv-wrapper' );
61 super( $wrapper );
62 this.$wrapper = $wrapper;
63
64 // When opening we might override the theme-color, so remember the original value
65 const metaElement = document.querySelector( 'meta[name="theme-color"]' );
66 this.originalThemeColor = metaElement ? metaElement.getAttribute( 'content' ) : null;
67
68 /**
69 * @property {ThumbnailWidthCalculator}
70 * @private
71 */
72 this.thumbnailWidthCalculator = new ThumbnailWidthCalculator();
73
74 this.$main = $( '<div>' )
75 .addClass( 'mw-mmv-main' );
76
77 // I blame CSS for this
78 this.$innerWrapper = $( '<div>' )
79 .addClass( 'mw-mmv-image-inner-wrapper' );
80
81 this.$imageWrapper = $( '<div>' )
82 .addClass( 'mw-mmv-image-wrapper' )
83 .append( this.$innerWrapper );
84
85 this.$preDiv = $( '<div>' )
86 // The overlay has no-invert, so the interface overlaid
87 // on it must also have no-invert
88 .addClass( 'mw-mmv-pre-image mw-no-invert' );
89
90 this.$postDiv = $( '<div>' )
91 .addClass( 'mw-mmv-post-image' );
92
93 this.$aboveFold = $( '<div>' )
94 .addClass( 'mw-mmv-above-fold' );
95
96 this.$main.append(
97 this.$preDiv,
98 this.$imageWrapper,
99 this.$postDiv
100 );
101
102 this.$wrapper.append(
103 this.$main
104 );
105
106 this.setupCanvasButtons();
107
108 this.panel = new MetadataPanel( this.$postDiv, this.$aboveFold );
109 this.buttons = new CanvasButtons( this.$preDiv, this.$closeButton, this.$fullscreenButton );
110 this.canvas = new Canvas( this.$innerWrapper, this.$imageWrapper, this.$wrapper );
111
112 /** @property {DialogProxy|ReuseDialog} */
113 this.fileReuse = new DialogProxy( 'mmv-reuse-open', ( req ) => {
114 const { ReuseDialog } = req( 'mmv.ui.reuse' );
115 this.fileReuse = new ReuseDialog( this.$preDiv, this.buttons.$download );
116 return this.fileReuse;
117 } );
118 /** @property {DialogProxy|DownloadDialog} */
119 this.downloadDialog = new DialogProxy( 'mmv-download-open', ( req ) => {
120 const { DownloadDialog } = req( 'mmv.ui.reuse' );
121 this.downloadDialog = new DownloadDialog( this.$preDiv, this.buttons.$download );
122 return this.downloadDialog;
123 } );
124 }
125
126 /**
127 * Sets up the file reuse data in the DOM
128 *
129 * @param {ImageModel} image
130 * @param {string} caption
131 * @param {string} alt
132 */
133 setFileReuseData( image, caption, alt ) {
134 this.buttons.set( image );
135 this.fileReuse.set( image, caption, alt );
136 this.downloadDialog.set( image );
137 }
138
139 /**
140 * Empties the interface.
141 */
142 empty() {
143 this.panel.empty();
144 this.canvas.empty();
145 this.buttons.empty();
146
147 this.$main.addClass( 'metadata-panel-is-closed' )
148 .removeClass( 'metadata-panel-is-open' );
149 }
150
151 /**
152 * Opens the lightbox.
153 */
154 open() {
155 this.empty();
156 this.attach();
157 }
158
159 /**
160 * Attaches the interface to the DOM.
161 *
162 * @param {string} [parentId] parent id where we want to attach the UI. Defaults to document
163 * element, override is mainly used for testing.
164 */
165 attach( parentId ) {
166 // Advanced description needs to be below the fold when the lightbox opens
167 // regardless of what the scroll value was prior to opening the lightbox
168 // If the lightbox is already attached, it means we're doing prev/next, and
169 // we should avoid scrolling the panel
170 if ( !this.attached ) {
171 $( window ).scrollTop( 0 );
172 }
173
174 // Make sure that the metadata is going to be at the bottom when it appears
175 // 83 is the height of the top metadata area. Which can't be measured by
176 // reading the DOM at this point of the execution, unfortunately
177 this.$postDiv.css( 'top', `${ $( window ).height() - 83 }px` );
178
179 // Re-appending the same content can have nasty side-effects
180 // Such as the browser leaving fullscreen mode if the fullscreened element is part of it
181 if ( this.currentlyAttached ) {
182 return;
183 }
184
185 // Make sure devices set their theming to dark to match the background of the viewer
186 this.setThemeColor( '#000000' );
187
188 this.handleEvent( 'keyup', ( e ) => {
189 if ( e.keyCode === 27 && !( e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) {
190 // Escape button pressed
191 this.unattach();
192 }
193 } );
194
195 this.handleEvent( 'fullscreenchange.lip', () => {
196 this.fullscreenChange();
197 } );
198
199 this.handleEvent( 'keydown', ( e ) => {
200 this.keydown( e );
201 } );
202
203 this.handleEvent( 'touchstart', ( e ) => {
204 this.touchTap( e );
205 } );
206
207 // mousemove generates a ton of events, which is why we throttle it
208 this.handleEvent( 'mousemove.lip', mw.util.throttle( ( e ) => {
209 this.mousemove( e );
210 }, 100, true ) );
211
212 this.buttons.connect( this, {
213 next: [ 'emit', 'next' ],
214 prev: [ 'emit', 'prev' ]
215 } );
216
217 const $parent = $( parentId || document.body );
218
219 // Clean up fullscreen data left attached to the DOM
220 this.$main.removeClass( 'jq-fullscreened' ).removeClass( 'user-inactive' );
221 this.isFullscreen = false;
222
223 $parent
224 .append(
225 this.$wrapper
226 );
227 this.currentlyAttached = true;
228
229 this.panel.attach();
230
231 this.canvas.attach();
232
233 // cross-communication between panel and canvas, sort of
234 this.$postDiv.on( 'mmv-metadata-open.lip', () => {
235 this.$main.addClass( 'metadata-panel-is-open' )
236 .removeClass( 'metadata-panel-is-closed' );
237 } ).on( 'mmv-metadata-close.lip', () => {
238 this.$main.removeClass( 'metadata-panel-is-open' )
239 .addClass( 'metadata-panel-is-closed' );
240 } );
241 this.$wrapper.on( 'mmv-panel-close-area-click.lip', () => {
242 this.panel.scroller.toggle( 'down' );
243 } );
244
245 this.buttons.attach();
246
247 this.fileReuse.attach();
248 this.downloadDialog.attach();
249
250 this.attached = true;
251 }
252
253 /**
254 * Detaches the interface from the DOM.
255 *
256 * @fires MultimediaViewer#mmv-close
257 */
258 unattach() {
259 // We trigger this event on the document because unattach() can run
260 // when the interface is unattached
261 // We're calling this before cleaning up (below) the DOM, as that
262 // appears to have an impact on automatic scroll restoration (which
263 // might happen as a result of this being closed) in FF
264 $( document ).trigger( $.Event( 'mmv-close' ) )
265 .off( 'fullscreenchange.lip' );
266
267 // Has to happen first so that the scroller can freeze with visible elements
268 this.panel.unattach();
269
270 this.$wrapper.detach();
271
272 this.currentlyAttached = false;
273
274 this.buttons.unattach();
275
276 this.$postDiv.off( '.lip' );
277 this.$wrapper.off( 'mmv-panel-close-area-click.lip' );
278
279 this.fileReuse.unattach();
280 this.fileReuse.closeDialog();
281
282 this.downloadDialog.unattach();
283 this.downloadDialog.closeDialog();
284
285 // Canvas listens for events from dialogs, so should be unattached at the end
286 this.canvas.unattach();
287
288 this.clearEvents();
289
290 this.buttons.disconnect( this, {
291 next: [ 'emit', 'next' ],
292 prev: [ 'emit', 'prev' ]
293 } );
294
295 this.setThemeColor( this.originalThemeColor );
296
297 this.attached = false;
298 }
299
300 /**
301 * Exits fullscreen mode.
302 */
303 exitFullscreen() {
304 this.fullscreenButtonJustPressed = true;
305 if ( this.$main.get( 0 ) === document.fullscreenElement ) {
306 if ( document.exitFullscreen ) {
307 document.exitFullscreen();
308 }
309 }
310 this.isFullscreen = false;
311 this.$main.removeClass( 'jq-fullscreened' );
312 clearTimeout( this.interactionTimer );
313 this.userActivity();
314 }
315
316 /**
317 * Enters fullscreen mode.
318 */
319 enterFullscreen() {
320 const el = this.$main.get( 0 );
321 if ( el.requestFullscreen ) {
322 el.requestFullscreen();
323 }
324 this.isFullscreen = true;
325 this.$main.addClass( 'jq-fullscreened' );
326 this.resetInteractionTimer();
327 this.userInactive();
328 }
329
330 /**
331 * Interrupt and reset the 3sec delay to hide the controls
332 */
333 resetInteractionTimer() {
334 clearTimeout( this.interactionTimer );
335 this.interactionTimer = setTimeout( () => {
336 this.userInactive();
337 }, 3000 );
338 }
339
340 /**
341 * In fullscreen, hide the mouse cursor and the controls
342 * Called from resetInteractionTimer()
343 */
344 userInactive() {
345 this.$main.addClass( 'user-inactive' );
346 }
347
348 /**
349 * In fullscreen, show the mouse cursor and the controls
350 * Call this after any interactivity
351 */
352 userActivity() {
353 this.$main.removeClass( 'user-inactive' );
354 }
355
356 /**
357 * Setup for canvas navigation buttons
358 */
359 setupCanvasButtons() {
360 this.$closeButton = $( '<button>' )
361 .addClass( 'cdx-button cdx-button--icon-only mw-mmv-button mw-mmv-close' )
362 .prop( 'title', mw.msg( 'multimediaviewer-close-popup-text' ) )
363 .append( $( '<span>' ).addClass( 'mw-mmv-icon' ) )
364 .on( 'click', () => {
365 this.unattach();
366 } );
367
368 this.$fullscreenButton = $( '<button>' )
369 .addClass( 'cdx-button cdx-button--icon-only mw-mmv-button mw-mmv-fullscreen' )
370 .prop( 'title', mw.msg( 'multimediaviewer-fullscreen-popup-text' ) )
371 .append( $( '<span>' ).addClass( 'mw-mmv-icon' ) )
372 .on( 'click', () => {
373 if ( this.isFullscreen ) {
374 this.exitFullscreen();
375 } else {
376 this.enterFullscreen();
377 }
378 } );
379 }
380
381 /**
382 * Handle a fullscreen change event.
383 */
384 fullscreenChange() {
385 // eslint-disable-next-line compat/compat
386 this.isFullscreen = !!document.fullscreenElement;
387 if ( this.isFullscreen ) {
388 this.$fullscreenButton
389 .prop( 'title', mw.msg( 'multimediaviewer-defullscreen-popup-text' ) )
390 .attr( 'alt', mw.msg( 'multimediaviewer-defullscreen-popup-text' ) );
391 } else {
392 this.$fullscreenButton
393 .prop( 'title', mw.msg( 'multimediaviewer-fullscreen-popup-text' ) )
394 .attr( 'alt', mw.msg( 'multimediaviewer-fullscreen-popup-text' ) );
395 }
396
397 if ( !this.fullscreenButtonJustPressed && !this.isFullscreen ) {
398 // Close the interface all the way if the user pressed 'esc'
399 this.unattach();
400 } else if ( this.fullscreenButtonJustPressed ) {
401 this.fullscreenButtonJustPressed = false;
402 }
403
404 // Fullscreen change events can happen after unattach(), in which
405 // case we shouldn't do anything UI-related
406 if ( !this.currentlyAttached ) {
407 return;
408 }
409
410 if ( this.isFullscreen ) {
411 this.userInactive();
412 }
413
414 // Some browsers only send resize events before toggling fullscreen, but not once the toggling is done
415 // This makes sure that the UI is properly resized after a fullscreen change
416 this.$main.trigger( $.Event( 'mmv-resize-end' ) );
417 }
418
419 /**
420 * Handles keydown events on the document
421 *
422 * @param {jQuery.Event} e The jQuery keypress event object
423 */
424 keydown( e ) {
425 const isRtl = $( document.body ).hasClass( 'rtl' );
426
427 if ( e.altKey || e.shiftKey || e.ctrlKey || e.metaKey ) {
428 return;
429 }
430 if ( e.key === 'ArrowLeft' ) {
431 this.emit( isRtl ? 'next' : 'prev' );
432 e.preventDefault();
433 } else if ( e.key === 'ArrowRight' ) {
434 this.emit( isRtl ? 'prev' : 'next' );
435 e.preventDefault();
436 } else if ( e.key === 'Home' ) {
437 this.emit( 'first' );
438 e.preventDefault();
439 } else if ( e.key === 'End' ) {
440 this.emit( 'last' );
441 e.preventDefault();
442 } else if ( this.isFullscreen ) {
443 // Any other key in fullscreen reveals the controls
444 this.resetInteractionTimer();
445 this.userActivity();
446 }
447 }
448
449 /**
450 * Handles mousemove events on the document
451 *
452 * @param {jQuery.Event} e The mousemove event object
453 */
454 mousemove( e ) {
455 // T77869 ignore fake mousemove events triggered by Chrome
456 if (
457 e &&
458 e.originalEvent &&
459 e.originalEvent.movementX === 0 &&
460 e.originalEvent.movementY === 0
461 ) {
462 return;
463 }
464
465 if ( this.isFullscreen ) {
466 this.resetInteractionTimer();
467 this.userActivity();
468 }
469 }
470
471 touchTap() {
472 if ( this.isFullscreen ) {
473 this.resetInteractionTimer();
474 this.userActivity();
475 }
476 }
477
478 /**
479 * Updates the next and prev buttons
480 *
481 * @param {boolean} showPrevNext Show prev/next button
482 */
483 updateControls( showPrevNext ) {
484 const prevNextTop = `${ ( this.$imageWrapper.height() - 60 ) / 2 }px`;
485
486 if ( this.isFullscreen ) {
487 this.$postDiv.css( 'top', '' );
488 } else {
489 this.$postDiv.css( 'top', this.$imageWrapper.height() );
490 }
491
492 this.buttons.setOffset( prevNextTop );
493 this.buttons.$nav.toggle( showPrevNext );
494 }
495
496 /**
497 * Update the theme-color of the document
498 *
499 * @param {string|null} color to set as theme-color or null to remove the theme-color
500 */
501 setThemeColor( color ) {
502 let metaElement = document.querySelector( 'meta[name="theme-color"]' );
503 if ( !metaElement ) {
504 metaElement = document.createElement( 'meta' );
505 metaElement.setAttribute( 'name', 'theme-color' );
506 document.head.appendChild( metaElement );
507 }
508 if ( color === null ) {
509 metaElement.remove();
510 } else {
511 this.originalThemeColor = metaElement.getAttribute( 'content' );
512 metaElement.setAttribute( 'content', color );
513 }
514 }
515 }
516
517 module.exports = LightboxInterface;