| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MediaViewer. |
| 3 | * |
| 4 | * MediaViewer 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 | * MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | const UiElement = require( './mmv.ui.js' ); |
| 19 | |
| 20 | /** |
| 21 | * A progress bar for the loading of the image. |
| 22 | */ |
| 23 | class ProgressBar extends UiElement { |
| 24 | /** |
| 25 | * @param {jQuery} $container |
| 26 | */ |
| 27 | constructor( $container ) { |
| 28 | super( $container ); |
| 29 | this.init(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Initializes the progress display at the top of the panel. |
| 34 | */ |
| 35 | init() { |
| 36 | this.$progress = $( '<div>' ) |
| 37 | .addClass( 'mw-mmv-progress empty' ) |
| 38 | .appendTo( this.$container ); |
| 39 | this.$percent = $( '<div>' ) |
| 40 | .addClass( 'mw-mmv-progress-percent' ) |
| 41 | .appendTo( this.$progress ); |
| 42 | } |
| 43 | |
| 44 | empty() { |
| 45 | this.hide(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Hides the bar, resets it to 0 and stops any animation in progress. |
| 50 | */ |
| 51 | hide() { |
| 52 | this.$progress.addClass( 'empty' ); |
| 53 | this.$percent.css( { width: 0 } ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handles the progress display when a percentage of progress is received |
| 58 | * |
| 59 | * @param {number} percent a number between 0 and 100 |
| 60 | */ |
| 61 | animateTo( percent ) { |
| 62 | this.$progress.removeClass( 'empty' ); |
| 63 | |
| 64 | if ( percent === 100 ) { |
| 65 | // When a 100% update comes in, we make sure that the bar is visible, we animate |
| 66 | // fast to 100 and we hide the bar |
| 67 | this.$percent.css( { width: `${ percent }%` } ); |
| 68 | this.hide(); |
| 69 | } else { |
| 70 | // When any other % update comes in, we make sure the bar is visible |
| 71 | // and we animate to the right position |
| 72 | this.$percent.css( { width: `${ percent }%` } ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Goes to the given percent (originally without animation) |
| 78 | * |
| 79 | * @param {number} percent a number between 0 and 100 |
| 80 | */ |
| 81 | jumpTo( percent ) { |
| 82 | this.animateTo( percent ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | module.exports = ProgressBar; |