blob: 9fb6f66620ac5751f57731f87d181e49249eeb5b [file] [log] [blame]
Pankaj Berdec4ae4b82013-01-12 09:56:47 -08001/*
2 Copyright 2012 IBM
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17var hackBase = "http://localhost:9000"; // put a URL here to access a different REST server
18
19var AppRouter = Backbone.Router.extend({
20
21 routes:{
22 "":"home",
23 "topology":"topology",
24 "switches":"switchList",
25 "switch/:id":"switchDetails",
26 "switch/:id/port/:p":"portDetails", // not clear if needed
27 "hosts":"hostList",
28 "host/:id":"hostDetails",
29 // "vlans":"vlanList" // maybe one day
30 // "vlan/:id":"vlanDetails"
31 },
32
33 initialize:function () {
34 this.headerView = new HeaderView();
35 $('.header').html(this.headerView.render().el);
36
37 // Close the search dropdown on click anywhere in the UI
38 $('body').click(function () {
39 $('.dropdown').removeClass("open");
40 });
41 },
42
43 home:function () {
44 $('#content').html(new HomeView().render().el);
45 $('ul[class="nav"] > li').removeClass('active');
46 $('a[href="/"]').parent().addClass('active');
47 },
48
49 topology:function () {
50 //console.log("switching to topology view");
51 var topo = new Topology();
52 $('#content').html(new TopologyView({model:topo, hosts:hl}).render().el);
53 // TODO factor this code out
54 $('ul.nav > li').removeClass('active');
55 $('li > a[href*="topology"]').parent().addClass('active');
56 },
57
58 switchDetails:function (id) {
59 //console.log("switching [sic] to single switch view");
60 var sw = swl.get(id);
61 $('#content').html(new SwitchView({model:sw}).render().el);
62 $('ul.nav > li').removeClass('active');
63 $('li > a[href*="/switches"]').parent().addClass('active');
64 },
65
66 switchList:function () {
67 //console.log("switching [sic] to switch list view");
68 $('#content').html(new SwitchListView({model:swl}).render().el);
69 $('ul.nav > li').removeClass('active');
70 $('li > a[href*="/switches"]').parent().addClass('active');
71 },
72
73 hostDetails:function (id) {
74 //console.log("switching to single host view");
75 var h = hl.get(id);
76 $('#content').html(new HostView({model:h}).render().el);
77 $('ul.nav > li').removeClass('active');
78 $('li > a[href*="/hosts"]').parent().addClass('active');
79 },
80
81 hostList:function () {
82 //console.log("switching to host list view");
83 $('#content').html(new HostListView({model:hl}).render().el);
84 $('ul.nav > li').removeClass('active');
85 $('li > a[href*="/hosts"]').parent().addClass('active');
86 },
87
88});
89
90// load global models and reuse them
91var swl = new SwitchCollection();
92var hl = new HostCollection();
93
94var updating = true;
95
96tpl.loadTemplates(['home', 'status', 'topology', 'header', 'switch', 'switch-list', 'switch-list-item', 'host', 'host-list', 'host-list-item', 'port-list', 'port-list-item', 'flow-list', 'flow-list-item'],
97 function () {
98 app = new AppRouter();
99 Backbone.history.start({pushState: true});
100 //console.log("started history")
101
102 $(document).ready(function () {
103 // trigger Backbone routing when clicking on links, thanks to Atinux and pbnv
104 app.navigate("", true);
105
106 window.document.addEventListener('click', function(e) {
107 e = e || window.event
108 var target = e.target || e.srcElement
109 if ( target.nodeName.toLowerCase() === 'a' ) {
110 e.preventDefault()
111 var uri = target.getAttribute('href')
112 app.navigate(uri.substr(1), true)
113 }
114 });
115 window.addEventListener('popstate', function(e) {
116 app.navigate(location.pathname.substr(1), true);
117 });
118
119 // wait for the page to be rendered before loading any data
120 swl.fetch();
121 hl.fetch();
122
123 setInterval(function () {
124 if(updating) {
125 swl.fetch();
126 hl.fetch();
127 }
128 }, 3000);
129 });
130 });