| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 65 |
const metaElement = document.querySelector( 'meta[name="theme-color"]' ); |
| 66 |
this.originalThemeColor = metaElement ? metaElement.getAttribute( 'content' ) : null; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
this.thumbnailWidthCalculator = new ThumbnailWidthCalculator(); |
| 73 |
|
| 74 |
this.$main = $( '<div>' ) |
| 75 |
.addClass( 'mw-mmv-main' ); |
| 76 |
|
| 77 |
|
| 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 |
|
| 87 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 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 |
|
| 153 |
|
| 154 |
open() { |
| 155 |
this.empty(); |
| 156 |
this.attach(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
attach( parentId ) { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if ( !this.attached ) { |
| 171 |
$( window ).scrollTop( 0 ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
this.$postDiv.css( 'top', `${ $( window ).height() - 83 }px` ); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if ( this.currentlyAttached ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
this.setThemeColor( '#000000' ); |
| 187 |
|
| 188 |
this.handleEvent( 'keyup', ( e ) => { |
| 189 |
if ( e.keyCode === 27 && !( e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) ) { |
| 190 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
unattach() { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
$( document ).trigger( $.Event( 'mmv-close' ) ) |
| 265 |
.off( 'fullscreenchange.lip' ); |
| 266 |
|
| 267 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 332 |
|
| 333 |
resetInteractionTimer() { |
| 334 |
clearTimeout( this.interactionTimer ); |
| 335 |
this.interactionTimer = setTimeout( () => { |
| 336 |
this.userInactive(); |
| 337 |
}, 3000 ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
userInactive() { |
| 345 |
this.$main.addClass( 'user-inactive' ); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
userActivity() { |
| 353 |
this.$main.removeClass( 'user-inactive' ); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 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 |
|
| 383 |
|
| 384 |
fullscreenChange() { |
| 385 |
|
| 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 |
|
| 399 |
this.unattach(); |
| 400 |
} else if ( this.fullscreenButtonJustPressed ) { |
| 401 |
this.fullscreenButtonJustPressed = false; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if ( !this.currentlyAttached ) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
if ( this.isFullscreen ) { |
| 411 |
this.userInactive(); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
this.$main.trigger( $.Event( 'mmv-resize-end' ) ); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 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 |
|
| 444 |
this.resetInteractionTimer(); |
| 445 |
this.userActivity(); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
mousemove( e ) { |
| 455 |
|
| 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 |
|
| 480 |
|
| 481 |
|
| 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 |
|
| 498 |
|
| 499 |
|
| 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; |