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
|
|
|
|
*/
|
|
|
|
|
2011-08-28 17:24:56 +00:00
|
|
|
var transmission,
|
|
|
|
dialog,
|
2011-08-31 00:50:28 +00:00
|
|
|
isMobileDevice = RegExp("(iPhone|iPod|Android)").test(navigator.userAgent),
|
2011-08-28 17:24:56 +00:00
|
|
|
scroll_timeout;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2011-08-28 17:24:56 +00:00
|
|
|
if (isMobileDevice){
|
2009-07-07 01:11:30 +00:00
|
|
|
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)
|
2011-08-28 17:24:56 +00:00
|
|
|
// Fix min height for isMobileDevice when run in full screen mode from home screen
|
2009-07-07 01:11:30 +00:00
|
|
|
// so the footer appears in the right place
|
|
|
|
$('body div#torrent_container').css('min-height', '338px');
|
|
|
|
$("label[for=torrent_upload_url]").text("URL: ");
|
2011-08-28 17:24:56 +00:00
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
$('.trans_menu ul, div#jqContextMenu, div.dialog_container div.dialog_window').css('border', '1px solid #777');
|
|
|
|
// and this kills the border we used to have
|
|
|
|
$('.trans_menu div.outerbox').css('border', 'none');
|
2009-07-07 01:11:30 +00:00
|
|
|
}
|
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*$/, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** 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._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';
|
|
|
|
|
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,
|
|
|
|
'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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
|
|
|
|
}
|
|
|
|
};
|