| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class Utils { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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' ) |
| 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 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static getThumbnailUrlPromise( width ) { |
| 105 |
return $( document ).triggerHandler( 'mmv-request-thumbnail', width ) || |
| 106 |
$.Deferred().reject(); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static getPossibleImageSizesForHtml( width, height ) { |
| 162 |
const buckets = { |
| 163 |
small: { width: 500, height: 480 }, |
| 164 |
medium: { width: 1280, height: 720 }, |
| 165 |
large: { width: 1920, height: 1080 }, |
| 166 |
xl: { width: 3840, height: 2160 } |
| 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 |
|
| 181 |
const currentGuess = bucketWidth; |
| 182 |
|
| 183 |
if ( currentGuess * widthToHeight > bucketHeight ) { |
| 184 |
|
| 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 |
|
| 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 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 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 |
|
| 224 |
return mw.msg( |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
`multimediaviewer-${ sizeLabel }-embed-dimensions`, |
| 233 |
dimensions |
| 234 |
); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
module.exports = Utils; |