2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright © Dave Perrett and Malcolm Jarvis
|
|
|
|
* This code is licensed under the GPL version 2.
|
|
|
|
* For more details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
*
|
|
|
|
* Common javascript
|
|
|
|
*/
|
|
|
|
|
|
|
|
var transmission;
|
2010-09-03 00:03:21 +00:00
|
|
|
var dialog;
|
2008-07-10 23:57:46 +00:00
|
|
|
// Test for a Webkit build that supports box-shadow: 521+ (release Safari 3 is
|
|
|
|
// actually 523.10.3). We need 3.1 for CSS animation (dialog sheets) but as it
|
|
|
|
// degrades gracefully let's not worry too much.
|
|
|
|
var Safari3 = testSafari3();
|
2011-08-16 18:49:26 +00:00
|
|
|
var iPhone = RegExp("(iPhone|iPod|Android)").test(navigator.userAgent);
|
2008-07-10 23:57:46 +00:00
|
|
|
if (iPhone) var scroll_timeout;
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (!Array.indexOf){
|
2010-10-26 00:18:41 +00:00
|
|
|
Array.prototype.indexOf = function(obj){
|
|
|
|
var i, len;
|
2011-08-24 02:04:35 +00:00
|
|
|
for (i=0, len=this.length; i<len; i++)
|
|
|
|
if (this[i]==obj)
|
2010-10-26 00:18:41 +00:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
function testSafari3()
|
|
|
|
{
|
2010-06-21 13:14:33 +00:00
|
|
|
var minimum = new Array(521,0);
|
2011-08-24 02:04:35 +00:00
|
|
|
var webKitFields = RegExp("(AppleWebKit/)([^ ]+)").exec(navigator.userAgent);
|
2010-06-21 13:14:33 +00:00
|
|
|
if (!webKitFields || webKitFields.length < 3) return false;
|
|
|
|
var version = webKitFields[2].split(".");
|
|
|
|
for (var i = 0; i < minimum.length; i++) {
|
|
|
|
var toInt = parseInt(version[i]);
|
|
|
|
var versionField = isNaN(toInt) ? 0 : toInt;
|
|
|
|
var minimumField = minimum[i];
|
|
|
|
|
|
|
|
if (versionField > minimumField) return true;
|
|
|
|
if (versionField < minimumField) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
$(document).ready(function() {
|
2010-09-03 00:03:21 +00:00
|
|
|
// Initialise the dialog controller
|
|
|
|
dialog = new Dialog();
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
// Initialise the main Transmission controller
|
|
|
|
transmission = new Transmission();
|
|
|
|
|
2010-09-03 00:03:21 +00:00
|
|
|
// IE specific fixes here
|
|
|
|
if ($.browser.msie) {
|
|
|
|
try {
|
|
|
|
document.execCommand("BackgroundImageCache", false, true);
|
|
|
|
} catch(err) {}
|
|
|
|
$('.dialog_container').css('height',$(window).height()+'px');
|
|
|
|
}
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
if ($.browser.safari) {
|
2008-08-09 04:38:51 +00:00
|
|
|
// Move search field's margin down for the styled input
|
2010-06-21 13:14:33 +00:00
|
|
|
$('#torrent_search').css('margin-top', 3);
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
if (!Safari3 && !iPhone) {
|
|
|
|
// Fix for non-Safari-3 browsers: dark borders to replace shadows.
|
|
|
|
// Opera messes up the menu if we use a border on .trans_menu
|
|
|
|
// div.outerbox so use ul instead
|
2010-09-03 00:03:21 +00:00
|
|
|
$('.trans_menu ul, div#jqContextMenu, div.dialog_container div.dialog_window').css('border', '1px solid #777');
|
2008-07-10 23:57:46 +00:00
|
|
|
// and this kills the border we used to have
|
|
|
|
$('.trans_menu div.outerbox').css('border', 'none');
|
|
|
|
} else if (!iPhone) {
|
|
|
|
// Used for Safari 3.1 CSS animation. Degrades gracefully (so Safari 3
|
|
|
|
// test is good enough) but we delay our hide/unhide to wait for the
|
|
|
|
// scrolling - no point making other browsers wait.
|
2010-09-03 00:03:21 +00:00
|
|
|
$('div#upload_container div.dialog_window').css('top', '-205px');
|
|
|
|
$('div#prefs_container div.dialog_window').css('top', '-425px');
|
|
|
|
$('div#dialog_container div.dialog_window').css('top', '-425px');
|
|
|
|
$('div.dialog_container div.dialog_window').css('-webkit-transition', 'top 0.3s');
|
|
|
|
// -webkit-appearance makes some links into buttons, but needs
|
|
|
|
// different padding.
|
|
|
|
$('div.dialog_container div.dialog_window a').css('padding', '2px 10px 3px');
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
2009-07-07 01:11:30 +00:00
|
|
|
if (iPhone){
|
|
|
|
window.onload = function(){ setTimeout(function() { window.scrollTo(0,1); },500); };
|
2011-08-24 02:04:35 +00:00
|
|
|
window.onorientationchange = function(){ setTimeout(function() { window.scrollTo(0,1); },100); };
|
|
|
|
if (window.navigator.standalone)
|
2009-07-07 01:11:30 +00:00
|
|
|
// Fix min height for iPhone when run in full screen mode from home screen
|
|
|
|
// so the footer appears in the right place
|
|
|
|
$('body div#torrent_container').css('min-height', '338px');
|
|
|
|
$("label[for=torrent_upload_url]").text("URL: ");
|
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a copy of the array
|
|
|
|
*
|
|
|
|
* @returns array
|
|
|
|
*/
|
|
|
|
Array.prototype.clone = function () {
|
|
|
|
return this.concat();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "innerHTML = html" is pretty slow in FF. Happily a lot of our innerHTML
|
|
|
|
* changes are triggered by periodic refreshes on torrents whose state hasn't
|
2009-03-14 21:33:08 +00:00
|
|
|
* changed since the last update, so even this simple test helps a lot.
|
2008-07-10 23:57:46 +00:00
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
function setInnerHTML(e, html)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (e == undefined)
|
2010-06-26 16:47:15 +00:00
|
|
|
return;
|
|
|
|
|
2009-05-23 21:14:03 +00:00
|
|
|
/* innerHTML is listed as a string, but the browser seems to change it.
|
|
|
|
* For example, "∞" gets changed to "∞" somewhere down the line.
|
|
|
|
* So, let's use an arbitrary different field to test our state... */
|
2011-08-24 02:04:35 +00:00
|
|
|
if (e.currentHTML != html)
|
2009-05-23 21:14:03 +00:00
|
|
|
{
|
|
|
|
e.currentHTML = html;
|
2008-07-10 23:57:46 +00:00
|
|
|
e.innerHTML = html;
|
2009-05-23 21:14:03 +00:00
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a numerator and denominator, return a ratio string
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
Math.ratio = function(numerator, denominator) {
|
2009-04-24 03:21:15 +00:00
|
|
|
var result = Math.floor(100 * numerator / denominator) / 100;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// check for special cases
|
2011-08-24 02:04:35 +00:00
|
|
|
if (result==Number.POSITIVE_INFINITY || result==Number.NEGATIVE_INFINITY) result = -2;
|
|
|
|
else if (isNaN(result)) result = -1;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2010-06-22 22:30:58 +00:00
|
|
|
/*
|
|
|
|
* Truncate a float to a specified number of decimal
|
|
|
|
* places, stripping trailing zeroes
|
|
|
|
*
|
|
|
|
* @param float floatnum
|
|
|
|
* @param integer precision
|
|
|
|
* @returns float
|
|
|
|
*/
|
|
|
|
Math.truncateWithPrecision = function(floatnum, precision) {
|
2011-08-24 02:04:35 +00:00
|
|
|
return Math.floor(floatnum * Math.pow (10, precision)) / Math.pow(10, precision);
|
2010-06-22 22:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Round a string of a number to a specified number of decimal
|
|
|
|
* places
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
Number.prototype.toTruncFixed = function(place) {
|
|
|
|
var ret = Math.truncateWithPrecision(this, place);
|
|
|
|
return ret.toFixed(place);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Trim whitespace from a string
|
|
|
|
*/
|
|
|
|
String.prototype.trim = function () {
|
|
|
|
return this.replace(/^\s*/, "").replace(/\s*$/, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief strcmp()-style compare useful for sorting
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
String.prototype.compareTo = function(that) {
|
2008-07-10 23:57:46 +00:00
|
|
|
// FIXME: how to fold these two comparisons together?
|
2011-08-24 02:04:35 +00:00
|
|
|
if (this < that) return -1;
|
|
|
|
if (this > that) return 1;
|
2010-06-21 13:14:33 +00:00
|
|
|
return 0;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Preferences
|
|
|
|
***/
|
|
|
|
|
|
|
|
function Prefs() { }
|
|
|
|
Prefs.prototype = { };
|
|
|
|
|
|
|
|
Prefs._RefreshRate = 'refresh_rate';
|
2010-06-21 13:14:33 +00:00
|
|
|
Prefs._SessionRefreshRate = 'session_refresh_rate';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
Prefs._ShowInspector = 'show_inspector';
|
|
|
|
|
|
|
|
Prefs._FilterMode = 'filter';
|
|
|
|
Prefs._FilterAll = 'all';
|
2010-06-17 04:38:03 +00:00
|
|
|
Prefs._FilterActive = 'active';
|
2008-07-10 23:57:46 +00:00
|
|
|
Prefs._FilterSeeding = 'seeding';
|
|
|
|
Prefs._FilterDownloading = 'downloading';
|
|
|
|
Prefs._FilterPaused = 'paused';
|
2010-09-03 00:20:40 +00:00
|
|
|
Prefs._FilterFinished = 'finished';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
Prefs._SortDirection = 'sort_direction';
|
|
|
|
Prefs._SortAscending = 'ascending';
|
|
|
|
Prefs._SortDescending = 'descending';
|
|
|
|
|
|
|
|
Prefs._SortMethod = 'sort_method';
|
|
|
|
Prefs._SortByAge = 'age';
|
|
|
|
Prefs._SortByActivity = 'activity';
|
|
|
|
Prefs._SortByName = 'name';
|
2011-08-02 03:59:54 +00:00
|
|
|
Prefs._SortByQueue = 'queue_order';
|
2008-07-10 23:57:46 +00:00
|
|
|
Prefs._SortByProgress = 'percent_completed';
|
2011-06-10 14:04:32 +00:00
|
|
|
Prefs._SortByRatio = 'ratio';
|
2008-07-10 23:57:46 +00:00
|
|
|
Prefs._SortByState = 'state';
|
|
|
|
Prefs._SortByTracker = 'tracker';
|
|
|
|
|
2010-09-03 00:03:21 +00:00
|
|
|
Prefs._TurtleState = 'turtle-state';
|
2010-06-19 16:36:00 +00:00
|
|
|
Prefs._CompactDisplayState= 'compact_display_state';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
Prefs._Defaults =
|
|
|
|
{
|
|
|
|
'filter': 'all',
|
|
|
|
'refresh_rate' : 5,
|
|
|
|
'show_filter': true,
|
|
|
|
'show_inspector': false,
|
|
|
|
'sort_direction': 'ascending',
|
2009-11-10 05:42:57 +00:00
|
|
|
'sort_method': 'name',
|
2010-06-19 16:36:00 +00:00
|
|
|
'turtle-state' : false,
|
|
|
|
'compact_display_state' : false
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a preference option
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
Prefs.setValue = function(key, val)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (Prefs._Defaults[key] == undefined)
|
|
|
|
console.warn("unrecognized preference key '%s'", key);
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
var days = 30;
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(date.getTime()+(days*24*60*60*1000));
|
|
|
|
document.cookie = key+"="+val+"; expires="+date.toGMTString()+"; path=/";
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a preference option
|
|
|
|
*
|
|
|
|
* @param key the preference's key
|
|
|
|
* @param fallback if the option isn't set, return this instead
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
Prefs.getValue = function(key, fallback)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
|
|
|
var val;
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (Prefs._Defaults[key] == undefined)
|
|
|
|
console.warn("unrecognized preference key '%s'", key);
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
var lines = document.cookie.split(';');
|
|
|
|
for (var i=0, len=lines.length; !val && i<len; ++i) {
|
|
|
|
var line = lines[i].trim();
|
|
|
|
var delim = line.indexOf('=');
|
|
|
|
if ((delim == key.length) && line.indexOf(key) == 0)
|
|
|
|
val = line.substring(delim + 1);
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we support strings and booleans... add number support too?
|
2011-08-24 02:04:35 +00:00
|
|
|
if (!val) val = fallback;
|
|
|
|
else if (val == 'true') val = true;
|
|
|
|
else if (val == 'false') val = false;
|
2008-07-10 23:57:46 +00:00
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object with all the Clutch preferences set
|
|
|
|
*
|
|
|
|
* @pararm o object to be populated (optional)
|
|
|
|
*/
|
2011-08-24 02:04:35 +00:00
|
|
|
Prefs.getClutchPrefs = function(o)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
if (!o)
|
2008-07-10 23:57:46 +00:00
|
|
|
o = { };
|
2011-08-24 02:04:35 +00:00
|
|
|
for (var key in Prefs._Defaults)
|
|
|
|
o[key] = Prefs.getValue(key, Prefs._Defaults[key]);
|
2008-07-10 23:57:46 +00:00
|
|
|
return o;
|
|
|
|
};
|
2011-08-21 05:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
// forceNumeric() plug-in implementation
|
|
|
|
jQuery.fn.forceNumeric = function () {
|
|
|
|
return this.each(function () {
|
|
|
|
$(this).keydown(function (e) {
|
|
|
|
var key = e.which || e.keyCode;
|
|
|
|
return !e.shiftKey && !e.altKey && !e.ctrlKey &&
|
|
|
|
// numbers
|
|
|
|
key >= 48 && key <= 57 ||
|
|
|
|
// Numeric keypad
|
|
|
|
key >= 96 && key <= 105 ||
|
|
|
|
// comma, period and minus, . on keypad
|
|
|
|
key == 190 || key == 188 || key == 109 || key == 110 ||
|
|
|
|
// Backspace and Tab and Enter
|
|
|
|
key == 8 || key == 9 || key == 13 ||
|
|
|
|
// Home and End
|
|
|
|
key == 35 || key == 36 ||
|
|
|
|
// left and right arrows
|
|
|
|
key == 37 || key == 39 ||
|
|
|
|
// Del and Ins
|
|
|
|
key == 46 || key == 45;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* http://blog.stevenlevithan.com/archives/parseuri
|
|
|
|
*
|
|
|
|
* parseUri 1.2.2
|
|
|
|
* (c) Steven Levithan <stevenlevithan.com>
|
|
|
|
* MIT License
|
|
|
|
*/
|
|
|
|
function parseUri (str) {
|
|
|
|
var o = parseUri.options,
|
|
|
|
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
|
|
|
|
uri = {},
|
|
|
|
i = 14;
|
|
|
|
|
|
|
|
while (i--) uri[o.key[i]] = m[i] || "";
|
|
|
|
|
|
|
|
uri[o.q.name] = {};
|
|
|
|
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
|
|
|
|
if ($1) uri[o.q.name][$1] = $2;
|
|
|
|
});
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
|
|
|
parseUri.options = {
|
|
|
|
strictMode: false,
|
|
|
|
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
|
|
|
|
q: {
|
|
|
|
name: "queryKey",
|
|
|
|
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
|
|
|
|
},
|
|
|
|
parser: {
|
|
|
|
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
|
|
|
|
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
|
|
|
|
}
|
|
|
|
};
|