blob: 02d23aaca69fa4eee223cce657d2d95d46b6c959 [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
75 genericListViewClass = XOSListView.extend({template: "#xos-list-template",
76 app: XOSAdminApp});
77 XOSAdminApp["genericListView"] = genericListViewClass;
78
Scott Baker694f5662014-11-03 23:46:20 -080079 for (var index in OBJS) {
80 name = OBJS[index];
81 tr_template = '#xosAdmin-' + name + '-listitem-template';
82 table_template = '#xosAdmin-' + name + '-list-template';
83 detail_template = '#xosAdmin-' + name + '-detail-template';
Scott Baker0a636cb2014-12-07 22:27:09 -080084 add_child_template = '#xosAdmin-' + name + '-add-child-template';
Scott Baker694f5662014-11-03 23:46:20 -080085 collection_name = name + "s";
86 region_name = name + "List";
Scott Baker694f5662014-11-03 23:46:20 -080087
Scott Baker0a636cb2014-12-07 22:27:09 -080088 if ($(detail_template).length) {
89 detailClass = XOSDetailView.extend({
90 template: detail_template,
91 app: XOSAdminApp,
92 });
93 } else {
94 detailClass = genericDetailClass;
95 }
Scott Baker694f5662014-11-03 23:46:20 -080096 XOSAdminApp[collection_name + "DetailView"] = detailClass;
Scott Baker0a636cb2014-12-07 22:27:09 -080097
98 if ($(add_child_template).length) {
99 addClass = XOSDetailView.extend({
100 template: add_child_template,
101 app: XOSAdminApp,
102 });
103 } else {
104 addClass = genericAddChildClass;
105 }
106 XOSAdminApp[collection_name + "AddChildView"] = addClass;
107
Scott Bakere68d37b2014-12-09 16:59:08 -0800108 if ($(tr_template).length) {
109 itemViewClass = XOSItemView.extend({
110 template: tr_template,
111 app: XOSAdminApp,
112 });
113 } else {
114 itemViewClass = genericItemViewClass;
115 }
Scott Baker694f5662014-11-03 23:46:20 -0800116
Scott Bakere68d37b2014-12-09 16:59:08 -0800117 if ($(table_template).length) {
118 listViewClass = XOSListView.extend({
119 childView: itemViewClass,
120 template: table_template,
121 collection: xos[collection_name],
122 title: name + "s",
123 app: XOSAdminApp,
124 });
125 } else {
126 listViewClass = genericListViewClass.extend( { childView: itemViewClass,
127 collection: xos[collection_name],
128 title: name + "s",
129 } );
130 }
Scott Baker694f5662014-11-03 23:46:20 -0800131
132 XOSAdminApp[collection_name + "ListView"] = listViewClass;
133
134 xos[collection_name].fetch(); //startPolling();
135 }
136};
137
138XOSAdminApp.initRouter = function() {
Scott Baker29e8a2c2014-12-02 17:59:02 -0800139 router = XOSRouter;
Scott Baker694f5662014-11-03 23:46:20 -0800140 var api = {};
141 var routes = {};
142
Scott Baker694f5662014-11-03 23:46:20 -0800143 for (var index in OBJS) {
144 name = OBJS[index];
145 collection_name = name + "s";
146 nav_url = collection_name;
Scott Baker7ce23652014-11-07 16:40:30 -0800147 api_command = "list" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800148 listViewName = collection_name + "ListView";
149 detailViewName = collection_name + "DetailView";
Scott Baker0a636cb2014-12-07 22:27:09 -0800150 addChildViewName = collection_name + "AddChildView";
Scott Baker694f5662014-11-03 23:46:20 -0800151
Scott Baker3e8d4732014-12-04 17:22:01 -0800152 api[api_command] = XOSAdminApp.createListHandler(listViewName, collection_name, "detail", collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800153 routes[nav_url] = api_command;
154
155 nav_url = collection_name + "/:id";
Scott Baker7ce23652014-11-07 16:40:30 -0800156 api_command = "detail" + firstCharUpper(collection_name);
Scott Baker694f5662014-11-03 23:46:20 -0800157
Scott Baker3e8d4732014-12-04 17:22:01 -0800158 api[api_command] = XOSAdminApp.createDetailHandler(detailViewName, collection_name, "detail", name);
Scott Baker694f5662014-11-03 23:46:20 -0800159 routes[nav_url] = api_command;
Scott Baker7ce23652014-11-07 16:40:30 -0800160
161 nav_url = "add" + firstCharUpper(name);
162 api_command = "add" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800163 api[api_command] = XOSAdminApp.createAddHandler(detailViewName, collection_name, "detail", name);
164 routes[nav_url] = api_command;
165
166 nav_url = "addChild" + firstCharUpper(name) + "/:parentModel/:parentField/:parentId";
167 api_command = "addChild" + firstCharUpper(name);
Scott Baker0a636cb2014-12-07 22:27:09 -0800168 api[api_command] = XOSAdminApp.createAddChildHandler(addChildViewName, collection_name);
Scott Baker7ce23652014-11-07 16:40:30 -0800169 routes[nav_url] = api_command;
Scott Baker29e8a2c2014-12-02 17:59:02 -0800170
171 nav_url = "delete" + firstCharUpper(name) + "/:id";
172 api_command = "delete" + firstCharUpper(name);
Scott Baker3e8d4732014-12-04 17:22:01 -0800173 api[api_command] = XOSAdminApp.createDeleteHandler(collection_name, name);
Scott Baker29e8a2c2014-12-02 17:59:02 -0800174 routes[nav_url] = api_command;
Scott Baker694f5662014-11-03 23:46:20 -0800175 };
176
177 XOSAdminApp.Router = new router({ appRoutes: routes, controller: api });
Scott Bakerac694222014-11-05 22:12:33 -0800178};
179
180/* rewriteLinks
181
182 Rewrite the links in the suit navbar from django-links to marionette
183 links. This let's us intercept the navbar and make it function within
184 this view rather than jumping back out to a django view.
185*/
186
187XOSAdminApp.rewriteLinks = function () {
188 $("a").each(function() {
189 href=$(this).attr("href");
190 rewrite_href=REWRITES[href];
191 if (rewrite_href) {
192 $(this).attr("href", rewrite_href);
193 }
194 });
195};
Scott Baker694f5662014-11-03 23:46:20 -0800196
Scott Baker66aaad42014-11-13 15:52:02 -0800197XOSAdminApp.startNavigation = function() {
198 Backbone.history.start();
199 XOSAdminApp.navigationStarted = true;
200}
201
202XOSAdminApp.collectionLoadChange = function() {
203 stats = xos.getCollectionStatus();
204
205 if (!XOSAdminApp.navigationStarted) {
206 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
207 XOSAdminApp.startNavigation();
208 } else {
209 $("#detail").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
210 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
211 }
212 }
213};
214
Scott Baker694f5662014-11-03 23:46:20 -0800215XOSAdminApp.on("start", function() {
216 XOSAdminApp.buildViews();
217
218 XOSAdminApp.initRouter();
219
220 XOSAdminApp.updateNavigationPanel();
221
Scott Bakerac694222014-11-05 22:12:33 -0800222 XOSAdminApp.rewriteLinks();
223
Scott Baker66aaad42014-11-13 15:52:02 -0800224 // fire it once to initially show the progress bar
225 XOSAdminApp.collectionLoadChange();
226
227 // fire it each time the collection load status is updated
228 Backbone.on("xoslib:collectionLoadChange", XOSAdminApp.collectionLoadChange);
Scott Baker694f5662014-11-03 23:46:20 -0800229});
230
231$(document).ready(function(){
232 XOSAdminApp.start();
233});
234