Merge "Reduced some master queries by adding flags to Revision functions."
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.upload.js
index 3fc9481..f7a4b1f 100644 (file)
@@ -46,20 +46,15 @@ jQuery( function( $ ) {
                var     previewSize = 180,
                        thumb = $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
                                                '<div class="thumbinner">' +
-                                                       '<canvas width="' + previewSize + '" height="' + previewSize + '" ></canvas>' +
+                                                       '<div class="mw-small-spinner" style="width: 180px; height: 180px"></div>' +
                                                        '<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' ),
-                       spinner = new Image();
-               spinner.onload = function() {
-                       ctx.drawImage( spinner, (previewSize - spinner.width) / 2,
-                                       (previewSize - spinner.height) / 2 );
-               };
-               spinner.src = mw.config.get( 'wgScriptPath' ) + '/skins/common/images/spinner.gif';
+               var     $canvas = $('<canvas width="' + previewSize + '" height="' + previewSize + '" ></canvas>'),
+                       ctx = $canvas[0].getContext( '2d' );
                $( '#mw-htmlform-source' ).parent().prepend( thumb );
 
                var meta;
@@ -131,6 +126,7 @@ jQuery( function( $ ) {
                                ctx.clearRect( 0, 0, 180, 180 );
                                ctx.rotate( rotation / 180 * Math.PI );
                                ctx.drawImage( img, x, y, width, height );
+                               thumb.find('.mw-small-spinner').replaceWith($canvas);
 
                                // Image size
                                var info = mw.msg( 'widthheight', logicalWidth, logicalHeight ) +
@@ -160,17 +156,33 @@ jQuery( function( $ ) {
         */
        function fetchPreview( file, callback, callbackBinary ) {
                var reader = new FileReader();
-               if ( callbackBinary ) {
+               if ( callbackBinary && 'readAsBinaryString' in reader ) {
                        // To fetch JPEG metadata we need a binary string; start there.
                        // todo: 
                        reader.onload = function() {
                                callbackBinary( reader.result );
 
                                // Now run back through the regular code path.
-                               fetchPreview(file, callback );
+                               fetchPreview( file, callback );
                        };
                        reader.readAsBinaryString( file );
-               } else if ('URL' in window && 'createObjectURL' in window.URL) {
+               } else if ( callbackBinary && 'readAsArrayBuffer' in reader ) {
+                       // readAsArrayBuffer replaces readAsBinaryString
+                       // However, our JPEG metadata library wants a string.
+                       // So, this is going to be an ugly conversion.
+                       reader.onload = function() {
+                               var buffer = new Uint8Array( reader.result ),
+                                       string = '';
+                               for ( var i = 0; i < buffer.byteLength; i++ ) {
+                                       string += String.fromCharCode( buffer[i] );
+                               }
+                               callbackBinary( string );
+
+                               // Now run back through the regular code path.
+                               fetchPreview( file, callback );
+                       };
+                       reader.readAsArrayBuffer( file );
+               } else if ( 'URL' in window && 'createObjectURL' in window.URL ) {
                        // Supported in Firefox 4.0 and above <https://developer.mozilla.org/en/DOM/window.URL.createObjectURL>
                        // WebKit has it in a namespace for now but that's ok. ;)
                        //
@@ -180,7 +192,7 @@ jQuery( function( $ ) {
                        //
                        // Prefer this over readAsDataURL for Firefox 7 due to bug reading
                        // some SVG files from data URIs <https://bugzilla.mozilla.org/show_bug.cgi?id=694165>
-                       callback(window.URL.createObjectURL(file));
+                       callback( window.URL.createObjectURL( file ) );
                } else {
                        // This ends up decoding the file to base-64 and back again, which
                        // feels horribly inefficient.