custom.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. window.setInterval(function(){
  2. refreshUpDownTotal();
  3. refreshList();
  4. }, 5000);
  5. $(document).ready(function() {
  6. $.ajaxSetup({ cache: false });
  7. $("a[data-torrent-id]").click(function() {
  8. var hash = $(this).attr("data-torrent-id");
  9. $.ajax({
  10. type: "POST",
  11. url: "share",
  12. data: { hash: hash }
  13. })
  14. .done(function( msg ) {
  15. $.notify( "URL : " + msg);
  16. });
  17. });
  18. refreshList();
  19. refreshUpDownTotal();
  20. });
  21. function refreshUpDownTotal() {
  22. $.getJSON( "api/get_updown_total", function( data ) {
  23. $("#up-info").text(bytesToSize(data["up"], true));
  24. $("#down-info").text(bytesToSize(data["down"], true));
  25. });
  26. }
  27. function refreshList(){
  28. $.getJSON( "api/get_list", function ( data ) {
  29. $.each( data, function( key, val ) {
  30. percent = (val['completed_bytes'] / val['size_bytes']) * 100;
  31. if(percent == 100) {
  32. space = bytesToSize(val['size_bytes'], false);
  33. } else {
  34. space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false);
  35. }
  36. $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9');
  37. $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space);
  38. $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000);
  39. $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true));
  40. $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true));
  41. });
  42. });
  43. }
  44. function bytesToSize(bytes, speed) {
  45. var sizes = ['o', 'Ko', 'Mo', 'Go', 'To'];
  46. var speeds = ['o/s', 'Ko/s', 'Mo/s'];
  47. if (bytes == 0) return '-';
  48. var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024)));
  49. if(speed) {
  50. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i];
  51. } else {
  52. return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
  53. }
  54. };