blob: 5274a81cd3ce0f866a67e8580c437b0fa0e8c593 [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
24 addChildDetail: "#xos-addchild-detail"
Scott Baker694f5662014-11-03 23:46:20 -080025});
26
Scott Baker13e6f0d2014-11-18 17:02:07 -080027XOSAdminApp.navigate = function(what, modelName, modelId) {
28 collection_name = modelName + "s";
29 if (what=="list") {
30 XOSAdminApp.Router.navigate(collection_name, {trigger: true})
31 } else if (what=="detail") {
32 XOSAdminApp.Router.navigate(collection_name + "/" + modelId, {trigger: true})
33 } else if (what=="add") {
34 XOSAdminApp.Router.navigate("add" + firstCharUpper(modelName), {trigger: true})
35 }
36}
37
Scott Bakerf76d33a2014-11-04 09:34:01 -080038ICON_CLASSES = {home: "icon-home", deployments: "icon-deployment", sites: "icon-site", slices: "icon-slice", users: "icon-user"};
39
Scott Baker694f5662014-11-03 23:46:20 -080040XOSAdminApp.updateNavigationPanel = function() {
41 buttonTemplate=$("#xos-navbutton").html();
42 assert(buttonTemplate != undefined, "buttonTemplate is undefined");
Scott Bakerf76d33a2014-11-04 09:34:01 -080043 html="<div class='left-nav'><ul>";
Scott Baker694f5662014-11-03 23:46:20 -080044 for (var index in NAV_OBJS) {
45 name = NAV_OBJS[index];
46 collection_name = name+"s";
Scott Bakerf76d33a2014-11-04 09:34:01 -080047 nav_url = "#" + collection_name;
Scott Baker694f5662014-11-03 23:46:20 -080048 id = "nav-"+name;
Scott Bakerf76d33a2014-11-04 09:34:01 -080049 icon_class = ICON_CLASSES[collection_name] || "icon-cog";
Scott Baker694f5662014-11-03 23:46:20 -080050
Scott Bakerf76d33a2014-11-04 09:34:01 -080051 html = html + _.template(buttonTemplate, {name: collection_name, router: "XOSAdminApp.Router", routeUrl: nav_url, iconClass: icon_class});
Scott Baker694f5662014-11-03 23:46:20 -080052 }
53
Scott Bakerf76d33a2014-11-04 09:34:01 -080054 html = html + "</ul>";
55
Scott Baker694f5662014-11-03 23:46:20 -080056 $("#navigationPanel").html(html);
57};
58
59XOSAdminApp.buildViews = function() {
Scott Baker0a636cb2014-12-07 22:27:09 -080060 genericAddChildClass = XOSDetailView.extend({template: "#xos-add-template",
61 app: XOSAdminApp});
62 XOSAdminApp["genericAddChildView"] = genericAddChildClass;
63
64 genericDetailClass = XOSDetailView.extend({template: "#xos-detail-template",
65 app: XOSAdminApp});
66 XOSAdminApp["genericDetailView"] = genericDetailClass;
67
Scott Bakere68d37b2014-12-09 16:59:08 -080068 genericItemViewClass = XOSItemView.extend({template: "#xos-listitem-template",
69 app: XOSAdminApp});
70 XOSAdminApp["genericItemView"] = genericItemViewClass;
71
72 genericListViewClass = XOSListView.extend({template: "#xos-list-template",
73 app: XOSAdminApp});
74 XOSAdminApp["genericListView"] = genericListViewClass;
75
Scott Baker694f5662014-11-03 23:46:20 -080076 for (var index in OBJS) {
77 name = OBJS[index];
78 tr_template = '#xosAdmin-' + name + '-listitem-template';
79 table_template = '#xosAdmin-' + name + '-list-template';
80 detail_template = '#xosAdmin-' + name + '-detail-template';
Scott Baker0a636cb2014-12-07 22:27:09 -080081 add_child_template = '#xosAdmin-' + name + '-add-child-template';
Scott Baker694f5662014-11-03 23:46:20 -080082 collection_name = name + "s";
83 region_name = name + "List";
Scott Baker694f5662014-11-03 23:46:20 -080084
Scott Baker0a636cb2014-12-07 22:27:09 -080085 if ($(detail_template).length) {
86 detailClass = XOSDetailView.extend({
87 template: detail_template,
88 app: XOSAdminApp,
89 });
90 } else {
91 detailClass = genericDetailClass;
92 }
Scott Baker694f5662014-11-03 23:46:20 -080093 XOSAdminApp[collection_name + "DetailView"] = detailClass;
Scott Baker0a636cb2014-12-07 22:27:09 -080094
95 if ($(add_child_template).length) {
96 addClass = XOSDetailView.extend({
97 template: add_child_template,
98 app: XOSAdminApp,
99 });
100 } else {
101 addClass = genericAddChildClass;
102 }
103 XOSAdminApp[collection_name + "AddChildView"] = addClass;
104
Scott Bakere68d37b2014-12-09 16:59:08 -0800105 if ($(tr_template).length) {
106 itemViewClass = XOSItemView.extend({
107 template: tr_template,
108 app: XOSAdminApp,
109 });
110 } else {
111 itemViewClass = genericItemViewClass;
112 }
Scott Baker694f5662014-11-03 23:46:20 -0800113
Scott Bakere68d37b2014-12-09 16:59:08 -0800114 if ($(table_template).length) {
115 listViewClass = XOSListView.extend({
116 childView: itemViewClass,
117 template: table_template,
118 collection: xos[collection_name],
119 title: name + "s",
120 app: XOSAdminApp,
121 });
122 } else {
123 listViewClass = genericListViewClass.extend( { childView: itemViewClass,
124 collection: xos[collection_name],
125 title: name + "s",
126 } );
127 }
Scott Baker694f5662014-11-03 23:46:20 -0800128
129 XOSAdminApp[collection_name + "ListView"] = listViewClass;
130
131 xos[collection_name].fetch(); //startPolling();
132 }
133};
134
135XOSAdminApp.initRouter = function() {
Scott Baker29e8a2c2014-12-02 17:59:02 -0800136 router = XOSRouter;
Scott Baker694f5662014-11-03 23:46:20 -0800137 var api = {};
138 var routes = {};
139
Scott Baker694f5662014-11-03 23:46:20 -0800140 for (var index in OBJS) {
141 name = OBJS[index];
142 collection_name = name + "s";
143 nav_url = collection_name;
Scott Baker7ce23652014-11-07 16:40:30 -0800144 api_command = "list" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800145 listViewName = collection_name + "ListView";
146 detailViewName = collection_name + "DetailView";
Scott Baker0a636cb2014-12-07 22:27:09 -0800147 addChildViewName = collection_name + "AddChildView";
Scott Baker694f5662014-11-03 23:46:20 -0800148
Scott Baker3e8d4732014-12-04 17:22:01 -0800149 api[api_command] = XOSAdminApp.createListHandler(listViewName, collection_name, "detail", collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800150 routes[nav_url] = api_command;
151
152 nav_url = collection_name + "/:id";
Scott Baker7ce23652014-11-07 16:40:30 -0800153 api_command = "detail" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800154
Scott Baker3e8d4732014-12-04 17:22:01 -0800155 api[api_command] = XOSAdminApp.createDetailHandler(detailViewName, collection_name, "detail", name);
Scott Baker694f5662014-11-03 23:46:20 -0800156 routes[nav_url] = api_command;
Scott Baker7ce23652014-11-07 16:40:30 -0800157
158 nav_url = "add" + firstCharUpper(name);
159 api_command = "add" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800160 api[api_command] = XOSAdminApp.createAddHandler(detailViewName, collection_name, "detail", name);
161 routes[nav_url] = api_command;
162
163 nav_url = "addChild" + firstCharUpper(name) + "/:parentModel/:parentField/:parentId";
164 api_command = "addChild" + firstCharUpper(name);
Scott Baker0a636cb2014-12-07 22:27:09 -0800165 api[api_command] = XOSAdminApp.createAddChildHandler(addChildViewName, collection_name);
Scott Baker7ce23652014-11-07 16:40:30 -0800166 routes[nav_url] = api_command;
Scott Baker29e8a2c2014-12-02 17:59:02 -0800167
168 nav_url = "delete" + firstCharUpper(name) + "/:id";
169 api_command = "delete" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800170 api[api_command] = XOSAdminApp.createDeleteHandler(collection_name, name);
Scott Baker29e8a2c2014-12-02 17:59:02 -0800171 routes[nav_url] = api_command;
Scott Baker694f5662014-11-03 23:46:20 -0800172 };
173
174 XOSAdminApp.Router = new router({ appRoutes: routes, controller: api });
Scott Bakerac694222014-11-05 22:12:33 -0800175};
176
177/* rewriteLinks
178
179 Rewrite the links in the suit navbar from django-links to marionette
180 links. This let's us intercept the navbar and make it function within
181 this view rather than jumping back out to a django view.
182*/
183
184XOSAdminApp.rewriteLinks = function () {
185 $("a").each(function() {
186 href=$(this).attr("href");
187 rewrite_href=REWRITES[href];
188 if (rewrite_href) {
189 $(this).attr("href", rewrite_href);
190 }
191 });
192};
Scott Baker694f5662014-11-03 23:46:20 -0800193
Scott Baker66aaad42014-11-13 15:52:02 -0800194XOSAdminApp.startNavigation = function() {
195 Backbone.history.start();
196 XOSAdminApp.navigationStarted = true;
197}
198
199XOSAdminApp.collectionLoadChange = function() {
200 stats = xos.getCollectionStatus();
201
202 if (!XOSAdminApp.navigationStarted) {
203 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
204 XOSAdminApp.startNavigation();
205 } else {
206 $("#detail").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
207 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
208 }
209 }
210};
211
Scott Baker694f5662014-11-03 23:46:20 -0800212XOSAdminApp.on("start", function() {
213 XOSAdminApp.buildViews();
214
215 XOSAdminApp.initRouter();
216
217 XOSAdminApp.updateNavigationPanel();
218
Scott Bakerac694222014-11-05 22:12:33 -0800219 XOSAdminApp.rewriteLinks();
220
Scott Baker66aaad42014-11-13 15:52:02 -0800221 // fire it once to initially show the progress bar
222 XOSAdminApp.collectionLoadChange();
223
224 // fire it each time the collection load status is updated
225 Backbone.on("xoslib:collectionLoadChange", XOSAdminApp.collectionLoadChange);
Scott Baker694f5662014-11-03 23:46:20 -0800226});
227
228$(document).ready(function(){
229 XOSAdminApp.start();
230});
231