custom.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. obj.parent().parent().fadeOut("slow", function(){
  32. alertify.success("Torrent deleted !");
  33. obj.parent().parent().remove();
  34. });
  35. } else {
  36. alertify.error("There is an error, I can't delete this torrent :(");
  37. }
  38. });
  39. });
  40. refreshList();
  41. refreshUpDownTotal();
  42. diskInfo();
  43. });
  44. function refreshUpDownTotal() {
  45. $.getJSON( "api/get_updown_total", function( data ) {
  46. $("#up-info").text(bytesToSize(data["up"], true));
  47. $("#down-info").text(bytesToSize(data["down"], true));
  48. });
  49. }
  50. function refreshList(){
  51. $.getJSON( "api/get_list", function ( data ) {
  52. $.each( data, function( key, val ) {
  53. percent = (val['completed_bytes'] / val['size_bytes']) * 100;
  54. if(percent == 100) {
  55. space = bytesToSize(val['size_bytes'], false);
  56. } else {
  57. space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false);
  58. }
  59. $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9');
  60. $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space);
  61. $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000);
  62. $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true));
  63. $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true));
  64. });
  65. });
  66. }
  67. function diskInfo(){
  68. $.getJSON( "api/get_diskinfo", function ( data ) {
  69. percent = 100 - (data['free'] / data['total'] * 100);
  70. $(".disk-info > .text-info").html(bytesToSize(data['free'], false) + ' Free');
  71. $(".disk-bar > .inner-bar").css('width', percent + '%');
  72. });
  73. }
  74. function bytesToSize(bytes, speed) {
  75. var sizes = ['o', 'Ko', 'Mo', 'Go', 'To'];
  76. var speeds = ['o/s', 'Ko/s', 'Mo/s'];
  77. if (bytes == 0) return '-';
  78. var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024)));
  79. if(speed) {
  80. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i];
  81. } else {
  82. return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
  83. }
  84. }
  85. function poppupAddTorrent(){
  86. 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');
  87. }