custom.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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. refreshList();
  22. refreshUpDownTotal();
  23. diskInfo();
  24. });
  25. function refreshUpDownTotal() {
  26. $.getJSON( "api/get_updown_total", function( data ) {
  27. $("#up-info").text(bytesToSize(data["up"], true));
  28. $("#down-info").text(bytesToSize(data["down"], true));
  29. });
  30. }
  31. function refreshList(){
  32. $.getJSON( "api/get_list", function ( data ) {
  33. $.each( data, function( key, val ) {
  34. percent = (val['completed_bytes'] / val['size_bytes']) * 100;
  35. if(percent == 100) {
  36. space = bytesToSize(val['size_bytes'], false);
  37. } else {
  38. space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false);
  39. }
  40. $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9');
  41. $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space);
  42. $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000);
  43. $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true));
  44. $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true));
  45. });
  46. });
  47. }
  48. function diskInfo(){
  49. $.getJSON( "api/get_diskinfo", function ( data ) {
  50. percent = 100 - (data['free'] / data['total'] * 100);
  51. $(".disk-info > .text-info").html(bytesToSize(data['free'], false) + ' Free');
  52. $(".disk-bar > .inner-bar").css('width', percent + '%');
  53. });
  54. }
  55. function bytesToSize(bytes, speed) {
  56. var sizes = ['o', 'Ko', 'Mo', 'Go', 'To'];
  57. var speeds = ['o/s', 'Ko/s', 'Mo/s'];
  58. if (bytes == 0) return '-';
  59. var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024)));
  60. if(speed) {
  61. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i];
  62. } else {
  63. return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
  64. }
  65. }
  66. function poppupAddTorrent(){
  67. 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');
  68. }