Merge branch 'master' of ssh://git.planet-lab.org/git/plstackapi
[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 REWRITES = {"/admin/core/deployment/": "#deployments",
5             "/admin/core/site/" : "#sites",
6             "/admin/core/slice/" : "#slices",
7             "/admin/core/user/" : "#users"};
8
9 XOSAdminApp = new XOSApplication({
10     logTableId: "#logTable",
11     statusMsgId: "#statusMsg",
12     hideTabsByDefault: true
13 });
14
15 XOSAdminApp.addRegions({
16     navigation: "#navigationPanel",
17
18     detail: "#detail",
19     linkedObjs1: "#linkedObjs1",
20     linkedObjs2: "#linkedObjs2",
21     linkedObjs3: "#linkedObjs3",
22     linkedObjs4: "#linkedObjs4",
23
24     addChildDetail: "#xos-addchild-detail"
25 });
26
27 XOSAdminApp.navigateToModel = function(app, detailClass, detailNavLink, model) {
28      XOSAdminApp.Router.navigate(detailNavLink + "/" + model.id, {trigger: true});
29 };\r
30 \r
31 XOSAdminApp.navigate = function(what, modelName, modelId) {\r
32     collection_name = modelName + "s";\r
33     if (what=="list") {\r
34         XOSAdminApp.Router.navigate(collection_name, {trigger: true})\r
35     } else if (what=="detail") {\r
36         XOSAdminApp.Router.navigate(collection_name + "/" + modelId, {trigger: true})\r
37     } else if (what=="add") {\r
38         XOSAdminApp.Router.navigate("add" + firstCharUpper(modelName), {trigger: true})\r
39     }\r
40 }\r
41 \r
42 ICON_CLASSES = {home: "icon-home", deployments: "icon-deployment", sites: "icon-site", slices: "icon-slice", users: "icon-user"};\r
43 \r
44 XOSAdminApp.updateNavigationPanel = function() {\r
45     buttonTemplate=$("#xos-navbutton").html();\r
46     assert(buttonTemplate != undefined, "buttonTemplate is undefined");\r
47     html="<div class='left-nav'><ul>";\r
48     for (var index in NAV_OBJS) {\r
49         name = NAV_OBJS[index];\r
50         collection_name = name+"s";\r
51         nav_url = "#" + collection_name;\r
52         id = "nav-"+name;\r
53         icon_class = ICON_CLASSES[collection_name] || "icon-cog";\r
54 \r
55         html = html + _.template(buttonTemplate, {name: collection_name, router: "XOSAdminApp.Router", routeUrl: nav_url, iconClass: icon_class});\r
56     }\r
57 \r
58     html = html + "</ul>";\r
59 \r
60     $("#navigationPanel").html(html);\r
61 };\r
62 \r
63 XOSAdminApp.buildViews = function() {\r
64      for (var index in OBJS) {\r
65          name = OBJS[index];
66          tr_template = '#xosAdmin-' + name + '-listitem-template';
67          table_template = '#xosAdmin-' + name + '-list-template';
68          detail_template = '#xosAdmin-' + name + '-detail-template';
69          collection_name = name + "s";
70          region_name = name + "List";
71          detailNavLink = collection_name;
72
73          detailClass = XOSDetailView.extend({
74             template: detail_template,\r
75             app: XOSAdminApp,\r
76          });\r
77          XOSAdminApp[collection_name + "DetailView"] = detailClass;\r
78
79          itemViewClass = XOSItemView.extend({
80              detailClass: detailClass,
81              template: tr_template,
82              app: XOSAdminApp,
83              detailNavLink: detailNavLink,
84          });
85
86          listViewClass = XOSListView.extend({
87              childView: itemViewClass,
88              template: table_template,
89              collection: xos[collection_name],
90              title: name + "s",
91              app: XOSAdminApp,
92          });
93
94          XOSAdminApp[collection_name + "ListView"] = listViewClass;
95
96          xos[collection_name].fetch(); //startPolling();
97      }\r
98 };\r
99 \r
100 XOSAdminApp.initRouter = function() {\r
101     router = XOSRouter;\r
102     var api = {};\r
103     var routes = {};\r
104 \r
105     for (var index in OBJS) {\r
106         name = OBJS[index];\r
107         collection_name = name + "s";\r
108         nav_url = collection_name;\r
109         api_command = "list" + firstCharUpper(collection_name);\r
110         listViewName = collection_name + "ListView";\r
111         detailViewName = collection_name + "DetailView";\r
112 \r
113         api[api_command] = XOSAdminApp.createListHandler(listViewName, collection_name, "detail", collection_name);\r
114         routes[nav_url] = api_command;\r
115 \r
116         nav_url = collection_name + "/:id";\r
117         api_command = "detail" + firstCharUpper(collection_name);\r
118 \r
119         api[api_command] = XOSAdminApp.createDetailHandler(detailViewName, collection_name, "detail", name);\r
120         routes[nav_url] = api_command;\r
121 \r
122         nav_url = "add" + firstCharUpper(name);\r
123         api_command = "add" + firstCharUpper(name);\r
124         api[api_command] = XOSAdminApp.createAddHandler(detailViewName, collection_name, "detail", name);\r
125         routes[nav_url] = api_command;\r
126 \r
127         nav_url = "addChild" + firstCharUpper(name) + "/:parentModel/:parentField/:parentId";\r
128         api_command = "addChild" + firstCharUpper(name);\r
129         api[api_command] = XOSAdminApp.createAddChildHandler(detailViewName, collection_name);\r
130         routes[nav_url] = api_command;\r
131 \r
132         nav_url = "delete" + firstCharUpper(name) + "/:id";\r
133         api_command = "delete" + firstCharUpper(name);\r
134         api[api_command] = XOSAdminApp.createDeleteHandler(collection_name, name);\r
135         routes[nav_url] = api_command;\r
136     };\r
137 \r
138     XOSAdminApp.Router = new router({ appRoutes: routes, controller: api });\r
139 };\r
140 \r
141 /* rewriteLinks\r
142 \r
143    Rewrite the links in the suit navbar from django-links to marionette\r
144    links. This let's us intercept the navbar and make it function within\r
145    this view rather than jumping back out to a django view.\r
146 */\r
147 \r
148 XOSAdminApp.rewriteLinks = function () {\r
149     $("a").each(function() {\r
150         href=$(this).attr("href");\r
151         rewrite_href=REWRITES[href];\r
152         if (rewrite_href) {\r
153             $(this).attr("href", rewrite_href);\r
154         }\r
155     });\r
156 };\r
157 \r
158 XOSAdminApp.startNavigation = function() {\r
159     Backbone.history.start();\r
160     XOSAdminApp.navigationStarted = true;\r
161 }\r
162 \r
163 XOSAdminApp.collectionLoadChange = function() {\r
164     stats = xos.getCollectionStatus();\r
165 \r
166     if (!XOSAdminApp.navigationStarted) {\r
167         if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {\r
168             XOSAdminApp.startNavigation();\r
169         } else {\r
170             $("#detail").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");\r
171             $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});\r
172         }\r
173     }\r
174 };\r
175 \r
176 XOSAdminApp.on("start", function() {\r
177      XOSAdminApp.buildViews();
178
179      XOSAdminApp.initRouter();
180
181      XOSAdminApp.updateNavigationPanel();
182
183      XOSAdminApp.rewriteLinks();
184
185      // fire it once to initially show the progress bar
186      XOSAdminApp.collectionLoadChange();
187
188      // fire it each time the collection load status is updated
189      Backbone.on("xoslib:collectionLoadChange", XOSAdminApp.collectionLoadChange);
190 });
191
192 $(document).ready(function(){
193     XOSAdminApp.start();
194 });
195