2014-04-21 21:48:40 +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/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
const UiElement = require( './mmv.ui.js' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-04-21 21:48:40 +00:00
|
|
|
|
|
|
|
/**
|
2016-07-18 13:49:27 +00:00
|
|
|
* A progress bar for the loading of the image.
|
2014-04-21 21:48:40 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
class ProgressBar extends UiElement {
|
|
|
|
/**
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
constructor( $container ) {
|
|
|
|
super( $container );
|
|
|
|
this.init();
|
|
|
|
}
|
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
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Initializes the progress display at the top of the panel.
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
this.$progress = $( '<div>' )
|
|
|
|
.addClass( 'mw-mmv-progress empty' )
|
|
|
|
.appendTo( this.$container );
|
|
|
|
this.$percent = $( '<div>' )
|
|
|
|
.addClass( 'mw-mmv-progress-percent' )
|
|
|
|
.appendTo( this.$progress );
|
|
|
|
}
|
|
|
|
empty() {
|
|
|
|
this.hide();
|
|
|
|
}
|
2014-04-21 21:48:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Hides the bar, resets it to 0 and stops any animation in progress.
|
|
|
|
*/
|
|
|
|
hide() {
|
|
|
|
this.$progress.addClass( 'empty' );
|
|
|
|
this.$percent.stop().css( { width: 0 } );
|
|
|
|
}
|
2014-04-21 21:48:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Handles the progress display when a percentage of progress is received
|
|
|
|
*
|
|
|
|
* @param {number} percent a number between 0 and 100
|
|
|
|
*/
|
|
|
|
animateTo( percent ) {
|
|
|
|
this.$progress.removeClass( 'empty' );
|
|
|
|
this.$percent.stop();
|
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
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( percent === 100 ) {
|
|
|
|
// When a 100% update comes in, we make sure that the bar is visible, we animate
|
|
|
|
// fast to 100 and we hide the bar when the animation is done
|
|
|
|
// FIXME: Use CSS transition
|
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
|
|
|
this.$percent.animate( { width: `${percent}%` }, 50, 'swing', this.hide.bind( this ) );
|
|
|
|
} else {
|
|
|
|
// When any other % update comes in, we make sure the bar is visible
|
|
|
|
// and we animate to the right position
|
|
|
|
// FIXME: Use CSS transition
|
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
|
|
|
this.$percent.animate( { width: `${percent}%` } );
|
|
|
|
}
|
2014-04-21 21:48:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Goes to the given percent without animation
|
|
|
|
*
|
|
|
|
* @param {number} percent a number between 0 and 100
|
|
|
|
*/
|
|
|
|
jumpTo( percent ) {
|
|
|
|
this.$progress.removeClass( 'empty' );
|
|
|
|
this.$percent.stop().css( { width: `${percent}%` } );
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
module.exports = ProgressBar;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|