Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer 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 * MultimediaViewer 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 MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const Description = require( './mmv.ui.description.js' );
19 const UiElement = require( './mmv.ui.js' );
20 const MetadataPanelScroller = require( './mmv.ui.metadataPanelScroller.js' );
21 const Permission = require( './mmv.ui.permission.js' );
22 const ProgressBar = require( './mmv.ui.progressBar.js' );
23 const StripeButtons = require( './mmv.ui.stripeButtons.js' );
24 const TruncatableTextField = require( './mmv.ui.truncatableTextField.js' );
25
26 /**
27 * Represents the metadata panel in the viewer
28 */
29 class MetadataPanel extends UiElement {
30 /**
31 * @param {jQuery} $container The container for the panel (.mw-mmv-post-image).
32 * @param {jQuery} $aboveFold The brighter headline of the metadata panel (.mw-mmv-above-fold).
33 * Called "aboveFold" for historical reasons, but actually a part of the next sibling of the element
34 * is also above the fold (bottom of the screen).
35 */
36 constructor( $container, $aboveFold ) {
37 super( $container );
38
39 this.$aboveFold = $aboveFold;
40
41 this.initializeHeader();
42 this.initializeImageMetadata();
43 }
44
45 /**
46 * FIXME this should be in the jquery.fullscreen plugin.
47 *
48 * @return {boolean}
49 */
50 isFullscreened() {
51 return $( this.$container ).closest( '.jq-fullscreened' ).length > 0;
52 }
53
54 attach() {
55 this.scroller.attach();
56 this.buttons.attach();
57 this.title.attach();
58 this.creditField.attach();
59
60 this.$title
61 .add( this.$authorAndSource )
62 .add( this.title.$ellipsis )
63 .add( this.creditField.$ellipsis )
64 .on( 'click.mmv-mp', ( e ) => {
65 const clickTargetIsLink = $( e.target ).is( 'a' );
66 const clickTargetIsTruncated = !!$( e.target ).closest( '.mw-mmv-ttf-truncated' ).length;
67 const someTextIsExpanded = !!$( e.target ).closest( '.mw-mmv-untruncated' ).length;
68
69 if ( !clickTargetIsLink && // don't interfere with clicks on links in the text
70 clickTargetIsTruncated && // don't expand when non-truncated text is clicked
71 !someTextIsExpanded // ignore clicks if text is already expanded
72 ) {
73 if ( this.isFullscreened() ) {
74 this.revealTruncatedText();
75 } else {
76 this.scroller.toggle( 'up' );
77 }
78 }
79 } );
80
81 $( this.$container ).on( 'mmv-metadata-open.mmv-mp mmv-metadata-reveal-truncated-text.mmv-mp', () => {
82 this.revealTruncatedText();
83 } ).on( 'mmv-metadata-close.mmv-mp', () => {
84 this.hideTruncatedText();
85 } ).on( 'mouseleave.mmv-mp', () => {
86 if ( this.isFullscreened() ) {
87 const duration = parseFloat( this.$container.css( 'transition-duration' ) ) * 1000 || 0;
88 this.panelShrinkTimeout = setTimeout( () => {
89 this.hideTruncatedText();
90 }, duration );
91 }
92 } ).on( 'mouseenter.mmv-mp', () => {
93 clearTimeout( this.panelShrinkTimeout );
94 } ).on( 'mmv-permission-grow.mmv-mp', () => {
95 this.$permissionLink
96 .text( mw.msg( 'multimediaviewer-permission-link-hide' ) );
97 } ).on( 'mmv-permission-shrink.mmv-mp', () => {
98 this.$permissionLink
99 .text( mw.msg( 'multimediaviewer-permission-link' ) );
100 } );
101
102 this.handleEvent( 'fullscreenchange.lip', () => {
103 this.hideTruncatedText();
104 } );
105 }
106
107 unattach() {
108 this.scroller.freezeHeight();
109
110 this.$title
111 .add( this.title.$ellipsis )
112 .add( this.$authorAndSource )
113 .add( this.creditField.$ellipsis )
114 .off( 'click.mmv-mp' );
115
116 $( this.$container ).off( '.mmv-mp' );
117
118 this.scroller.unattach();
119 this.buttons.unattach();
120 this.clearEvents();
121 }
122
123 empty() {
124 this.scroller.freezeHeight();
125 this.scroller.empty();
126
127 this.buttons.empty();
128
129 this.description.empty();
130 this.permission.empty();
131
132 this.$title.removeClass( 'error' );
133 this.title.empty();
134 this.creditField.empty();
135
136 this.$license.empty().prop( 'href', '#' );
137 this.$licenseLi.addClass( 'empty' );
138 this.$permissionLink.hide();
139 this.$restrictions.children().hide();
140
141 this.$filename.empty();
142 this.$filenamePrefix.empty();
143 this.$filenameLi.addClass( 'empty' );
144
145 this.$datetimeCreated.empty();
146 this.$datetimeCreatedLi.addClass( 'empty' );
147 this.$datetimeUpdated.empty();
148 this.$datetimeUpdatedLi.addClass( 'empty' );
149
150 this.$location.empty();
151 this.$locationLi.addClass( 'empty' );
152
153 this.progressBar.empty();
154
155 this.$container.removeClass( 'mw-mmv-untruncated' );
156 }
157
158 /* Initialization methods */
159 /**
160 * Initializes the header, which contains the title, credit, and license elements.
161 */
162 initializeHeader() {
163 this.progressBar = new ProgressBar( this.$aboveFold );
164
165 this.scroller = new MetadataPanelScroller( this.$container, this.$aboveFold );
166
167 this.$titleDiv = $( '<div>' )
168 .addClass( 'mw-mmv-title-contain' )
169 .appendTo( this.$aboveFold );
170
171 this.$container.append( this.$aboveFold );
172
173 this.initializeButtons(); // float, needs to be on top
174 this.initializeTitle();
175 }
176
177 /**
178 * Initializes the title elements.
179 */
180 initializeTitle() {
181 this.$titlePara = $( '<p>' )
182 .addClass( 'mw-mmv-title-para mw-parser-output' )
183 .appendTo( this.$aboveFold );
184
185 this.$title = $( '<span>' )
186 .addClass( 'mw-mmv-title' );
187
188 this.title = new TruncatableTextField( this.$titlePara, this.$title, {
189 styles: [ 'mw-mmv-title-small', 'mw-mmv-title-smaller' ]
190 } );
191 this.title.setTitle(
192 mw.message( 'multimediaviewer-title-popup-text' ),
193 mw.message( 'multimediaviewer-title-popup-text-more' )
194 );
195
196 this.$title.add( this.title.$ellipsis );
197 }
198
199 initializeButtons() {
200 this.buttons = new StripeButtons( this.$titleDiv );
201 }
202
203 /**
204 * Initializes the main body of metadata elements.
205 */
206 initializeImageMetadata() {
207 this.$container.addClass( 'mw-mmv-ttf-ellipsis-container' );
208
209 this.$imageMetadata = $( '<div>' )
210 .addClass( 'mw-mmv-image-metadata' )
211 .appendTo( this.$container );
212
213 this.$imageMetadataLeft = $( '<div>' )
214 .addClass( 'mw-mmv-image-metadata-column mw-mmv-image-metadata-desc-column' )
215 .appendTo( this.$imageMetadata );
216
217 this.$imageMetadataRight = $( '<div>' )
218 .addClass( 'mw-mmv-image-metadata-column mw-mmv-image-metadata-links-column' )
219 .appendTo( this.$imageMetadata );
220
221 this.initializeCredit();
222 this.description = new Description( this.$imageMetadataLeft );
223 this.permission = new Permission( this.$imageMetadataLeft, this.scroller );
224 this.initializeImageLinks();
225 }
226
227 /**
228 * Initializes the credit elements.
229 */
230 initializeCredit() {
231 this.$credit = $( '<p>' )
232 .addClass( 'mw-mmv-credit empty' )
233 .appendTo( this.$imageMetadataLeft );
234
235 // we need an inline container for tipsy, otherwise it would be centered weirdly
236 this.$authorAndSource = $( '<span>' )
237 .addClass( 'mw-mmv-source-author' );
238
239 this.creditField = new TruncatableTextField(
240 this.$credit,
241 this.$authorAndSource,
242 { styles: [] }
243 );
244
245 this.creditField.setTitle(
246 mw.message( 'multimediaviewer-credit-popup-text' ),
247 mw.message( 'multimediaviewer-credit-popup-text-more' )
248 );
249
250 this.$authorAndSource.add( this.creditField.$ellipsis );
251 }
252
253 /**
254 * Initializes the list of image metadata on the right side of the panel.
255 */
256 initializeImageLinks() {
257 this.$imageLinkDiv = $( '<div>' )
258 .addClass( 'mw-mmv-image-links-div' )
259 .appendTo( this.$imageMetadataRight );
260
261 this.$imageLinks = $( '<ul>' )
262 .addClass( 'mw-mmv-image-links' )
263 .appendTo( this.$imageLinkDiv );
264
265 this.initializeLicense();
266 this.initializeFilename();
267 this.initializeDatetime();
268 this.initializeLocation();
269 }
270
271 /**
272 * Initializes the license elements.
273 */
274 initializeLicense() {
275 this.$licenseLi = $( '<li>' )
276 .addClass( 'mw-mmv-license-li empty' )
277 .appendTo( this.$imageLinks );
278
279 this.$license = $( '<a>' )
280 .addClass( 'mw-mmv-license' )
281 .prop( 'href', '#' )
282 .appendTo( this.$licenseLi );
283
284 this.$restrictions = $( '<span>' )
285 .addClass( 'mw-mmv-restrictions' )
286 .appendTo( this.$licenseLi );
287
288 this.$permissionLink = $( '<span>' )
289 .addClass( 'mw-mmv-permission-link mw-mmv-label' )
290 .text( mw.msg( 'multimediaviewer-permission-link' ) )
291 .appendTo( this.$licenseLi )
292 .hide()
293 .on( 'click', () => {
294 if ( this.permission.isFullSize() ) {
295 this.permission.shrink();
296 } else {
297 this.permission.grow();
298 this.scroller.toggle( 'up' );
299 }
300 return false;
301 } );
302 }
303
304 /**
305 * Initializes the filename element.
306 */
307 initializeFilename() {
308 this.$filenameLi = $( '<li>' )
309 .addClass( 'mw-mmv-filename-li empty' )
310 .appendTo( this.$imageLinks );
311
312 this.$filenamePrefix = $( '<span>' )
313 .addClass( 'mw-mmv-filename-prefix' )
314 .appendTo( this.$filenameLi );
315
316 this.$filename = $( '<span>' )
317 .addClass( 'mw-mmv-filename' )
318 .appendTo( this.$filenameLi );
319 }
320
321 /**
322 * Initializes the upload date/time element.
323 */
324 initializeDatetime() {
325 this.$datetimeCreatedLi = $( '<li>' )
326 .addClass( 'mw-mmv-datetime-li empty' )
327 .appendTo( this.$imageLinks );
328
329 this.$datetimeCreated = $( '<span>' )
330 .addClass( 'mw-mmv-datetime' )
331 .appendTo( this.$datetimeCreatedLi );
332
333 this.$datetimeUpdatedLi = $( '<li>' )
334 .addClass( 'mw-mmv-datetime-li empty' )
335 .appendTo( this.$imageLinks );
336
337 this.$datetimeUpdated = $( '<span>' )
338 .addClass( 'mw-mmv-datetime' )
339 .appendTo( this.$datetimeUpdatedLi );
340 }
341
342 /**
343 * Initializes the geolocation element.
344 */
345 initializeLocation() {
346 this.$locationLi = $( '<li>' )
347 .addClass( 'mw-mmv-location-li empty' )
348 .appendTo( this.$imageLinks );
349
350 this.$location = $( '<a>' )
351 .addClass( 'mw-mmv-location' )
352 .appendTo( this.$locationLi );
353 }
354
355 /* Setters */
356 /**
357 * Sets the image title at the top of the metadata panel.
358 * The title will be the first one available form the options below:
359 * - the image caption
360 * - the description from the filepage
361 * - the filename (without extension)
362 *
363 * @param {LightboxImage} image
364 * @param {ImageModel} imageData
365 */
366 setTitle( image, imageData ) {
367 let title;
368
369 if ( image.caption ) {
370 title = image.caption;
371 } else if ( imageData.description ) {
372 title = imageData.description;
373 } else {
374 title = image.filePageTitle.getNameText();
375 }
376
377 this.title.set( title );
378 }
379
380 /**
381 * Sets the file name in the panel.
382 *
383 * @param {string} filename The file name to set, without prefix
384 */
385 setFileName( filename ) {
386 this.$filenamePrefix.text(
387 mw.config.get( 'wgFormattedNamespaces' )[
388 mw.config.get( 'wgNamespaceIds' ).file
389 ] + ':'
390 );
391 this.$filename.text( filename );
392
393 this.$filenameLi.removeClass( 'empty' );
394 }
395
396 /**
397 * Set source and author.
398 *
399 * @param {string} attribution Custom attribution string
400 * @param {string} source With unsafe HTML
401 * @param {string} author With unsafe HTML
402 * @param {number} authorCount
403 * @param {string} filepageUrl URL of the file page (used when other data is not available)
404 */
405 setCredit( attribution, source, author, authorCount, filepageUrl ) {
406 // sanitization will be done by TruncatableTextField.set()
407 if ( attribution && ( authorCount <= 1 || !authorCount ) ) {
408 this.creditField.set( this.wrapAttribution( attribution ) );
409 } else if ( author && source ) {
410 this.creditField.set(
411 mw.message(
412 'multimediaviewer-credit',
413 this.wrapAuthor( author, authorCount, filepageUrl ),
414 this.wrapSource( source )
415 ).plain()
416 );
417 } else if ( author ) {
418 this.creditField.set( this.wrapAuthor( author, authorCount, filepageUrl ) );
419 } else if ( source ) {
420 this.creditField.set( this.wrapSource( source ) );
421 } else {
422 this.creditField.set(
423 $( '<a>' )
424 .addClass( 'mw-mmv-credit-fallback' )
425 .prop( 'href', filepageUrl )
426 .text( mw.message( 'multimediaviewer-credit-fallback' ).plain() )
427 .get( 0 ).outerHTML
428 );
429 }
430
431 this.$credit.removeClass( 'empty' );
432 }
433
434 /**
435 * Wraps a source string it with MediaViewer styles
436 *
437 * @param {string} source Warning - unsafe HTML sometimes goes here
438 * @return {string} unsafe HTML
439 */
440 wrapSource( source ) {
441 return $( '<span>' )
442 .addClass( 'mw-mmv-source' )
443 .append( $.parseHTML( source ) )
444 .get( 0 ).outerHTML;
445 }
446
447 /**
448 * Wraps an author string with MediaViewer styles
449 *
450 * @param {string} author Warning - unsafe HTML sometimes goes here
451 * @param {number} authorCount
452 * @param {string} filepageUrl URL of the file page (used when some author data is not available)
453 * @return {string} unsafe HTML
454 */
455 wrapAuthor( author, authorCount, filepageUrl ) {
456 const $wrapper = $( '<span>' )
457 .addClass( 'mw-mmv-author' );
458
459 if ( authorCount > 1 ) {
460 const $moreLink = $( '<a>' )
461 .addClass( 'mw-mmv-more-authors' )
462 .text( mw.msg( 'multimediaviewer-multiple-authors', authorCount - 1 ) )
463 .attr( 'href', filepageUrl );
464 $wrapper.append( mw.message(
465 'multimediaviewer-multiple-authors-combine',
466 $( $.parseHTML( author ) ),
467 $moreLink
468 ).parseDom() );
469 } else {
470 $wrapper.append( author );
471 }
472
473 return $wrapper.get( 0 ).outerHTML;
474 }
475
476 /**
477 * Wraps an attribution string with MediaViewer styles
478 *
479 * @param {string} attribution Warning - unsafe HTML sometimes goes here
480 * @return {string} unsafe HTML
481 */
482 wrapAttribution( attribution ) {
483 return $( '<span>' )
484 .addClass( 'mw-mmv-author' )
485 .addClass( 'mw-mmv-source' )
486 .append( $.parseHTML( attribution ) )
487 .get( 0 ).outerHTML;
488 }
489
490 /**
491 * Sets the license display in the panel
492 *
493 * @param {License|null} license license data (could be missing)
494 * @param {string} filePageUrl URL of the file description page
495 */
496 setLicense( license, filePageUrl ) {
497 let shortName;
498 let url;
499 let isCc;
500 let isPd;
501
502 filePageUrl += `?uselang=${ mw.config.get( 'wgUserLanguage' ) }#${ mw.msg( 'license-header' ) }`;
503
504 if ( license ) {
505 shortName = license.getShortName();
506 url = license.deedUrl || filePageUrl;
507 isCc = license.isCc();
508 isPd = license.isPd();
509 } else {
510 shortName = mw.msg( 'multimediaviewer-license-default' );
511 url = filePageUrl;
512 isCc = isPd = false;
513 }
514
515 this.$license
516 .text( shortName )
517 .prop( 'href', url )
518 .prop( 'target', license && license.deedUrl ? '_blank' : '' );
519
520 this.$licenseLi
521 .toggleClass( 'cc-license', isCc )
522 .toggleClass( 'pd-license', isPd )
523 .removeClass( 'empty' );
524 }
525
526 /**
527 * Set an extra permission text which should be displayed.
528 *
529 * @param {string} permission
530 */
531 setPermission( permission ) {
532 this.$permissionLink.show();
533 this.permission.set( permission );
534 }
535
536 /**
537 * Sets any special restrictions that should be displayed.
538 *
539 * @param {string[]} restrictions Array of restrictions
540 */
541 setRestrictions( restrictions ) {
542 const restrictionsSet = {};
543 let showDefault = false;
544 let validRestrictions = 0;
545
546 restrictions.forEach( ( value, index ) => {
547 // The following messages are used here:
548 // * multimediaviewer-restriction-2257
549 // * multimediaviewer-restriction-aus-reserve
550 // * multimediaviewer-restriction-communist
551 // * multimediaviewer-restriction-costume
552 // * multimediaviewer-restriction-currency
553 // * multimediaviewer-restriction-design
554 // * multimediaviewer-restriction-fan-art
555 // * multimediaviewer-restriction-ihl
556 // * multimediaviewer-restriction-insignia
557 // * multimediaviewer-restriction-ita-mibac
558 // * multimediaviewer-restriction-nazi
559 // * multimediaviewer-restriction-personality
560 // * multimediaviewer-restriction-trademarked
561 // * multimediaviewer-restriction-default
562 // * multimediaviewer-restriction-default-and-others
563 if ( !mw.message( `multimediaviewer-restriction-${ value }` ).exists() || value === 'default' || index + 1 > MetadataPanel.MAX_RESTRICT ) {
564 showDefault = true; // If the restriction isn't defined or there are more than MAX_RESTRICT of them, show a generic symbol at the end
565 return;
566 }
567 if ( restrictionsSet[ value ] ) {
568 return; // Only show one of each symbol
569 } else {
570 restrictionsSet[ value ] = true;
571 }
572
573 this.$restrictions.append( this.createRestriction( value ) );
574 validRestrictions++; // See how many defined restrictions are added so we know which default i18n msg to use
575 } );
576
577 if ( showDefault ) {
578 if ( validRestrictions ) {
579 this.$restrictions.append( this.createRestriction( 'default-and-others' ) );
580 } else {
581 this.$restrictions.append( this.createRestriction( 'default' ) );
582 }
583 }
584 }
585
586 /**
587 * Helper function that generates restriction labels
588 *
589 * @param {string} type Restriction type
590 * @return {jQuery} jQuery object of label
591 */
592 createRestriction( type ) {
593 mw.loader.using( 'mmv.ui.restriction', () => {} ); // for restriction icons
594 const $label = $( '<span>' )
595 .addClass( 'mw-mmv-label mw-mmv-restriction-label' )
596 // Messages duplicated from above for linter
597 // * multimediaviewer-restriction-2257
598 // * multimediaviewer-restriction-aus-reserve
599 // * multimediaviewer-restriction-communist
600 // * multimediaviewer-restriction-costume
601 // * multimediaviewer-restriction-currency
602 // * multimediaviewer-restriction-design
603 // * multimediaviewer-restriction-fan-art
604 // * multimediaviewer-restriction-ihl
605 // * multimediaviewer-restriction-insignia
606 // * multimediaviewer-restriction-ita-mibac
607 // * multimediaviewer-restriction-nazi
608 // * multimediaviewer-restriction-personality
609 // * multimediaviewer-restriction-trademarked
610 // * multimediaviewer-restriction-default
611 // * multimediaviewer-restriction-default-and-others
612 .prop( 'title', mw.msg( `multimediaviewer-restriction-${ type }` ) );
613
614 $( '<span>' )
615 // The following classes are used here:
616 // * mw-mmv-restriction-2257
617 // * mw-mmv-restriction-aus-reserve
618 // * mw-mmv-restriction-communist
619 // * mw-mmv-restriction-costume
620 // * mw-mmv-restriction-currency
621 // * mw-mmv-restriction-design
622 // * mw-mmv-restriction-fan-art
623 // * mw-mmv-restriction-ihl
624 // * mw-mmv-restriction-insignia
625 // * mw-mmv-restriction-ita-mibac
626 // * mw-mmv-restriction-nazi
627 // * mw-mmv-restriction-personality
628 // * mw-mmv-restriction-trademarked:after
629 // * mw-mmv-restriction-default
630 .addClass( `mw-mmv-restriction-label-inner mw-mmv-restriction-${ type === 'default-and-others' ? 'default' : type }` )
631 .text( mw.msg( `multimediaviewer-restriction-${ type }` ) )
632 .appendTo( $label );
633
634 return $label;
635 }
636
637 /**
638 * Sets location data in the interface.
639 *
640 * @param {ImageModel} imageData
641 */
642 setLocationData( imageData ) {
643 if ( !imageData.hasCoords() ) {
644 return;
645 }
646
647 /**
648 * Form at latitude or longitude as deg/min/sec array
649 *
650 * @param {number} value latitude or longitude
651 * @return {string[]} formatted deg/min/sec array
652 */
653 function convertDegMinSec( value ) {
654 value = Math.abs( value );
655 const deg = Math.floor( value );
656 value -= deg;
657 const min = Math.floor( value * 60 );
658 value -= min / 60;
659 const sec = Math.round( value * 100 * 60 * 60 ) / 100;
660 return [ deg, min, sec ].map( ( n ) => mw.language.convertNumber( n ) );
661 }
662
663 this.$location.text(
664 mw.msg( 'multimediaviewer-geolocation',
665 mw.msg( 'multimediaviewer-geoloc-coords',
666 mw.msg( 'multimediaviewer-geoloc-coord',
667 ...convertDegMinSec( imageData.latitude ),
668 // The following messages are used here:
669 // * multimediaviewer-geoloc-north
670 // * multimediaviewer-geoloc-south
671 mw.msg( `multimediaviewer-geoloc-${ imageData.latitude >= 0 ? 'north' : 'south' }` )
672 ),
673
674 mw.msg( 'multimediaviewer-geoloc-coord',
675 ...convertDegMinSec( imageData.longitude ),
676 // The following messages are used here:
677 // * multimediaviewer-geoloc-east
678 // * multimediaviewer-geoloc-west
679 mw.msg( `multimediaviewer-geoloc-${ imageData.longitude >= 0 ? 'east' : 'west' }` )
680 )
681 )
682 )
683 );
684
685 this.$location.prop( 'href', (
686 'https://geohack.toolforge.org/geohack.php?pagename=' +
687 `${ imageData.title.getPrefixedText()
688 }&params=${
689 Math.abs( imageData.latitude ) }${ imageData.latitude >= 0 ? '_N_' : '_S_'
690 }${ Math.abs( imageData.longitude ) }${ imageData.longitude >= 0 ? '_E_' : '_W_'
691 }&language=${ encodeURIComponent( mw.config.get( 'wgUserLanguage' ) ) }`
692 ) );
693
694 this.$locationLi.removeClass( 'empty' );
695 }
696
697 /**
698 * Set all the image information in the panel
699 *
700 * @param {LightboxImage} image
701 * @param {ImageModel} imageData
702 */
703 setImageInfo( image, imageData ) {
704 if ( imageData.creationDateTime ) {
705 this.$datetimeCreated.text(
706 mw.msg( 'multimediaviewer-datetime-created', this.formatDate( imageData.creationDateTime ) )
707 );
708 this.$datetimeCreatedLi.removeClass( 'empty' );
709 }
710 if ( imageData.uploadDateTime ) {
711 this.$datetimeUpdated.text(
712 mw.msg( 'multimediaviewer-datetime-uploaded', this.formatDate( imageData.uploadDateTime ) )
713 );
714 this.$datetimeUpdatedLi.removeClass( 'empty' );
715 }
716
717 this.buttons.set( image, imageData );
718 this.description.set( imageData.description, image.caption );
719
720 this.setLicense( imageData.license, imageData.descriptionUrl );
721
722 this.setFileName( imageData.title.getMainText() );
723
724 // these handle text truncation and should be called when everything that can push text down
725 // (e.g. floated buttons) has already been laid out
726 this.setTitle( image, imageData );
727 this.setCredit( imageData.attribution, imageData.source, imageData.author, imageData.authorCount, imageData.descriptionUrl );
728
729 if ( imageData.permission ) {
730 this.setPermission( imageData.permission );
731 }
732
733 if ( imageData.restrictions ) {
734 this.setRestrictions( imageData.restrictions );
735 }
736
737 this.setLocationData( imageData );
738
739 this.resetTruncatedText();
740 this.scroller.unfreezeHeight();
741 }
742
743 /**
744 * Show an error message, in case the data could not be loaded
745 *
746 * @param {string} title image title
747 * @param {string} error error message
748 */
749 showError( title, error ) {
750 this.$credit.text( mw.msg( 'multimediaviewer-metadata-error', error ) );
751 this.$title.html( title );
752 }
753
754 /**
755 * Transforms a date string into localized, human-readable format.
756 * Unrecognized strings are returned unchanged.
757 *
758 * @param {string} dateString
759 * @return {string} formatted date
760 */
761 formatDate( dateString ) {
762 let lang = mw.config.get( 'wgUserLanguage' );
763 if ( lang === 'en' || lang === 'qqx' ) {
764 // prefer "D MMMM YYYY" format
765 // avoid passing invalid "qqx" to native toLocaleString(),
766 // which would cause developer's browser locale to be used,
767 // and thus sometimes cause tests to fail.
768 lang = 'en-GB';
769 }
770 if ( dateString.length === 4 ) {
771 // assume yyyy
772 return dateString;
773 }
774 const date = new Date( dateString );
775 try {
776 if ( date instanceof Date && !isNaN( date ) ) {
777 return date.toLocaleString( lang, {
778 day: 'numeric',
779 month: 'long',
780 year: 'numeric',
781 timeZone: 'UTC'
782 } );
783 }
784 } catch ( ignore ) { }
785 // fallback to original date string
786 return dateString;
787 }
788
789 /**
790 * Shows truncated text in the title and credit (this also rearranges the layout a bit).
791 */
792 revealTruncatedText() {
793 if ( this.$container.hasClass( 'mw-mmv-untruncated' ) ) {
794 return;
795 }
796 this.$container.addClass( 'mw-mmv-untruncated' );
797 this.title.grow();
798 this.creditField.grow();
799 }
800
801 /**
802 * Undoes changes made by revealTruncatedText().
803 */
804 hideTruncatedText() {
805 if ( !this.$container.hasClass( 'mw-mmv-untruncated' ) ) {
806 return;
807 }
808 this.title.shrink();
809 this.creditField.shrink();
810 this.$container.removeClass( 'mw-mmv-untruncated' );
811 }
812
813 /**
814 * Hide or reveal truncated text based on whether the panel is open. This is normally handled by
815 * MetadataPanelScroller, but when the panel is reset (e.g. on a prev/next event) sometimes the panel position can change without a panel , such as on a
816 * prev/next event; in such cases this function has to be called.
817 */
818 resetTruncatedText() {
819 if ( this.scroller.panelIsOpen() ) {
820 this.revealTruncatedText();
821 } else {
822 this.hideTruncatedText();
823 }
824 }
825 }
826
827 /**
828 * Maximum number of restriction icons before default icon is used
829 *
830 * @property {number} MAX_RESTRICT
831 * @static
832 */
833 MetadataPanel.MAX_RESTRICT = 4;
834
835 module.exports = MetadataPanel;