| 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 |
class DownloadPane extends UiElement { |
| 26 |
|
| 27 |
|
| 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 |
|
| 37 |
this.image = null; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
createSizePulldownMenu( $container ) { |
| 93 |
this.$downloadSizeMenu = Utils.createSelectMenu( |
| 94 |
[ 'original', 'small', 'medium', 'large', 'xl' ], |
| 95 |
'original' |
| 96 |
).appendTo( $container ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 176 |
|
| 177 |
attach() { |
| 178 |
|
| 179 |
this.$downloadSizeMenu.on( 'change', () => this.handleSizeSwitch() ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
unattach() { |
| 186 |
super.unattach(); |
| 187 |
this.$downloadSizeMenu.off( 'change' ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
handleSizeSwitch() { |
| 194 |
|
| 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 |
|
| 206 |
this.$downloadButton.addClass( 'disabledLink' ); |
| 207 |
Utils.getThumbnailUrlPromise( value.width ).done( ( thumbnail ) => { |
| 208 |
this.setDownloadUrl( thumbnail.url ); |
| 209 |
} ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
setDownloadUrl( url ) { |
| 219 |
this.$downloadButton.attr( 'href', `${ url }?download` ); |
| 220 |
this.$previewLink.attr( 'href', url ); |
| 221 |
|
| 222 |
|
| 223 |
this.$downloadButton.removeClass( 'disabledLink' ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
getExtensionFromUrl( url ) { |
| 244 |
const urlParts = url.split( '.' ); |
| 245 |
return urlParts[ urlParts.length - 1 ]; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 262 |
this.imageExtension = image.title.getExtension().toLowerCase(); |
| 263 |
|
| 264 |
|
| 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 |
|
| 276 |
|
| 277 |
this.$attributionHowHeader.text( mw.msg( attributionCtaMessage ) ); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 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; |