deluge/deluge/ui/web/js/deluge.js

202 lines
5.5 KiB
JavaScript

/*
Script: deluge.js
Contains the events object, formatters and keys for get_torrent(s)_status.
Copyright:
(C) Damien Churchill 2009 <damoxc@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, write to:
The Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor
Boston, MA 02110-1301, USA.
*/
var Deluge = {
author: 'Damien Churchill <damoxc@gmail.com>',
version: '1.2-dev'
};
(function() {
var tpl = '<div class="x-progress-wrap x-progress-renderered">' +
'<div class="x-progress-inner">' +
'<div style="width: {2}px" class="x-progress-bar">' +
'<div style="z-index: 99; width: {3}px" class="x-progress-text">' +
'<div style="width: {1}px;">{0}</div>' +
'</div>' +
'</div>' +
'<div class="x-progress-text x-progress-text-back">' +
'<div style="width: {1}px;">{0}</div>' +
'</div>' +
'</div>' +
'</div>';
Deluge.progressBar = function(progress, width, text) {
var progressWidth = (width / 100.0) * progress;
var barWidth = progressWidth.toInt() - 1;
var textWidth = ((progressWidth.toInt() - 10) > 0 ? progressWidth.toInt() - 10 : 0);
return String.format(tpl, text, width, barWidth, textWidth);
}
})();
Deluge.Events = {
_events: new Hash(),
fire: function() {
arguments = $A(arguments);
var eventName = arguments.shift();
var eventArgs = arguments;
if (!this._events[eventName]) return;
$each(this._events[eventName], function(fn) {
fn.attempt(eventArgs);
});
},
on: function(eventName, fn) {
var e = $pick(this._events[eventName], new Array());
e.include(fn);
this._events[eventName] = e;
},
removeListener: function(eventName, fn) {
if (!this._events[eventName]) return;
this._events[eventName].remove(fn);
}
};
Deluge.Formatters = {
date: function(timestamp) {
function zeroPad(num, count) {
var numZeropad = num + '';
while (numZeropad.length < count) {
numZeropad = '0' + numZeropad;
}
return numZeropad;
}
timestamp = timestamp * 1000;
var date = new Date(timestamp);
return String.format('{0}/{1}/{2}', zeroPad(date.getDate(), 2), zeroPad(date.getMonth() + 1, 2), date.getFullYear());
},
size: function(bytes) {
bytes = bytes / 1024.0;
if (bytes < 1024) { return bytes.toFixed(1) + ' KiB'; }
else { bytes = bytes / 1024; }
if (bytes < 1024) { return bytes.toFixed(1) + ' MiB'; }
else { bytes = bytes / 1024; }
return bytes.toFixed(1) + ' GiB'
},
speed: function(bits) {
return fsize(bits) + '/s'
},
timeRemaining: function(time) {
if (time == 0) { return '∞' }
if (time < 60) { return time + 's'; }
else { time = time / 60; }
if (time < 60) {
var minutes = Math.floor(time)
var seconds = Math.round(60 * (time - minutes))
if (seconds > 0) {
return minutes + 'm ' + seconds + 's';
} else {
return minutes + 'm'; }
}
else { time = time / 60; }
if (time < 24) {
var hours = Math.floor(time)
var minutes = Math.round(60 * (time - hours))
if (minutes > 0) {
return hours + 'h ' + minutes + 'm';
} else {
return hours + 'h';
}
}
else { time = time / 24; }
var days = Math.floor(time)
var hours = Math.round(24 * (time - days))
if (hours > 0) {
return days + 'd ' + hours + 'h';
} else {
return days + 'd';
}
},
plain: function(value) {
return value;
}
}
// _('Do Not Download')
// _('Normal Priority')
// _('High Priority')
// _('Highest Priority')
FILE_PRIORITY = {
0: 'Do Not Download',
1: 'Normal Priority',
2: 'High Priority',
5: 'Highest Priority',
'Do Not Download': 0,
'Normal Priority': 1,
'High Priority': 2,
'Highest Priority': 5
}
FILE_PRIORITY_CSS = {
0: 'x-no-download',
1: 'x-normal-download',
2: 'x-high-download',
5: 'x-highest-download'
}
var fsize = Deluge.Formatters.size;
var fspeed = Deluge.Formatters.speed;
var ftime = Deluge.Formatters.timeRemaining;
var fdate = Deluge.Formatters.date;
var fplain = Deluge.Formatters.plain;
Deluge.Keys = {
Grid: [
'queue', 'name', 'total_size', 'state', 'progress', 'num_seeds',
'total_seeds', 'num_peers', 'total_peers', 'download_payload_rate',
'upload_payload_rate', 'eta', 'ratio', 'distributed_copies',
'is_auto_managed', 'time_added', 'tracker_host'
],
Status: [
'total_done', 'total_payload_download', 'total_uploaded',
'total_payload_upload', 'next_announce', 'tracker_status', 'num_pieces',
'piece_length', 'is_auto_managed', 'active_time', 'seeding_time',
'seed_rank'
],
Files: [
'files', 'file_progress', 'file_priorities'
],
Peers: [
'peers', 'seeds'
],
Details: [
'name', 'save_path', 'total_size', 'num_files', 'tracker_status',
'tracker', 'comment'
],
Options: [
'max_download_speed', 'max_upload_speed', 'max_connections',
'max_upload_slots','is_auto_managed', 'stop_at_ratio', 'stop_ratio',
'remove_at_ratio', 'private', 'prioritize_first_last'
]
};
Deluge.Keys.Status.extend(Deluge.Keys.Grid);