andrew4582

stats.js

Aug 22nd, 2010
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Scripts used to change or enable/disable the refresh intraval to load server data
  3.  
  4. last updated : Sunday August 22,2010 12:53 PM
  5. */
  6.  
  7. var _loadedStylesheet;
  8. var _refreshEnabled = true;
  9. var _refreshInterval = 2000;
  10. var _customInterval = _refreshInterval;
  11. var _loading = false;
  12. var _lastSelectedDisable = false;
  13. var _timeoutId = -1;
  14. var _lastBeforeDisable = 2000;
  15. var _disableRefreshClicked = false;
  16.  
  17. /* inital document load */
  18. $(document).ready(function() {
  19.     //default values
  20.     $("#customIntervalText").val(_customInterval);
  21.    
  22.     /* inital value is 'Disable' to make sure click works */
  23.     $('#disableRefresh').html("Disable");
  24.    
  25.     /* first load */
  26.     loadData();
  27.    
  28.     /*
  29.         Event handling - rest of function
  30.     */
  31.     $("#title a").click(function () {
  32.        
  33.         loadData();
  34.     });
  35.    
  36.     /* resets the select to inital values */
  37.     $('#resetRefresh').click(function() {
  38.         _lastBeforeDisable = 2000;
  39.         $('#disableRefresh').html("Enable");
  40.         $('#disableRefresh').click();
  41.     });
  42.    
  43.     /* Quick link to disable/enable refresh */
  44.     $('#disableRefresh').click(function() {
  45.         _disableRefreshClicked = true;
  46.        
  47.         if($(this).html() == "Disable"){
  48.             _lastBeforeDisable = $("#refreshIntervalSelect").find("option[selected]").attr("value");
  49.            
  50.             $("#refreshIntervalSelect").find("option[value='0']").attr("selected","selected");
  51.             $("#refreshIntervalSelect").change();
  52.             $(this).html("Enable");
  53.            
  54.             $("#refreshIntervalSelect").attr("disabled","disabled");
  55.         }else{
  56.             $("#refreshIntervalSelect").find("option[value='"+ _lastBeforeDisable +"']").attr("selected","selected");
  57.             $("#refreshIntervalSelect").change();
  58.             $(this).html("Disable");
  59.        
  60.             $("#refreshIntervalSelect").attr("disabled","");
  61.         }
  62.        
  63.     });
  64.     $("#refreshIntervalSelect").change(function () {
  65.  
  66.         var selected = $(this).find("option[selected]").first();
  67.         var interval = selected.attr("value");
  68.  
  69.         if (interval == -1) { //custom
  70.             $("#refreshIntervalCustom").removeClass("hidded");
  71.             $("#refreshIntervalCustom").addClass("visible");
  72.             _refreshInterval = 1000;
  73.  
  74.             var customVal = $("#customIntervalText").val();
  75.             customVal = customVal * 1;
  76.             if (customVal > 0) {
  77.                 _refreshInterval = customVal;
  78.                 _refreshEnabled = true;
  79.                 $("#customIntervalText").select();
  80.             }
  81.             loadData();
  82.         }
  83.         else {
  84.             $("#refreshIntervalCustom").removeClass("visible");
  85.             $("#refreshIntervalCustom").addClass("hidded");
  86.  
  87.             if (interval == 0) { //disabled
  88.                 _refreshInterval = 0;
  89.                 _refreshEnabled = false;
  90.                 _lastSelectedDisable = true;
  91.                 $("#status").html("<i>Refresh Disabled</i>");
  92.                 $("#timeoutcrums").html("");
  93.            
  94.                  
  95.             }
  96.             else {
  97.                 _refreshInterval = interval;
  98.                 _refreshEnabled = true;
  99.                 _lastSelectedDisable = false;
  100.                 loadData();
  101.                  
  102.             }
  103.         }
  104.     });
  105.     $("#customIntervalText").blur(function () {
  106.         var selected = $("#refreshIntervalSelect option[selected]").first();
  107.         var interval = selected.attr("value");
  108.  
  109.         var customVal = $(this).val();
  110.  
  111.         customVal = customVal * 1;
  112.         if (customVal > 0) {
  113.             _refreshInterval = customVal;
  114.             _refreshEnabled = true;
  115.             loadData();
  116.         }
  117.     });
  118. }); //document ready
  119.  
  120. function loadData() {
  121.    
  122.     if (_lastSelectedDisable == true) {
  123.         _lastSelectedDisable = false;
  124.         return;
  125.     }
  126.     if (_loading == true)
  127.         return;
  128.     _loading = true;
  129.     try {
  130.         var refreshms = _refreshInterval;
  131.         var txhtml = null;
  132.         var xmlurl = "Azureus_Stats.xml";
  133.         var xslurl = "azureus.xsl";
  134.         // code for IE
  135.         if (window.ActiveXObject) {
  136.             try {
  137.                 var source = null; //xml source
  138.                 var attempts = 0;
  139.                 var maxattempts = 3;
  140.  
  141.                 while (attempts <= maxattempts) {
  142.                     try {
  143.                         source = new ActiveXObject("Msxml2.DOMDocument.3.0");
  144.                         source.async = false;
  145.                         source.load(xmlurl);
  146.                         break;
  147.                     }
  148.                     catch (e) {
  149.                         attempts++;
  150.                         if (attempts >= maxattempts) {
  151.                             displayError(e);
  152.                             break;
  153.                         }
  154.                         source = null;
  155.                     }
  156.                 } //while
  157.  
  158.                 if (_loadedStylesheet == null) {
  159.                     _loadedStylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");
  160.                     _loadedStylesheet.async = false
  161.                     _loadedStylesheet.load(xslurl);
  162.                 }
  163.  
  164.                 txhtml = source.transformNode(_loadedStylesheet)
  165.  
  166.             } catch (e) {
  167.                 _loadedStylesheet = null;
  168.                 displayError(e);
  169.             }
  170.         }
  171.         // code for Mozilla, Firefox, Opera, etc.
  172.         else if (document.implementation && document.implementation.createDocument) {
  173.             try {
  174.                 var source = $.ajax({
  175.                     url: xmlurl,
  176.                     cache: false,
  177.                     async: false
  178.                 }).responseText;
  179.                 var stylesheet = $.ajax({
  180.                     url: xslurl,
  181.                     cache: false,
  182.                     async: false
  183.                 }).responseText;
  184.                 var xsltProcessor = new XSLTProcessor();
  185.                 xsltProcessor.importStylesheet(stylesheet);
  186.                 txhtml = xsltProcessor.transformToFragment(source, document);
  187.             } catch (e) { displayError(e.message); }
  188.         }
  189.         //load html into page
  190.         $('#result').html(txhtml);
  191.         var showdebug = false;
  192.         //update status
  193.         if(showdebug==true)
  194.             $("body").append("<textarea style='height:300px;width:100%;' rows='2' cols='2'>" + txhtml + "</textarea>");
  195.         $("#status").html("" + getTime() + " Interval: " + (refreshms / 1000) + " seconds");
  196.        
  197.         //set timeout crums
  198.         var count = $("#timeoutcrums").attr("count");
  199.         if (!count)
  200.             count = 0;
  201.         if (count > 4) {
  202.             $("#timeoutcrums").html("");
  203.             count = 0;
  204.         }
  205.         count++;
  206.  
  207.         $("#timeoutcrums").append(".");
  208.         $("#timeoutcrums").attr("count", count);
  209.  
  210.         //reset timeout
  211.         if (_refreshEnabled == true) {
  212.             //set timer
  213.             if (1000 >= refreshms)
  214.                 refreshms = 1200;
  215.             if(_timeoutId != -1)
  216.                 clearTimeout(_timeoutId);
  217.             _timeoutId = window.setTimeout("loadData()", refreshms);
  218.         }
  219.     }
  220.     catch (e) { displayError(e.message); }
  221.     finally {
  222.         updateGlobals();
  223.         _loading = false;
  224.     }
  225. }
  226. function updateGlobals() {
  227.     //TODO:
  228.     return;
  229.     var dlspeed =  _globals.values[0].dlspeed;
  230.     var ulspeed = _globals.values[0].ulspeed;
  231.     var total_peers = _globals.values[0].total_peers;
  232.  
  233.     $("#previous_results").html("dlspeed = " + dlspeed);
  234. }
  235. function getTime() {
  236.     var today = new Date();
  237.     var h = today.getHours();
  238.     var m = today.getMinutes();
  239.     var s = today.getSeconds();
  240.     var ampm = "AM";
  241.     if (h > 12) {
  242.         h = h - 12;
  243.         ampm = "PM";
  244.     }
  245.     m = checkTime(m);
  246.     s = checkTime(s);
  247.     return h + ":" + m + ":" + s + " " + ampm;
  248. }
  249. function checkTime(i) {
  250.     if (i < 10) {
  251.         i = "0" + i;
  252.     }
  253.     return i;
  254. }
  255. function displayError(e) {
  256.     var message = e ? e.toString() : " unknown error";
  257.     $("#errorOutput").append("<div>" + message + "</div>");
  258. }
Advertisement
Add Comment
Please, Sign In to add comment