| 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 | * Handles scrolling behavior of the metadata panel. |
| 22 | */ |
| 23 | class MetadataPanelScroller extends UiElement { |
| 24 | /** |
| 25 | * @param {jQuery} $container The container for the panel (.mw-mmv-post-image). |
| 26 | * @param {jQuery} $aboveFold The control bar element (.mw-mmv-above-fold). |
| 27 | */ |
| 28 | constructor( $container, $aboveFold ) { |
| 29 | super( $container ); |
| 30 | |
| 31 | this.$aboveFold = $aboveFold; |
| 32 | |
| 33 | /** @property {boolean} panelWasOpen state flag which will be used to detect open <-> closed transitions */ |
| 34 | this.panelWasOpen = null; |
| 35 | |
| 36 | /** |
| 37 | * Whether this user has ever opened the metadata panel. |
| 38 | * Based on a localstorage flag; will be set to true if the client does not support localstorage. |
| 39 | * |
| 40 | * @type {boolean} |
| 41 | */ |
| 42 | this.hasOpenedMetadata = undefined; |
| 43 | |
| 44 | /** |
| 45 | * Whether we've already fired an animation for the metadata div in this lightbox session. |
| 46 | * |
| 47 | * @property {boolean} |
| 48 | * @private |
| 49 | */ |
| 50 | this.hasAnimatedMetadata = false; |
| 51 | |
| 52 | this.initialize(); |
| 53 | } |
| 54 | |
| 55 | attach() { |
| 56 | this.handleEvent( 'keydown', ( e ) => { |
| 57 | this.keydown( e ); |
| 58 | } ); |
| 59 | |
| 60 | $( window ).on( 'scroll.mmvp', mw.util.throttle( () => { |
| 61 | this.scroll(); |
| 62 | }, 250 ) ); |
| 63 | |
| 64 | this.$container.on( 'mmv-metadata-open', () => { |
| 65 | if ( !this.hasOpenedMetadata && mw.storage.store ) { |
| 66 | this.hasOpenedMetadata = true; |
| 67 | mw.storage.set( 'mmv.hasOpenedMetadata', '1' ); |
| 68 | } |
| 69 | } ); |
| 70 | |
| 71 | // reset animation flag when the viewer is reopened |
| 72 | this.hasAnimatedMetadata = false; |
| 73 | } |
| 74 | |
| 75 | unattach() { |
| 76 | this.clearEvents(); |
| 77 | $( window ).off( 'scroll.mmvp' ); |
| 78 | this.$container.off( 'mmv-metadata-open' ); |
| 79 | } |
| 80 | |
| 81 | empty() { |
| 82 | // need to remove this to avoid animating again when reopening lightbox on same page |
| 83 | this.$container.removeClass( 'invite' ); |
| 84 | |
| 85 | this.panelWasOpen = this.panelIsOpen(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns scroll top position when the panel is fully open. |
| 90 | * (In other words, the height of the area that is outside the screen, in pixels.) |
| 91 | * |
| 92 | * @return {number} |
| 93 | */ |
| 94 | getScrollTopWhenOpen() { |
| 95 | return this.$container.outerHeight() - parseInt( this.$aboveFold.css( 'min-height' ), 10 ) - |
| 96 | parseInt( this.$aboveFold.css( 'padding-bottom' ), 10 ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Makes sure the panel does not contract when it is emptied and thus keeps its position as much as possible. |
| 101 | * This should be called when switching images, before the panel is emptied, and should be undone with |
| 102 | * unfreezeHeight after the panel has been populated with the new metadata. |
| 103 | */ |
| 104 | freezeHeight() { |
| 105 | // TODO: Store visibility in model |
| 106 | // eslint-disable-next-line no-jquery/no-sizzle |
| 107 | if ( !this.$container.is( ':visible' ) ) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | const scrollTop = $( window ).scrollTop(); |
| 112 | const scrollTopWhenOpen = this.getScrollTopWhenOpen(); |
| 113 | |
| 114 | this.panelWasFullyOpen = ( scrollTop === scrollTopWhenOpen ); |
| 115 | this.$container.css( 'min-height', this.$container.height() ); |
| 116 | } |
| 117 | |
| 118 | unfreezeHeight() { |
| 119 | // TODO: Store visibility in model |
| 120 | // eslint-disable-next-line no-jquery/no-sizzle |
| 121 | if ( !this.$container.is( ':visible' ) ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | this.$container.css( 'min-height', '' ); |
| 126 | if ( this.panelWasFullyOpen ) { |
| 127 | $( window ).scrollTop( this.getScrollTopWhenOpen() ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | initialize() { |
| 132 | const value = mw.storage.get( 'mmv.hasOpenedMetadata' ); |
| 133 | |
| 134 | // localStorage will only store strings; if values `null`, `false` or |
| 135 | // `0` are set, they'll come out as `"null"`, `"false"` or `"0"`, so we |
| 136 | // can be certain that an actual null is a failure to locate the item, |
| 137 | // and false is an issue with localStorage itself |
| 138 | if ( value !== false ) { |
| 139 | this.hasOpenedMetadata = value !== null; |
| 140 | } else { |
| 141 | // if there was an issue with localStorage, treat it as opened |
| 142 | this.hasOpenedMetadata = true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Animates the metadata area when the viewer is first opened. |
| 148 | */ |
| 149 | animateMetadataOnce() { |
| 150 | if ( !this.hasOpenedMetadata && !this.hasAnimatedMetadata ) { |
| 151 | this.hasAnimatedMetadata = true; |
| 152 | this.$container.addClass( 'invite' ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Toggles the metadata div being totally visible. |
| 158 | * |
| 159 | * @param {string} [forceDirection] 'up' or 'down' makes the panel move on that direction (and is a noop |
| 160 | * if the panel is already at the upmost/bottommost position); without the parameter, the panel position |
| 161 | * is toggled. (Partially open counts as open.) |
| 162 | * @return {jQuery.Promise} A promise which resolves after the animation has finished. |
| 163 | */ |
| 164 | toggle( forceDirection ) { |
| 165 | const scrollTopWhenOpen = this.getScrollTopWhenOpen(); |
| 166 | const scrollTopWhenClosed = 0; |
| 167 | const scrollTop = $( window ).scrollTop(); |
| 168 | const panelIsOpen = scrollTop > scrollTopWhenClosed; |
| 169 | const direction = forceDirection || ( panelIsOpen ? 'down' : 'up' ); |
| 170 | let scrollTopTarget = ( direction === 'up' ) ? scrollTopWhenOpen : scrollTopWhenClosed; |
| 171 | |
| 172 | // don't log / animate if the panel is already in the end position |
| 173 | if ( scrollTopTarget === scrollTop ) { |
| 174 | return $.Deferred().resolve().promise(); |
| 175 | } else { |
| 176 | if ( direction === 'up' && !panelIsOpen ) { |
| 177 | // FIXME nasty. This is not really an event but a command sent to the metadata panel; |
| 178 | // child UI elements should not send commands to their parents. However, there is no way |
| 179 | // to calculate the target scrollTop accurately without revealing the text, and the event |
| 180 | // which does that (metadata-open) is only triggered later in the process, when the panel |
| 181 | // actually scrolled, so we cannot use it here without risking triggering it multiple times. |
| 182 | this.$container.trigger( 'mmv-metadata-reveal-truncated-text' ); |
| 183 | scrollTopTarget = this.getScrollTopWhenOpen(); |
| 184 | } |
| 185 | // eslint-disable-next-line no-jquery/no-global-selector |
| 186 | return $( 'html, body' ).animate( { scrollTop: scrollTopTarget }, 'fast' ).promise(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Handles keydown events for this element. |
| 192 | * |
| 193 | * @param {jQuery.Event} e Key down event |
| 194 | */ |
| 195 | keydown( e ) { |
| 196 | if ( e.altKey || e.shiftKey || e.ctrlKey || e.metaKey ) { |
| 197 | return; |
| 198 | } |
| 199 | switch ( e.which ) { |
| 200 | case 40: // Down arrow |
| 201 | |
| 202 | // fall through |
| 203 | case 38: // Up arrow |
| 204 | this.toggle(); |
| 205 | e.preventDefault(); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns whether the metadata panel is open. (Partially open is considered to be open.) |
| 212 | * |
| 213 | * @return {boolean} |
| 214 | */ |
| 215 | panelIsOpen() { |
| 216 | return $( window ).scrollTop() > 0; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @event MetadataPanelScroller#mmv-metadata-open |
| 221 | */ |
| 222 | |
| 223 | /** |
| 224 | * @event MetadataPanelScroller#mmv-metadata-close |
| 225 | */ |
| 226 | |
| 227 | /** |
| 228 | * Receives the window's scroll events and and turns them into business logic events |
| 229 | * |
| 230 | * @fires MetadataPanelScroller#mmv-metadata-open |
| 231 | * @fires MetadataPanelScroller#mmv-metadata-close |
| 232 | */ |
| 233 | scroll() { |
| 234 | const panelIsOpen = this.panelIsOpen(); |
| 235 | |
| 236 | if ( panelIsOpen && !this.panelWasOpen ) { // just opened |
| 237 | this.$container.trigger( 'mmv-metadata-open' ); |
| 238 | } else if ( !panelIsOpen && this.panelWasOpen ) { // just closed |
| 239 | this.$container.trigger( 'mmv-metadata-close' ); |
| 240 | } |
| 241 | this.panelWasOpen = panelIsOpen; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | module.exports = MetadataPanelScroller; |