2 * JavaScript for Special:Upload
3 * Note that additional code still lives in skins/common/upload.js
7 * Add a preview to the upload form
9 jQuery( function( $ ) {
11 * Is the FileAPI available with sufficient functionality?
13 function hasFileAPI(){
14 return typeof window
.FileReader
!== 'undefined';
18 * Check if this is a recognizable image type...
19 * Also excludes files over 10M to avoid going insane on memory usage.
21 * @todo is there a way we can ask the browser what's supported in <img>s?
26 function fileIsPreviewable( file
) {
27 var known
= ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
28 tooHuge
= 10 * 1024 * 1024;
29 return ( $.inArray( file
.type
, known
) !== -1 ) && file
.size
> 0 && file
.size
< tooHuge
;
33 * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
34 * in browsers supporting HTML5 FileAPI.
36 * As of this writing, known good:
38 * - Chrome 7.something
40 * @todo check file size limits and warn of likely failures
44 function showPreview( file
) {
45 var previewSize
= 180,
46 thumb
= $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
47 '<div class="thumbinner">' +
48 '<canvas width="' + previewSize
+ '" height="' + previewSize
+ '" ></canvas>' +
49 '<div class="thumbcaption"><div class="filename"></div><div class="fileinfo"></div></div>' +
52 thumb
.find( '.filename' ).text( file
.name
).end()
53 .find( '.fileinfo' ).text( prettySize( file
.size
) ).end();
55 var ctx
= thumb
.find( 'canvas' )[0].getContext( '2d' ),
56 spinner
= new Image();
57 spinner
.onload = function() {
58 ctx
.drawImage( spinner
, (previewSize
- spinner
.width
) / 2,
59 (previewSize
- spinner
.height
) / 2 );
61 spinner
.src
= mw
.config
.get( 'wgScriptPath' ) + '/skins/common/images/spinner.gif';
62 $( '#mw-htmlform-source' ).parent().prepend( thumb
);
65 fetchPreview( file
, function( dataURL
) {
66 var img
= new Image(),
69 if ( meta
&& meta
.tiff
&& meta
.tiff
.Orientation
) {
70 rotation
= (360 - function () {
71 // See includes/media/Bitmap.php
72 switch ( meta
.tiff
.Orientation
.value
) {
85 img
.onload = function() {
86 var width
, height
, x
, y
, dx
, dy
, logicalWidth
, logicalHeight
;
87 // Fit the image within the previewSizexpreviewSize box
88 if ( img
.width
> img
.height
) {
90 height
= img
.height
/ img
.width
* previewSize
;
93 width
= img
.width
/ img
.height
* previewSize
;
95 // Determine the offset required to center the image
96 dx
= (180 - width
) / 2;
97 dy
= (180 - height
) / 2;
99 // If a rotation is applied, the direction of the axis
100 // changes as well. You can derive the values below by
101 // drawing on paper an axis system, rotate it and see
102 // where the positive axis direction is
106 logicalWidth
= img
.width
;
107 logicalHeight
= img
.height
;
112 y
= dy
- previewSize
;
113 logicalWidth
= img
.height
;
114 logicalHeight
= img
.width
;
117 x
= dx
- previewSize
;
118 y
= dy
- previewSize
;
119 logicalWidth
= img
.width
;
120 logicalHeight
= img
.height
;
123 x
= dx
- previewSize
;
125 logicalWidth
= img
.height
;
126 logicalHeight
= img
.width
;
130 ctx
.clearRect( 0, 0, 180, 180 );
131 ctx
.rotate( rotation
/ 180 * Math
.PI
);
132 ctx
.drawImage( img
, x
, y
, width
, height
);
135 var info
= mw
.msg( 'widthheight', logicalWidth
, logicalHeight
) +
136 ', ' + prettySize( file
.size
);
137 $( '#mw-upload-thumbnail .fileinfo' ).text( info
);
140 }, mw
.config
.get( 'wgFileCanRotate' ) ? function ( data
) {
142 meta
= mw
.libs
.jpegmeta( data
, file
.fileName
);
143 meta
._binary_data
= null;
151 * Start loading a file into memory; when complete, pass it as a
152 * data URL to the callback function. If the callbackBinary is set it will
153 * first be read as binary and afterwards as data URL. Useful if you want
154 * to do preprocessing on the binary data first.
157 * @param {function} callback
158 * @param {function} callbackBinary
160 function fetchPreview( file
, callback
, callbackBinary
) {
161 var reader
= new FileReader();
162 reader
.onload = function() {
163 if ( callbackBinary
) {
164 callbackBinary( reader
.result
);
165 reader
.onload = function() {
166 callback( reader
.result
);
168 reader
.readAsDataURL( file
);
170 callback( reader
.result
);
173 if ( callbackBinary
) {
174 reader
.readAsBinaryString( file
);
176 reader
.readAsDataURL( file
);
181 * Format a file size attractively.
182 * @todo match numeric formatting
187 function prettySize( s
) {
188 var sizes
= ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
189 while ( s
>= 1024 && sizes
.length
> 1 ) {
191 sizes
= sizes
.slice( 1 );
193 return mw
.msg( sizes
[0], Math
.round( s
) );
197 * Clear the file upload preview area.
199 function clearPreview() {
200 $( '#mw-upload-thumbnail' ).remove();
204 * Check if the file does not exceed the maximum size
206 function checkMaxUploadSize( file
) {
207 function getMaxUploadSize( type
) {
208 var sizes
= mw
.config
.get( 'wgMaxUploadSize' );
209 if ( sizes
[type
] !== undefined ) {
214 $( '.mw-upload-source-error' ).remove();
216 var maxSize
= getMaxUploadSize( 'file' );
217 if ( file
.size
> maxSize
) {
218 var error
= $( '<p class="error mw-upload-source-error" id="wpSourceTypeFile-error">' +
219 mw
.message( 'largefileserver', file
.size
, maxSize
).escaped() + '</p>' );
220 $( '#wpUploadFile' ).after( error
);
230 if ( hasFileAPI() ) {
231 // Update thumbnail when the file selection control is updated.
232 $( '#wpUploadFile' ).change( function() {
234 if ( this.files
&& this.files
.length
) {
235 // Note: would need to be updated to handle multiple files.
236 var file
= this.files
[0];
238 if ( !checkMaxUploadSize( file
) ) {
242 if ( fileIsPreviewable( file
) ) {
251 * Disable all upload source fields except the selected one
253 jQuery( function ( $ ) {
254 var rows
= $( '.mw-htmlform-field-UploadSourceField' );
255 for ( var i
= rows
.length
; i
; i
-- ) {
256 var row
= rows
[i
- 1];
257 $( 'input[name="wpSourceType"]', row
).change( function () {
258 var currentRow
= row
; // Store current row in our own scope
260 $( '.mw-upload-source-error' ).remove();
261 if ( this.checked
) {
262 // Disable all inputs
263 $( 'input[name!="wpSourceType"]', rows
).prop( 'disabled', 'disabled' );
264 // Re-enable the current one
265 $( 'input', currentRow
).prop( 'disabled', false );