webui-aria2/js/script.js

216 lines
5.5 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 log = $('#console');
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() {
modals.newDownload_modal.modal('show');
});
2012-06-04 16:08:46 +02:00
$('#addNewDownload').click(newDownload);
});
2012-06-04 16:08:46 +02:00
function newDownload() {
2012-06-05 07:25:57 +02:00
2012-06-04 16:08:46 +02:00
var url = $('#newDownload_url').val();
aria_syscall({
func: 'addUri',
2012-06-05 11:44:54 +02:00
params: [[url]],
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();
}
});
}
2012-06-05 11:44:54 +02:00
var d_files = {
active: [],
waiting: [],
stopped: []
};
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!!!!');
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
percentage = Math.round(percentage*1000)/1000;
ctx = {
name: data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, ''),
status: data[i].status,
percentage:percentage,
gid: data[i].gid
};
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);
}
});
});
}
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!!!!');
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
percentage = Math.round(percentage*1000)/1000;
ctx = {
name: data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, ''),
status: data[i].status,
percentage:percentage,
gid: data[i].gid
};
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);
}
});
});
}
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!!!!');
}
for(var i = 0; i < data.length; i++) {
var percentage =(data[i].completedLength / data[i].totalLength)*100;
percentage = Math.round(percentage*1000)/1000;
ctx = {
name: data[i].files[0].uris[0].uri.replace(/^.*[\\\/]/, ''),
status: data[i].status,
percentage:percentage,
gid: data[i].gid
};
var item = Mustache.render(down_template, ctx);
$('#stopped_downloads').append(item);
}
$('.download_stopped_item .download_play').click(function() {
var gid = $(this).parents('.download_stopped_item').attr('data-gid');
aria_syscall({
func: 'unpause',
params: [gid],
sucess: function() {
update_ui();
},
error: function(err) {
console.log("error playing stopped download!!!");
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,5]
}, {
methodName: 'aria2.tellStopped',
params: [0, 5]
}]],
2012-06-04 16:08:46 +02:00
sucess: function(data) {
2012-06-05 11:44:54 +02:00
//updateActiveDownloads(data.result);
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
log.append('error connecting for downloads!!<br>');
2012-06-04 16:08:46 +02:00
}
2012-06-05 11:44:54 +02:00
}, true);
}
2012-06-05 07:25:57 +02:00
setInterval(update_ui, 1000);