2014-02-26 20:09:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the MediaWiki extension MediaViewer.
|
|
|
|
|
*
|
|
|
|
|
* MediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* MediaViewer is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
|
( function () {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
var C;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* UI component that contains the multimedia element to be displayed.
|
|
|
|
|
* This first version assumes an image but it can be extended to other
|
|
|
|
|
* media types (video, sound, presentation, etc.).
|
|
|
|
|
*
|
|
|
|
|
* @class mw.mmv.ui.Canvas
|
|
|
|
|
* @extends mw.mmv.ui.Element
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {jQuery} $container Canvas' container
|
|
|
|
|
* @param {jQuery} $imageWrapper
|
|
|
|
|
* @param {jQuery} $mainWrapper
|
|
|
|
|
*/
|
|
|
|
|
function Canvas( $container, $imageWrapper, $mainWrapper ) {
|
|
|
|
|
mw.mmv.ui.Element.call( this, $container );
|
|
|
|
|
|
2014-09-27 02:03:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* @property {boolean}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.dialogOpen = false;
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* @property {mw.mmv.ThumbnailWidthCalculator}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.thumbnailWidthCalculator = new mw.mmv.ThumbnailWidthCalculator();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Contains image.
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @property {jQuery}
|
|
|
|
|
*/
|
|
|
|
|
this.$imageDiv = $( '<div>' )
|
2014-03-31 21:33:12 +00:00
|
|
|
|
.addClass( 'mw-mmv-image' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
this.$imageDiv.appendTo( this.$container );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Container of canvas and controls, needed for canvas size calculations.
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @property {jQuery}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.$imageWrapper = $imageWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-26 01:26:49 +00:00
|
|
|
|
* Main container of image and metadata, needed to propagate events.
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @property {jQuery}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.$mainWrapper = $mainWrapper;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raw metadata of current image, needed for canvas size calculations.
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @property {mw.mmv.LightboxImage}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.imageRawMetadata = null;
|
|
|
|
|
}
|
2018-11-12 16:33:24 +00:00
|
|
|
|
OO.inheritClass( Canvas, mw.mmv.ui.Element );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
C = Canvas.prototype;
|
|
|
|
|
|
2014-04-02 01:58:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* Maximum blownup factor tolerated
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2020-06-26 10:45:20 +00:00
|
|
|
|
* @property {number} MAX_BLOWUP_FACTOR
|
2014-04-02 01:58:41 +00:00
|
|
|
|
* @static
|
|
|
|
|
*/
|
2014-03-04 01:58:27 +00:00
|
|
|
|
Canvas.MAX_BLOWUP_FACTOR = 11;
|
|
|
|
|
|
2014-04-02 01:58:41 +00:00
|
|
|
|
/**
|
|
|
|
|
* Blowup factor threshold at which blurring kicks in
|
2020-06-26 10:21:04 +00:00
|
|
|
|
*
|
2020-06-26 10:45:20 +00:00
|
|
|
|
* @property {number} BLUR_BLOWUP_FACTOR_THRESHOLD
|
2014-04-02 01:58:41 +00:00
|
|
|
|
* @static
|
|
|
|
|
*/
|
2014-03-04 01:58:27 +00:00
|
|
|
|
Canvas.BLUR_BLOWUP_FACTOR_THRESHOLD = 2;
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* Clears everything.
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.empty = function () {
|
2014-03-01 01:10:51 +00:00
|
|
|
|
this.$imageDiv.addClass( 'empty' ).removeClass( 'error' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
this.$imageDiv.empty();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2015-02-20 01:20:21 +00:00
|
|
|
|
* Sets image on the canvas; does not resize it to fit. This is used to make the placeholder
|
|
|
|
|
* image available; it will be resized and displayed by #maybeDisplayPlaceholder().
|
|
|
|
|
* FIXME maybeDisplayPlaceholder() receives the placeholder so it is very unclear why this
|
|
|
|
|
* is necessary at all (apart from setting the LightboxImage, which is used in size calculations).
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @param {mw.mmv.LightboxImage} imageRawMetadata
|
|
|
|
|
* @param {jQuery} $imageElement
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.set = function ( imageRawMetadata, $imageElement ) {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
this.$imageDiv.removeClass( 'empty' );
|
|
|
|
|
|
|
|
|
|
this.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
this.$image = $imageElement;
|
2014-09-04 21:50:14 +00:00
|
|
|
|
this.setUpImageClick();
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
this.$imageDiv.html( this.$image );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2015-02-20 01:20:21 +00:00
|
|
|
|
* Resizes image to the given dimensions and displays it on the canvas.
|
|
|
|
|
* This is used to display the actual image; it assumes set function was already called before.
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-03-04 01:58:27 +00:00
|
|
|
|
* @param {mw.mmv.model.Thumbnail} thumbnail thumbnail information
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
* @param {HTMLImageElement} imageElement
|
2014-03-04 01:58:27 +00:00
|
|
|
|
* @param {mw.mmv.model.ThumbnailWidth} imageWidths
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.setImageAndMaxDimensions = function ( thumbnail, imageElement, imageWidths ) {
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
var $image = $( imageElement );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
|
// we downscale larger images but do not scale up smaller ones, that would look ugly
|
|
|
|
|
if ( thumbnail.width > imageWidths.cssWidth ) {
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
imageElement.width = imageWidths.cssWidth;
|
2015-02-20 01:20:21 +00:00
|
|
|
|
imageElement.height = imageWidths.cssHeight;
|
2014-03-04 01:58:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
Make max-height computation more stable
Instead of setting the parent's height as max height of the
<img> element, find the first parent which has a non-automatic
height (that would be .mw-mmv-image-wrapper).
With the old structure, the height of the parent element could
be determined by the height of the image, which would then be
written back into the max-height of the image, messing up the
aspect ratio. I did not see this in the wild, but it was easy
to reproduce by changing the timing of the resize handler (in
particular, I tried to call the resize handler before loading
the new resolution, to make the UI more responsive, and ran
into this problem). This cannot happen anymore now.
This also fix a bug on some browsers (IE 10, maybe iOS Safari)
where the size of the image could be slightly larger than the
available space, and the bottom of the image was obscured by
the metadata panel. I am still not sure how exactly that
happened, but it was related to the <img> parents with automatic
heights having incorrect height. After making sure the <img>
has a max-height derived from an element with non-automatic
height, I cannot reproduce the bug on IE 10 anymore.
Change-Id: I193aefc42e6d6072717643659a9e4c0c8b7c7e93
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/698
Bug: 66244
2014-06-17 21:16:26 +00:00
|
|
|
|
if ( !this.$image.is( imageElement ) ) { // http://bugs.jquery.com/ticket/4087
|
|
|
|
|
this.$image.replaceWith( $image );
|
|
|
|
|
this.$image = $image;
|
2014-09-04 21:50:14 +00:00
|
|
|
|
|
2014-09-24 23:19:03 +00:00
|
|
|
|
// Since the image element got replaced, we need to rescue the dialog-open class.
|
|
|
|
|
this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen );
|
|
|
|
|
|
2014-09-04 21:50:14 +00:00
|
|
|
|
this.setUpImageClick();
|
2014-02-26 20:09:37 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-12 18:05:13 +00:00
|
|
|
|
/**
|
2014-09-19 18:34:18 +00:00
|
|
|
|
* Handles a "dialog open/close" event from dialogs on the page.
|
2016-12-14 13:09:10 +00:00
|
|
|
|
*
|
|
|
|
|
* @param {jQuery.Event} e
|
2014-09-12 18:05:13 +00:00
|
|
|
|
*/
|
2014-09-19 18:34:18 +00:00
|
|
|
|
C.handleDialogEvent = function ( e ) {
|
|
|
|
|
switch ( e.type ) {
|
|
|
|
|
case 'mmv-download-opened':
|
|
|
|
|
this.downloadOpen = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'mmv-download-closed':
|
|
|
|
|
this.downloadOpen = false;
|
|
|
|
|
break;
|
|
|
|
|
case 'mmv-reuse-opened':
|
|
|
|
|
this.reuseOpen = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'mmv-reuse-closed':
|
|
|
|
|
this.reuseOpen = false;
|
|
|
|
|
break;
|
2014-07-23 23:00:37 +00:00
|
|
|
|
case 'mmv-options-opened':
|
|
|
|
|
this.optionsOpen = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'mmv-options-closed':
|
|
|
|
|
this.optionsOpen = false;
|
|
|
|
|
break;
|
2014-09-19 18:34:18 +00:00
|
|
|
|
}
|
2014-09-12 18:05:13 +00:00
|
|
|
|
|
2014-07-23 23:00:37 +00:00
|
|
|
|
this.dialogOpen = this.reuseOpen || this.downloadOpen || this.optionsOpen;
|
2014-09-19 18:34:18 +00:00
|
|
|
|
this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen );
|
2014-09-12 18:05:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-09-04 21:50:14 +00:00
|
|
|
|
/**
|
2014-09-11 17:39:19 +00:00
|
|
|
|
* Registers click listener on the image.
|
2014-09-04 21:50:14 +00:00
|
|
|
|
*/
|
|
|
|
|
C.setUpImageClick = function () {
|
2014-09-12 18:05:13 +00:00
|
|
|
|
var canvas = this;
|
|
|
|
|
|
2019-01-03 13:47:16 +00:00
|
|
|
|
this.handleEvent( 'mmv-reuse-opened', this.handleDialogEvent.bind( this ) );
|
|
|
|
|
this.handleEvent( 'mmv-reuse-closed', this.handleDialogEvent.bind( this ) );
|
|
|
|
|
this.handleEvent( 'mmv-download-opened', this.handleDialogEvent.bind( this ) );
|
|
|
|
|
this.handleEvent( 'mmv-download-closed', this.handleDialogEvent.bind( this ) );
|
|
|
|
|
this.handleEvent( 'mmv-options-opened', this.handleDialogEvent.bind( this ) );
|
|
|
|
|
this.handleEvent( 'mmv-options-closed', this.handleDialogEvent.bind( this ) );
|
2014-09-12 18:05:13 +00:00
|
|
|
|
|
2014-11-11 15:21:36 +00:00
|
|
|
|
this.$image.on( 'click.mmv-canvas', function ( e ) {
|
2014-11-22 02:46:51 +00:00
|
|
|
|
// ignore clicks if the metadata panel or one of the dialogs is open - assume the intent is to
|
|
|
|
|
// close it in this case; that will be handled elsewhere
|
|
|
|
|
if (
|
2016-07-18 13:49:27 +00:00
|
|
|
|
!canvas.dialogOpen &&
|
2014-11-22 02:46:51 +00:00
|
|
|
|
// FIXME a UI component should not know about its parents
|
2016-07-18 13:49:27 +00:00
|
|
|
|
canvas.$container.closest( '.metadata-panel-is-open' ).length === 0
|
2014-11-22 02:46:51 +00:00
|
|
|
|
) {
|
2014-11-11 15:21:36 +00:00
|
|
|
|
e.stopPropagation(); // don't let $imageWrapper handle this
|
2015-01-23 12:48:27 +00:00
|
|
|
|
mw.mmv.actionLogger.log( 'view-original-file' ).always( function () {
|
2014-11-11 15:21:36 +00:00
|
|
|
|
$( document ).trigger( 'mmv-viewfile' );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
} );
|
2015-01-01 02:24:32 +00:00
|
|
|
|
|
2014-12-31 08:11:26 +00:00
|
|
|
|
// open the download panel on right clicking the image
|
2015-01-01 02:24:32 +00:00
|
|
|
|
this.$image.on( 'mousedown.mmv-canvas', function ( e ) {
|
|
|
|
|
if ( e.which === 3 ) {
|
|
|
|
|
mw.mmv.actionLogger.log( 'right-click-image' );
|
2014-12-31 08:11:26 +00:00
|
|
|
|
if ( !canvas.downloadOpen ) {
|
|
|
|
|
$( document ).trigger( 'mmv-download-open', e );
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
2015-01-01 02:24:32 +00:00
|
|
|
|
}
|
|
|
|
|
} );
|
2014-09-04 21:50:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* Registers listeners.
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.attach = function () {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
var canvas = this;
|
|
|
|
|
|
2014-09-30 14:16:08 +00:00
|
|
|
|
$( window ).on( 'resize.mmv-canvas', $.debounce( 100, function () {
|
|
|
|
|
canvas.$mainWrapper.trigger( $.Event( 'mmv-resize-end' ) );
|
|
|
|
|
} ) );
|
2014-06-26 01:26:49 +00:00
|
|
|
|
|
2014-11-22 02:46:51 +00:00
|
|
|
|
this.$imageWrapper.on( 'click.mmv-canvas', function () {
|
2015-01-23 12:48:27 +00:00
|
|
|
|
if ( canvas.$container.closest( '.metadata-panel-is-open' ).length > 0 ) {
|
2014-11-22 02:46:51 +00:00
|
|
|
|
canvas.$mainWrapper.trigger( 'mmv-panel-close-area-click' );
|
|
|
|
|
}
|
2014-06-26 01:26:49 +00:00
|
|
|
|
} );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears listeners.
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.unattach = function () {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
this.clearEvents();
|
|
|
|
|
|
2014-09-09 08:56:13 +00:00
|
|
|
|
$( window ).off( 'resize.mmv-canvas' );
|
2014-06-26 01:26:49 +00:00
|
|
|
|
|
2014-11-22 02:46:51 +00:00
|
|
|
|
this.$imageWrapper.off( 'click.mmv-canvas' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-04 01:58:27 +00:00
|
|
|
|
* Sets page thumbnail for display if blowupFactor <= MAX_BLOWUP_FACTOR. Otherwise thumb is not set.
|
|
|
|
|
* The image gets also blured to avoid pixelation if blowupFactor > BLUR_BLOWUP_FACTOR_THRESHOLD.
|
|
|
|
|
* We set SVG files to the maximum screen size available.
|
|
|
|
|
* Assumes set function called before.
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*
|
2014-04-24 01:03:24 +00:00
|
|
|
|
* @param {{width: number, height: number}} size
|
2014-03-04 01:58:27 +00:00
|
|
|
|
* @param {jQuery} $imagePlaceholder Image placeholder to be displayed while the real image loads.
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @param {mw.mmv.model.ThumbnailWidth} imageWidths
|
2016-07-18 13:49:27 +00:00
|
|
|
|
* @return {boolean} Whether the image was blured or not
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*/
|
2014-11-22 00:16:36 +00:00
|
|
|
|
C.maybeDisplayPlaceholder = function ( size, $imagePlaceholder, imageWidths ) {
|
2014-03-04 01:58:27 +00:00
|
|
|
|
var targetWidth,
|
2014-02-26 20:09:37 +00:00
|
|
|
|
targetHeight,
|
|
|
|
|
blowupFactor,
|
2014-04-15 00:01:03 +00:00
|
|
|
|
blurredThumbnailShown = false;
|
2014-03-04 01:58:27 +00:00
|
|
|
|
|
|
|
|
|
// Assume natural thumbnail size¸
|
2014-04-24 01:03:24 +00:00
|
|
|
|
targetWidth = size.width;
|
|
|
|
|
targetHeight = size.height;
|
2014-03-04 01:58:27 +00:00
|
|
|
|
|
|
|
|
|
// If the image is bigger than the screen we need to resize it
|
2014-04-24 01:03:24 +00:00
|
|
|
|
if ( size.width > imageWidths.cssWidth ) { // This assumes imageInfo.width in CSS units
|
2014-03-04 01:58:27 +00:00
|
|
|
|
targetWidth = imageWidths.cssWidth;
|
|
|
|
|
targetHeight = imageWidths.cssHeight;
|
2014-02-26 20:09:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
|
blowupFactor = targetWidth / $imagePlaceholder.width();
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
// If the placeholder is too blown up, it's not worth showing it
|
2014-03-04 01:58:27 +00:00
|
|
|
|
if ( blowupFactor > Canvas.MAX_BLOWUP_FACTOR ) {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
return blurredThumbnailShown;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
|
$imagePlaceholder.width( targetWidth );
|
|
|
|
|
$imagePlaceholder.height( targetHeight );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
// Only blur the placeholder if it's blown up significantly
|
2014-03-04 01:58:27 +00:00
|
|
|
|
if ( blowupFactor > Canvas.BLUR_BLOWUP_FACTOR_THRESHOLD ) {
|
|
|
|
|
this.blur( $imagePlaceholder );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
blurredThumbnailShown = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
|
this.set( this.imageRawMetadata, $imagePlaceholder.show() );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
return blurredThumbnailShown;
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* Blur image
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-03-04 01:58:27 +00:00
|
|
|
|
* @param {jQuery} $image Image to be blurred.
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.blur = function ( $image ) {
|
2014-03-04 01:58:27 +00:00
|
|
|
|
// We have to apply the SVG filter here, it doesn't work when defined in the .less file
|
|
|
|
|
// We can't use an external SVG file because filters can't be accessed cross-domain
|
|
|
|
|
// We can't embed the SVG file because accessing the filter inside of it doesn't work
|
|
|
|
|
$image.addClass( 'blurred' ).css( 'filter', 'url("#gaussian-blur")' );
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* Animates the image into focus
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.unblurWithAnimation = function () {
|
2014-02-26 20:09:37 +00:00
|
|
|
|
var self = this,
|
|
|
|
|
animationLength = 300;
|
|
|
|
|
|
|
|
|
|
// The blurred class has an opacity < 1. This animated the image to become fully opaque
|
2019-02-06 01:56:53 +00:00
|
|
|
|
// FIXME: Use CSS transition
|
2019-02-20 19:22:21 +00:00
|
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
2014-02-26 20:09:37 +00:00
|
|
|
|
this.$image
|
|
|
|
|
.addClass( 'blurred' )
|
|
|
|
|
.animate( { opacity: 1.0 }, animationLength );
|
|
|
|
|
|
|
|
|
|
// During the same amount of time (animationLength) we animate a blur value from 3.0 to 0.0
|
|
|
|
|
// We pass that value to an inline CSS Gaussian blur effect
|
2019-02-06 01:56:53 +00:00
|
|
|
|
// FIXME: Use CSS transition
|
2019-02-20 19:22:21 +00:00
|
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
2014-02-26 20:09:37 +00:00
|
|
|
|
$( { blur: 3.0 } ).animate( { blur: 0.0 }, {
|
|
|
|
|
duration: animationLength,
|
|
|
|
|
step: function ( step ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
|
self.$image.css( { '-webkit-filter': 'blur(' + step + 'px)',
|
|
|
|
|
filter: 'blur(' + step + 'px)' } );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
},
|
|
|
|
|
complete: function () {
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
// When the animation is complete, the blur value is 0, clean things up
|
|
|
|
|
self.unblur();
|
2014-02-26 20:09:37 +00:00
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.unblur = function () {
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
// We apply empty CSS values to remove the inline styles applied by jQuery
|
|
|
|
|
// so that they don't get in the way of styles defined in CSS
|
2016-07-18 13:49:27 +00:00
|
|
|
|
this.$image.css( { '-webkit-filter': '', opacity: '', filter: '' } )
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
.removeClass( 'blurred' );
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-01 01:10:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Displays a message and error icon when loading the image fails.
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-03-01 01:10:51 +00:00
|
|
|
|
* @param {string} error error message
|
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
|
C.showError = function ( error ) {
|
2020-01-08 19:27:58 +00:00
|
|
|
|
var errorDetails, description, errorUri, $retryLink, $reportLink,
|
2015-10-22 04:31:47 +00:00
|
|
|
|
canvasDimensions = this.getDimensions(),
|
|
|
|
|
thumbnailDimensions = this.getCurrentImageWidths(),
|
|
|
|
|
htmlUtils = new mw.mmv.HtmlUtils();
|
|
|
|
|
|
|
|
|
|
errorDetails = [
|
|
|
|
|
'error: ' + error,
|
|
|
|
|
'URL: ' + location.href,
|
|
|
|
|
'user agent: ' + navigator.userAgent,
|
|
|
|
|
'screen size: ' + screen.width + 'x' + screen.height,
|
|
|
|
|
'canvas size: ' + canvasDimensions.width + 'x' + canvasDimensions.height,
|
|
|
|
|
'image size: ' + this.imageRawMetadata.originalWidth + 'x' + this.imageRawMetadata.originalHeight,
|
2016-07-18 13:49:27 +00:00
|
|
|
|
'thumbnail size: CSS: ' + thumbnailDimensions.cssWidth + 'x' + thumbnailDimensions.cssHeight +
|
|
|
|
|
', screen width: ' + thumbnailDimensions.screen + ', real width: ' + thumbnailDimensions.real
|
2015-10-22 04:31:47 +00:00
|
|
|
|
];
|
|
|
|
|
// ** is bolding in Phabricator
|
2016-07-18 13:49:27 +00:00
|
|
|
|
description = '**' + mw.message( 'multimediaviewer-errorreport-privacywarning' ).text() + '**\n\n\n' +
|
|
|
|
|
'Error details:\n\n' + errorDetails.join( '\n' );
|
2015-10-22 04:31:47 +00:00
|
|
|
|
errorUri = mw.msg( 'multimediaviewer-report-issue-url', encodeURIComponent( description ) );
|
|
|
|
|
|
2020-01-08 19:27:58 +00:00
|
|
|
|
$retryLink = $( '<a>' ).addClass( 'mw-mmv-retry-link' ).text(
|
2015-10-22 04:31:47 +00:00
|
|
|
|
mw.msg( 'multimediaviewer-thumbnail-error-retry' ) );
|
2020-01-08 19:27:58 +00:00
|
|
|
|
$reportLink = $( '<a>' ).attr( 'href', errorUri ).text(
|
2015-10-22 04:31:47 +00:00
|
|
|
|
mw.msg( 'multimediaviewer-thumbnail-error-report' ) );
|
|
|
|
|
|
2014-03-01 01:10:51 +00:00
|
|
|
|
this.$imageDiv.empty()
|
|
|
|
|
.addClass( 'error' )
|
|
|
|
|
.append(
|
2015-01-13 21:07:37 +00:00
|
|
|
|
$( '<div>' ).addClass( 'error-box' ).append(
|
|
|
|
|
$( '<div>' ).addClass( 'mw-mmv-error-text' ).text(
|
2015-10-22 04:31:47 +00:00
|
|
|
|
mw.msg( 'multimediaviewer-thumbnail-error' )
|
2015-01-13 21:07:37 +00:00
|
|
|
|
)
|
|
|
|
|
).append(
|
|
|
|
|
$( '<div>' ).addClass( 'mw-mmv-error-description' ).append(
|
2015-10-22 04:31:47 +00:00
|
|
|
|
mw.msg( 'multimediaviewer-thumbnail-error-description',
|
2020-01-08 19:27:58 +00:00
|
|
|
|
htmlUtils.jqueryToHtml( $retryLink ),
|
2015-10-22 04:31:47 +00:00
|
|
|
|
error,
|
2020-01-08 19:27:58 +00:00
|
|
|
|
htmlUtils.jqueryToHtml( $reportLink )
|
2015-10-22 04:31:47 +00:00
|
|
|
|
)
|
2015-01-13 21:07:37 +00:00
|
|
|
|
)
|
2014-03-01 01:10:51 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2018-04-24 14:47:41 +00:00
|
|
|
|
this.$imageDiv.find( '.mw-mmv-retry-link' ).on( 'click', function () {
|
2015-01-13 21:07:37 +00:00
|
|
|
|
location.reload();
|
|
|
|
|
} );
|
2014-03-01 01:10:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
/**
|
2014-09-26 18:37:53 +00:00
|
|
|
|
* Returns width and height of the canvas area (i.e. the space available for the image).
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-09-26 18:37:53 +00:00
|
|
|
|
* @param {boolean} forFullscreen if true, return size in fullscreen mode; otherwise, return current size
|
|
|
|
|
* (which might still be fullscreen mode).
|
2016-07-18 13:49:27 +00:00
|
|
|
|
* @return {Object} Width and height in CSS pixels
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*/
|
2014-09-26 18:37:53 +00:00
|
|
|
|
C.getDimensions = function ( forFullscreen ) {
|
|
|
|
|
var $window = $( window ),
|
2019-02-20 19:22:21 +00:00
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2014-06-24 19:30:23 +00:00
|
|
|
|
$aboveFold = $( '.mw-mmv-above-fold' ),
|
2014-06-17 21:57:44 +00:00
|
|
|
|
isFullscreened = !!$aboveFold.closest( '.jq-fullscreened' ).length,
|
|
|
|
|
// Don't rely on this.$imageWrapper's sizing because it's fragile.
|
|
|
|
|
// Depending on what the wrapper contains, its size can be 0 on some browsers.
|
|
|
|
|
// Therefore, we calculate the available space manually
|
2014-05-14 10:02:39 +00:00
|
|
|
|
availableWidth = $window.width(),
|
2015-02-19 07:20:15 +00:00
|
|
|
|
availableHeight = $window.height() - ( isFullscreened ? 0 : $aboveFold.outerHeight() );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
2014-09-26 18:37:53 +00:00
|
|
|
|
if ( forFullscreen ) {
|
|
|
|
|
return {
|
|
|
|
|
width: screen.width,
|
|
|
|
|
height: screen.height
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
width: availableWidth,
|
|
|
|
|
height: availableHeight
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the widths for a given lightbox image.
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-09-26 18:37:53 +00:00
|
|
|
|
* @param {mw.mmv.LightboxImage} image
|
2016-07-18 13:49:27 +00:00
|
|
|
|
* @return {mw.mmv.model.ThumbnailWidth}
|
2014-09-26 18:37:53 +00:00
|
|
|
|
*/
|
|
|
|
|
C.getLightboxImageWidths = function ( image ) {
|
|
|
|
|
var thumb = image.thumbnail,
|
|
|
|
|
canvasDimensions = this.getDimensions();
|
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
|
return this.thumbnailWidthCalculator.calculateWidths(
|
2014-09-26 18:37:53 +00:00
|
|
|
|
canvasDimensions.width, canvasDimensions.height, thumb.width, thumb.height );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the fullscreen widths for a given lightbox image.
|
|
|
|
|
* Intended for use before the viewer is in fullscreen mode
|
|
|
|
|
* (in fullscreen mode getLightboxImageWidths() works fine).
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
2014-02-26 20:09:37 +00:00
|
|
|
|
* @param {mw.mmv.LightboxImage} image
|
2016-07-18 13:49:27 +00:00
|
|
|
|
* @return {mw.mmv.model.ThumbnailWidth}
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*/
|
|
|
|
|
C.getLightboxImageWidthsForFullscreen = function ( image ) {
|
2014-09-26 18:37:53 +00:00
|
|
|
|
var thumb = image.thumbnail,
|
|
|
|
|
canvasDimensions = this.getDimensions( true );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
|
|
return this.thumbnailWidthCalculator.calculateWidths(
|
2014-09-26 18:37:53 +00:00
|
|
|
|
canvasDimensions.width, canvasDimensions.height, thumb.width, thumb.height );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the widths for the current lightbox image.
|
2016-07-18 13:49:27 +00:00
|
|
|
|
*
|
|
|
|
|
* @return {mw.mmv.model.ThumbnailWidth}
|
2014-02-26 20:09:37 +00:00
|
|
|
|
*/
|
|
|
|
|
C.getCurrentImageWidths = function () {
|
|
|
|
|
return this.getLightboxImageWidths( this.imageRawMetadata );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mw.mmv.ui.Canvas = Canvas;
|
2018-11-12 16:33:24 +00:00
|
|
|
|
}() );
|