2 * JavaScript for Special:Upload
3 * Note that additional code still lives in skins/common/upload.js
8 * Is the FileAPI available with sufficient functionality?
10 function hasFileAPI() {
11 return (typeof window
.FileReader
!= "undefined");
15 * Check if this is a recognizable image type...
16 * Also excludes files over 10M to avoid going insane on memory usage.
18 * @todo is there a way we can ask the browser what's supported in <img>s?
23 function fileIsPreviewable(file
) {
24 var known
= ['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml'];
25 var tooHuge
= 10 * 1024 * 1024;
26 return ($.inArray(file
.type
, known
) != -1) && (file
.size
> 0) && (file
.size
< tooHuge
);
30 * Show a thumbnail preview of PNG, JPEG, GIF, and SVG files prior to upload
31 * in browsers supporting HTML5 FileAPI.
33 * As of this writing, known good:
35 * - Chrome 7.something
37 * @todo check file size limits and warn of likely failures
41 function showPreview(file
) {
42 var previewSize
= 180;
44 var thumb
= $("<div id='mw-upload-thumbnail' class='thumb tright'>" +
45 "<div class='thumbinner'>" +
46 "<canvas width=" + previewSize
+ " height=" + previewSize
+ " ></canvas>" +
47 "<div class='thumbcaption'><div class='filename'></div><div class='fileinfo'></div></div>" +
50 thumb
.find('.filename').text(file
.name
).end()
51 .find('.fileinfo').text(prettySize(file
.size
)).end();
53 var ctx
= thumb
.find('canvas')[0].getContext('2d');
54 var spinner
= new Image();
55 spinner
.onload = function () {
56 ctx
.drawImage( spinner
, (previewSize
- spinner
.width
) / 2,
57 (previewSize
- spinner
.height
) / 2 );
59 spinner
.src
= wgScriptPath
+ '/skins/common/images/spinner.gif';
60 $('#mw-htmlform-source').parent().prepend(thumb
);
62 fetchPreview(file
, function(dataURL
) {
63 var img
= new Image();
65 img
.onload = function() {
66 // Fit the image within the previewSizexpreviewSize box
67 if ( img
.width
> img
.height
) {
69 height
= img
.height
/ img
.width
* previewSize
;
72 width
= img
.width
/ img
.height
* previewSize
;
74 // Determine the offset required to center the image
75 dx
= (180 - width
) / 2;
76 dy
= (180 - height
) / 2;
78 // If a rotation is applied, the direction of the axis
79 // changes as well. You can derive the values below by
80 // drawing on paper an axis system, rotate it and see
81 // where the positive axis direction is
100 ctx
.clearRect( 0, 0, 180, 180 );
101 ctx
.rotate( rotation
/ 180 * Math
.PI
);
102 ctx
.drawImage( img
, x
, y
, width
, height
);
105 var info
= mediaWiki
.msg('widthheight', img
.width
, img
.height
) +
106 ', ' + prettySize(file
.size
);
107 $('#mw-upload-thumbnail .fileinfo').text(info
);
114 * Start loading a file into memory; when complete, pass it as a
115 * data URL to the callback function.
118 * @param {function} callback
120 function fetchPreview(file
, callback
) {
121 var reader
= new FileReader();
122 reader
.onload = function() {
123 callback(reader
.result
);
125 reader
.readAsDataURL(file
);
129 * Format a file size attractively.
130 * @todo match numeric formatting
135 function prettySize(s
) {
136 var sizes
= ['size-bytes', 'size-kilobytes', 'size-megabytes', 'size-gigabytes'];
137 while (s
>= 1024 && sizes
.length
> 1) {
139 sizes
= sizes
.slice(1);
141 return mediaWiki
.msg(sizes
[0], Math
.round(s
))
145 * Clear the file upload preview area.
147 function clearPreview() {
148 $('#mw-upload-thumbnail').remove();
153 // Update thumbnail when the file selection control is updated.
154 $('#wpUploadFile').change(function() {
156 if (this.files
&& this.files
.length
) {
157 // Note: would need to be updated to handle multiple files.
158 var file
= this.files
[0];
159 if (fileIsPreviewable(file
)) {