r88307 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88306‎ | r88307 | r88308 >
Date:14:49, 17 May 2011
Author:neilk
Status:resolved (Comments)
Tags:
Comment:
better formatting for file sizes -- similar to / should replace routine in mw.special.upload.js
Modified paths:
  • /trunk/extensions/UploadWizard/resources/mw.units.js (modified) (history)
  • /trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html
@@ -19,6 +19,7 @@
2020 <script type="text/javascript" src="../../resources/mw.Api.js"></script>
2121 <script type="text/javascript" src="../../resources/mw.Api.edit.js"></script>
2222 <script type="text/javascript" src="../../resources/mw.Title.js"></script>
 23+ <script type="text/javascript" src="../../resources/mw.units.js"></script>
2324
2425 <script type="text/javascript" src="spec/mediawiki.language.parser.spec.data.js"></script>
2526
@@ -31,6 +32,7 @@
3233 <script type="text/javascript" src="spec/mediawiki.language.parser.spec.js"></script>
3334
3435 <script type="text/javascript" src="spec/mw.Title.spec.js"></script>
 36+ <script type="text/javascript" src="spec/mw.units.spec.js"></script>
3537
3638 </head>
3739 <body>
Index: trunk/extensions/UploadWizard/resources/mw.units.js
@@ -1,35 +1,26 @@
2 -/**
3 - * Format a size in bytes for output, using an appropriate
4 - * unit (B, KB, MB or GB) according to the magnitude in question
5 - *
6 - * @param size Size to format
7 - * @return string Plain text (not HTML)
8 - */
9 -mw.units.bytes = function ( size ) {
10 - // For small sizes no decimal places are necessary
11 - var round = 0;
12 - var msg = '';
13 - if ( size > 1024 ) {
14 - size = size / 1024;
15 - if ( size > 1024 ) {
16 - size = size / 1024;
17 - // For MB and bigger two decimal places are smarter
18 - round = 2;
19 - if ( size > 1024 ) {
20 - size = size / 1024;
21 - msg = 'mwe-size-gigabytes';
22 - } else {
23 - msg = 'mwe-size-megabytes';
 2+( function( mw ) {
 3+
 4+ var scales = [ 'bytes', 'kilobytes', 'megabytes', 'gigabytes', 'terabytes' ];
 5+
 6+ mw.units = {
 7+
 8+ /**
 9+ * Format a size in bytes for output, using an appropriate
 10+ * unit (B, KB, MB, GB, or TB) according to the magnitude in question
 11+ *
 12+ * @param {Number} size, positive integer
 13+ * @return {String} formatted size
 14+ */
 15+ bytes: function ( size ) {
 16+ var i = 0;
 17+ // while the scale is less than terabytes, bit-shift size over by 1024
 18+ while ( size >= 1024 && i < scales.length ) {
 19+ size /= 1024.0;
 20+ i++;
2421 }
25 - } else {
26 - msg = 'mwe-size-kilobytes';
 22+ return gM( 'size-' + scales[i], size.toFixed( i > 1 ? 2 : 0 ) );
2723 }
28 - } else {
29 - msg = 'mwe-size-bytes';
30 - }
31 - // JavaScript does not let you choose the precision when rounding
32 - var p = Math.pow( 10, round );
33 - size = Math.round( size * p ) / p;
34 - return gM( msg , size );
35 -};
 24+ };
3625
 26+} )( mediaWiki );
 27+

Comments

#Comment by NeilK (talk | contribs)   20:50, 13 June 2011

Most of this is obsolete -- has been replaced by other revs already marked okay

Status & tagging log