window.setInterval(function(){ refreshUpDownTotal(); refreshList(); }, 5000); $(document).ready(function() { $.ajaxSetup({ cache: false }); $(".share-torrent").click(function() { var hash = $(this).attr("data-torrent-id"); $.ajax({ type: "POST", url: "share", data: { hash: hash } }) .done(function( msg ) { var resp = $.parseJSON(msg); $(".share-torrent-link").val(resp.url); $(".share-torrent-name").text(resp.name); $('#shareModal').foundation('reveal', 'open'); }); }); $(".del-torrent").click(function() { var hash = $(this).attr("data-torrent-id"); var obj = $(this); $.ajax({ type: "POST", url: "api/delete", data: { hash: hash } }) .done(function( msg ) { if( msg == "OK" ) { obj.parent().parent().fadeOut("slow", function(){ alertify.success("Torrent deleted !"); obj.parent().parent().remove(); }); } else { alertify.error("There is an error, I can't delete this torrent :("); } }); }); $("#form-add-link").submit(function ( event ){ // Stop form from submitting normally event.preventDefault(); // Get some values from elements on the page: var $form = $( this ), torrent = $form.find( "input[name='link']" ).val(), url = $form.attr( "action" ); // Send the data using post var posting = $.post( url, { link: torrent } ); // Put the results in a div posting.done(function( data ) { alertify.success( data ); }); }); refreshList(); refreshUpDownTotal(); diskInfo(); }); function refreshUpDownTotal() { $.getJSON( "api/get_updown_total", function( data ) { $("#up-info").text(bytesToSize(data["up"], true)); $("#down-info").text(bytesToSize(data["down"], true)); }); } function refreshList(){ $.getJSON( "api/get_list", function ( data ) { $.each( data, function( key, val ) { percent = (val['completed_bytes'] / val['size_bytes']) * 100; if(percent == 100) { space = bytesToSize(val['size_bytes'], false); } else { space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false); } $("tr[data-torrent-id='" + val['hash'] + "'] > td.name").css('background', 'linear-gradient(to right, #43ac6a ' + percent + '%, #f9f9f9 ' + percent + '%, #f9f9f9'); $("tr[data-torrent-id='" + val['hash'] + "'] > td > span.space").html(space); $("tr[data-torrent-id='" + val['hash'] + "'] > td.ratio").html(val['ratio']/1000); $("tr[data-torrent-id='" + val['hash'] + "'] > td.downspeed").html(bytesToSize(val['down'], true)); $("tr[data-torrent-id='" + val['hash'] + "'] > td.upspeed").html(bytesToSize(val['up'], true)); }); }); } function diskInfo(){ $.getJSON( "api/get_diskinfo", function ( data ) { percent = 100 - (data['free'] / data['total'] * 100); $(".disk-info > .text-info").html(bytesToSize(data['free'], false) + ' Free'); $(".disk-bar > .inner-bar").css('width', percent + '%'); }); } function bytesToSize(bytes, speed) { var sizes = ['o', 'Ko', 'Mo', 'Go', 'To']; var speeds = ['o/s', 'Ko/s', 'Mo/s']; if (bytes == 0) return '-'; var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024))); if(speed) { return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i]; } else { return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]; } } function poppupAddTorrent(){ 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'); }