transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / transmission / web / javascript / formatter.js
blobfb984173f53297c7f9d611e2cef530182f00a843
1 /**
2 ***  This file Copyright (C) Mnemosyne LLC
3 ***
4 ***  This code is licensed under the GPL version 2.
5 ***  For more details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6 **/
8 Transmission.fmt = (function()
10         var speed_K = 1000;
11         var speed_B_str = 'B';
12         var speed_K_str = 'KB/s';
13         var speed_M_str = 'MB/s';
14         var speed_G_str = 'GB/s';
15         var speed_T_str = 'TB/s';
17         var size_K = 1024;
18         var size_B_str = 'B';
19         var size_K_str = 'KB';
20         var size_M_str = 'MB';
21         var size_G_str = 'GB';
22         var size_T_str = 'TB';
24         var mem_K = 1024;
25         var mem_B_str = 'B';
26         var mem_K_str = 'KB';
27         var mem_M_str = 'MB';
28         var mem_G_str = 'GB';
29         var mem_T_str = 'TB';
31         return {
33                 updateUnits: function( u )
34                 {
35                         speed_K     = u['speed-bytes'];
36                         speed_K_str = u['speed-units'][0];
37                         speed_M_str = u['speed-units'][1];
38                         speed_G_str = u['speed-units'][2];
39                         speed_T_str = u['speed-units'][3];
41                         size_K     = u['size-bytes'];
42                         size_K_str = u['size-units'][0];
43                         size_M_str = u['size-units'][1];
44                         size_G_str = u['size-units'][2];
45                         size_T_str = u['size-units'][3];
47                         mem_K     = u['memory-bytes'];
48                         mem_K_str = u['memory-units'][0];
49                         mem_M_str = u['memory-units'][1];
50                         mem_G_str = u['memory-units'][2];
51                         mem_T_str = u['memory-units'][3];
52                 },
54                 /*
55                  *   Format a percentage to a string
56                  */
57                 percentString: function( x ) {
58                         if( x < 10.0 )
59                                 return x.toTruncFixed( 2 );
60                         else if( x < 100.0 )
61                                 return x.toTruncFixed( 1 );
62                         else
63                                 return x.toTruncFixed( 0 );
64                 },
66                 /*
67                  *   Format a ratio to a string
68                  */
69                 ratioString: function( x ) {
70                         if( x ==  -1 )
71                                 return "None";
72                         else if( x == -2 )
73                                 return '&infin;';
74                         else
75                                 return this.percentString( x );
76                 },
78                 /**
79                  * Formats the a memory size into a human-readable string
80                  * @param {Number} bytes the filesize in bytes
81                  * @return {String} human-readable string
82                  */
83                 mem: function( bytes )
84                 {
85                         if( bytes < mem_K )
86                                 return [ bytes, mem_B_str ].join(' ');
88                         var convertedSize;
89                         var unit;
91                         if( bytes < Math.pow( mem_K, 2 ) )
92                         {
93                                 convertedSize = bytes / mem_K;
94                                 unit = mem_K_str;
95                         }
96                         else if( bytes < Math.pow( mem_K, 3 ) )
97                         {
98                                 convertedSize = bytes / Math.pow( mem_K, 2 );
99                                 unit = mem_M_str;
100                         }
101                         else if( bytes < Math.pow( mem_K, 4 ) )
102                         {
103                                 convertedSize = bytes / Math.pow( mem_K, 3 );
104                                 unit = mem_G_str;
105                         }
106                         else
107                         {
108                                 convertedSize = bytes / Math.pow( mem_K, 4 );
109                                 unit = mem_T_str;
110                         }
112                         // try to have at least 3 digits and at least 1 decimal
113                         return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
114                                                       : [ convertedSize.toTruncFixed(1), unit ].join(' ');
115                 },
117                 /**
118                  * Formats the a disk capacity or file size into a human-readable string
119                  * @param {Number} bytes the filesize in bytes
120                  * @return {String} human-readable string
121                  */
122                 size: function( bytes )
123                 {
124                         if( bytes < size_K )
125                                 return [ bytes, size_B_str ].join(' ');
127                         var convertedSize;
128                         var unit;
130                         if( bytes < Math.pow( size_K, 2 ) )
131                         {
132                                 convertedSize = bytes / size_K;
133                                 unit = size_K_str;
134                         }
135                         else if( bytes < Math.pow( size_K, 3 ) )
136                         {
137                                 convertedSize = bytes / Math.pow( size_K, 2 );
138                                 unit = size_M_str;
139                         }
140                         else if( bytes < Math.pow( size_K, 4 ) )
141                         {
142                                 convertedSize = bytes / Math.pow( size_K, 3 );
143                                 unit = size_G_str;
144                         }
145                         else
146                         {
147                                 convertedSize = bytes / Math.pow( size_K, 4 );
148                                 unit = size_T_str;
149                         }
151                         // try to have at least 3 digits and at least 1 decimal
152                         return convertedSize <= 9.995 ? [ convertedSize.toTruncFixed(2), unit ].join(' ')
153                                                       : [ convertedSize.toTruncFixed(1), unit ].join(' ');
154                 },
156                 speedBps: function( Bps )
157                 {
158                         return this.speed( this.toKBps( Bps ) );
159                 },
161                 toKBps: function( Bps )
162                 {
163                         return Math.floor( Bps / speed_K );
164                 },
166                 speed: function( KBps )
167                 {
168                         var speed = KBps;
170                         if (speed <= 999.95) // 0 KBps to 999.9 K
171                                 return [ speed.toTruncFixed(1), speed_K_str ].join(' ');
173                         speed /= speed_K;
175                         if (speed <= 99.995) // 1 M to 99.99 M
176                                 return [ speed.toTruncFixed(2), speed_M_str ].join(' ');
177                         if (speed <= 999.95) // 100 M to 999.9 M
178                                 return [ speed.toTruncFixed(1), speed_M_str ].join(' ');
180                         // insane speeds
181                         speed /= speed_K;
182                         return [ speed.toTruncFixed(2), speed_G_str ].join(' ');
183                 },
185                 timeInterval: function( seconds )
186                 {
187                         var result;
188                         var days = Math.floor(seconds / 86400);
189                         var hours = Math.floor((seconds % 86400) / 3600);
190                         var minutes = Math.floor((seconds % 3600) / 60);
191                         var seconds = Math.floor((seconds % 3600) % 60);
193                         if (days > 0 && hours == 0)
194                                 result = [ days, 'days' ];
195                         else if (days > 0 && hours > 0)
196                                 result = [ days, 'days', hours, 'hr' ];
197                         else if (hours > 0 && minutes == 0)
198                                 result = [ hours, 'hr' ];
199                         else if (hours > 0 && minutes > 0)
200                                 result = [ hours,'hr', minutes, 'min' ];
201                         else if (minutes > 0 && seconds == 0)
202                                 result = [ minutes, 'min' ];
203                         else if (minutes > 0 && seconds > 0)
204                                 result = [ minutes, 'min', seconds, 'seconds' ];
205                         else
206                                 result = [ seconds, 'seconds' ];
208                         return result.join(' ');
209                 },
211                 timestamp: function( seconds )
212                 {
213                         if( !seconds )
214                                 return 'N/A';
216                         var myDate = new Date(seconds*1000);
217                         var now = new Date();
219                         var date = "";
220                         var time = "";
222                         var sameYear = now.getFullYear() == myDate.getFullYear();
223                         var sameMonth = now.getMonth() == myDate.getMonth();
225                         var dateDiff = now.getDate() - myDate.getDate();
226                         if(sameYear && sameMonth && Math.abs(dateDiff) <= 1){
227                                 if(dateDiff == 0){
228                                         date = "Today";
229                                 }
230                                 else if(dateDiff == 1){
231                                         date = "Yesterday";
232                                 }
233                                 else{
234                                         date = "Tomorrow";
235                                 }
236                         }
237                         else{
238                                 date = myDate.toDateString();
239                         }
241                         var hours = myDate.getHours();
242                         var period = "AM";
243                         if(hours > 12){
244                                 hours = hours - 12;
245                                 period = "PM";
246                         }
247                         if(hours == 0){
248                                 hours = 12;
249                         }
250                         if(hours < 10){
251                                 hours = "0" + hours;
252                         }
253                         var minutes = myDate.getMinutes();
254                         if(minutes < 10){
255                                 minutes = "0" + minutes;
256                         }
257                         var seconds = myDate.getSeconds();
258                                 if(seconds < 10){
259                                         seconds = "0" + seconds;
260                         }
262                         time = [hours, minutes, seconds].join(':');
264                         return [date, time, period].join(' ');
265                 },
267                 plural: function( i, word )
268                 {
269                         return [ i, ' ', word, (word==1?'':'s') ].join('');
270                 }
271         }
272 })();