* Note that additional code still lives in skins/common/upload.js
*/
-$(function() {
+$( function() {
/**
* Is the FileAPI available with sufficient functionality?
*/
- function hasFileAPI() {
- return (typeof window.FileReader != "undefined");
+ function hasFileAPI(){
+ return typeof window.FileReader !== 'undefined';
}
/**
* @param {File} file
* @return boolean
*/
- function fileIsPreviewable(file) {
+ function fileIsPreviewable( file ) {
var known = ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'];
var tooHuge = 10 * 1024 * 1024;
- return ($.inArray(file.type, known) != -1) && (file.size > 0) && (file.size < tooHuge);
+ return ($.inArray( file.type, known ) !== -1) && file.size > 0 && file.size < tooHuge;
}
/**
*
* @param {File} file
*/
- function showPreview(file) {
+ function showPreview( file ) {
var previewSize = 180;
- var thumb = $("<div id='mw-upload-thumbnail' class='thumb tright'>" +
- "<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();
+ var thumb = $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
+ '<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();
- var ctx = thumb.find('canvas')[0].getContext('2d');
+ var ctx = thumb.find( 'canvas' )[0].getContext( '2d' );
var spinner = new Image();
- spinner.onload = function () {
+ spinner.onload = function() {
ctx.drawImage( spinner, (previewSize - spinner.width) / 2,
(previewSize - spinner.height) / 2 );
};
spinner.src = wgScriptPath + '/skins/common/images/spinner.gif';
- $('#mw-htmlform-source').parent().prepend(thumb);
+ $( '#mw-htmlform-source' ).parent().prepend( thumb );
- fetchPreview(file, function(dataURL) {
+ fetchPreview( file, function( dataURL ) {
var img = new Image();
var rotation = 0;
img.onload = function() {
// Determine the offset required to center the image
dx = (180 - width) / 2;
dy = (180 - height) / 2;
- switch (rotation) {
+ switch ( rotation ) {
// 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
ctx.drawImage( img, x, y, width, height );
// Image size
- var info = mediaWiki.msg('widthheight', img.width, img.height) +
- ', ' + prettySize(file.size);
- $('#mw-upload-thumbnail .fileinfo').text(info);
- }
+ var info = mw.msg( 'widthheight', img.width, img.height ) +
+ ', ' + prettySize( file.size );
+ $( '#mw-upload-thumbnail .fileinfo' ).text( info );
+ };
img.src = dataURL;
});
}
* @param {File} file
* @param {function} callback
*/
- function fetchPreview(file, callback) {
+ function fetchPreview( file, callback ) {
var reader = new FileReader();
reader.onload = function() {
- callback(reader.result);
+ callback( reader.result );
};
- reader.readAsDataURL(file);
+ reader.readAsDataURL( file );
}
/**
* @param {number} s
* @return string
*/
- function prettySize(s) {
+ function prettySize( s ) {
var sizes = ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
- while (s >= 1024 && sizes.length > 1) {
+ while ( s >= 1024 && sizes.length > 1 ) {
s /= 1024;
- sizes = sizes.slice(1);
+ sizes = sizes.slice( 1 );
}
- return mediaWiki.msg(sizes[0], Math.round(s))
+ return mw.msg( sizes[0], Math.round( s ) );
}
/**
* Clear the file upload preview area.
*/
function clearPreview() {
- $('#mw-upload-thumbnail').remove();
+ $( '#mw-upload-thumbnail' ).remove();
}
- if (hasFileAPI()) {
+ if ( hasFileAPI() ) {
// Update thumbnail when the file selection control is updated.
- $('#wpUploadFile').change(function() {
+ $( '#wpUploadFile' ).change( function() {
clearPreview();
- if (this.files && this.files.length) {
+ if ( this.files && this.files.length ) {
// Note: would need to be updated to handle multiple files.
var file = this.files[0];
- if (fileIsPreviewable(file)) {
- showPreview(file);
+ if ( fileIsPreviewable( file ) ) {
+ showPreview( file );
}
}
});