| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const { Config } = require( 'mmv.bootstrap' ); |
| 19 |
const { HtmlUtils } = require( 'mmv' ); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class EmbedFileFormatter { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
getThumbnailWikitext( title, width, caption, alt ) { |
| 35 |
const widthSection = width ? `|${ width }px` : ''; |
| 36 |
const captionSection = caption ? `|${ caption }` : ''; |
| 37 |
const altSection = alt ? `|alt=${ alt }` : ''; |
| 38 |
|
| 39 |
return `[[${ title.getPrefixedText() }${ widthSection }|thumb${ captionSection }${ altSection }]]`; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
getThumbnailWikitextFromEmbedFileInfo( info, width ) { |
| 51 |
const caption = info.caption ? HtmlUtils.htmlToText( info.caption ) : info.imageInfo.title.getNameText(); |
| 52 |
return this.getThumbnailWikitext( info.imageInfo.title, width, caption, info.alt ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
getByline( author, source, attribution, formatterFunction ) { |
| 65 |
formatterFunction = formatterFunction || ( ( txt ) => HtmlUtils.htmlToTextWithLinks( txt ) ); |
| 66 |
|
| 67 |
if ( attribution ) { |
| 68 |
attribution = attribution && formatterFunction( attribution ); |
| 69 |
return attribution; |
| 70 |
} else { |
| 71 |
author = author && formatterFunction( author ); |
| 72 |
source = source && formatterFunction( source ); |
| 73 |
|
| 74 |
if ( author && source ) { |
| 75 |
return mw.msg( 'multimediaviewer-credit', author, source ); |
| 76 |
} else { |
| 77 |
return author || source; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
getCreditText( imageInfo ) { |
| 89 |
const shortURL = imageInfo.descriptionShortUrl; |
| 90 |
const license = imageInfo.license; |
| 91 |
const byline = this.getByline( |
| 92 |
imageInfo.author, |
| 93 |
imageInfo.source, |
| 94 |
imageInfo.attribution, |
| 95 |
( txt ) => HtmlUtils.htmlToText( txt ) |
| 96 |
); |
| 97 |
|
| 98 |
|
| 99 |
if ( !byline && !license ) { |
| 100 |
return shortURL; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
const creditParams = [ |
| 108 |
'multimediaviewer-text-embed-credit-text-' |
| 109 |
]; |
| 110 |
|
| 111 |
if ( byline ) { |
| 112 |
creditParams[ 0 ] += 'b'; |
| 113 |
creditParams.push( byline ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( license ) { |
| 117 |
creditParams[ 0 ] += 'l'; |
| 118 |
creditParams.push( HtmlUtils.htmlToText( license.getShortName() ) ); |
| 119 |
} |
| 120 |
|
| 121 |
creditParams.push( shortURL ); |
| 122 |
return mw.message.apply( mw, creditParams ).plain(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
getCreditHtml( imageInfo ) { |
| 132 |
const shortURL = imageInfo.descriptionShortUrl; |
| 133 |
const shortLink = HtmlUtils.makeLinkText( mw.message( 'multimediaviewer-html-embed-credit-link-text' ), { href: shortURL } ); |
| 134 |
const license = imageInfo.license; |
| 135 |
const byline = this.getByline( imageInfo.author, imageInfo.source, imageInfo.attribution ); |
| 136 |
|
| 137 |
if ( !byline && !license ) { |
| 138 |
return shortLink; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
const creditParams = [ |
| 146 |
'multimediaviewer-html-embed-credit-text-' |
| 147 |
]; |
| 148 |
|
| 149 |
if ( byline ) { |
| 150 |
creditParams[ 0 ] += 'b'; |
| 151 |
creditParams.push( byline ); |
| 152 |
} |
| 153 |
if ( license ) { |
| 154 |
creditParams[ 0 ] += 'l'; |
| 155 |
creditParams.push( license.getShortLink() ); |
| 156 |
} |
| 157 |
|
| 158 |
creditParams.push( shortLink ); |
| 159 |
return mw.message.apply( mw, creditParams ).plain(); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
getThumbnailHtml( info, imgUrl, width, height ) { |
| 173 |
return HtmlUtils.jqueryToHtml( |
| 174 |
$( '<p>' ).append( |
| 175 |
$( '<a>' ) |
| 176 |
.attr( 'href', info.imageInfo.descriptionUrl + Config.getMediaHash( info.imageInfo.title ) ) |
| 177 |
.append( |
| 178 |
$( '<img>' ) |
| 179 |
.attr( 'src', imgUrl ) |
| 180 |
.attr( 'alt', info.alt || info.imageInfo.title.getMainText() ) |
| 181 |
.attr( 'height', height ) |
| 182 |
.attr( 'width', width ) |
| 183 |
), |
| 184 |
$( '<br>' ), |
| 185 |
this.getCreditHtml( info.imageInfo ) |
| 186 |
) |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
module.exports = EmbedFileFormatter; |