xosAdminSite, WIP
[plstackapi.git] / planetstack / core / xoslib / static / js / xosAdminSite.js
1 OBJS = ['deployment', 'image', 'networkTemplate', 'network', 'networkSliver', 'networkDeployment', 'node', 'service', 'site', 'slice', 'sliceDeployment', 'slicePrivilege', 'sliver', 'user', 'sliceRole', 'userDeployment'];
2 NAV_OBJS = ['deployment', 'site', 'slice', 'user'];
3
4 function assert(outcome, description) {
5     if (!outcome) {
6         console.log(description);
7     }
8 }
9
10 XOSAdminApp = new XOSApplication();
11
12 XOSAdminApp.addRegions({
13     navigation: "#navigationPanel",
14
15     detail: "#detail",
16     linkedObjs1: "#linkedObjs1",
17     linkedObjs2: "#linkedObjs2",
18     linkedObjs3: "#linkedObjs3",
19     linkedObjs4: "#linkedObjs4"
20 });
21
22 XOSAdminApp.navigateToModel = function(app, detailClass, detailNavLink, model) {
23      XOSAdminApp.Router.navigate(detailNavLink + "/" + model.id, {trigger: true});
24 };\r
25 \r
26 XOSAdminApp.updateNavigationPanel = function() {\r
27     buttonTemplate=$("#xos-navbutton").html();\r
28     assert(buttonTemplate != undefined, "buttonTemplate is undefined");\r
29     html=""\r
30     for (var index in NAV_OBJS) {\r
31         name = NAV_OBJS[index];\r
32         collection_name = name+"s";\r
33         nav_url = "/" + collection_name;\r
34         id = "nav-"+name;\r
35 \r
36         html = html + _.template(buttonTemplate, {name: collection_name, router: "XOSAdminApp.Router", routeUrl: nav_url});\r
37     }\r
38 \r
39     $("#navigationPanel").html(html);\r
40 };\r
41 \r
42 XOSAdminApp.buildViews = function() {\r
43      for (var index in OBJS) {\r
44          name = OBJS[index];
45          tr_template = '#xosAdmin-' + name + '-listitem-template';
46          table_template = '#xosAdmin-' + name + '-list-template';
47          detail_template = '#xosAdmin-' + name + '-detail-template';
48          collection_name = name + "s";
49          region_name = name + "List";
50          detailNavLink = collection_name;
51
52          detailClass = XOSDetailView.extend({
53             template: detail_template,\r
54             app: XOSAdminApp,\r
55          });\r
56          XOSAdminApp[collection_name + "DetailView"] = detailClass;\r
57
58          itemViewClass = XOSItemView.extend({
59              detailClass: detailClass,
60              template: tr_template,
61              app: XOSAdminApp,
62              detailNavLink: detailNavLink,
63          });
64
65          listViewClass = XOSListView.extend({
66              childView: itemViewClass,
67              template: table_template,
68              collection: xos[collection_name],
69              title: name + "s",
70              app: XOSAdminApp,
71          });
72
73          XOSAdminApp[collection_name + "ListView"] = listViewClass;
74
75          xos[collection_name].fetch(); //startPolling();
76      }\r
77 };\r
78 \r
79 XOSAdminApp.initRouter = function() {\r
80     router = Marionette.AppRouter.extend({\r
81     });\r
82 \r
83     var api = {};\r
84     var routes = {};\r
85 \r
86     function listViewShower(listViewName) {\r
87         return function() {\r
88             XOSAdminApp.detail.show(new XOSAdminApp[listViewName]);\r
89         }\r
90     };\r
91 \r
92     function detailShower(detailName, collection_name) {\r
93         shower = function(model_id) {\r
94             model = xos[collection_name].get(model_id);\r
95             if (model == undefined) {\r
96                 $("#detail").html("not ready yet");\r
97                 return;\r
98             }\r
99             detailViewClass = XOSAdminApp[detailName];\r
100             detailView = new detailViewClass({model: model});\r
101             XOSAdminApp.detail.show(detailView);\r
102             detailView.showLinkedItems();\r
103         }\r
104         return shower;\r
105     };\r
106 \r
107     for (var index in OBJS) {\r
108         name = OBJS[index];\r
109         collection_name = name + "s";\r
110         nav_url = collection_name;\r
111         api_command = "list" + collection_name.charAt(0).toUpperCase() + collection_name.slice(1);\r
112         listViewName = collection_name + "ListView";\r
113         detailViewName = collection_name + "DetailView";\r
114 \r
115         api[api_command] = listViewShower(listViewName);\r
116         routes[nav_url] = api_command;\r
117 \r
118         nav_url = collection_name + "/:id";\r
119         api_command = "detail" + collection_name.charAt(0).toUpperCase() + collection_name.slice(1);\r
120 \r
121         api[api_command] = detailShower(detailViewName, collection_name);\r
122         routes[nav_url] = api_command;\r
123     };\r
124 \r
125     XOSAdminApp.Router = new router({ appRoutes: routes, controller: api });\r
126 }\r
127 \r
128 XOSAdminApp.on("start", function() {\r
129      XOSAdminApp.buildViews();
130
131      XOSAdminApp.initRouter();
132
133      XOSAdminApp.updateNavigationPanel();
134
135      if (Backbone.history) {
136          console.log("history start");
137          Backbone.history.start();
138      }
139 });
140
141 $(document).ready(function(){
142     XOSAdminApp.start();
143 });
144