custom.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $("#form-add-link").submit(function ( event ){
  41. // Stop form from submitting normally
  42. event.preventDefault();
  43. // Get some values from elements on the page:
  44. var $form = $( this ),
  45. torrent = $form.find( "input[name='link']" ).val(),
  46. url = $form.attr( "action" );
  47. // Send the data using post
  48. var posting = $.post( url, { link: torrent } );
  49. // Put the results in a div
  50. posting.done(function( data ) {
  51. alertify.success( data );
  52. });
  53. });
  54. refreshList();
  55. refreshUpDownTotal();
  56. diskInfo();
  57. });
  58. function refreshUpDownTotal() {
  59. $.getJSON( "api/get_updown_total", function( data ) {
  60. $("#up-info").text(bytesToSize(data["up"], true));
  61. $("#down-info").text(bytesToSize(data["down"], true));
  62. });
  63. }
  64. function refreshList(){
  65. $.getJSON( "api/get_list", function ( data ) {
  66. $.each( data, function( key, val ) {
  67. percent = (val['completed_bytes'] / val['size_bytes']) * 100;
  68. if(percent == 100) {
  69. space = bytesToSize(val['size_bytes'], false);
  70. } else {
  71. space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false);
  72. }
  73. $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9');
  74. $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space);
  75. $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000);
  76. $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true));
  77. $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true));
  78. });
  79. });
  80. }
  81. function diskInfo(){
  82. $.getJSON( "api/get_diskinfo", function ( data ) {
  83. percent = 100 - (data['free'] / data['total'] * 100);
  84. $(".disk-info > .text-info").html(bytesToSize(data['free'], false) + ' Free');
  85. $(".disk-bar > .inner-bar").css('width', percent + '%');
  86. });
  87. }
  88. function bytesToSize(bytes, speed) {
  89. var sizes = ['o', 'Ko', 'Mo', 'Go', 'To'];
  90. var speeds = ['o/s', 'Ko/s', 'Mo/s'];
  91. if (bytes == 0) return '-';
  92. var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024)));
  93. if(speed) {
  94. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i];
  95. } else {
  96. return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
  97. }
  98. }
  99. function poppupAddTorrent(){
  100. 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');
  101. }
  102. // Dropbox for files.
  103. var box = document.getElementById('dropbox')
  104. var dropbox = new Dropbox(box, '/add/file')
  105. // Optional settings and callbacks
  106. dropbox.max_size = 4 // MB
  107. dropbox.max_concurrent = 1 // Do not upload more than two files at a time
  108. dropbox.mime_types = /(torrent)/i // Only allow pngs, jpgs, and gifs
  109. dropbox.success = function(file, response) {
  110. alertify.success(file.name + ' added !');
  111. $('#addModal').foundation('reveal', 'close');
  112. }
  113. dropbox.error = function(file, status, response) {
  114. alertify.error(file.name + ' failed to upload');
  115. }