Debian Codegraph @

extensions/MultimediaViewer/resources/mmv.ui.reuse/mmv.ui.utils.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 /**
19 * A helper class for reuse logic.
20 */
21 class Utils {
22 /**
23 * Creates header from given options.
24 *
25 * @param {string} text
26 * @return {jQuery}
27 */
28 static createHeader( text ) {
29 const $header = $( '<div>' ).addClass( 'cdx-dialog__header' );
30 $( '<p>' )
31 .addClass( 'cdx-dialog__header__title' )
32 .text( text )
33 .appendTo( $header );
34 return $header;
35 }
36
37 /**
38 * Creates input element with copy button from given options.
39 *
40 * @param {string} title
41 * @param {string} placeholder
42 * @return {jQuery[]} [$input, $div]
43 */
44 static createInputWithCopy( title, placeholder ) {
45 const $input = $( '<input>' )
46 .addClass( 'cdx-text-input__input' )
47 .attr( 'placeholder', placeholder );
48 const $copyButton = $( '<button>' )
49 .addClass( 'cdx-button cdx-button--action-default cdx-button--weight-normal cdx-button--size-medium cdx-button--framed' )
50 .addClass( 'mw-mmv-pt-0 mw-mmv-pb-0' ) // override padding provided by ".oo-ui-buttonElement-framed.oo-ui-labelElement > .oo-ui-buttonElement-button, button"
51 .attr( 'title', title )
52 .append( $( '<span>' ).addClass( 'cdx-button__icon cdx-button__icon--copy' ).attr( 'aria-hidden', 'true' ) )
53 .append( mw.message( 'multimediaviewer-copy-button' ).escaped() )
54 .on( 'click', () => {
55 // navigator.clipboard() is not supported in Safari 11.1, iOS Safari 11.3-11.4
56 if ( navigator.clipboard && navigator.clipboard.writeText ) {
57 navigator.clipboard.writeText( $input.val() );
58 mw.notify( mw.msg( 'mw-widgets-copytextlayout-copy-success' ) );
59 }
60 } );
61 const $div = $( '<div>' )
62 .addClass( 'mw-mmv-flex mw-mmv-gap-50' )
63 .append(
64 $( '<div>' )
65 .addClass( 'cdx-text-input mw-mmv-flex-grow-1' )
66 .append( $input ),
67 $copyButton
68 );
69
70 return [ $input, $div ];
71 }
72
73 /**
74 * Creates select menu from given options.
75 *
76 * @param {string[]} options
77 * @param {string} def
78 * @return {jQuery}
79 */
80 static createSelectMenu( options, def ) {
81 const $select = $( '<select>' ).addClass( 'cdx-select mw-mmv-flex-grow-1' );
82 options.forEach( ( size ) => $( '<option>' )
83 .attr( 'value', size )
84 .data( 'name', size )
85 .text( Utils.getDimensionsMessageHtml( size ) )
86 .appendTo( $select )
87 );
88 $select.val( def );
89 return $select;
90 }
91
92 /**
93 * Gets a promise for the large thumbnail URL. This is needed because thumbnail URLs cannot
94 * be reliably guessed, even if we know the full size of the image - most of the time replacing
95 * the size in another thumbnail URL works (as long as the new size is not larger than the full
96 * size), but if the file name is very long and with the larger size the URL length would
97 * exceed a certain threshold, a different schema is used instead.
98 *
99 * @param {number} width
100 *
101 * @fires MultimediaViewer#mmv-request-thumbnail
102 * @return {jQuery.Promise.<string>}
103 */
104 static getThumbnailUrlPromise( width ) {
105 return $( document ).triggerHandler( 'mmv-request-thumbnail', width ) ||
106 $.Deferred().reject();
107 }
108
109 /**
110 * Updates the select options based on calculated sizes.
111 *
112 * @private
113 * @param {Object} sizes
114 * @param {jQuery} options
115 */
116 static updateSelectOptions( sizes, options ) {
117 for ( let i = 0; i < options.length; i++ ) {
118 const $option = $( options[ i ] );
119 const name = $option.data( 'name' );
120 if ( sizes[ name ] ) {
121 $option.prop( 'disabled', false );
122
123 // These values are later used when the item is selected
124 $option.data( 'width', sizes[ name ].width );
125 $option.data( 'height', sizes[ name ].height );
126
127 $option.text( Utils.getDimensionsMessageHtml( name, sizes[ name ].width, sizes[ name ].height ) );
128 } else {
129 $option.prop( 'disabled', true );
130 $option.text( Utils.getDimensionsMessageHtml( name, undefined, undefined ) );
131 }
132 }
133 }
134
135 /**
136 * @typedef {Object} Dimensions
137 * @memberof Utils
138 * @property {number} width
139 * @property {number} height
140 */
141
142 /**
143 * @typedef {Object} ImageSizes
144 * @memberof Utils
145 * @property {Utils.Dimensions} small
146 * @property {Utils.Dimensions} medium
147 * @property {Utils.Dimensions} large
148 * @property {Utils.Dimensions} [xl] Only present in HTML
149 * @property {Utils.Dimensions} [original] Only present in HTML
150 */
151
152 /**
153 * Calculates possible image sizes for html snippets. It returns up to
154 * three possible snippet frame sizes (small, medium, large) plus the
155 * original image size.
156 *
157 * @param {number} width
158 * @param {number} height
159 * @return {Utils.ImageSizes}
160 */
161 static getPossibleImageSizesForHtml( width, height ) {
162 const buckets = {
163 small: { width: 500, height: 480 },
164 medium: { width: 1280, height: 720 }, // HD ready = 720p
165 large: { width: 1920, height: 1080 }, // Full HD = 1080p
166 xl: { width: 3840, height: 2160 } // 4K = 2160p
167 };
168 const sizes = {};
169 const bucketNames = Object.keys( buckets );
170 const widthToHeight = height / width;
171 const heightToWidth = width / height;
172
173 for ( let i = 0; i < bucketNames.length; i++ ) {
174 const bucketName = bucketNames[ i ];
175 const dimensions = buckets[ bucketName ];
176 const bucketWidth = dimensions.width;
177 const bucketHeight = dimensions.height;
178
179 if ( width > bucketWidth ) {
180 // Width fits in the current bucket
181 const currentGuess = bucketWidth;
182
183 if ( currentGuess * widthToHeight > bucketHeight ) {
184 // Constrain in height, resize width accordingly
185 sizes[ bucketName ] = {
186 width: Math.round( bucketHeight * heightToWidth ),
187 height: bucketHeight
188 };
189 } else {
190 sizes[ bucketName ] = {
191 width: currentGuess,
192 height: Math.round( currentGuess * widthToHeight )
193 };
194 }
195 } else if ( height > bucketHeight ) {
196 // Height fits in the current bucket, resize width accordingly
197 sizes[ bucketName ] = {
198 width: Math.round( bucketHeight * heightToWidth ),
199 height: bucketHeight
200 };
201 }
202 }
203
204 sizes.original = { width: width, height: height };
205
206 return sizes;
207 }
208
209 /**
210 * Generates an i18n message for a label, given a size label and image dimensions
211 *
212 * @param {string} sizeLabel
213 * @param {number} width
214 * @param {number} height
215 * @return {string} i18n label html
216 */
217 static getDimensionsMessageHtml( sizeLabel, width, height ) {
218 const dimensions = !width || !height ? '' : mw.msg(
219 'multimediaviewer-embed-dimensions-separated',
220 mw.msg( 'multimediaviewer-embed-dimensions',
221 width, height ) );
222
223 // The following messages are used here:
224 return mw.msg(
225 // The following messages are used here:
226 // * multimediaviewer-default-embed-dimensions
227 // * multimediaviewer-original-embed-dimensions
228 // * multimediaviewer-xl-embed-dimensions
229 // * multimediaviewer-large-embed-dimensions
230 // * multimediaviewer-medium-embed-dimensions
231 // * multimediaviewer-small-embed-dimensions
232 `multimediaviewer-${ sizeLabel }-embed-dimensions`,
233 dimensions
234 );
235 }
236 }
237
238 module.exports = Utils;