From 469b56ed8f9885b7bc7a18723b14f4690c8d5df4 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 15 May 2013 02:46:51 +0200 Subject: js: move all the js code for the page into own file --- index.html | 89 ++------------------------------------------------------------ js/page.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 87 deletions(-) create mode 100644 js/page.js diff --git a/index.html b/index.html index a80ca97..d9fb675 100644 --- a/index.html +++ b/index.html @@ -16,94 +16,9 @@ - + +
diff --git a/js/page.js b/js/page.js new file mode 100644 index 0000000..6177e41 --- /dev/null +++ b/js/page.js @@ -0,0 +1,84 @@ +var strip = function(data) { return data.split('.')[0] }; + +render = { + 'index' : function() { + jQuery.get('data/metadata/hosts', function(raw) { + var data = jsyaml.load(raw), + hosts = []; + + for(var i=0; i < data.hosts.length; i++) { + var entry = data.hosts[i]; + hosts.push({ + 'name' : strip(entry), + 'uri' : entry + }); + } + + $.Mustache.load('./templates/index.html').done(function () { + $('#content').mustache('hosts', {'hosts':hosts}, {method: 'html'}); + + $("#hosts a").click("hover", function(event){ + var uri = $(this).attr('data-uri'); + render.host(uri); + History.pushState({host: uri}, 'Hostinfo: ' + uri, 'index.html?'+uri); + return false; + }); + }); + }); + }, + + 'host' : function(uri) { + jQuery.get('data/'+uri, function(raw) { + var data = jsyaml.load(raw); + + data.name = strip(data.hostname); + data.interfaces = _.map( + _.groupBy( + _.filter(data.addresses, function(x) { + return typeof x.vserver == 'undefined'; + }), + function(x) { + return x.interface; + }), + function(obj, key) { + obj.device = key; + return obj; + }); + + $.Mustache.load('./templates/host.html').done(function () { + $('#content').mustache('hostinfo', data, {method: 'html'}); + + $("#index").click("hover", function(event){ + render.index(); + History.pushState({host: undefined}, 'Hostinfo: Overview', 'index.html'); + return false; + }); + }); + }); + } +}; + +$(document).ready(function() { + var History = window.History; + if (History.enabled) { + History.Adapter.bind(window,'statechange',function(){ + var State = History.getState(); + if (State.data.host) { + render.host(State.data.host); + } + else { + render.index(); + } + }); + } + + var host = window.location.search.split('?')[1] || undefined; + if (host) { + render.host(host); + document.title = 'Hostinfo: ' + host; + History.replaceState({host: host}, 'Hostinfo: ' + host, 'index.html?' + host); + } + else { + render.index(); + } +}); -- cgit v1.2.3-1-g7c22