blob: 3aa33d3b79a4be8b1b6b052c688843a1776b5c99 [file] [log] [blame]
Scott Baker694f5662014-11-03 23:46:20 -08001OBJS = ['deployment', 'image', 'networkTemplate', 'network', 'networkSliver', 'networkDeployment', 'node', 'service', 'site', 'slice', 'sliceDeployment', 'slicePrivilege', 'sliver', 'user', 'sliceRole', 'userDeployment'];
2NAV_OBJS = ['deployment', 'site', 'slice', 'user'];
3
Scott Bakerac694222014-11-05 22:12:33 -08004REWRITES = {"/admin/core/deployment/": "#deployments",
5 "/admin/core/site/" : "#sites",
6 "/admin/core/slice/" : "#slices",
7 "/admin/core/user/" : "#users"};
8
9XOSAdminApp = new XOSApplication({
10 logTableId: "#logTable",
11 statusMsgId: "#statusMsg",
12 hideTabsByDefault: true
13});
Scott Baker694f5662014-11-03 23:46:20 -080014
15XOSAdminApp.addRegions({
16 navigation: "#navigationPanel",
17
18 detail: "#detail",
19 linkedObjs1: "#linkedObjs1",
20 linkedObjs2: "#linkedObjs2",
21 linkedObjs3: "#linkedObjs3",
Scott Baker3e8d4732014-12-04 17:22:01 -080022 linkedObjs4: "#linkedObjs4",
23
Scott Bakerca4bf922014-12-09 18:38:13 -080024 addChildDetail: "#xos-addchild-detail",
25
26 rightButtonPanel: "#rightButtonPanel"
Scott Baker694f5662014-11-03 23:46:20 -080027});
28
Scott Baker13e6f0d2014-11-18 17:02:07 -080029XOSAdminApp.navigate = function(what, modelName, modelId) {
Scott Bakerab5f1362014-12-09 19:39:45 -080030 console.log("XOSAsminApp.navigate");
Scott Baker13e6f0d2014-11-18 17:02:07 -080031 collection_name = modelName + "s";
32 if (what=="list") {
33 XOSAdminApp.Router.navigate(collection_name, {trigger: true})
34 } else if (what=="detail") {
35 XOSAdminApp.Router.navigate(collection_name + "/" + modelId, {trigger: true})
36 } else if (what=="add") {
Scott Bakerab5f1362014-12-09 19:39:45 -080037 XOSAdminApp.Router.navigate("add" + firstCharUpper(modelName), {trigger: true, force: true})
Scott Baker13e6f0d2014-11-18 17:02:07 -080038 }
39}
40
Scott Bakerf76d33a2014-11-04 09:34:01 -080041ICON_CLASSES = {home: "icon-home", deployments: "icon-deployment", sites: "icon-site", slices: "icon-slice", users: "icon-user"};
42
Scott Baker694f5662014-11-03 23:46:20 -080043XOSAdminApp.updateNavigationPanel = function() {
44 buttonTemplate=$("#xos-navbutton").html();
45 assert(buttonTemplate != undefined, "buttonTemplate is undefined");
Scott Bakerf76d33a2014-11-04 09:34:01 -080046 html="<div class='left-nav'><ul>";
Scott Baker694f5662014-11-03 23:46:20 -080047 for (var index in NAV_OBJS) {
48 name = NAV_OBJS[index];
49 collection_name = name+"s";
Scott Bakerf76d33a2014-11-04 09:34:01 -080050 nav_url = "#" + collection_name;
Scott Baker694f5662014-11-03 23:46:20 -080051 id = "nav-"+name;
Scott Bakerf76d33a2014-11-04 09:34:01 -080052 icon_class = ICON_CLASSES[collection_name] || "icon-cog";
Scott Baker694f5662014-11-03 23:46:20 -080053
Scott Bakerf76d33a2014-11-04 09:34:01 -080054 html = html + _.template(buttonTemplate, {name: collection_name, router: "XOSAdminApp.Router", routeUrl: nav_url, iconClass: icon_class});
Scott Baker694f5662014-11-03 23:46:20 -080055 }
56
Scott Bakerf76d33a2014-11-04 09:34:01 -080057 html = html + "</ul>";
58
Scott Baker694f5662014-11-03 23:46:20 -080059 $("#navigationPanel").html(html);
60};
61
62XOSAdminApp.buildViews = function() {
Scott Baker0a636cb2014-12-07 22:27:09 -080063 genericAddChildClass = XOSDetailView.extend({template: "#xos-add-template",
64 app: XOSAdminApp});
65 XOSAdminApp["genericAddChildView"] = genericAddChildClass;
66
67 genericDetailClass = XOSDetailView.extend({template: "#xos-detail-template",
68 app: XOSAdminApp});
69 XOSAdminApp["genericDetailView"] = genericDetailClass;
70
Scott Bakere68d37b2014-12-09 16:59:08 -080071 genericItemViewClass = XOSItemView.extend({template: "#xos-listitem-template",
72 app: XOSAdminApp});
73 XOSAdminApp["genericItemView"] = genericItemViewClass;
74
Scott Baker00609072014-12-12 14:45:40 -080075 //genericListViewClass = XOSListView.extend({template: "#xos-list-template",
76 // app: XOSAdminApp});
77
78 genericListViewClass = XOSDataTableView.extend({template: "#xos-list-template", app: XOSAdminApp});
Scott Bakere68d37b2014-12-09 16:59:08 -080079 XOSAdminApp["genericListView"] = genericListViewClass;
80
Scott Baker694f5662014-11-03 23:46:20 -080081 for (var index in OBJS) {
82 name = OBJS[index];
83 tr_template = '#xosAdmin-' + name + '-listitem-template';
84 table_template = '#xosAdmin-' + name + '-list-template';
85 detail_template = '#xosAdmin-' + name + '-detail-template';
Scott Baker0a636cb2014-12-07 22:27:09 -080086 add_child_template = '#xosAdmin-' + name + '-add-child-template';
Scott Baker694f5662014-11-03 23:46:20 -080087 collection_name = name + "s";
88 region_name = name + "List";
Scott Baker694f5662014-11-03 23:46:20 -080089
Scott Baker0a636cb2014-12-07 22:27:09 -080090 if ($(detail_template).length) {
91 detailClass = XOSDetailView.extend({
92 template: detail_template,
93 app: XOSAdminApp,
94 });
95 } else {
96 detailClass = genericDetailClass;
97 }
Scott Baker694f5662014-11-03 23:46:20 -080098 XOSAdminApp[collection_name + "DetailView"] = detailClass;
Scott Baker0a636cb2014-12-07 22:27:09 -080099
100 if ($(add_child_template).length) {
101 addClass = XOSDetailView.extend({
102 template: add_child_template,
103 app: XOSAdminApp,
104 });
105 } else {
106 addClass = genericAddChildClass;
107 }
108 XOSAdminApp[collection_name + "AddChildView"] = addClass;
109
Scott Bakere68d37b2014-12-09 16:59:08 -0800110 if ($(tr_template).length) {
111 itemViewClass = XOSItemView.extend({
112 template: tr_template,
113 app: XOSAdminApp,
114 });
115 } else {
116 itemViewClass = genericItemViewClass;
117 }
Scott Baker694f5662014-11-03 23:46:20 -0800118
Scott Bakere68d37b2014-12-09 16:59:08 -0800119 if ($(table_template).length) {
120 listViewClass = XOSListView.extend({
121 childView: itemViewClass,
122 template: table_template,
123 collection: xos[collection_name],
124 title: name + "s",
125 app: XOSAdminApp,
126 });
127 } else {
128 listViewClass = genericListViewClass.extend( { childView: itemViewClass,
129 collection: xos[collection_name],
130 title: name + "s",
131 } );
132 }
Scott Baker694f5662014-11-03 23:46:20 -0800133
134 XOSAdminApp[collection_name + "ListView"] = listViewClass;
135
136 xos[collection_name].fetch(); //startPolling();
137 }
138};
139
140XOSAdminApp.initRouter = function() {
Scott Baker29e8a2c2014-12-02 17:59:02 -0800141 router = XOSRouter;
Scott Baker694f5662014-11-03 23:46:20 -0800142 var api = {};
143 var routes = {};
144
Scott Baker694f5662014-11-03 23:46:20 -0800145 for (var index in OBJS) {
146 name = OBJS[index];
147 collection_name = name + "s";
148 nav_url = collection_name;
Scott Baker7ce23652014-11-07 16:40:30 -0800149 api_command = "list" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800150 listViewName = collection_name + "ListView";
151 detailViewName = collection_name + "DetailView";
Scott Baker0a636cb2014-12-07 22:27:09 -0800152 addChildViewName = collection_name + "AddChildView";
Scott Baker694f5662014-11-03 23:46:20 -0800153
Scott Baker3e8d4732014-12-04 17:22:01 -0800154 api[api_command] = XOSAdminApp.createListHandler(listViewName, collection_name, "detail", collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800155 routes[nav_url] = api_command;
156
157 nav_url = collection_name + "/:id";
Scott Baker7ce23652014-11-07 16:40:30 -0800158 api_command = "detail" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800159
Scott Baker3e8d4732014-12-04 17:22:01 -0800160 api[api_command] = XOSAdminApp.createDetailHandler(detailViewName, collection_name, "detail", name);
Scott Baker694f5662014-11-03 23:46:20 -0800161 routes[nav_url] = api_command;
Scott Baker7ce23652014-11-07 16:40:30 -0800162
163 nav_url = "add" + firstCharUpper(name);
164 api_command = "add" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800165 api[api_command] = XOSAdminApp.createAddHandler(detailViewName, collection_name, "detail", name);
166 routes[nav_url] = api_command;
167
168 nav_url = "addChild" + firstCharUpper(name) + "/:parentModel/:parentField/:parentId";
169 api_command = "addChild" + firstCharUpper(name);
Scott Baker0a636cb2014-12-07 22:27:09 -0800170 api[api_command] = XOSAdminApp.createAddChildHandler(addChildViewName, collection_name);
Scott Baker7ce23652014-11-07 16:40:30 -0800171 routes[nav_url] = api_command;
Scott Baker29e8a2c2014-12-02 17:59:02 -0800172
173 nav_url = "delete" + firstCharUpper(name) + "/:id";
174 api_command = "delete" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800175 api[api_command] = XOSAdminApp.createDeleteHandler(collection_name, name);
Scott Baker29e8a2c2014-12-02 17:59:02 -0800176 routes[nav_url] = api_command;
Scott Baker694f5662014-11-03 23:46:20 -0800177 };
178
179 XOSAdminApp.Router = new router({ appRoutes: routes, controller: api });
Scott Bakerac694222014-11-05 22:12:33 -0800180};
181
182/* rewriteLinks
183
184 Rewrite the links in the suit navbar from django-links to marionette
185 links. This let's us intercept the navbar and make it function within
186 this view rather than jumping back out to a django view.
187*/
188
189XOSAdminApp.rewriteLinks = function () {
190 $("a").each(function() {
191 href=$(this).attr("href");
192 rewrite_href=REWRITES[href];
193 if (rewrite_href) {
194 $(this).attr("href", rewrite_href);
195 }
196 });
197};
Scott Baker694f5662014-11-03 23:46:20 -0800198
Scott Baker66aaad42014-11-13 15:52:02 -0800199XOSAdminApp.startNavigation = function() {
200 Backbone.history.start();
201 XOSAdminApp.navigationStarted = true;
202}
203
204XOSAdminApp.collectionLoadChange = function() {
205 stats = xos.getCollectionStatus();
206
207 if (!XOSAdminApp.navigationStarted) {
208 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
209 XOSAdminApp.startNavigation();
210 } else {
211 $("#detail").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
212 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
213 }
214 }
215};
216
Scott Baker694f5662014-11-03 23:46:20 -0800217XOSAdminApp.on("start", function() {
218 XOSAdminApp.buildViews();
219
220 XOSAdminApp.initRouter();
221
222 XOSAdminApp.updateNavigationPanel();
223
Scott Bakerac694222014-11-05 22:12:33 -0800224 XOSAdminApp.rewriteLinks();
225
Scott Baker66aaad42014-11-13 15:52:02 -0800226 // fire it once to initially show the progress bar
227 XOSAdminApp.collectionLoadChange();
228
229 // fire it each time the collection load status is updated
230 Backbone.on("xoslib:collectionLoadChange", XOSAdminApp.collectionLoadChange);
Scott Baker694f5662014-11-03 23:46:20 -0800231});
232
233$(document).ready(function(){
234 XOSAdminApp.start();
235});
236