Debian Codegraph @

extensions/MultimediaViewer/resources/mmv.ui.reuse/mmv.ui.reuse.embed.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 the user html/wikitext snippets needed to share
24 * and/or embed a media asset.
25 */
26 class Embed extends UiElement {
27 /**
28 * @param {jQuery} $container
29 */
30 constructor( $container ) {
31 super( $container );
32
33 /**
34 * Formatter converting image data into formats needed for output
35 *
36 * @property {EmbedFileFormatter}
37 */
38 this.formatter = new EmbedFileFormatter();
39
40 Utils.createHeader( mw.msg( 'multimediaviewer-embed-tab' ) )
41 .appendTo( $container );
42
43 const $body = $( '<div>' )
44 .addClass( 'cdx-dialog__body mw-mmv-pt-0 mw-mmv-pb-150' )
45 .appendTo( $container );
46
47 this.createSnippetSelectionButtons( $body );
48 this.createSizePulldownMenus( $body );
49 this.createSnippetTextAreas( $body );
50 }
51
52 /**
53 * Creates text areas for html and wikitext snippets.
54 *
55 * @param {jQuery} $container
56 */
57 createSnippetTextAreas( $container ) {
58 [ this.$embedTextHtml, this.$embedTextHtmlDiv ] = Utils.createInputWithCopy(
59 mw.msg( 'multimediaviewer-reuse-copy-embed' ),
60 mw.msg( 'multimediaviewer-reuse-loading-placeholder' )
61 );
62 this.$embedTextHtml.attr( 'title', mw.msg( 'multimediaviewer-embed-explanation' ) );
63
64 [ this.$embedTextWikitext, this.$embedTextWikitextDiv ] = Utils.createInputWithCopy(
65 mw.msg( 'multimediaviewer-reuse-copy-embed' ),
66 mw.msg( 'multimediaviewer-reuse-loading-placeholder' )
67 );
68 this.$embedTextWikitext.attr( 'title', mw.msg( 'multimediaviewer-embed-explanation' ) );
69
70 $container.append(
71 this.$embedTextHtmlDiv,
72 this.$embedTextWikitextDiv
73 );
74 }
75
76 /**
77 * Creates snippet selection buttons.
78 *
79 * @param {jQuery} $container
80 */
81 createSnippetSelectionButtons( $container ) {
82 const $embedSwitch = $( '<div>' ).addClass( 'cdx-tabs mw-mmv-mb-75' ).appendTo( $container );
83 const $embedSwitchHeader = $( '<div>' ).addClass( 'cdx-tabs__header' ).appendTo( $embedSwitch );
84 this.$embedSwitchList = $( '<div>' ).addClass( 'cdx-tabs__list' ).attr( 'role', 'tablist' ).appendTo( $embedSwitchHeader );
85 [ 'wikitext', 'html' ].forEach( ( name ) => $( '<button>' )
86 .addClass( 'cdx-tabs__list__item' )
87 .attr( 'role', 'tab' )
88 .data( 'name', name )
89 .text( mw.msg( name === 'wikitext' ? 'multimediaviewer-embed-wt' : 'multimediaviewer-embed-html' ) )
90 .on( 'click', () => this.handleTypeSwitch( name ) )
91 .appendTo( this.$embedSwitchList )
92 );
93 }
94
95 /**
96 * Creates pulldown menus to select file sizes.
97 *
98 * @param {jQuery} $container
99 */
100 createSizePulldownMenus( $container ) {
101 // Wikitext sizes pulldown menu
102 this.$embedSizeSwitchWikitext = Utils.createSelectMenu(
103 [ 'default', 'small', 'medium', 'large' ],
104 'default'
105 );
106
107 // Html sizes pulldown menu
108 this.$embedSizeSwitchHtml = Utils.createSelectMenu(
109 [ 'small', 'medium', 'large', 'original' ],
110 'original'
111 );
112
113 $( '<div>' ).addClass( 'mw-mmv-flex mw-mmv-mb-75' ).append(
114 this.$embedSizeSwitchHtml,
115 this.$embedSizeSwitchWikitext
116 ).appendTo( $container );
117 }
118
119 /**
120 * Registers listeners.
121 */
122 attach() {
123 // Logged-out defaults to 'html', logged-in to 'wikitext'
124 this.handleTypeSwitch( mw.user.isAnon() ? 'html' : 'wikitext' );
125
126 // Register handlers for switching between file sizes
127 this.$embedSizeSwitchHtml.on( 'change', () => this.handleSizeSwitch() );
128 this.$embedSizeSwitchWikitext.on( 'change', () => this.handleSizeSwitch() );
129 }
130
131 /**
132 * Clears listeners.
133 */
134 unattach() {
135 super.unattach();
136
137 this.$embedSizeSwitchHtml.off( 'change' );
138 this.$embedSizeSwitchWikitext.off( 'change' );
139 }
140
141 /**
142 * Handles size menu change events.
143 */
144 handleSizeSwitch() {
145 // eslint-disable-next-line no-jquery/no-sizzle
146 const $html = this.$embedSizeSwitchHtml.find( ':selected' );
147 if ( $html.length ) {
148 this.updateEmbedHtml( {}, $html.data( 'width' ), $html.data( 'height' ) );
149 }
150 // eslint-disable-next-line no-jquery/no-sizzle
151 const $wikitext = this.$embedSizeSwitchWikitext.find( ':selected' );
152 if ( $wikitext.length ) {
153 this.updateEmbedWikitext( $wikitext.data( 'width' ) );
154 }
155 }
156
157 /**
158 * Handles snippet type switch.
159 *
160 * @param {string} value 'html' or 'wikitext'
161 */
162 handleTypeSwitch( value ) {
163 this.$embedSwitchList.children().each( ( _i, element ) => {
164 const $element = $( element );
165 $element.attr( 'aria-selected', $element.data( 'name' ) === value );
166 } );
167
168 this.$embedTextHtmlDiv.toggle( value === 'html' );
169 this.$embedSizeSwitchHtml.toggle( value === 'html' );
170
171 this.$embedTextWikitextDiv.toggle( value === 'wikitext' );
172 this.$embedSizeSwitchWikitext.toggle( value === 'wikitext' );
173 }
174
175 /**
176 * Reset current menu selection to default item.
177 */
178 resetCurrentSizeMenuToDefault() {
179 this.$embedSizeSwitchWikitext.val( 'default' );
180 this.$embedSizeSwitchHtml.val( 'original' );
181 this.handleSizeSwitch();
182 }
183
184 /**
185 * Sets the HTML embed text.
186 *
187 * Assumes that the set() method has already been called to update this.embedFileInfo
188 *
189 * @param {Thumbnail} thumbnail (can be just an empty object)
190 * @param {number} width New width to set
191 * @param {number} height New height to set
192 */
193 updateEmbedHtml( thumbnail, width, height ) {
194 if ( !this.embedFileInfo ) {
195 return;
196 }
197
198 let src = thumbnail.url || this.embedFileInfo.imageInfo.url;
199
200 // If the image dimension requested are "large", use the current image url
201 if ( width > Embed.LARGE_IMAGE_WIDTH_THRESHOLD || height > Embed.LARGE_IMAGE_HEIGHT_THRESHOLD ) {
202 src = this.embedFileInfo.imageInfo.url;
203 }
204
205 this.$embedTextHtml.val(
206 this.formatter.getThumbnailHtml( this.embedFileInfo, src, width, height )
207 );
208 }
209
210 /**
211 * Updates the wikitext embed text with a new value for width.
212 *
213 * Assumes that the set method has already been called.
214 *
215 * @param {number} width
216 */
217 updateEmbedWikitext( width ) {
218 if ( !this.embedFileInfo ) {
219 return;
220 }
221
222 this.$embedTextWikitext.val(
223 this.formatter.getThumbnailWikitextFromEmbedFileInfo( this.embedFileInfo, width )
224 );
225 }
226
227 /**
228 * @typedef {Object} SizeOptions
229 * @memberof Embed
230 * @property {Utils.ImageSizes} html Collection of possible image sizes for html snippets
231 * @property {Utils.ImageSizes} wikitext Collection of possible image sizes for wikitext snippets
232 */
233
234 /**
235 * Gets size options for html and wikitext snippets.
236 *
237 * @param {number} width
238 * @param {number} height
239 * @return {Embed.SizeOptions}
240 */
241 getSizeOptions( width, height ) {
242 const sizes = {};
243
244 sizes.html = Utils.getPossibleImageSizesForHtml( width, height );
245 sizes.wikitext = this.getPossibleImageSizesForWikitext( width, height );
246
247 return sizes;
248 }
249
250 /**
251 * Sets the data on the element.
252 *
253 * @param {ImageModel} imageInfo
254 * @param {string} [caption]
255 * @param {string} [alt]
256 */
257 set( imageInfo, caption, alt ) {
258 const sizes = this.getSizeOptions( imageInfo.width, imageInfo.height );
259
260 this.embedFileInfo = { imageInfo, caption, alt };
261
262 Utils.updateSelectOptions( sizes.html, this.$embedSizeSwitchHtml.children() );
263 Utils.updateSelectOptions( sizes.wikitext, this.$embedSizeSwitchWikitext.children() );
264
265 // Reset defaults
266 this.resetCurrentSizeMenuToDefault();
267
268 Utils.getThumbnailUrlPromise( this.LARGE_IMAGE_WIDTH_THRESHOLD )
269 .done( ( thumbnail ) => {
270 this.updateEmbedHtml( thumbnail );
271 } );
272 }
273
274 /**
275 * @inheritdoc
276 */
277 empty() {
278 this.$embedTextHtml.val( '' );
279 this.$embedTextWikitext.val( '' );
280
281 this.$embedSizeSwitchHtml.toggle( false );
282 this.$embedSizeSwitchWikitext.toggle( false );
283 }
284
285 /**
286 * Calculates possible image sizes for wikitext snippets. It returns up to
287 * three possible snippet frame sizes (small, medium, large).
288 *
289 * @param {number} width
290 * @param {number} height
291 * @return {Utils.ImageSizes}
292 */
293 getPossibleImageSizesForWikitext( width, height ) {
294 const buckets = {
295 small: 300,
296 medium: 400,
297 large: 500
298 };
299 const sizes = {};
300 const widthToHeight = height / width;
301
302 for ( const bucketName in buckets ) {
303 const bucketWidth = buckets[ bucketName ];
304
305 if ( width > bucketWidth ) {
306 sizes[ bucketName ] = {
307 width: bucketWidth,
308 height: Math.round( bucketWidth * widthToHeight )
309 };
310 }
311 }
312
313 sizes.default = { width: null, height: null };
314
315 return sizes;
316 }
317 }
318
319 /**
320 * @property {number} Width threshold at which an image is to be considered "large"
321 * @static
322 */
323 Embed.LARGE_IMAGE_WIDTH_THRESHOLD = 1200;
324
325 /**
326 * @property {number} Height threshold at which an image is to be considered "large"
327 * @static
328 */
329 Embed.LARGE_IMAGE_HEIGHT_THRESHOLD = 900;
330
331 module.exports = Embed;