Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.canvas.js

raw symbols SCIP
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 HtmlUtils = require( '../mmv.HtmlUtils.js' );
19 const ThumbnailWidthCalculator = require( '../mmv.ThumbnailWidthCalculator.js' );
20 const UiElement = require( './mmv.ui.js' );
21
22 /**
23 * UI component that contains the multimedia element to be displayed.
24 * This first version assumes an image but it can be extended to other
25 * media types (video, sound, presentation, etc.).
26 */
27 class Canvas extends UiElement {
28 /**
29 * @param {jQuery} $container Canvas' container
30 * @param {jQuery} $imageWrapper
31 * @param {jQuery} $mainWrapper
32 */
33 constructor( $container, $imageWrapper, $mainWrapper ) {
34 super( $container );
35
36 /**
37 * @property {boolean}
38 * @private
39 */
40 this.dialogOpen = false;
41
42 /**
43 * @property {ThumbnailWidthCalculator}
44 * @private
45 */
46 this.thumbnailWidthCalculator = new ThumbnailWidthCalculator();
47
48 /**
49 * Contains image.
50 *
51 * @property {jQuery}
52 */
53 this.$imageDiv = $( '<div>' )
54 .addClass( 'mw-mmv-image' );
55
56 this.$imageDiv.appendTo( this.$container );
57
58 /**
59 * Container of canvas and controls, needed for canvas size calculations.
60 *
61 * @property {jQuery}
62 * @private
63 */
64 this.$imageWrapper = $imageWrapper;
65
66 /**
67 * Main container of image and metadata, needed to propagate events.
68 *
69 * @property {jQuery}
70 * @private
71 */
72 this.$mainWrapper = $mainWrapper;
73
74 /**
75 * Raw metadata of current image, needed for canvas size calculations.
76 *
77 * @property {LightboxImage}
78 * @private
79 */
80 this.imageRawMetadata = null;
81 }
82
83 /**
84 * Clears everything.
85 */
86 empty() {
87 this.$imageDiv.addClass( 'empty' ).removeClass( 'error' );
88
89 this.$imageDiv.empty();
90 }
91
92 /**
93 * Sets image on the canvas; does not resize it to fit. This is used to make the placeholder
94 * image available; it will be resized and displayed by #maybeDisplayPlaceholder().
95 * FIXME maybeDisplayPlaceholder() receives the placeholder so it is very unclear why this
96 * is necessary at all (apart from setting the LightboxImage, which is used in size calculations).
97 *
98 * @param {LightboxImage} imageRawMetadata
99 * @param {jQuery} $imageElement
100 */
101 set( imageRawMetadata, $imageElement ) {
102 this.$imageDiv.removeClass( 'empty' );
103
104 this.imageRawMetadata = imageRawMetadata;
105 this.$image = $imageElement;
106 this.setUpImageClick();
107
108 this.$imageDiv.html( this.$image );
109 }
110
111 /**
112 * Resizes image to the given dimensions and displays it on the canvas.
113 * This is used to display the actual image; it assumes set function was already called before.
114 *
115 * @param {Thumbnail} thumbnail thumbnail information
116 * @param {HTMLImageElement} imageElement
117 * @param {ThumbnailWidth} imageWidths
118 */
119 setImageAndMaxDimensions( thumbnail, imageElement, imageWidths ) {
120 const $image = $( imageElement );
121
122 // we downscale larger images but do not scale up smaller ones, that would look ugly
123 if ( thumbnail.width > imageWidths.cssWidth ) {
124 imageElement.width = imageWidths.cssWidth;
125 imageElement.height = imageWidths.cssHeight;
126 }
127
128 if ( !this.$image.is( imageElement ) ) { // http://bugs.jquery.com/ticket/4087
129 this.$image.replaceWith( $image );
130 this.$image = $image;
131
132 // Since the image element got replaced, we need to rescue the dialog-open class.
133 this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen );
134
135 this.setUpImageClick();
136 }
137 }
138
139 /**
140 * Handles a "dialog open/close" event from dialogs on the page.
141 *
142 * @param {jQuery.Event} e
143 */
144 handleDialogEvent( e ) {
145 switch ( e.type ) {
146 case 'mmv-download-opened':
147 this.downloadOpen = true;
148 break;
149 case 'mmv-download-closed':
150 this.downloadOpen = false;
151 break;
152 case 'mmv-reuse-opened':
153 this.reuseOpen = true;
154 break;
155 case 'mmv-reuse-closed':
156 this.reuseOpen = false;
157 break;
158 }
159
160 this.dialogOpen = this.reuseOpen || this.downloadOpen;
161 this.$image.toggleClass( 'mw-mmv-dialog-is-open', this.dialogOpen );
162 }
163
164 /**
165 * Registers click listener on the image.
166 *
167 * @fires ReuseDialog#mmv-reuse-opened
168 * @fires ReuseDialog#mmv-reuse-closed
169 * @fires DownloadDialog#mmv-download-opened
170 * @fires DownloadDialog#mmv-download-closed
171 */
172 setUpImageClick() {
173 this.handleEvent( 'mmv-reuse-opened', this.handleDialogEvent.bind( this ) );
174 this.handleEvent( 'mmv-reuse-closed', this.handleDialogEvent.bind( this ) );
175 this.handleEvent( 'mmv-download-opened', this.handleDialogEvent.bind( this ) );
176 this.handleEvent( 'mmv-download-closed', this.handleDialogEvent.bind( this ) );
177
178 this.$image.on( 'click.mmv-canvas', ( e ) => {
179 // ignore clicks if the metadata panel or one of the dialogs is open - assume the intent is to
180 // close it in this case; that will be handled elsewhere
181 if ( !this.dialogOpen &&
182 // FIXME a UI component should not know about its parents
183 this.$container.closest( '.metadata-panel-is-open' ).length === 0 ) {
184 e.stopPropagation(); // don't let $imageWrapper handle this
185 $( document ).trigger( 'mmv-viewfile' );
186 }
187 } );
188
189 // open the download panel on right clicking the image
190 this.$image.on( 'mousedown.mmv-canvas', ( e ) => {
191 if ( e.which === 3 && !this.downloadOpen ) {
192 $( document ).trigger( 'mmv-download-open', e );
193 e.stopPropagation();
194 }
195 } );
196 }
197
198 /**
199 * Registers listeners.
200 *
201 * @fires MultimediaViewer#mmv-resize-end
202 */
203 attach() {
204 /**
205 * Fired when the screen size changes. Debounced to avoid continuous triggering while resizing with a mouse.
206 *
207 * @event MultimediaViewer#mmv-resize-end
208 */
209 $( window ).on( 'resize.mmv-canvas', mw.util.debounce( () => {
210 this.$mainWrapper.trigger( $.Event( 'mmv-resize-end' ) );
211 }, 100 ) );
212
213 this.$imageWrapper.on( 'click.mmv-canvas', () => {
214 if ( this.$container.closest( '.metadata-panel-is-open' ).length > 0 ) {
215 this.$mainWrapper.trigger( 'mmv-panel-close-area-click' );
216 }
217 } );
218 }
219
220 /**
221 * Clears listeners.
222 */
223 unattach() {
224 this.clearEvents();
225
226 $( window ).off( 'resize.mmv-canvas' );
227
228 this.$imageWrapper.off( 'click.mmv-canvas' );
229 }
230
231 /**
232 * Sets page thumbnail for display if blowupFactor
233 * <= MAX_BLOWUP_FACTOR. Otherwise thumb is not set.
234 * We set SVG files to the maximum screen size available.
235 * Assumes set function called before.
236 *
237 * @param {{width: number, height: number}} size
238 * @param {jQuery} $imagePlaceholder Image placeholder to be displayed while the real image loads.
239 * @param {ThumbnailWidth} imageWidths
240 */
241 maybeDisplayPlaceholder( size, $imagePlaceholder, imageWidths ) {
242 // Assume natural thumbnail size¸
243 let targetWidth = size.width;
244 let targetHeight = size.height;
245
246 // If the image is bigger than the screen we need to resize it
247 if ( size.width > imageWidths.cssWidth ) { // This assumes imageInfo.width in CSS units
248 targetWidth = imageWidths.cssWidth;
249 targetHeight = imageWidths.cssHeight;
250 }
251
252 const blowupFactor = targetWidth / $imagePlaceholder.width();
253 // If the placeholder is too blown up, it's not worth showing it
254 if ( blowupFactor > Canvas.MAX_BLOWUP_FACTOR ) {
255 return;
256 }
257
258 $imagePlaceholder.width( targetWidth );
259 $imagePlaceholder.height( targetHeight );
260 this.set( this.imageRawMetadata, $imagePlaceholder.show() );
261 }
262
263 /**
264 * Displays a message and error icon when loading the image fails.
265 *
266 * @param {string} error error message
267 */
268 showError( error ) {
269 const $retryLink = $( '<a>' ).addClass( 'mw-mmv-retry-link' ).text(
270 mw.msg( 'multimediaviewer-thumbnail-error-retry' ) );
271
272 this.$imageDiv.empty()
273 .addClass( 'error' )
274 .append(
275 $( '<div>' ).addClass( 'error-box' ).append(
276 $( '<div>' ).addClass( 'mw-mmv-error-text' ).text(
277 mw.msg( 'multimediaviewer-thumbnail-error' )
278 )
279 ).append(
280 $( '<div>' ).addClass( 'mw-mmv-error-description' ).append(
281 mw.msg( 'multimediaviewer-thumbnail-error-description',
282 HtmlUtils.jqueryToHtml( $retryLink ),
283 error
284 )
285 )
286 )
287 );
288 this.$imageDiv.find( '.mw-mmv-retry-link' ).on( 'click', () => {
289 location.reload();
290 } );
291 }
292
293 /**
294 * Returns width and height of the canvas area (i.e. the space available for the image).
295 *
296 * @return {Object} Width and height in CSS pixels
297 */
298 getDimensions() {
299 const $window = $( window );
300 // eslint-disable-next-line no-jquery/no-global-selector
301 const $aboveFold = $( '.mw-mmv-above-fold' );
302 const isFullscreened = !!$aboveFold.closest( '.jq-fullscreened' ).length;
303 // Don't rely on this.$imageWrapper's sizing because it's fragile.
304 // Depending on what the wrapper contains, its size can be 0 on some browsers.
305 // Therefore, we calculate the available space manually
306 const availableWidth = $window.width();
307 const availableHeight = $window.height() - ( isFullscreened ? 0 : $aboveFold.outerHeight() );
308
309 return {
310 width: availableWidth,
311 height: availableHeight
312 };
313 }
314
315 /**
316 * Gets the widths for a given lightbox image.
317 *
318 * @param {LightboxImage} image
319 * @return {ThumbnailWidth}
320 */
321 getLightboxImageWidths( image ) {
322 const thumb = image.thumbnail;
323 const canvasDimensions = this.getDimensions();
324
325 return this.thumbnailWidthCalculator.calculateWidths(
326 canvasDimensions.width,
327 canvasDimensions.height,
328 image.originalWidth || thumb.width,
329 image.originalHeight || thumb.height
330 );
331 }
332
333 /**
334 * Gets the widths for the current lightbox image.
335 *
336 * @return {ThumbnailWidth}
337 */
338 getCurrentImageWidths() {
339 return this.getLightboxImageWidths( this.imageRawMetadata );
340 }
341 }
342
343 /**
344 * Maximum blowup factor tolerated
345 *
346 * @property {number} MAX_BLOWUP_FACTOR
347 * @static
348 */
349 Canvas.MAX_BLOWUP_FACTOR = 11;
350
351 module.exports = Canvas;