Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/ui/mmv.ui.stripeButtons.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 UiElement = require( './mmv.ui.js' );
19
20 /**
21 * Class for buttons which are placed on the metadata stripe (the always visible part of the
22 * metadata panel).
23 */
24 class StripeButtons extends UiElement {
25 /**
26 * @param {jQuery} $container the title block (.mw-mmv-title-contain) which wraps the buttons and all
27 * other title elements
28 */
29 constructor( $container ) {
30 super( $container );
31
32 this.$buttonContainer = $( '<div>' )
33 .addClass( 'mw-mmv-stripe-button-container' )
34 .appendTo( $container );
35
36 /**
37 * A button linking to the file description page.
38 */
39 this.$descriptionPage = $( '<a>' )
40 .addClass( 'mw-mmv-stripe-button empty mw-mmv-description-page-button cdx-button cdx-button--weight-primary cdx-button--action-progressive cdx-button--size-large cdx-button--fake-button cdx-button--fake-button--enabled' )
41 // elements are right-floated so we use prepend instead of append to keep the order
42 .prependTo( this.$buttonContainer );
43 }
44
45 /**
46 * @inheritdoc
47 * @param {LightboxImage} image
48 * @param {ImageModel} imageInfo
49 */
50 set( image, imageInfo ) {
51 const match = image && image.src ?
52 image.src.match( /(lang|page)([\d\-a-z]+)-(\d+)px/ ) : // multi lingual SVG or PDF page
53 null;
54
55 const commons = '//commons.wikimedia.org';
56 const isCommonsServer = String( mw.config.get( 'wgServer' ) ).includes( commons );
57 let descriptionUrl = imageInfo.descriptionUrl;
58 let isCommons = String( descriptionUrl ).includes( commons );
59
60 if ( imageInfo.pageID && !isCommonsServer ) {
61 const params = {};
62 if ( match ) {
63 params[ match[ 1 ] ] = match[ 2 ];
64 }
65 // The file has a local description page, override the description URL
66 descriptionUrl = imageInfo.title.getUrl( params );
67 isCommons = false;
68 } else {
69 const parsedUrl = new URL( descriptionUrl, location );
70 if ( match ) {
71 parsedUrl.searchParams.set( match[ 1 ], match[ 2 ] );
72 }
73 descriptionUrl = parsedUrl.toString();
74 }
75
76 this.$descriptionPage.empty()
77 .append( $( '<span>' ).addClass( 'cdx-button__icon' ) )
78 .append( mw.message( 'multimediaviewer-repository-local' ).escaped() )
79 .attr( 'href', descriptionUrl )
80 .removeClass( 'empty' )
81 .toggleClass( 'mw-mmv-repo-button-commons', isCommons );
82 }
83
84 /**
85 * @inheritdoc
86 */
87 empty() {
88 this.$descriptionPage.attr( { href: null, title: null, 'original-title': null } )
89 .addClass( 'empty' )
90 .removeClass( 'mw-mmv-repo-button-commons' );
91 }
92 }
93
94 module.exports = StripeButtons;