2010-12-26 22:01:26 +00:00
|
|
|
/*
|
|
|
|
|
* JavaScript for Special:Upload
|
|
|
|
|
* Note that additional code still lives in skins/common/upload.js
|
|
|
|
|
*/
|
|
|
|
|
|
2011-01-19 16:26:31 +00:00
|
|
|
jQuery( function( $ ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
/**
|
|
|
|
|
* Is the FileAPI available with sufficient functionality?
|
|
|
|
|
*/
|
2011-01-08 16:43:25 +00:00
|
|
|
function hasFileAPI(){
|
|
|
|
|
return typeof window.FileReader !== 'undefined';
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if this is a recognizable image type...
|
|
|
|
|
* Also excludes files over 10M to avoid going insane on memory usage.
|
|
|
|
|
*
|
|
|
|
|
* @todo is there a way we can ask the browser what's supported in <img>s?
|
|
|
|
|
*
|
|
|
|
|
* @param {File} file
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2011-01-08 16:43:25 +00:00
|
|
|
function fileIsPreviewable( file ) {
|
2011-01-19 16:26:31 +00:00
|
|
|
var known = ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
|
|
|
|
|
tooHuge = 10 * 1024 * 1024;
|
|
|
|
|
return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
|
|
|
|
|
* in browsers supporting HTML5 FileAPI.
|
|
|
|
|
*
|
|
|
|
|
* As of this writing, known good:
|
|
|
|
|
* - Firefox 3.6+
|
|
|
|
|
* - Chrome 7.something
|
|
|
|
|
*
|
|
|
|
|
* @todo check file size limits and warn of likely failures
|
|
|
|
|
*
|
|
|
|
|
* @param {File} file
|
|
|
|
|
*/
|
2011-01-08 16:43:25 +00:00
|
|
|
function showPreview( file ) {
|
2011-01-19 16:26:31 +00:00
|
|
|
var previewSize = 180,
|
|
|
|
|
thumb = $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
|
2011-01-08 16:43:25 +00:00
|
|
|
'<div class="thumbinner">' +
|
|
|
|
|
'<canvas width="' + previewSize + '" height="' + previewSize + '" ></canvas>' +
|
|
|
|
|
'<div class="thumbcaption"><div class="filename"></div><div class="fileinfo"></div></div>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'</div>' );
|
|
|
|
|
thumb.find( '.filename' ).text( file.name ).end()
|
|
|
|
|
.find( '.fileinfo' ).text( prettySize( file.size ) ).end();
|
2011-01-08 16:24:11 +00:00
|
|
|
|
2011-01-19 16:26:31 +00:00
|
|
|
var ctx = thumb.find( 'canvas' )[0].getContext( '2d' ),
|
|
|
|
|
spinner = new Image();
|
2011-01-08 16:43:25 +00:00
|
|
|
spinner.onload = function() {
|
2011-01-08 16:24:11 +00:00
|
|
|
ctx.drawImage( spinner, (previewSize - spinner.width) / 2,
|
|
|
|
|
(previewSize - spinner.height) / 2 );
|
|
|
|
|
};
|
2011-01-19 16:26:31 +00:00
|
|
|
spinner.src = mw.config.get( 'wgScriptPath' ) + '/skins/common/images/spinner.gif';
|
2011-01-08 16:43:25 +00:00
|
|
|
$( '#mw-htmlform-source' ).parent().prepend( thumb );
|
2010-12-26 22:01:26 +00:00
|
|
|
|
2011-01-08 16:43:25 +00:00
|
|
|
fetchPreview( file, function( dataURL ) {
|
2011-01-19 16:26:31 +00:00
|
|
|
var img = new Image(),
|
|
|
|
|
rotation = 0;
|
2011-01-08 16:24:11 +00:00
|
|
|
img.onload = function() {
|
|
|
|
|
// Fit the image within the previewSizexpreviewSize box
|
|
|
|
|
if ( img.width > img.height ) {
|
|
|
|
|
width = previewSize;
|
|
|
|
|
height = img.height / img.width * previewSize;
|
|
|
|
|
} else {
|
|
|
|
|
height = previewSize;
|
|
|
|
|
width = img.width / img.height * previewSize;
|
|
|
|
|
}
|
|
|
|
|
// Determine the offset required to center the image
|
|
|
|
|
dx = (180 - width) / 2;
|
|
|
|
|
dy = (180 - height) / 2;
|
2011-01-08 16:43:25 +00:00
|
|
|
switch ( rotation ) {
|
2011-01-08 16:24:11 +00:00
|
|
|
// If a rotation is applied, the direction of the axis
|
|
|
|
|
// changes as well. You can derive the values below by
|
|
|
|
|
// drawing on paper an axis system, rotate it and see
|
|
|
|
|
// where the positive axis direction is
|
|
|
|
|
case 0:
|
|
|
|
|
x = dx;
|
|
|
|
|
y = dy;
|
|
|
|
|
break;
|
|
|
|
|
case 90:
|
|
|
|
|
x = dx;
|
|
|
|
|
y = dy - previewSize;
|
|
|
|
|
break;
|
|
|
|
|
case 180:
|
|
|
|
|
x = dx - previewSize;
|
|
|
|
|
y = dy - previewSize;
|
|
|
|
|
break;
|
|
|
|
|
case 270:
|
|
|
|
|
x = dx - previewSize;
|
|
|
|
|
y = dy;
|
|
|
|
|
break;
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
2011-01-08 16:24:11 +00:00
|
|
|
|
|
|
|
|
ctx.clearRect( 0, 0, 180, 180 );
|
|
|
|
|
ctx.rotate( rotation / 180 * Math.PI );
|
|
|
|
|
ctx.drawImage( img, x, y, width, height );
|
|
|
|
|
|
|
|
|
|
// Image size
|
2011-01-08 16:43:25 +00:00
|
|
|
var info = mw.msg( 'widthheight', img.width, img.height ) +
|
|
|
|
|
', ' + prettySize( file.size );
|
|
|
|
|
$( '#mw-upload-thumbnail .fileinfo' ).text( info );
|
|
|
|
|
};
|
2011-01-08 16:24:11 +00:00
|
|
|
img.src = dataURL;
|
2011-01-08 17:14:39 +00:00
|
|
|
} );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start loading a file into memory; when complete, pass it as a
|
|
|
|
|
* data URL to the callback function.
|
|
|
|
|
*
|
|
|
|
|
* @param {File} file
|
|
|
|
|
* @param {function} callback
|
|
|
|
|
*/
|
2011-01-08 16:43:25 +00:00
|
|
|
function fetchPreview( file, callback ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
var reader = new FileReader();
|
|
|
|
|
reader.onload = function() {
|
2011-01-08 16:43:25 +00:00
|
|
|
callback( reader.result );
|
2010-12-26 22:01:26 +00:00
|
|
|
};
|
2011-01-08 16:43:25 +00:00
|
|
|
reader.readAsDataURL( file );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format a file size attractively.
|
|
|
|
|
* @todo match numeric formatting
|
|
|
|
|
*
|
|
|
|
|
* @param {number} s
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-01-08 16:43:25 +00:00
|
|
|
function prettySize( s ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
var sizes = ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
|
2011-01-08 16:43:25 +00:00
|
|
|
while ( s >= 1024 && sizes.length > 1 ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
s /= 1024;
|
2011-01-08 16:43:25 +00:00
|
|
|
sizes = sizes.slice( 1 );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
2011-01-08 16:43:25 +00:00
|
|
|
return mw.msg( sizes[0], Math.round( s ) );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the file upload preview area.
|
|
|
|
|
*/
|
|
|
|
|
function clearPreview() {
|
2011-01-08 16:43:25 +00:00
|
|
|
$( '#mw-upload-thumbnail' ).remove();
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
2011-01-08 16:24:11 +00:00
|
|
|
|
2010-12-26 22:01:26 +00:00
|
|
|
|
2011-01-08 16:43:25 +00:00
|
|
|
if ( hasFileAPI() ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
// Update thumbnail when the file selection control is updated.
|
2011-01-08 16:43:25 +00:00
|
|
|
$( '#wpUploadFile' ).change( function() {
|
2010-12-26 22:01:26 +00:00
|
|
|
clearPreview();
|
2011-01-08 16:43:25 +00:00
|
|
|
if ( this.files && this.files.length ) {
|
2010-12-26 22:01:26 +00:00
|
|
|
// Note: would need to be updated to handle multiple files.
|
|
|
|
|
var file = this.files[0];
|
2011-01-08 16:43:25 +00:00
|
|
|
if ( fileIsPreviewable( file ) ) {
|
|
|
|
|
showPreview( file );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-01-08 17:14:39 +00:00
|
|
|
} );
|
2010-12-26 22:01:26 +00:00
|
|
|
}
|
2011-01-08 17:14:39 +00:00
|
|
|
} );
|