Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Scripts used to change or enable/disable the refresh intraval to load server data
- last updated : Sunday August 22,2010 12:53 PM
- */
- var _loadedStylesheet;
- var _refreshEnabled = true;
- var _refreshInterval = 2000;
- var _customInterval = _refreshInterval;
- var _loading = false;
- var _lastSelectedDisable = false;
- var _timeoutId = -1;
- var _lastBeforeDisable = 2000;
- var _disableRefreshClicked = false;
- /* inital document load */
- $(document).ready(function() {
- //default values
- $("#customIntervalText").val(_customInterval);
- /* inital value is 'Disable' to make sure click works */
- $('#disableRefresh').html("Disable");
- /* first load */
- loadData();
- /*
- Event handling - rest of function
- */
- $("#title a").click(function () {
- loadData();
- });
- /* resets the select to inital values */
- $('#resetRefresh').click(function() {
- _lastBeforeDisable = 2000;
- $('#disableRefresh').html("Enable");
- $('#disableRefresh').click();
- });
- /* Quick link to disable/enable refresh */
- $('#disableRefresh').click(function() {
- _disableRefreshClicked = true;
- if($(this).html() == "Disable"){
- _lastBeforeDisable = $("#refreshIntervalSelect").find("option[selected]").attr("value");
- $("#refreshIntervalSelect").find("option[value='0']").attr("selected","selected");
- $("#refreshIntervalSelect").change();
- $(this).html("Enable");
- $("#refreshIntervalSelect").attr("disabled","disabled");
- }else{
- $("#refreshIntervalSelect").find("option[value='"+ _lastBeforeDisable +"']").attr("selected","selected");
- $("#refreshIntervalSelect").change();
- $(this).html("Disable");
- $("#refreshIntervalSelect").attr("disabled","");
- }
- });
- $("#refreshIntervalSelect").change(function () {
- var selected = $(this).find("option[selected]").first();
- var interval = selected.attr("value");
- if (interval == -1) { //custom
- $("#refreshIntervalCustom").removeClass("hidded");
- $("#refreshIntervalCustom").addClass("visible");
- _refreshInterval = 1000;
- var customVal = $("#customIntervalText").val();
- customVal = customVal * 1;
- if (customVal > 0) {
- _refreshInterval = customVal;
- _refreshEnabled = true;
- $("#customIntervalText").select();
- }
- loadData();
- }
- else {
- $("#refreshIntervalCustom").removeClass("visible");
- $("#refreshIntervalCustom").addClass("hidded");
- if (interval == 0) { //disabled
- _refreshInterval = 0;
- _refreshEnabled = false;
- _lastSelectedDisable = true;
- $("#status").html("<i>Refresh Disabled</i>");
- $("#timeoutcrums").html("");
- }
- else {
- _refreshInterval = interval;
- _refreshEnabled = true;
- _lastSelectedDisable = false;
- loadData();
- }
- }
- });
- $("#customIntervalText").blur(function () {
- var selected = $("#refreshIntervalSelect option[selected]").first();
- var interval = selected.attr("value");
- var customVal = $(this).val();
- customVal = customVal * 1;
- if (customVal > 0) {
- _refreshInterval = customVal;
- _refreshEnabled = true;
- loadData();
- }
- });
- }); //document ready
- function loadData() {
- if (_lastSelectedDisable == true) {
- _lastSelectedDisable = false;
- return;
- }
- if (_loading == true)
- return;
- _loading = true;
- try {
- var refreshms = _refreshInterval;
- var txhtml = null;
- var xmlurl = "Azureus_Stats.xml";
- var xslurl = "azureus.xsl";
- // code for IE
- if (window.ActiveXObject) {
- try {
- var source = null; //xml source
- var attempts = 0;
- var maxattempts = 3;
- while (attempts <= maxattempts) {
- try {
- source = new ActiveXObject("Msxml2.DOMDocument.3.0");
- source.async = false;
- source.load(xmlurl);
- break;
- }
- catch (e) {
- attempts++;
- if (attempts >= maxattempts) {
- displayError(e);
- break;
- }
- source = null;
- }
- } //while
- if (_loadedStylesheet == null) {
- _loadedStylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");
- _loadedStylesheet.async = false
- _loadedStylesheet.load(xslurl);
- }
- txhtml = source.transformNode(_loadedStylesheet)
- } catch (e) {
- _loadedStylesheet = null;
- displayError(e);
- }
- }
- // code for Mozilla, Firefox, Opera, etc.
- else if (document.implementation && document.implementation.createDocument) {
- try {
- var source = $.ajax({
- url: xmlurl,
- cache: false,
- async: false
- }).responseText;
- var stylesheet = $.ajax({
- url: xslurl,
- cache: false,
- async: false
- }).responseText;
- var xsltProcessor = new XSLTProcessor();
- xsltProcessor.importStylesheet(stylesheet);
- txhtml = xsltProcessor.transformToFragment(source, document);
- } catch (e) { displayError(e.message); }
- }
- //load html into page
- $('#result').html(txhtml);
- var showdebug = false;
- //update status
- if(showdebug==true)
- $("body").append("<textarea style='height:300px;width:100%;' rows='2' cols='2'>" + txhtml + "</textarea>");
- $("#status").html("" + getTime() + " Interval: " + (refreshms / 1000) + " seconds");
- //set timeout crums
- var count = $("#timeoutcrums").attr("count");
- if (!count)
- count = 0;
- if (count > 4) {
- $("#timeoutcrums").html("");
- count = 0;
- }
- count++;
- $("#timeoutcrums").append(".");
- $("#timeoutcrums").attr("count", count);
- //reset timeout
- if (_refreshEnabled == true) {
- //set timer
- if (1000 >= refreshms)
- refreshms = 1200;
- if(_timeoutId != -1)
- clearTimeout(_timeoutId);
- _timeoutId = window.setTimeout("loadData()", refreshms);
- }
- }
- catch (e) { displayError(e.message); }
- finally {
- updateGlobals();
- _loading = false;
- }
- }
- function updateGlobals() {
- //TODO:
- return;
- var dlspeed = _globals.values[0].dlspeed;
- var ulspeed = _globals.values[0].ulspeed;
- var total_peers = _globals.values[0].total_peers;
- $("#previous_results").html("dlspeed = " + dlspeed);
- }
- function getTime() {
- var today = new Date();
- var h = today.getHours();
- var m = today.getMinutes();
- var s = today.getSeconds();
- var ampm = "AM";
- if (h > 12) {
- h = h - 12;
- ampm = "PM";
- }
- m = checkTime(m);
- s = checkTime(s);
- return h + ":" + m + ":" + s + " " + ampm;
- }
- function checkTime(i) {
- if (i < 10) {
- i = "0" + i;
- }
- return i;
- }
- function displayError(e) {
- var message = e ? e.toString() : " unknown error";
- $("#errorOutput").append("<div>" + message + "</div>");
- }
Advertisement
Add Comment
Please, Sign In to add comment