custom.js 2.1 KB

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