More missing semi colons
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.upload.js
1 /*
2 * JavaScript for Special:Upload
3 * Note that additional code still lives in skins/common/upload.js
4 */
5
6 jQuery( function( $ ) {
7 /**
8 * Is the FileAPI available with sufficient functionality?
9 */
10 function hasFileAPI(){
11 return typeof window.FileReader !== 'undefined';
12 }
13
14 /**
15 * Check if this is a recognizable image type...
16 * Also excludes files over 10M to avoid going insane on memory usage.
17 *
18 * @todo is there a way we can ask the browser what's supported in <img>s?
19 *
20 * @param {File} file
21 * @return boolean
22 */
23 function fileIsPreviewable( file ) {
24 var known = ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'],
25 tooHuge = 10 * 1024 * 1024;
26 return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
27 }
28
29 /**
30 * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
31 * in browsers supporting HTML5 FileAPI.
32 *
33 * As of this writing, known good:
34 * - Firefox 3.6+
35 * - Chrome 7.something
36 *
37 * @todo check file size limits and warn of likely failures
38 *
39 * @param {File} file
40 */
41 function showPreview( file ) {
42 var previewSize = 180,
43 thumb = $( '<div id="mw-upload-thumbnail" class="thumb tright">' +
44 '<div class="thumbinner">' +
45 '<canvas width="' + previewSize + '" height="' + previewSize + '" ></canvas>' +
46 '<div class="thumbcaption"><div class="filename"></div><div class="fileinfo"></div></div>' +
47 '</div>' +
48 '</div>' );
49 thumb.find( '.filename' ).text( file.name ).end()
50 .find( '.fileinfo' ).text( prettySize( file.size ) ).end();
51
52 var ctx = thumb.find( 'canvas' )[0].getContext( '2d' ),
53 spinner = new Image();
54 spinner.onload = function() {
55 ctx.drawImage( spinner, (previewSize - spinner.width) / 2,
56 (previewSize - spinner.height) / 2 );
57 };
58 spinner.src = mw.config.get( 'wgScriptPath' ) + '/skins/common/images/spinner.gif';
59 $( '#mw-htmlform-source' ).parent().prepend( thumb );
60
61 var meta;
62 fetchPreview( file, function( dataURL ) {
63 var img = new Image(),
64 rotation = 0;
65
66 if ( meta && meta.tiff && meta.tiff.Orientation ) {
67 rotation = (360 - function () {
68 // See includes/media/Bitmap.php
69 switch ( meta.tiff.Orientation.value ) {
70 case 8:
71 return 90;
72 case 3:
73 return 180;
74 case 6:
75 return 270;
76 default:
77 return 0;
78 }
79 }() ) % 360;
80 }
81
82 img.onload = function() {
83 // Fit the image within the previewSizexpreviewSize box
84 if ( img.width > img.height ) {
85 width = previewSize;
86 height = img.height / img.width * previewSize;
87 } else {
88 height = previewSize;
89 width = img.width / img.height * previewSize;
90 }
91 // Determine the offset required to center the image
92 dx = (180 - width) / 2;
93 dy = (180 - height) / 2;
94 switch ( rotation ) {
95 // If a rotation is applied, the direction of the axis
96 // changes as well. You can derive the values below by
97 // drawing on paper an axis system, rotate it and see
98 // where the positive axis direction is
99 case 0:
100 x = dx;
101 y = dy;
102 break;
103 case 90:
104
105 x = dx;
106 y = dy - previewSize;
107 break;
108 case 180:
109 x = dx - previewSize;
110 y = dy - previewSize;
111 break;
112 case 270:
113 x = dx - previewSize;
114 y = dy;
115 break;
116 }
117
118 ctx.clearRect( 0, 0, 180, 180 );
119 ctx.rotate( rotation / 180 * Math.PI );
120 ctx.drawImage( img, x, y, width, height );
121
122 // Image size
123 var info = mw.msg( 'widthheight', img.width, img.height ) +
124 ', ' + prettySize( file.size );
125 $( '#mw-upload-thumbnail .fileinfo' ).text( info );
126 };
127 img.src = dataURL;
128 }, mediaWiki.config.get( 'wgFileCanRotate' ) ? function ( data ) {
129 try {
130 meta = mediaWiki.util.jpegmeta( data, file.fileName );
131 meta._binary_data = null;
132 } catch ( e ) {
133 meta = null;
134 }
135 } : null );
136 }
137
138 /**
139 * Start loading a file into memory; when complete, pass it as a
140 * data URL to the callback function. If the callbackBinary is set it will
141 * first be read as binary and afterwards as data URL. Useful if you want
142 * to do preprocessing on the binary data first.
143 *
144 * @param {File} file
145 * @param {function} callback
146 * @param {function} callbackBinary
147 */
148 function fetchPreview( file, callback, callbackBinary ) {
149 var reader = new FileReader();
150 reader.onload = function() {
151 if ( callbackBinary ) {
152 callbackBinary( reader.result );
153 reader.onload = function() {
154 callback( reader.result );
155 };
156 reader.readAsDataURL( file );
157 } else {
158 callback( reader.result );
159 }
160 };
161 if ( callbackBinary ) {
162 reader.readAsBinaryString( file );
163 } else {
164 reader.readAsDataURL( file );
165 }
166 }
167
168 /**
169 * Format a file size attractively.
170 * @todo match numeric formatting
171 *
172 * @param {number} s
173 * @return string
174 */
175 function prettySize( s ) {
176 var sizes = ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
177 while ( s >= 1024 && sizes.length > 1 ) {
178 s /= 1024;
179 sizes = sizes.slice( 1 );
180 }
181 return mw.msg( sizes[0], Math.round( s ) );
182 }
183
184 /**
185 * Clear the file upload preview area.
186 */
187 function clearPreview() {
188 $( '#mw-upload-thumbnail' ).remove();
189 }
190
191
192 if ( hasFileAPI() ) {
193 // Update thumbnail when the file selection control is updated.
194 $( '#wpUploadFile' ).change( function() {
195 clearPreview();
196 if ( this.files && this.files.length ) {
197 // Note: would need to be updated to handle multiple files.
198 var file = this.files[0];
199 if ( fileIsPreviewable( file ) ) {
200 showPreview( file );
201 }
202 }
203 } );
204 }
205 } );