0ab8e819378a4d045a08b817979b9f7b03ae3cb2
[plstackapi.git] / planetstack / core / xoslib / static / js / xosTenant.js
1 XOSTenantSite = XOSModel.extend( {
2     listFields: ["name", "allocated"],
3     modelName: "tenantSite",
4     collectionName: "tenantSites"
5 });
6
7 XOSTenantSiteCollection = XOSCollection.extend( {
8     listFields: ["name", "allocated"],
9     modelName: "tenantSite",
10     collectionName: "tenantSites",
11
12     getFromSlice: function(slice) {
13         var tenantSites = [];
14         var id = 0;
15         for (siteName in slice.attributes.site_allocation) {
16             allocated = slice.attributes.site_allocation[siteName];
17             tenantSites.push(new XOSTenantSite( { name: siteName, allocated: allocated, id: id} ));
18             id = id + 1;
19         }
20         for (index in xos.tenantview.models[0].attributes.blessed_site_names) {
21             siteName = xos.tenantview.models[0].attributes.blessed_site_names[index];
22             if (! (siteName in slice.attributes.site_allocation)) {
23                 tenantSites.push(new XOSTenantSite( { name: siteName, allocated: 0, id: id} ));
24                 id = id + 1;
25             }
26         }
27         this.set(tenantSites);
28     },
29
30     putToSlice: function(slice) {
31         slice.attributes.site_allocation = {};
32         for (index in this.models) {
33             var model = this.models[index];
34             slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
35         }
36     },
37 });
38
39 XOSEditUsersView = Marionette.ItemView.extend({
40             template: "#tenant-edit-users",
41             viewInitializers: [],
42
43             onShow: function() {
44                 _.each(this.viewInitializers, function(initializer) {
45                     initializer();
46                 });
47             },
48
49             templateHelpers: function() { return { detailView: this, model: this.model }; },
50
51             });
52
53
54 XOSTenantButtonView = Marionette.ItemView.extend({
55             template: "#xos-tenant-buttons-template",
56
57             events: {"click button.btn-tenant-create": "createClicked",
58                      "click button.btn-tenant-delete": "deleteClicked",
59                      "click button.btn-tenant-add-user": "addUserClicked",
60                      "click button.btn-tenant-save": "saveClicked",
61                      },
62
63             createClicked: function(e) {
64                      XOSTenantApp.addSlice();
65                      },
66
67             deleteClicked: function(e) {
68                      XOSTenantApp.deleteSlice(this.options.linkedView.model);
69                      },
70
71             addUserClicked: function(e) {
72                      XOSTenantApp.editUsers(this.options.linkedView.model);
73                      },
74
75             saveClicked: function(e) {
76                      model = this.options.linkedView.model;
77                      model.tenantSiteCollection.putToSlice(model);
78                      model.attributes.users = model.usersBuffer;
79                      this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
80                      },
81             });
82
83 XOSTenantApp = new XOSApplication({
84     logTableId: "#logTable",
85     statusMsgId: "#statusMsg",
86     hideTabsByDefault: true,
87     varName: "XOSTenantApp",
88 });
89
90 XOSTenantApp.addRegions({
91     tenantSliceSelector: "#tenantSliceSelector",
92     tenantSummary: "#tenantSummary",
93     tenantSiteList: "#tenantSiteList",
94     tenantButtons: "#tenantButtons",
95     tenantAddSliceInterior: "#tenant-addslice-interior",
96     tenantEditUsersInterior: "#tenant-edit-users-interior",
97 });
98
99 XOSTenantApp.buildViews = function() {
100      XOSTenantApp.tenantSites = new XOSTenantSiteCollection();\r
101 \r
102      tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",\r
103                                                 app: XOSTenantApp,\r
104                                                 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],\r
105                                                 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},\r
106 \r
107                                                 onShow: function() {\r
108                                                     // the slice selector is in a different table, so make every label cell the maximal width\r
109                                                     make_same_width("#xos-tenant-view-panel", ".xos-label-cell");\r
110                                                 },\r
111                                                 });\r
112 \r
113      XOSTenantApp.tenantSummaryView = tenantSummaryClass;\r
114 \r
115      tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",\r
116                                                 app: XOSTenantApp,\r
117                                                 detailFields: ["name", "description"]});\r
118 \r
119      XOSTenantApp.tenantAddView = tenantAddClass;\r
120 \r
121      tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",\r
122                                                app: XOSTenantApp});\r
123 \r
124      XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;\r
125 \r
126      tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",\r
127                                                app: XOSTenantApp,\r
128                                                childView: tenantSiteItemClass,\r
129                                                collection: XOSTenantApp.tenantSites,\r
130                                                title: "sites",\r
131                                                inputType: {allocated: "spinner"},\r
132                                                noDeleteColumn: true,\r
133                                                disablePaginate: true,\r
134                                                disableFilter: true,\r
135                                                fieldDisplayNames: {"name": "Site"},\r
136                                                });\r
137 \r
138      XOSTenantApp.tenantSiteListView = tenantSiteListClass;\r
139 \r
140      XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {\r
141          sliceChanged: function(id) {\r
142              XOSTenantApp.navToSlice(id);\r
143          },\r
144      });\r
145 \r
146      xos.sites.fetch();\r
147      xos.slicesPlus.fetch();\r
148      xos.tenantview.fetch();\r
149 };\r
150 \r
151 make_choices = function(list_of_names, list_of_values) {\r
152     result = [];\r
153     if (!list_of_values) {\r
154         for (index in list_of_names) {\r
155             displayName = list_of_names[index];\r
156             result.push( [displayName, displayName] );\r
157         }\r
158     } else {\r
159         for (index in list_of_names) {\r
160             displayName = list_of_names[index];\r
161             id = list_of_values[index];\r
162             result.push( [displayName, id] );\r
163         }\r
164     }\r
165     return result;\r
166 };\r
167 \r
168 XOSTenantApp.navToSlice = function(id) {\r
169     XOSTenantApp.viewSlice(xos.slicesPlus.get(id));\r
170 };\r
171 \r
172 XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {\r
173     model = XOSTenantApp[collectionName].get(id);\r
174     model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));\r
175 };\r
176 \r
177 XOSTenantApp.addSlice = function() {\r
178     var app=this;\r
179 \r
180     if (!xos.tenant().current_user_can_create_slice) {\r
181         window.alert("You do not have sufficient rights to create a slice on your site");\r
182         return;\r
183     }\r
184 \r
185     model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,\r
186                                       name: xos.tenant().current_user_login_base + "_",\r
187                                       creator: xos.tenant().current_user_id});\r
188     console.log(model);\r
189     var detailView = new XOSTenantApp.tenantAddView({model: model,\r
190                                                      collection: xos.slicesPlus,\r
191                                                      noSubmitButton: true,\r
192                                                     });\r
193     detailView.dialog = $("#tenant-addslice-dialog");\r
194     app.tenantAddSliceInterior.show(detailView);\r
195     $("#tenant-addslice-dialog").dialog({\r
196        autoOpen: false,
197        modal: true,
198        width: 640,
199        buttons : {
200             "Create Slice" : function() {
201               var addDialog = this;
202               console.log("SAVE!!!");
203               detailView.synchronous = true;
204               detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
205               detailView.save();
206             },
207             "Cancel" : function() {
208               $(this).dialog("close");
209             }
210           }
211         });
212     $("#tenant-addslice-dialog").dialog("open");\r
213 };\r
214 \r
215 XOSTenantApp.editUsers = function(model) {\r
216     var app=this;\r
217     var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});\r
218     detailView.dialog = $("#tenant-edit-users-dialog");\r
219     app.tenantEditUsersInterior.show(detailView);\r
220     $("#tenant-edit-users-dialog").dialog({\r
221        autoOpen: false,
222        modal: true,
223        width: 640,
224        buttons : {
225             "Ok" : function() {
226               var editDialog = this;
227               user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
228               user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
229               model.usersBuffer = user_ids;
230               $(editDialog).dialog("close");
231             },
232             "Cancel" : function() {
233               $(this).dialog("close");
234             }
235           }
236         });
237     $("#tenant-edit-users-dialog").dialog("open");\r
238 };\r
239 \r
240 XOSTenantApp.deleteSlice = function(model) {\r
241     var app=this;\r
242     app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });\r
243 };\r
244 \r
245 XOSTenantApp.viewSlice = function(model) {\r
246     if (!model && xos.slicesPlus.models.length > 0) {\r
247         model = xos.slicesPlus.models[0];\r
248     }\r
249 \r
250     if (model) {\r
251         sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,\r
252                                                                   selectedID: model ? model.id : null,\r
253                                                                  } );\r
254         XOSTenantApp.sliceSelector = sliceSelector;\r
255         XOSTenantApp.tenantSliceSelector.show(sliceSelector);\r
256 \r
257         tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,\r
258                                                             choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),\r
259                                                                       serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),\r
260                                                                       default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),\r
261                                                                       default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},\r
262                                                            });\r
263         XOSTenantApp.tenantSummary.show(tenantSummary);\r
264 \r
265         tenantSites = new XOSTenantSiteCollection();\r
266         tenantSites.getFromSlice(model);\r
267         model.usersBuffer = model.attributes.users; /* save a copy of 'users' that we can edit. This prevents another view (developer) from overwriting our copy with a fetch from the server */\r
268         model.usersOrig = model.attributes.users;   /* save an immutable copy that we'll use for username lookups */\r
269         model.user_namesOrig = model.attributes.user_names;\r
270         model.tenantSiteCollection = tenantSites;\r
271         XOSTenantApp.tenantSites = tenantSites;\r
272 \r
273         tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });\r
274         XOSTenantApp.tenantSiteList.show(tenantSiteList);\r
275         // on xos.slicePlus.sort, need to update xostenantapp.tenantSites\r
276 \r
277         XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
278                                                                     linkedView: tenantSummary } ) );\r
279     } else {\r
280         XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));\r
281         XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));\r
282         XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));\r
283         XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",\r
284                                                                     app: XOSTenantApp,\r
285                                                                     linkedView: tenantSummary } ) );\r
286     }\r
287 };\r
288 \r
289 XOSTenantApp.sanityCheck = function() {\r
290     errors = [];\r
291     if (xos.tenant().blessed_service_classes.length == 0) {\r
292         errors.push("no blessed service classes");\r
293     }\r
294     if (xos.tenant().blessed_flavors.length == 0) {\r
295         errors.push("no blessed flavors");\r
296     }\r
297     if (xos.tenant().blessed_images.length == 0) {\r
298         errors.push("no blessed images");\r
299     }\r
300     if (xos.tenant().blessed_sites.length == 0) {\r
301         errors.push("no blessed sites");\r
302     }\r
303     if (xos.tenant().current_user_site_id == null) {\r
304         errors.push("current user does not have a site");\r
305     }\r
306 \r
307     if (errors.length > 0) {\r
308          $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));\r
309          return false;\r
310     }\r
311 \r
312     return true;\r
313 }\r
314 \r
315 XOSTenantApp.collectionLoadChange = function() {\r
316     stats = xos.getCollectionStatus();\r
317 \r
318     if (!XOSTenantApp.navigationStarted) {\r
319         if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {\r
320             if (XOSTenantApp.sanityCheck()) {\r
321                 XOSTenantApp.viewSlice(undefined);\r
322             }\r
323         } else {\r
324             $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");\r
325             $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});\r
326         }\r
327     }\r
328 };\r
329 \r
330 XOSTenantApp.on("start", function() {\r
331      XOSTenantApp.buildViews();
332
333      // fire it once to initially show the progress bar
334      XOSTenantApp.collectionLoadChange();
335
336      // fire it each time the collection load status is updated
337      Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
338 });
339
340 $(document).ready(function(){
341     XOSTenantApp.start();
342 });
343