Debian Codegraph @

extensions/MultimediaViewer/resources/mmv.ui.reuse/mmv.ui.download.pane.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 EmbedFileFormatter = require( './mmv.EmbedFileFormatter.js' );
19 const Utils = require( './mmv.ui.utils.js' );
20 const { UiElement } = require( 'mmv' );
21
22 /**
23 * UI component that provides functionality to download the media asset displayed.
24 */
25 class DownloadPane extends UiElement {
26 /**
27 * @param {jQuery} $container
28 */
29 constructor( $container ) {
30 super( $container );
31
32 this.formatter = new EmbedFileFormatter();
33 this.createDownloadSection( this.$container );
34 this.createAttributionButton( this.$container );
35
36 /** @property {ImageModel|null} Image the download button currently points to. */
37 this.image = null;
38 }
39
40 /**
41 * Creates dialog download section.
42 *
43 * @param {jQuery} $container
44 */
45 createDownloadSection( $container ) {
46 const $header = $( '<div>' )
47 .addClass( 'cdx-dialog__header' )
48 .appendTo( $container );
49 $( '<div>' )
50 .addClass( 'cdx-dialog__header__title' )
51 .text( mw.msg( 'multimediaviewer-download-link' ) )
52 .appendTo( $header );
53
54 const $body = $( '<div>' )
55 .addClass( 'cdx-dialog__body mw-mmv-pt-0' )
56 .appendTo( $container );
57
58 this.$downloadArea = $( '<div>' )
59 .addClass( 'mw-mmv-flex mw-mmv-gap-50' )
60 .appendTo( $body );
61 this.createSizePulldownMenu( this.$downloadArea );
62 this.createDownloadButton( this.$downloadArea );
63
64 const $p = $( '<p>' ).addClass( 'mw-mmv-mt-75' ).appendTo( $body );
65 this.createPreviewLink( $p );
66 }
67
68 /**
69 * Creates download split button. It is a link with the "download" property set plus an
70 * arrow that allows the user to select the image size desired. The "download" property
71 * triggers native browser downloading in browsers that support it. The fallback is the
72 * 'download' parameter which instructs the server to send the right headers so the browser
73 * downloads the file instead of just displaying it. If all this fails, the image will appear
74 * in another window/tab.
75 *
76 * @param {jQuery} $container
77 */
78 createDownloadButton( $container ) {
79 this.$downloadButton = $( '<a>' )
80 .attr( 'target', '_blank' )
81 .attr( 'download', '' )
82 .addClass( 'cdx-button cdx-button--weight-primary cdx-button--action-progressive cdx-button--fake-button cdx-button--fake-button--enabled' )
83 .html( '<span class="cdx-button__icon cdx-button__icon--download" aria-hidden="true"></span>' + mw.message( 'multimediaviewer-download' ).escaped() )
84 .appendTo( $container );
85 }
86
87 /**
88 * Creates pulldown menu to select image sizes.
89 *
90 * @param {jQuery} $container
91 */
92 createSizePulldownMenu( $container ) {
93 this.$downloadSizeMenu = Utils.createSelectMenu(
94 [ 'original', 'small', 'medium', 'large', 'xl' ],
95 'original'
96 ).appendTo( $container );
97 }
98
99 /**
100 * Creates preview link.
101 *
102 * @param {jQuery} $container
103 */
104 createPreviewLink( $container ) {
105 this.$previewLink = $( '<a>' )
106 .attr( 'target', '_blank' )
107 .addClass( 'cdx-docs-link' )
108 .text( mw.msg( 'multimediaviewer-download-preview-link-title' ) )
109 .appendTo( $container );
110 }
111
112 createAttributionButton( $container ) {
113 [ this.$attributionInput, this.$attributionInputDiv ] = Utils.createInputWithCopy(
114 mw.msg( 'multimediaviewer-download-attribution-copy' ), ''
115 );
116
117 const $header = $( '<div>' )
118 .addClass( 'cdx-dialog__header' )
119 .appendTo( $container );
120 $( '<p>' )
121 .addClass( 'cdx-dialog__header__title' )
122 .text( mw.msg( 'multimediaviewer-download-attribution' ) )
123 .appendTo( $header );
124 this.$attributionHowHeader = $( '<p>' )
125 .addClass( 'mw-mmv-mb-75' )
126 .text( mw.msg( 'multimediaviewer-download-attribution-cta-header' ) );
127
128 const $attributionTabs = $( '<div>' ).addClass( 'cdx-tabs' );
129 const $attributionTabsHeader = $( '<div>' ).addClass( 'cdx-tabs__header' ).appendTo( $attributionTabs );
130 this.$attributionTabsList = $( '<div>' ).addClass( 'cdx-tabs__list' ).attr( 'role', 'tablist' ).appendTo( $attributionTabsHeader );
131 [ 'plain', 'html' ].forEach( ( name ) => $( '<button>' )
132 .addClass( 'cdx-tabs__list__item' )
133 .attr( 'role', 'tab' )
134 .data( 'name', name )
135 // The following messages are used here:
136 // * multimediaviewer-attr-plain
137 // * multimediaviewer-attr-html
138 .text( mw.msg( 'multimediaviewer-attr-' + name ) )
139 .on( 'click', () => this.selectAttribution( name ) )
140 .appendTo( this.$attributionTabsList )
141 );
142 this.selectAttribution( 'plain' );
143
144 $( '<div>' )
145 .addClass( 'cdx-dialog__body mw-mmv-pt-0 mw-mmv-pb-150' )
146 .append(
147 this.$attributionHowHeader,
148 $attributionTabs,
149 this.$attributionInputDiv.addClass( ' mw-mmv-mt-75' )
150 )
151 .appendTo( $container );
152 }
153
154 /**
155 * Selects the specified attribution type.
156 *
157 * @param {string} [name='plain'] The attribution type to use ('plain' or 'html')
158 */
159 selectAttribution( name ) {
160 this.currentAttrView = name;
161
162 this.$attributionTabsList.children().each( ( _i, element ) => {
163 const $element = $( element );
164 $element.attr( 'aria-selected', $element.data( 'name' ) === name );
165 } );
166
167 if ( this.currentAttrView === 'html' ) {
168 this.$attributionInput.val( this.htmlCredit );
169 } else {
170 this.$attributionInput.val( this.textCredit );
171 }
172 }
173
174 /**
175 * Registers listeners.
176 */
177 attach() {
178 // Register handlers for switching between file sizes
179 this.$downloadSizeMenu.on( 'change', () => this.handleSizeSwitch() );
180 }
181
182 /**
183 * Clears listeners.
184 */
185 unattach() {
186 super.unattach();
187 this.$downloadSizeMenu.off( 'change' );
188 }
189
190 /**
191 * Handles size menu change events.
192 */
193 handleSizeSwitch() {
194 // eslint-disable-next-line no-jquery/no-sizzle
195 const $option = this.$downloadSizeMenu.find( ':selected' );
196 const value = {
197 name: $option.data( 'name' ),
198 width: $option.data( 'width' ),
199 height: $option.data( 'height' )
200 };
201
202 if ( value.name === 'original' && this.image !== null ) {
203 this.setDownloadUrl( this.image.url );
204 } else {
205 // Disable download while we get the image
206 this.$downloadButton.addClass( 'disabledLink' );
207 Utils.getThumbnailUrlPromise( value.width ).done( ( thumbnail ) => {
208 this.setDownloadUrl( thumbnail.url );
209 } );
210 }
211 }
212
213 /**
214 * Sets the URL on the download button.
215 *
216 * @param {string} url
217 */
218 setDownloadUrl( url ) {
219 this.$downloadButton.attr( 'href', `${ url }?download` );
220 this.$previewLink.attr( 'href', url );
221
222 // Re-enable download
223 this.$downloadButton.removeClass( 'disabledLink' );
224 }
225
226 /**
227 * Sets the text in the attribution input element.
228 *
229 * @param {ImageModel} imageInfo
230 */
231 setAttributionText( imageInfo ) {
232 this.htmlCredit = this.formatter.getCreditHtml( imageInfo );
233 this.textCredit = this.formatter.getCreditText( imageInfo );
234 this.selectAttribution( this.currentAttrView );
235 }
236
237 /**
238 * Chops off the extension part of an URL.
239 *
240 * @param {string} url URL
241 * @return {string} Extension
242 */
243 getExtensionFromUrl( url ) {
244 const urlParts = url.split( '.' );
245 return urlParts[ urlParts.length - 1 ];
246 }
247
248 /**
249 * Sets the data on the element.
250 *
251 * @param {ImageModel} image
252 */
253 set( image ) {
254 const license = image && image.license;
255 const sizes = Utils.getPossibleImageSizesForHtml( image.width, image.height );
256
257 this.image = image;
258
259 Utils.updateSelectOptions( sizes, this.$downloadSizeMenu.children() );
260
261 // Note: This extension will not be the real one for file types other than: png/gif/jpg/jpeg
262 this.imageExtension = image.title.getExtension().toLowerCase();
263
264 // Reset size menu to default item and update download button label now that we have the info
265 this.$downloadSizeMenu.val( 'original' );
266 this.handleSizeSwitch();
267
268 if ( image ) {
269 this.setAttributionText( image );
270 }
271
272 const attributionCtaMessage = ( license && license.needsAttribution() ) ?
273 'multimediaviewer-download-attribution-cta-header' :
274 'multimediaviewer-download-optional-attribution-cta-header';
275 // Message defined above
276 // eslint-disable-next-line mediawiki/msg-doc
277 this.$attributionHowHeader.text( mw.msg( attributionCtaMessage ) );
278 }
279
280 /**
281 * @inheritdoc
282 */
283 empty() {
284 this.$downloadButton.attr( 'href', '' );
285 this.$previewLink.attr( 'href', '' );
286 this.imageExtension = undefined;
287
288 this.image = null;
289 }
290 }
291
292 module.exports = DownloadPane;