From 841eca04429a69d74904ffc210c2d0845be1c455 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Wed, 22 Jun 2016 19:13:43 +0800 Subject: Use location hash for server name --- static/mywatch.js | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file 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('
  • ' + s + '
  • ') + serverList.append('
  • ' + s + '
  • ') }); - $("#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 -- cgit v1.2.3