custom.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. window.setInterval(function(){
  2. refreshUpDownTotal();
  3. refreshList();
  4. }, 5000);
  5. $(document).ready(function() {
  6. $.ajaxSetup({ cache: false });
  7. $(".share-torrent").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. var resp = $.parseJSON(msg);
  16. $(".share-torrent-link").val(resp.url);
  17. $(".share-torrent-name").text(resp.name);
  18. $('#shareModal').foundation('reveal', 'open');
  19. });
  20. });
  21. $(".del-torrent").click(function() {
  22. var hash = $(this).attr("data-torrent-id");
  23. var obj = $(this);
  24. $.ajax({
  25. type: "POST",
  26. url: "api/delete",
  27. data: { hash: hash }
  28. })
  29. .done(function( msg ) {
  30. if( msg == "OK" ) {
  31. alertify.success("Torrent deleted !");
  32. obj.parent().parent().remove();
  33. } else {
  34. alertify.error("There is an error, I can't delete this torrent :(");
  35. }
  36. });
  37. });
  38. refreshList();
  39. refreshUpDownTotal();
  40. diskInfo();
  41. });
  42. function refreshUpDownTotal() {
  43. $.getJSON( "api/get_updown_total", function( data ) {
  44. $("#up-info").text(bytesToSize(data["up"], true));
  45. $("#down-info").text(bytesToSize(data["down"], true));
  46. });
  47. }
  48. function refreshList(){
  49. $.getJSON( "api/get_list", function ( data ) {
  50. $.each( data, function( key, val ) {
  51. percent = (val['completed_bytes'] / val['size_bytes']) * 100;
  52. if(percent == 100) {
  53. space = bytesToSize(val['size_bytes'], false);
  54. } else {
  55. space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false);
  56. }
  57. $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9');
  58. $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space);
  59. $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000);
  60. $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true));
  61. $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true));
  62. });
  63. });
  64. }
  65. function diskInfo(){
  66. $.getJSON( "api/get_diskinfo", function ( data ) {
  67. percent = 100 - (data['free'] / data['total'] * 100);
  68. $(".disk-info > .text-info").html(bytesToSize(data['free'], false) + ' Free');
  69. $(".disk-bar > .inner-bar").css('width', percent + '%');
  70. });
  71. }
  72. function bytesToSize(bytes, speed) {
  73. var sizes = ['o', 'Ko', 'Mo', 'Go', 'To'];
  74. var speeds = ['o/s', 'Ko/s', 'Mo/s'];
  75. if (bytes == 0) return '-';
  76. var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024)));
  77. if(speed) {
  78. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i];
  79. } else {
  80. return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
  81. }
  82. }
  83. function poppupAddTorrent(){
  84. window.open("/add", "Ajout d'un torrent", config='height=100, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
  85. }