Changeset 10804
- Timestamp:
- Jun 19, 2010, 6:40:07 PM (12 years ago)
- Location:
- trunk/web/javascript
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/web/javascript/common.js
r10802 r10804 109 109 110 110 /* 111 * Converts file & folder byte size values to more112 * readable values (bytes, KiB, MiB, GiB or TiB).113 *114 * @param integer bytes115 * @returns string116 */117 Math.formatBytes = function(bytes) {118 var size;119 var unit;120 121 // Terabytes (TiB).122 if ( bytes >= 1099511627776 ) {123 size = bytes / 1099511627776;124 unit = ' TiB';125 126 // Gigabytes (GiB).127 } else if ( bytes >= 1073741824 ) {128 size = bytes / 1073741824;129 unit = ' GiB';130 131 // Megabytes (MiB).132 } else if ( bytes >= 1048576 ) {133 size = bytes / 1048576;134 unit = ' MiB';135 136 // Kilobytes (KiB).137 } else if ( bytes >= 1024 ) {138 size = bytes / 1024;139 unit = ' KiB';140 141 // The file is less than one KiB142 } else {143 size = bytes;144 unit = ' bytes';145 }146 147 // Single-digit numbers have greater precision148 var precision = 1;149 if (size < 10) {150 precision = 2;151 }152 size = Math.roundWithPrecision(size, precision);153 154 // Add the decimal if this is an integer155 if ((size % 1) == 0 && unit != ' bytes') {156 size = size + '.0';157 }158 159 return size + unit;160 };161 162 163 /*164 * Converts seconds to more readable units (hours, minutes etc).165 *166 * @param integer seconds167 * @returns string168 */169 Math.formatSeconds = function(seconds)170 {171 var result;172 var days = Math.floor(seconds / 86400);173 var hours = Math.floor((seconds % 86400) / 3600);174 var minutes = Math.floor((seconds % 3600) / 60);175 var seconds = Math.floor((seconds % 3600) % 60);176 177 if (days > 0 && hours == 0)178 result = days + ' days';179 else if (days > 0 && hours > 0)180 result = days + ' days ' + hours + ' hr';181 else if (hours > 0 && minutes == 0)182 result = hours + ' hr';183 else if (hours > 0 && minutes > 0)184 result = hours + ' hr ' + minutes + ' min';185 else if (minutes > 0 && seconds == 0)186 result = minutes + ' min';187 else if (minutes > 0 && seconds > 0)188 result = minutes + ' min ' + seconds + ' seconds';189 else190 result = seconds + ' seconds';191 192 return result;193 };194 195 196 /*197 * Converts a unix timestamp to a human readable value198 *199 * @param integer seconds200 * @returns string201 */202 Math.formatTimestamp = function(seconds) {203 var myDate = new Date(seconds*1000);204 var now = new Date();205 206 var date = "";207 var time = "";208 209 var sameYear = now.getFullYear() == myDate.getFullYear();210 var sameMonth = now.getMonth() == myDate.getMonth();211 212 var dateDiff = now.getDate() - myDate.getDate();213 if(sameYear && sameMonth && Math.abs(dateDiff) <= 1){214 if(dateDiff == 0){215 date = "Today";216 }217 else if(dateDiff == 1){218 date = "Yesterday";219 }220 else{221 date = "Tomorrow";222 }223 }224 else{225 date = myDate.toDateString();226 }227 228 var hours = myDate.getHours();229 var period = "AM";230 if(hours > 12){231 hours = hours - 12;232 period = "PM";233 }234 if(hours == 0){235 hours = 12;236 }237 if(hours < 10){238 hours = "0" + hours;239 }240 var minutes = myDate.getMinutes();241 if(minutes < 10){242 minutes = "0" + minutes;243 }244 var seconds = myDate.getSeconds();245 if(seconds < 10){246 seconds = "0" + seconds;247 }248 249 time = [hours, minutes, seconds].join(':');250 251 return [date, time, period].join(' ');252 };253 254 /*255 111 * Round a float to a specified number of decimal 256 112 * places, stripping trailing zeroes -
trunk/web/javascript/torrent.js
r10802 r10804 436 436 }, 437 437 438 formatUL: function() { 439 return 'UL: ' + Transmission.fmt.speed(this._upload_speed); 440 }, 441 formatDL: function() { 442 return 'DL: ' + Transmission.fmt.speed(this._download_speed); 443 }, 444 438 445 getPeerDetails: function() 439 446 { … … 454 461 case Torrent._StatusDownloading: 455 462 if(compact_mode){ 456 c = 'DL: ' 457 c += Math.formatBytes(this._download_speed); 458 c += '/s UL: '; 459 c += Math.formatBytes(this._upload_speed); 460 c += '/s'; 463 c = this.formatDL(); 464 c += ' '; 465 c += this.formatUL(); 461 466 } else { 462 467 // 'Downloading from 36 of 40 peers - DL: 60.2 KiB/s UL: 4.3 KiB/s' … … 465 470 c += ' of '; 466 471 c += this._peers_connected; 467 c += ' peers - DL: '; 468 c += Math.formatBytes(this._download_speed); 469 c += '/s UL: '; 470 c += Math.formatBytes(this._upload_speed); 471 c += '/s'; 472 c += ' peers - '; 473 c = this.formatDL(); 474 c += ' '; 475 c += this.formatUL(); 472 476 } 473 477 break; … … 475 479 case Torrent._StatusSeeding: 476 480 if(compact_mode){ 477 c = 'UL: ' 478 c += Math.formatBytes(this._upload_speed); 479 c += '/s'; 481 c += this.formatUL(); 480 482 } else { 481 483 // 'Seeding to 13 of 22 peers - UL: 36.2 KiB/s' … … 484 486 c += ' of '; 485 487 c += this._peers_connected; 486 c += ' peers - UL: '; 487 c += Math.formatBytes(this._upload_speed); 488 c += '/s'; 488 c += ' peers - '; 489 c += this.formatUL(); 489 490 } 490 491 break; … … 556 557 eta += 'remaining time unknown'; 557 558 else 558 eta += Math.formatSeconds(this._eta) + ' remaining';559 eta += Transmission.fmt.timeInterval(this._eta) + ' remaining'; 559 560 } 560 561 561 562 // Create the 'progress details' label 562 563 // Eg: '101 MiB of 631 MiB (16.02%) - 2 hr remaining' 563 c = Math.formatBytes( this._sizeWhenDone - this._leftUntilDone );564 c = Transmission.fmt.size( this._sizeWhenDone - this._leftUntilDone ); 564 565 c += ' of '; 565 c += Math.formatBytes( this._sizeWhenDone );566 c += Transmission.fmt.size( this._sizeWhenDone ); 566 567 c += ' ('; 567 568 c += this.getPercentDoneStr(); … … 597 598 eta += 'remaining time unknown'; 598 599 else 599 eta += Math.formatSeconds(this._eta) + ' remaining';600 eta += Transmission.fmt.timeInterval(this._eta) + ' remaining'; 600 601 } 601 602 602 603 // Create the 'progress details' label 603 604 // Eg: '698.05 MiB, uploaded 8.59 GiB (Ratio: 12.3)' 604 c = Math.formatBytes( this._size );605 c = Transmission.fmt.size( this._size ); 605 606 c += ', uploaded '; 606 c += Math.formatBytes( this._upload_total );607 c += Transmission.fmt.size( this._upload_total ); 607 608 c += ' (Ratio '; 608 609 if(this._upload_ratio > -1) … … 958 959 959 960 refreshProgressHTML: function() { 960 var c = Math.formatBytes(this._done);961 var c = Transmission.fmt.size(this._done); 961 962 c += ' of '; 962 c += Math.formatBytes(this._size);963 c += Transmission.fmt.size(this._size); 963 964 c += ' ('; 964 965 c += Math.ratio(100 * this._done, this._size); -
trunk/web/javascript/transmission.js
r10802 r10804 882 882 t = "Click to enable Temporary Speed Limits"; 883 883 } 884 t += " (" + this._prefs[RPC._TurtleUpSpeedLimit] + " kB/sup, "885 + this._prefs[RPC._TurtleDownSpeedLimit] + " kB/sdown)";884 t += " (" + Transmission.fmt.speed(this._prefs[RPC._TurtleUpSpeedLimit]) + " up, " 885 + Transmission.fmt.speed(this._prefs[RPC._TurtleDownSpeedLimit]) + " down)"; 886 886 w.attr( 'title', t ); 887 887 }, … … 951 951 if (!iPhone) 952 952 { 953 setInnerHTML( $('#limited_download_rate')[0], 'Limit (' + down_limit + ' KiB/s)' );953 setInnerHTML( $('#limited_download_rate')[0], 'Limit (' + Transmission.fmt.speed(down_limit) + ')' ); 954 954 var key = down_limited ? '#limited_download_rate' 955 955 : '#unlimited_download_rate'; 956 956 $(key).deselectMenuSiblings().selectMenuItem(); 957 957 958 setInnerHTML( $('#limited_ upload_rate')[0], 'Limit (' + up_limit + ' KiB/s)' );958 setInnerHTML( $('#limited_download_rate')[0], 'Limit (' + Transmission.fmt.speed(up_limit) + ')' ); 959 959 key = up_limited ? '#limited_upload_rate' 960 960 : '#unlimited_upload_rate'; … … 1003 1003 var total = stats["cumulative-stats"]; 1004 1004 1005 setInnerHTML( $('#stats_session_uploaded')[0], Math.formatBytes(session["uploadedBytes"]) );1006 setInnerHTML( $('#stats_session_downloaded')[0], Math.formatBytes(session["downloadedBytes"]) );1005 setInnerHTML( $('#stats_session_uploaded')[0], Transmission.fmt.size(session["uploadedBytes"]) ); 1006 setInnerHTML( $('#stats_session_downloaded')[0], Transmission.fmt.size(session["downloadedBytes"]) ); 1007 1007 setInnerHTML( $('#stats_session_ratio')[0], Math.ratio(session["uploadedBytes"],session["downloadedBytes"])); 1008 setInnerHTML( $('#stats_session_duration')[0], Math.formatSeconds(session["secondsActive"]) );1008 setInnerHTML( $('#stats_session_duration')[0], Transmission.fmt.timeInterval(session["secondsActive"]) ); 1009 1009 setInnerHTML( $('#stats_total_count')[0], total["sessionCount"] + " times" ); 1010 setInnerHTML( $('#stats_total_uploaded')[0], Math.formatBytes(total["uploadedBytes"]) );1011 setInnerHTML( $('#stats_total_downloaded')[0], Math.formatBytes(total["downloadedBytes"]) );1010 setInnerHTML( $('#stats_total_uploaded')[0], Transmission.fmt.size(total["uploadedBytes"]) ); 1011 setInnerHTML( $('#stats_total_downloaded')[0], Transmission.fmt.size(total["downloadedBytes"]) ); 1012 1012 setInnerHTML( $('#stats_total_ratio')[0], Math.ratio(total["uploadedBytes"],total["downloadedBytes"])); 1013 setInnerHTML( $('#stats_total_duration')[0], Math.formatSeconds(total["secondsActive"]) );1013 setInnerHTML( $('#stats_total_duration')[0], Transmission.fmt.timeInterval(total["secondsActive"]) ); 1014 1014 }, 1015 1015 … … 1070 1070 args[RPC._DownSpeedLimited] = false; 1071 1071 } else { 1072 setInnerHTML( $('#limited_download_rate')[0], 'Limit (' + rate + ' KiB/s)' );1072 setInnerHTML( $('#limited_download_rate')[0], 'Limit (' + Transmission.fmt.speed(rate) + ')' ); 1073 1073 $('#limited_download_rate').deselectMenuSiblings().selectMenuItem(); 1074 1074 $('div.preference input#download_rate')[0].value = rate; … … 1088 1088 args[RPC._UpSpeedLimited] = false; 1089 1089 } else { 1090 setInnerHTML( $('#limited_upload_rate')[0], 'Limit (' + rate + ' KiB/s)' );1090 setInnerHTML( $('#limited_upload_rate')[0], 'Limit (' + Transmission.fmt.speed(rate) + ')' ); 1091 1091 $('#limited_upload_rate').deselectMenuSiblings().selectMenuItem(); 1092 1092 $('div.preference input#upload_rate')[0].value = rate; … … 1154 1154 var download_dir = 'N/A'; 1155 1155 var date_created = 'N/A'; 1156 var error = ' ';1156 var error = 'None'; 1157 1157 var hash = 'N/A'; 1158 1158 var have_public = false; … … 1227 1227 1228 1228 hash = t.hash(); 1229 pieces = t._pieceCount + ', ' + Math.formatBytes(t._pieceSize);1230 date_created = Math.formatTimestamp( t._creator_date );1229 pieces = t._pieceCount + ', ' + Transmission.fmt.size(t._pieceSize); 1230 date_created = Transmission.fmt.timestamp( t._creator_date ); 1231 1231 } 1232 1232 … … 1260 1260 1261 1261 setInnerHTML( tab.name, name ); 1262 setInnerHTML( tab.size, torrents.length ? Math.formatBytes( total_size ) : na );1262 setInnerHTML( tab.size, torrents.length ? Transmission.fmt.size( total_size ) : na ); 1263 1263 setInnerHTML( tab.pieces, pieces ); 1264 1264 setInnerHTML( tab.hash, hash ); 1265 1265 setInnerHTML( tab.state, total_state ); 1266 setInnerHTML( tab.download_speed, torrents.length ? Math.formatBytes( total_download_speed ) + '/s': na );1267 setInnerHTML( tab.upload_speed, torrents.length ? Math.formatBytes( total_upload_speed ) + '/s': na );1268 setInnerHTML( tab.uploaded, torrents.length ? Math.formatBytes( total_upload ) : na );1269 setInnerHTML( tab.downloaded, torrents.length ? Math.formatBytes( total_download ) : na );1266 setInnerHTML( tab.download_speed, torrents.length ? Transmission.fmt.speed( total_download_speed ) : na ); 1267 setInnerHTML( tab.upload_speed, torrents.length ? Transmission.fmt.speed( total_upload_speed ) : na ); 1268 setInnerHTML( tab.uploaded, torrents.length ? Transmission.fmt.size( total_upload ) : na ); 1269 setInnerHTML( tab.downloaded, torrents.length ? Transmission.fmt.size( total_download ) : na ); 1270 1270 setInnerHTML( tab.availability, torrents.length ? Math.ratio( total_availability*100, sizeWhenDone ) + '%' : na ); 1271 1271 setInnerHTML( tab.ratio, torrents.length ? Math.ratio( total_upload, total_download ) : na ); 1272 setInnerHTML( tab.have, torrents.length ? Math.formatBytes(total_completed) + ' (' + Math.formatBytes(total_verified) + ' verified)' : na );1272 setInnerHTML( tab.have, torrents.length ? Transmission.fmt.size(total_completed) + ' (' + Transmission.fmt.size(total_verified) + ' verified)' : na ); 1273 1273 setInnerHTML( tab.upload_to, torrents.length ? total_upload_peers : na ); 1274 1274 setInnerHTML( tab.download_from, torrents.length ? total_download_peers : na ); … … 1326 1326 html += '<tr class="inspector_peer_entry ' + parity + '">'; 1327 1327 html += '<td>' + (peer.isEncrypted ? '<img src="images/graphics/lock_icon.png" alt="Encrypted"/>' : '') + '</td>'; 1328 html += '<td>' + (peer.rateToPeer ? Math.formatBytes(peer.rateToPeer) + '/s' : '') + '</td>';1329 html += '<td>' + (peer.rateToClient ? Math.formatBytes(peer.rateToClient) + '/s' : '') + '</td>';1328 html += '<td>' + Transmission.fmt.speed(peer.rateToPeer) + '</td>'; 1329 html += '<td>' + Transmission.fmt.speed(peer.rateToClient) + '</td>'; 1330 1330 html += '<td class="percentCol">' + Math.floor(peer.progress*100) + '%' + '</td>'; 1331 1331 html += '<td>' + peer.flagStr + '</td>'; … … 1379 1379 var lastAnnounce = 'N/A'; 1380 1380 if (tracker.hasAnnounced) { 1381 var lastAnnounceTime = Math.formatTimestamp(tracker.lastAnnounceTime);1381 var lastAnnounceTime = Transmission.fmt.timestamp(tracker.lastAnnounceTime); 1382 1382 if (tracker.lastAnnounceSucceeded) { 1383 1383 lastAnnounce = lastAnnounceTime; … … 1404 1404 timeUntilAnnounce = 0; 1405 1405 } 1406 announceState = 'Next announce in ' + Math.formatSeconds(timeUntilAnnounce);1406 announceState = 'Next announce in ' + Transmission.fmt.timeInterval(timeUntilAnnounce); 1407 1407 break; 1408 1408 case Torrent._TrackerQueued: … … 1424 1424 var lastScrape = 'N/A'; 1425 1425 if (tracker.hasScraped) { 1426 var lastScrapeTime = Math.formatTimestamp(tracker.lastScrapeTime);1426 var lastScrapeTime = Transmission.fmt.timestamp(tracker.lastScrapeTime); 1427 1427 if (tracker.lastScrapeSucceeded) { 1428 1428 lastScrape = lastScrapeTime; … … 1686 1686 1687 1687 // update the speeds 1688 s = Math.formatBytes( upSpeed ) + '/s';1688 s = Transmission.fmt.speed( upSpeed ); 1689 1689 if( iPhone ) s = 'UL: ' + s; 1690 1690 setInnerHTML( $('#torrent_global_upload')[0], s ); 1691 1691 1692 1692 // download speeds 1693 s = Math.formatBytes( downSpeed ) + '/s';1693 s = Transmission.fmt.speed( downSpeed ); 1694 1694 if( iPhone ) s = 'DL: ' + s; 1695 1695 setInnerHTML( $('#torrent_global_download')[0], s );
Note: See TracChangeset
for help on using the changeset viewer.