webui-aria2/js/script.js

339 lines
8.9 KiB
JavaScript
Raw Normal View History

2012-06-04 16:08:46 +02:00
var modals = {
err_connect: undefined,
change_conf: undefined,
newDownload_modal: undefined
2012-06-04 16:08:46 +02:00
};
2012-06-05 07:25:57 +02:00
var clear_dialogs = function() {
modals.err_connect = $('#error_connect').modal('hide');
modals.change_conf = $('#change_conf').modal('hide');
modals.newDownload_modal = $('#newDownload_modal').modal('hide');
};
2012-06-04 16:08:46 +02:00
var server_conf = {
host: 'localhost',
port: 6800
};
var custom_aria2_connect = function() {
modals.err_connect.modal('hide');
modals.change_conf.modal('show');
};
var update_server_conf = function() {
server_conf.host = $('#input_host').val();
server_conf.port = $('#input_port').val();
2012-06-05 07:25:57 +02:00
clear_dialogs();
2012-06-04 16:08:46 +02:00
update_ui();
};
2012-06-05 11:44:54 +02:00
function param_encode(param) {
if(param) {
param = base64.btoa(JSON.stringify(param));
}
return param;
}
var aria_syscall = function(conf, multicall) {
$.ajax({
2012-06-04 16:08:46 +02:00
url: 'http://' + server_conf.host + ':' + server_conf.port + '/jsonrpc',
2012-06-05 11:44:54 +02:00
timeout: 1000,
data: {
jsonrpc: 2.0,
2012-06-04 16:08:46 +02:00
id: 'webui',
2012-06-05 11:44:54 +02:00
method: multicall? conf.func:'aria2.' + conf.func,
params: param_encode(conf.params)
},
2012-06-04 16:08:46 +02:00
success: conf.sucess,
error: conf.error,
dataType: 'jsonp',
jsonp: 'jsoncallback'
});
}
var update_ui = function() {
2012-06-05 11:44:54 +02:00
updateDownloads();
2012-06-04 16:08:46 +02:00
};
$(function() {
2012-06-05 07:25:57 +02:00
clear_dialogs();
2012-06-04 16:08:46 +02:00
update_ui();
$('#newDownload').click(function() {
$('#newDownload_url').val("");
$('.download_urls').html("");
2012-06-04 16:08:46 +02:00
modals.newDownload_modal.modal('show');
});
$('#multiple_uris').click(function() {
var url = $('#newDownload_url').val();
var html = '<li>';
html += '<span class="uris">';
html += url;
html += '</span>';
html += ' ';
html += '<a href="#"><i class="icon-trash"></i></a></li>';
$(html).appendTo('.download_urls');
$('#newDownload_url').val("");
$('.download_urls a').click(function() {
$(this).parents('li').remove();
});
});
2012-06-04 16:08:46 +02:00
$('#addNewDownload').click(newDownload);
});
function addDownload(uris) {
console.log("adding download:");
console.log(uris);
2012-06-04 16:08:46 +02:00
aria_syscall({
func: 'addUri',
params: uris,
2012-06-04 16:08:46 +02:00
sucess: function() {
2012-06-05 07:25:57 +02:00
clear_dialogs();
2012-06-04 16:08:46 +02:00
update_ui();
}
});
}
function newDownload() {
var li = $('.download_urls li');
var urls = [];
for(var i = 0; i < li.length; i++) {
urls.push($(li[i]).text().trim());
}
alert(JSON.stringify(urls));
addDownload([urls]);
}
2012-06-05 11:44:54 +02:00
var d_files = {
active: [],
waiting: [],
stopped: []
};
2012-06-05 14:53:52 +02:00
function changeLength(len, pref) {
len = parseInt(len);
if(len <= 1000) return len + " " + pref;
else if(len <= 1000000) return Math.round(len/1000 * 100)/100 + " k" + pref;
else if(len <= 1000000000) return Math.round(len/1000000 *100)/100 + " m" + pref;
else if(len <= 1000000000000) return Math.round(len/1000000000 *100)/100 + " g" + pref;
2012-06-05 19:55:59 +02:00
}
function changeTime(time) {
2012-06-05 14:53:52 +02:00
}
2012-06-05 11:44:54 +02:00
function updateActiveDownloads(data) {
var down_template = $('#download_active_template').text();
$('#active_downloads').html("");
if(!data || (data && data.length === 0)) {
$('#active_downloads').append('no active downloads yet!!!!');
2012-06-05 11:44:54 +02:00
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
2012-06-05 19:55:59 +02:00
percentage = Math.round(percentage*100)/100;
2012-06-05 16:12:01 +02:00
if(!percentage) percentage = 0;
2012-06-05 19:55:59 +02:00
var name;
if(data[i].files[0].uris.length) {
name = data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, '');
}
else {
name = data[i].files[0].path.replace(/^.*[\\\/\]]/, '');
}
2012-06-05 11:44:54 +02:00
ctx = {
2012-06-05 19:55:59 +02:00
name: name,
2012-06-05 11:44:54 +02:00
status: data[i].status,
percentage:percentage,
2012-06-05 14:53:52 +02:00
gid: data[i].gid,
size: changeLength(data[i].totalLength, "b"),
down: changeLength(data[i].downloadSpeed, "b/s")
2012-06-05 11:44:54 +02:00
};
var item = Mustache.render(down_template, ctx);
$('#active_downloads').append(item);
}
$('.download_active_item .download_pause').click(function() {
var gid = $(this).parents('.download_active_item').attr('data-gid');
aria_syscall({
func: 'pause',
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error pausing active download!!!");
console.log(err);
}
});
});
$('.download_active_item .download_remove').click(function() {
var gid = $(this).parents('.download_active_item').attr('data-gid');
aria_syscall({
func: 'remove',
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error removing active download!!!");
console.log(err);
}
});
});
2012-06-05 11:44:54 +02:00
}
function updateWaitingDownloads(data) {
var down_template = $('#download_waiting_template').text();
$('#waiting_downloads').html("");
if(!data || (data && data.length === 0)) {
$('#waiting_downloads').append('no waiting downloads yet!!!!');
2012-06-05 11:44:54 +02:00
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
2012-06-05 19:55:59 +02:00
percentage = Math.round(percentage*100)/100;
2012-06-05 16:12:01 +02:00
if(!percentage) percentage = 0;
2012-06-05 19:55:59 +02:00
var name;
if(data[i].files[0].uris.length) {
name = data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, '');
}
else {
name = data[i].files[0].path.replace(/^.*[\\\/\]]/, '');
}
2012-06-05 11:44:54 +02:00
ctx = {
2012-06-05 19:55:59 +02:00
name: name,
2012-06-05 11:44:54 +02:00
status: data[i].status,
percentage:percentage,
2012-06-05 14:53:52 +02:00
gid: data[i].gid,
size: changeLength(data[i].totalLength, "b"),
down: changeLength(data[i].downloadSpeed, "b/s")
2012-06-05 11:44:54 +02:00
};
var item = Mustache.render(down_template, ctx);
$('#waiting_downloads').append(item);
}
$('.download_waiting_item .download_play').click(function() {
var gid = $(this).parents('.download_waiting_item').attr('data-gid');
aria_syscall({
func: 'unpause',
params: [gid],
sucess: function(data) {
update_ui();
},
error: function(err) {
console.log("error playing waiting download!!!");
console.log(err);
}
});
});
$('.download_waiting_item .download_remove').click(function() {
var gid = $(this).parents('.download_waiting_item').attr('data-gid');
aria_syscall({
func: 'remove',
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error removing waiting download!!!");
console.log(err);
}
});
});
2012-06-05 11:44:54 +02:00
}
2012-06-05 14:53:52 +02:00
2012-06-05 11:44:54 +02:00
function updateStoppedDownloads(data) {
var down_template = $('#download_stopped_template').text();
$('#stopped_downloads').html("");
if(!data || (data && data.length === 0)) {
$('#stopped_downloads').append('no stopped downloads yet!!!!');
2012-06-05 11:44:54 +02:00
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
2012-06-05 19:55:59 +02:00
percentage = Math.round(percentage*100)/100;
2012-06-05 16:12:01 +02:00
if(!percentage) percentage = 0;
2012-06-05 19:55:59 +02:00
var name;
if(data[i].files[0].uris.length) {
name = data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, '');
}
else {
name = data[i].files[0].path.replace(/^.*[\\\/\]]/, '');
}
2012-06-05 11:44:54 +02:00
ctx = {
2012-06-05 19:55:59 +02:00
name: name,
2012-06-05 11:44:54 +02:00
status: data[i].status,
percentage:percentage,
2012-06-05 14:53:52 +02:00
gid: data[i].gid,
size: changeLength(data[i].totalLength, "b"),
down: changeLength(data[i].downloadSpeed, "b/s")
2012-06-05 11:44:54 +02:00
};
var item = Mustache.render(down_template, ctx);
$('#stopped_downloads').append(item);
}
$('.download_stopped_item .download_remove').click(function() {
2012-06-05 11:44:54 +02:00
var gid = $(this).parents('.download_stopped_item').attr('data-gid');
aria_syscall({
func: 'removeDownloadResult',
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error removing stopped download!!!");
console.log(err);
}
});
});
$('.download_stopped_item .download_restart').click(function() {
var gid = $(this).parents('.download_stopped_item').attr('data-gid');
var files;
var uris = [];
for(var i = 0; i < d_files.stopped.length; i++) {
if(d_files.stopped[i].gid === gid) {
files = d_files.stopped[i].files;
break;
}
}
for(var i = 0; i < files.length; i++) {
var tmp_uris = [];
for(var j = 0; j < files[i].uris.length; j++) {
tmp_uris.push(files[i].uris[j].uri);
}
uris.push(tmp_uris);
}
addDownload(uris);
aria_syscall({
func: 'removeDownloadResult',
2012-06-05 11:44:54 +02:00
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error removing stopped download!!!");
2012-06-05 11:44:54 +02:00
console.log(err);
}
});
});
}
function mergeDownloads(data) {
d_files.active = data[0][0];
d_files.waiting = data[1][0];
d_files.stopped = data[2][0];
}
function updateDownloads() {
2012-06-04 16:08:46 +02:00
aria_syscall({
2012-06-05 11:44:54 +02:00
func: 'system.multicall',
params:[[{
methodName: 'aria2.tellActive'
}, {
methodName: 'aria2.tellWaiting',
params: [0,100]
2012-06-05 11:44:54 +02:00
}, {
methodName: 'aria2.tellStopped',
params: [0, 100]
2012-06-05 11:44:54 +02:00
}]],
2012-06-04 16:08:46 +02:00
sucess: function(data) {
2012-06-05 11:44:54 +02:00
mergeDownloads(data.result);
updateActiveDownloads(d_files.active);
updateWaitingDownloads(d_files.waiting);
updateStoppedDownloads(d_files.stopped);
2012-06-04 16:08:46 +02:00
},
error: function() {
modals.err_connect.modal('show');
}
2012-06-05 11:44:54 +02:00
}, true);
}
2012-06-05 07:25:57 +02:00
setInterval(update_ui, 1000);