| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const HtmlUtils = require( '../mmv.HtmlUtils.js' ); |
| 19 |
const ThumbnailWidthCalculator = require( '../mmv.ThumbnailWidthCalculator.js' ); |
| 20 |
const UiElement = require( './mmv.ui.js' ); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class Canvas extends UiElement { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
constructor( $container, $imageWrapper, $mainWrapper ) { |
| 34 |
super( $container ); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
this.dialogOpen = false; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
this.thumbnailWidthCalculator = new ThumbnailWidthCalculator(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
this.$imageDiv = $( '<div>' ) |
| 54 |
.addClass( 'mw-mmv-image' ); |
| 55 |
|
| 56 |
this.$imageDiv.appendTo( this.$container ); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
this.$imageWrapper = $imageWrapper; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
this.$mainWrapper = $mainWrapper; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
this.imageRawMetadata = null; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
empty() { |
| 87 |
this.$imageDiv.addClass( 'empty' ).removeClass( 'error' ); |
| 88 |
|
| 89 |
this.$imageDiv.empty(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
set( imageRawMetadata, $imageElement ) { |
| 102 |
this.$imageDiv.removeClass( 'empty' ); |
| 103 |
|
| 104 |
this.imageRawMetadata = imageRawMetadata; |
| 105 |
this.$image = $imageElement; |
| 106 |
this.setUpImageClick(); |
| 107 |
|
| 108 |
this.$imageDiv.html( this.$image ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
setImageAndMaxDimensions( thumbnail, imageElement, imageWidths ) { |
| 120 |
const $image = $( imageElement ); |
| 121 |
|
| 122 |
|
| 123 |
if ( thumbnail.width > imageWidths.cssWidth ) { |
| 124 |
imageElement.width = imageWidths.cssWidth; |
| 125 |
imageElement.height = imageWidths.cssHeight; |
| 126 |
} |
| 127 |
|
| 128 |
if ( !this.$image.is( imageElement ) ) { |
| 129 |
this.$image.replaceWith( $image ); |
| 130 |
this.$image = $image; |
| 131 |
|
| 132 |
|
| 133 |
this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen ); |
| 134 |
|
| 135 |
this.setUpImageClick(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
handleDialogEvent( e ) { |
| 145 |
switch ( e.type ) { |
| 146 |
case 'mmv-download-opened': |
| 147 |
this.downloadOpen = true; |
| 148 |
break; |
| 149 |
case 'mmv-download-closed': |
| 150 |
this.downloadOpen = false; |
| 151 |
break; |
| 152 |
case 'mmv-reuse-opened': |
| 153 |
this.reuseOpen = true; |
| 154 |
break; |
| 155 |
case 'mmv-reuse-closed': |
| 156 |
this.reuseOpen = false; |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
this.dialogOpen = this.reuseOpen || this.downloadOpen; |
| 161 |
this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
setUpImageClick() { |
| 173 |
this.handleEvent( 'mmv-reuse-opened', this.handleDialogEvent.bind( this ) ); |
| 174 |
this.handleEvent( 'mmv-reuse-closed', this.handleDialogEvent.bind( this ) ); |
| 175 |
this.handleEvent( 'mmv-download-opened', this.handleDialogEvent.bind( this ) ); |
| 176 |
this.handleEvent( 'mmv-download-closed', this.handleDialogEvent.bind( this ) ); |
| 177 |
|
| 178 |
this.$image.on( 'click.mmv-canvas', ( e ) => { |
| 179 |
|
| 180 |
|
| 181 |
if ( !this.dialogOpen && |
| 182 |
|
| 183 |
this.$container.closest( '.metadata-panel-is-open' ).length === 0 ) { |
| 184 |
e.stopPropagation(); |
| 185 |
$( document ).trigger( 'mmv-viewfile' ); |
| 186 |
} |
| 187 |
} ); |
| 188 |
|
| 189 |
|
| 190 |
this.$image.on( 'mousedown.mmv-canvas', ( e ) => { |
| 191 |
if ( e.which === 3 && !this.downloadOpen ) { |
| 192 |
$( document ).trigger( 'mmv-download-open', e ); |
| 193 |
e.stopPropagation(); |
| 194 |
} |
| 195 |
} ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
attach() { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
$( window ).on( 'resize.mmv-canvas', mw.util.debounce( () => { |
| 210 |
this.$mainWrapper.trigger( $.Event( 'mmv-resize-end' ) ); |
| 211 |
}, 100 ) ); |
| 212 |
|
| 213 |
this.$imageWrapper.on( 'click.mmv-canvas', () => { |
| 214 |
if ( this.$container.closest( '.metadata-panel-is-open' ).length > 0 ) { |
| 215 |
this.$mainWrapper.trigger( 'mmv-panel-close-area-click' ); |
| 216 |
} |
| 217 |
} ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
unattach() { |
| 224 |
this.clearEvents(); |
| 225 |
|
| 226 |
$( window ).off( 'resize.mmv-canvas' ); |
| 227 |
|
| 228 |
this.$imageWrapper.off( 'click.mmv-canvas' ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
maybeDisplayPlaceholder( size, $imagePlaceholder, imageWidths ) { |
| 242 |
¸ |
| 243 |
let targetWidth = size.width; |
| 244 |
let targetHeight = size.height; |
| 245 |
|
| 246 |
|
| 247 |
if ( size.width > imageWidths.cssWidth ) { |
| 248 |
targetWidth = imageWidths.cssWidth; |
| 249 |
targetHeight = imageWidths.cssHeight; |
| 250 |
} |
| 251 |
|
| 252 |
const blowupFactor = targetWidth / $imagePlaceholder.width(); |
| 253 |
|
| 254 |
if ( blowupFactor > Canvas.MAX_BLOWUP_FACTOR ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
$imagePlaceholder.width( targetWidth ); |
| 259 |
$imagePlaceholder.height( targetHeight ); |
| 260 |
this.set( this.imageRawMetadata, $imagePlaceholder.show() ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
showError( error ) { |
| 269 |
const $retryLink = $( '<a>' ).addClass( 'mw-mmv-retry-link' ).text( |
| 270 |
mw.msg( 'multimediaviewer-thumbnail-error-retry' ) ); |
| 271 |
|
| 272 |
this.$imageDiv.empty() |
| 273 |
.addClass( 'error' ) |
| 274 |
.append( |
| 275 |
$( '<div>' ).addClass( 'error-box' ).append( |
| 276 |
$( '<div>' ).addClass( 'mw-mmv-error-text' ).text( |
| 277 |
mw.msg( 'multimediaviewer-thumbnail-error' ) |
| 278 |
) |
| 279 |
).append( |
| 280 |
$( '<div>' ).addClass( 'mw-mmv-error-description' ).append( |
| 281 |
mw.msg( 'multimediaviewer-thumbnail-error-description', |
| 282 |
HtmlUtils.jqueryToHtml( $retryLink ), |
| 283 |
error |
| 284 |
) |
| 285 |
) |
| 286 |
) |
| 287 |
); |
| 288 |
this.$imageDiv.find( '.mw-mmv-retry-link' ).on( 'click', () => { |
| 289 |
location.reload(); |
| 290 |
} ); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
getDimensions() { |
| 299 |
const $window = $( window ); |
| 300 |
|
| 301 |
const $aboveFold = $( '.mw-mmv-above-fold' ); |
| 302 |
const isFullscreened = !!$aboveFold.closest( '.jq-fullscreened' ).length; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
const availableWidth = $window.width(); |
| 307 |
const availableHeight = $window.height() - ( isFullscreened ? 0 : $aboveFold.outerHeight() ); |
| 308 |
|
| 309 |
return { |
| 310 |
width: availableWidth, |
| 311 |
height: availableHeight |
| 312 |
}; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
getLightboxImageWidths( image ) { |
| 322 |
const thumb = image.thumbnail; |
| 323 |
const canvasDimensions = this.getDimensions(); |
| 324 |
|
| 325 |
return this.thumbnailWidthCalculator.calculateWidths( |
| 326 |
canvasDimensions.width, |
| 327 |
canvasDimensions.height, |
| 328 |
image.originalWidth || thumb.width, |
| 329 |
image.originalHeight || thumb.height |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
getCurrentImageWidths() { |
| 339 |
return this.getLightboxImageWidths( this.imageRawMetadata ); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
Canvas.MAX_BLOWUP_FACTOR = 11; |
| 350 |
|
| 351 |
module.exports = Canvas; |