summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.canvasButtons.js
blob: 0bf6c2c6dfd361f4e26cf1916677a89248fd5d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer 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.
 *
 * MultimediaViewer 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 MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $, oo ) {
	var CBP;

	/**
	 * Represents the buttons which are displayed over the image - next, previous, close
	 * and fullscreen.
	 *
	 * @class mw.mmv.ui.CanvasButtons
	 * @extends mw.mmv.ui.Element
	 * @constructor
	 * @param {jQuery} $container The parent element we should put the buttons into.
	 * @param {jQuery} $closeButton The close button element from the parent class.
	 * @param {jQuery} $fullscreenButton The fullscreen button from the parent class.
	 */
	function CanvasButtons( $container, $closeButton, $fullscreenButton ) {
		var buttons = this,
			tooltipDelay = mw.config.get( 'wgMultimediaViewer' ).tooltipDelay;

		mw.mmv.ui.Element.call( this, $container );

		this.$close = $closeButton;
		this.$fullscreen = $fullscreenButton;

		this.$reuse = $( '<button>' )
			.addClass( 'mw-mmv-reuse-button' )
			.html( '&nbsp;' )
			.prop( 'title', mw.message( 'multimediaviewer-reuse-link' ).text() )
			.tipsy( {
				delayIn: tooltipDelay,
				gravity: this.correctEW( 'se' )
			} );

		this.$options = $( '<button>' )
			.text( ' ' )
			.prop( 'title', mw.message( 'multimediaviewer-options-tooltip' ).text() )
			.addClass( 'mw-mmv-options-button' )
			.tipsy( {
				delayIn: tooltipDelay,
				gravity: this.correctEW( 'se' )
			} );

		this.$download = $( '<button>' )
			.addClass( 'mw-mmv-download-button' )
			.html( '&nbsp;' )
			.prop( 'title', mw.message( 'multimediaviewer-download-link' ).text() )
			.tipsy( {
				delayIn: tooltipDelay,
				gravity: this.correctEW( 'se' )
			} );

		this.$next = $( '<button>' )
			.prop( 'title', mw.message( 'multimediaviewer-next-image-alt-text' ).text() )
			.addClass( 'mw-mmv-next-image disabled' )
			.html( '&nbsp;' );

		this.$prev = $( '<button>' )
			.prop( 'title', mw.message( 'multimediaviewer-prev-image-alt-text' ).text() )
			.addClass( 'mw-mmv-prev-image disabled' )
			.html( '&nbsp;' );

		this.$nav = this.$next
			.add( this.$prev );

		this.$buttons = this.$close
			.add( this.$download )
			.add( this.$reuse )
			.add( this.$fullscreen )
			.add( this.$options )
			.add( this.$next )
			.add( this.$prev );

		this.$buttons.appendTo( this.$container );

		$( document ).on( 'mmv-close', function () {
			buttons.$nav.addClass( 'disabled' );
		} );

		this.$close.click( function () {
			$container.trigger( $.Event( 'mmv-close' ) );
		} );

		this.$next.click( function () {
			buttons.emit( 'next' );
		} );

		this.$prev.click( function () {
			buttons.emit( 'prev' );
		} );
	}
	oo.inheritClass( CanvasButtons, mw.mmv.ui.Element );
	CBP = CanvasButtons.prototype;

	/**
	 * Sets the top offset for the navigation buttons.
	 *
	 * @param {number} offset
	 */
	CBP.setOffset = function ( offset ) {
		this.$nav.css( {
			top: offset
		} );
	};

	/**
	 * Stops the fading animation of the buttons and cancel any opacity value
	 */
	CBP.stopFade = function () {
		this.$buttons
			.stop( true )
			.removeClass( 'hidden' )
			.css( 'opacity', '' );

		this.$container.trigger( $.Event( 'mmv-fade-stopped' ) );
	};

	/**
	 * Toggles buttons being disabled or not
	 *
	 * @param {boolean} showPrevButton
	 * @param {boolean} showNextButton
	 */
	CBP.toggle = function ( showPrevButton, showNextButton ) {
		this.$next.toggleClass( 'disabled', !showPrevButton );
		this.$prev.toggleClass( 'disabled', !showNextButton );
	};

	/**
	 * Fades out the active buttons
	 */
	CBP.fadeOut = function () {
		var buttons = this;

		// We don't use animation chaining because delay() can't be stop()ed
		this.buttonsFadeTimeout = setTimeout( function () {
			buttons.$buttons.not( '.disabled' ).animate( { opacity: 0 }, 1000, 'swing',
				function () {
					buttons.$buttons.addClass( 'hidden' );
					buttons.$container.trigger( $.Event( 'mmv-faded-out' ) );
				} );
		}, 1500 );
	};

	/**
	 * Checks if any active buttons are currently hovered, given a position
	 *
	 * @param {number} x The horizontal coordinate of the position
	 * @param {number} y The vertical coordinate of the position
	 * @return {boolean}
	 */
	CBP.isAnyActiveButtonHovered = function ( x, y ) {
		// We don't use mouseenter/mouseleave events because content is subject
		// to change underneath the cursor, eg. when entering fullscreen or
		// when going prev/next (the button can disappear when reaching ends)
		var hovered = false;

		this.$buttons.not( '.disabled' ).each( function ( idx, e ) {
			var $e = $( e ),
				offset = $e.offset();

			if ( y >= offset.top &&
				// using css( 'height' ) & css( 'width' ) instead of .height()
				// and .width() since those don't include padding, and as a
				// result can return a smaller size than is actually the button
				y <= offset.top + parseInt( $e.css( 'height' ) ) &&
				x >= offset.left &&
				x <= offset.left + parseInt( $e.css( 'width' ) ) ) {
				hovered = true;
			}
		} );

		return hovered;
	};

	/**
	 * Reveals all active buttons and schedule a fade out if needed
	 *
	 * @param {Object} [mousePosition] Mouse position containing 'x' and 'y' properties
	 */
	CBP.revealAndFade = function ( mousePosition ) {
		if ( this.buttonsFadeTimeout ) {
			clearTimeout( this.buttonsFadeTimeout );
		}

		// Stop ongoing animations and make sure the buttons that need to be displayed are displayed
		this.stopFade();

		// mousePosition can be empty, for instance when we enter fullscreen and haven't
		// recorded a real mousemove event yet
		if ( !mousePosition ||
			!this.isAnyActiveButtonHovered( mousePosition.x, mousePosition.y ) ) {
			this.fadeOut();
		}
	};

	/**
	 * @event mmv-reuse-open
	 * Fired when the button to open the reuse dialog is clicked.
	 */
	/**
	 * Registers listeners.
	 */
	CBP.attach = function () {
		var buttons = this;

		this.$reuse.on( 'click.mmv-canvasButtons', function ( e ) {
			$( document ).trigger( 'mmv-reuse-open', e );
			e.stopPropagation(); // the dialog would take it as an outside click and close
		} );
		this.handleEvent( 'mmv-reuse-opened', function () {
			buttons.$reuse.addClass( 'open' );
		} );
		this.handleEvent( 'mmv-reuse-closed', function () {
			buttons.$reuse.removeClass( 'open' );
		} );

		this.$download.on( 'click.mmv-canvasButtons', function ( e ) {
			$( document ).trigger( 'mmv-download-open', e );
			e.stopPropagation();
		} );
		this.handleEvent( 'mmv-download-opened', function () {
			buttons.$download.addClass( 'open' );
		} );
		this.handleEvent( 'mmv-download-closed', function () {
			buttons.$download.removeClass( 'open' );
		} );

		this.$options.on( 'click.mmv-canvasButtons', function ( e ) {
			$( document ).trigger( 'mmv-options-open', e );
			e.stopPropagation();
		} );
		this.handleEvent( 'mmv-options-opened', function () {
			buttons.$options.addClass( 'open' );
		} );
		this.handleEvent( 'mmv-options-closed', function () {
			buttons.$options.removeClass( 'open' );
		} );

		this.$download
			.add( this.$reuse )
			.add( this.$options )
			.add( this.$close )
			.add( this.$fullscreen )
			.each( function () {
				$( this ).tipsy( 'enable' );
			} );
	};

	/**
	 * Removes all UI things from the DOM, or hides them
	 */
	CBP.unattach = function () {
		this.$download
			.add( this.$reuse )
			.add( this.$options )
			.add( this.$close )
			.add( this.$fullscreen )
			.off( 'click.mmv-canvasButtons' )
			.each( function () {
				$( this ).tipsy( 'hide' ).tipsy( 'disable' );
			} );
	};

	CBP.empty = function () {
		this.$reuse.removeClass( 'open' );
	};

	mw.mmv.ui.CanvasButtons = CanvasButtons;
}( mediaWiki, jQuery, OO ) );