diff options
-rw-r--r-- | static/mywatch.js | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/static/mywatch.js b/static/mywatch.js index d648764..ec9400f 100644 --- a/static/mywatch.js +++ b/static/mywatch.js @@ -1,9 +1,10 @@ $(function() { var info = $('#info'); - var infoHead = $('#info>h1'); var infoAlert = $('#info>div'); + var infoHead = $('#info>h1'); var main = $('#main'); var plBody = $('#processList>tbody'); + var serverList = $('#serverList>ul'); var interval = null; @@ -22,6 +23,27 @@ $(function() { info.show(); } + function switchServer(server) { + clearInterval(interval); + if ('' !== server) { + document.title = server + ' — ' + 'MyWatch'; + serverList.find('.active').removeClass('active'); + var s = $('a[href="#' + server + '"]'); + if (s) { + s.parent().addClass('active'); + getProcessList(server); + interval = setInterval(getProcessList, 60 * 1000, server); + } + } else { + document.title = 'MyWatch'; + } + } + + function onHash() { + switchServer(location.hash.substring(1)); + }; + window.onhashchange = onHash; + function getProcessList(server) { $.ajax({ url: "server/" + server + "/processlist.json", @@ -52,8 +74,8 @@ $(function() { }; $.ajax({ - url: "serverlist.json", - method: "GET", + url: 'serverlist.json', + method: 'GET', error: commonError, success: function(servers) { var total = servers.length; @@ -61,32 +83,29 @@ $(function() { var checked = 0; $.each(servers, function(i, s) { $.ajax({ - url: "server/" + s + "/processlist.json", - method: "HEAD", + url: 'server/' + s + '/processlist.json', + method: 'HEAD', success: function() { available.push(s); }, complete: function() { - var menu = $('#serverList>ul'); checked++; if (checked === total) { $.each(available.sort(), function(i, s) { - menu.append('<li><a href="#">' + s + '</a></li>') + serverList.append('<li><a href="#' + s + '">' + s + '</a></li>') }); - $("#serverList a").on("click", function() { - var server = $(this).text(); - document.title = server + ' — ' + 'MyWatch'; - $(this).parent().parent().find('.active').removeClass('active'); - $(this).parent().addClass('active'); - clearInterval(interval); - getProcessList(server); - interval = setInterval(getProcessList, 60 * 1000, server); + serverList.find('a').on('click', function() { + var s = $(this).text(); + if ('#' + s === location.hash) { + getProcessList(s); + } }); info.hide(); + onHash(); } } }); }); } }); -}); +});
\ No newline at end of file |