http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88307

Revision: 88307
Author:   neilk
Date:     2011-05-17 14:49:25 +0000 (Tue, 17 May 2011)
Log Message:
-----------
better formatting for file sizes -- similar to / should replace routine in 
mw.special.upload.js

Modified Paths:
--------------
    trunk/extensions/UploadWizard/resources/mw.units.js
    trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html

Modified: trunk/extensions/UploadWizard/resources/mw.units.js
===================================================================
--- trunk/extensions/UploadWizard/resources/mw.units.js 2011-05-17 13:51:14 UTC 
(rev 88306)
+++ trunk/extensions/UploadWizard/resources/mw.units.js 2011-05-17 14:49:25 UTC 
(rev 88307)
@@ -1,35 +1,26 @@
-/**
- * Format a size in bytes for output, using an appropriate
- * unit (B, KB, MB or GB) according to the magnitude in question
- *
- * @param size Size to format
- * @return string Plain text (not HTML)
- */
-mw.units.bytes = function ( size ) {
-       // For small sizes no decimal places are necessary
-       var round = 0;
-       var msg = '';
-       if ( size > 1024 ) {
-               size = size / 1024;
-               if ( size > 1024 ) {
-                       size = size / 1024;
-                       // For MB and bigger two decimal places are smarter
-                       round = 2;
-                       if ( size > 1024 ) {
-                               size = size / 1024;
-                               msg = 'mwe-size-gigabytes';
-                       } else {
-                               msg = 'mwe-size-megabytes';
+( function( mw ) { 
+
+       var scales = [ 'bytes', 'kilobytes', 'megabytes', 'gigabytes', 
'terabytes' ];
+
+       mw.units = {
+
+               /**
+                * Format a size in bytes for output, using an appropriate
+                * unit (B, KB, MB, GB, or TB) according to the magnitude in 
question
+                *
+                * @param {Number} size, positive integer
+                * @return {String} formatted size
+                */
+               bytes: function ( size ) {
+                       var i = 0;
+                       // while the scale is less than terabytes, bit-shift 
size over by 1024
+                       while ( size >= 1024 && i < scales.length ) {
+                               size /= 1024.0;
+                               i++;
                        }
-               } else {
-                       msg = 'mwe-size-kilobytes';
+                       return gM( 'size-' + scales[i], size.toFixed( i > 1 ? 2 
: 0 ) );
                }
-       } else {
-               msg = 'mwe-size-bytes';
-       }
-       // JavaScript does not let you choose the precision when rounding
-       var p = Math.pow( 10, round );
-       size = Math.round( size * p ) / p;
-       return gM( msg , size );
-};
+       };
 
+} )( mediaWiki );
+       

Modified: trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html
===================================================================
--- trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html  2011-05-17 
13:51:14 UTC (rev 88306)
+++ trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html  2011-05-17 
14:49:25 UTC (rev 88307)
@@ -19,6 +19,7 @@
   <script type="text/javascript" src="../../resources/mw.Api.js"></script>
   <script type="text/javascript" src="../../resources/mw.Api.edit.js"></script>
   <script type="text/javascript" src="../../resources/mw.Title.js"></script>
+  <script type="text/javascript" src="../../resources/mw.units.js"></script>
 
   <script type="text/javascript" 
src="spec/mediawiki.language.parser.spec.data.js"></script>
 
@@ -31,6 +32,7 @@
   <script type="text/javascript" 
src="spec/mediawiki.language.parser.spec.js"></script>
 
   <script type="text/javascript" src="spec/mw.Title.spec.js"></script>
+  <script type="text/javascript" src="spec/mw.units.spec.js"></script>
 
 </head>
 <body>


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to