blob: 2362757ac690fb3c27bc6a6a54fbe3e2eead68a6 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -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 = ""; // 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
94tpl.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'],
95 function () {
96 app = new AppRouter();
97 Backbone.history.start({pushState: true});
98 // console.log("started history")
99
100 $(document).ready(function () {
101 // trigger Backbone routing when clicking on links, thanks to Atinux and pbnv
102 app.navigate("", true);
103
104 window.document.addEventListener('click', function(e) {
105 e = e || window.event
106 var target = e.target || e.srcElement
107 if ( target.nodeName.toLowerCase() === 'a' ) {
108 e.preventDefault()
109 var uri = target.getAttribute('href')
110 app.navigate(uri.substr(1), true)
111 }
112 });
113 window.addEventListener('popstate', function(e) {
114 app.navigate(location.pathname.substr(1), true);
115 });
116
117 });
118 });
119
120setInterval(function () {
121 swl.fetch();
122}, 3000);
123
124setInterval(function () {
125 hl.fetch();
126}, 3000);
127