| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
class Embed extends UiElement { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
constructor( $container ) { |
| 31 |
super( $container ); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
createSizePulldownMenus( $container ) { |
| 101 |
|
| 102 |
this.$embedSizeSwitchWikitext = Utils.createSelectMenu( |
| 103 |
[ 'default', 'small', 'medium', 'large' ], |
| 104 |
'default' |
| 105 |
); |
| 106 |
|
| 107 |
|
| 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 |
|
| 121 |
|
| 122 |
attach() { |
| 123 |
|
| 124 |
this.handleTypeSwitch( mw.user.isAnon() ? 'html' : 'wikitext' ); |
| 125 |
|
| 126 |
|
| 127 |
this.$embedSizeSwitchHtml.on( 'change', () => this.handleSizeSwitch() ); |
| 128 |
this.$embedSizeSwitchWikitext.on( 'change', () => this.handleSizeSwitch() ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
unattach() { |
| 135 |
super.unattach(); |
| 136 |
|
| 137 |
this.$embedSizeSwitchHtml.off( 'change' ); |
| 138 |
this.$embedSizeSwitchWikitext.off( 'change' ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
handleSizeSwitch() { |
| 145 |
|
| 146 |
const $html = this.$embedSizeSwitchHtml.find( ':selected' ); |
| 147 |
if ( $html.length ) { |
| 148 |
this.updateEmbedHtml( {}, $html.data( 'width' ), $html.data( 'height' ) ); |
| 149 |
} |
| 150 |
|
| 151 |
const $wikitext = this.$embedSizeSwitchWikitext.find( ':selected' ); |
| 152 |
if ( $wikitext.length ) { |
| 153 |
this.updateEmbedWikitext( $wikitext.data( 'width' ) ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 177 |
|
| 178 |
resetCurrentSizeMenuToDefault() { |
| 179 |
this.$embedSizeSwitchWikitext.val( 'default' ); |
| 180 |
this.$embedSizeSwitchHtml.val( 'original' ); |
| 181 |
this.handleSizeSwitch(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 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 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 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 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 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 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 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 |
|
| 266 |
this.resetCurrentSizeMenuToDefault(); |
| 267 |
|
| 268 |
Utils.getThumbnailUrlPromise( this.LARGE_IMAGE_WIDTH_THRESHOLD ) |
| 269 |
.done( ( thumbnail ) => { |
| 270 |
this.updateEmbedHtml( thumbnail ); |
| 271 |
} ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 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 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 321 |
|
| 322 |
|
| 323 |
Embed.LARGE_IMAGE_WIDTH_THRESHOLD = 1200; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
Embed.LARGE_IMAGE_HEIGHT_THRESHOLD = 900; |
| 330 |
|
| 331 |
module.exports = Embed; |