save users supported in tenant view
[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"},\r
106                                                 });\r
107 \r
108      XOSTenantApp.tenantSummaryView = tenantSummaryClass;\r
109 \r
110      tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",\r
111                                                 app: XOSTenantApp,\r
112                                                 detailFields: ["name", "description"]});\r
113 \r
114      XOSTenantApp.tenantAddView = tenantAddClass;\r
115 \r
116      tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",\r
117                                                app: XOSTenantApp});\r
118 \r
119      XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;\r
120 \r
121      tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",\r
122                                                app: XOSTenantApp,\r
123                                                childView: tenantSiteItemClass,\r
124                                                collection: XOSTenantApp.tenantSites,\r
125                                                title: "sites",\r
126                                                inputType: {allocated: "spinner"},\r
127                                                noDeleteColumn: true,\r
128                                                });\r
129 \r
130      XOSTenantApp.tenantSiteListView = tenantSiteListClass;\r
131 \r
132      XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {\r
133          sliceChanged: function(id) {\r
134              XOSTenantApp.navToSlice(id);\r
135          },\r
136      });\r
137 \r
138      xos.sites.fetch();\r
139      xos.slicesPlus.fetch();\r
140      xos.tenantview.fetch();\r
141 };\r
142 \r
143 make_choices = function(list_of_names, list_of_values) {\r
144     result = [];\r
145     if (!list_of_values) {\r
146         for (index in list_of_names) {\r
147             displayName = list_of_names[index];\r
148             result.push( [displayName, displayName] );\r
149         }\r
150     } else {\r
151         for (index in list_of_names) {\r
152             displayName = list_of_names[index];\r
153             id = list_of_values[index];\r
154             result.push( [displayName, id] );\r
155         }\r
156     }\r
157     return result;\r
158 };\r
159 \r
160 XOSTenantApp.navToSlice = function(id) {\r
161     XOSTenantApp.viewSlice(xos.slicesPlus.get(id));\r
162 };\r
163 \r
164 XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {\r
165     model = XOSTenantApp[collectionName].get(id);\r
166     model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));\r
167 };\r
168 \r
169 XOSTenantApp.addSlice = function() {\r
170     var app=this;\r
171     model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,\r
172                                       name: xos.tenant().current_user_login_base + "_"});\r
173     console.log(model);\r
174     var detailView = new XOSTenantApp.tenantAddView({model: model,\r
175                                                     collection: xos.slicesPlus,\r
176                                                     noSubmitButton: true,\r
177                                                     });\r
178     detailView.dialog = $("#tenant-addslice-dialog");\r
179     app.tenantAddSliceInterior.show(detailView);\r
180     $("#tenant-addslice-dialog").dialog({\r
181        autoOpen: false,
182        modal: true,
183        width: 640,
184        buttons : {
185             "Create Slice" : function() {
186               var addDialog = this;
187               console.log("SAVE!!!");
188               detailView.synchronous = true;
189               detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
190               detailView.save();
191             },
192             "Cancel" : function() {
193               $(this).dialog("close");
194             }
195           }
196         });
197     $("#tenant-addslice-dialog").dialog("open");\r
198 };\r
199 \r
200 XOSTenantApp.editUsers = function(model) {\r
201     var app=this;\r
202     var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});\r
203     detailView.dialog = $("#tenant-edit-users-dialog");\r
204     app.tenantEditUsersInterior.show(detailView);\r
205     $("#tenant-edit-users-dialog").dialog({\r
206        autoOpen: false,
207        modal: true,
208        width: 640,
209        buttons : {
210             "Ok" : function() {
211               var editDialog = this;
212               user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
213               user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
214               model.usersBuffer = user_ids;
215               $(editDialog).dialog("close");
216             },
217             "Cancel" : function() {
218               $(this).dialog("close");
219             }
220           }
221         });
222     $("#tenant-edit-users-dialog").dialog("open");\r
223 };\r
224 \r
225 XOSTenantApp.deleteSlice = function(model) {\r
226     var app=this;\r
227     app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });\r
228 };\r
229 \r
230 XOSTenantApp.viewSlice = function(model) {\r
231     if (!model && xos.slicesPlus.models.length > 0) {\r
232         model = xos.slicesPlus.models[0];\r
233     }\r
234 \r
235     if (model) {\r
236         sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,\r
237                                                                   selectedID: model ? model.id : null,\r
238                                                                  } );\r
239         XOSTenantApp.sliceSelector = sliceSelector;\r
240         XOSTenantApp.tenantSliceSelector.show(sliceSelector);\r
241 \r
242         tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,\r
243                                                             choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),\r
244                                                                       serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),\r
245                                                                       default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),\r
246                                                                       default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},\r
247                                                            });\r
248         XOSTenantApp.tenantSummary.show(tenantSummary);\r
249 \r
250         tenantSites = new XOSTenantSiteCollection();\r
251         tenantSites.getFromSlice(model);\r
252         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
253         model.tenantSiteCollection = tenantSites;\r
254         XOSTenantApp.tenantSites = tenantSites;\r
255 \r
256         tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });\r
257         XOSTenantApp.tenantSiteList.show(tenantSiteList);\r
258         // on xos.slicePlus.sort, need to update xostenantapp.tenantSites\r
259 \r
260         XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
261                                                                     linkedView: tenantSummary } ) );\r
262     } else {\r
263         XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));\r
264         XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));\r
265         XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));\r
266         XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",\r
267                                                                     app: XOSTenantApp,\r
268                                                                     linkedView: tenantSummary } ) );\r
269     }\r
270 };\r
271 \r
272 XOSTenantApp.sanityCheck = function() {\r
273     errors = [];\r
274     if (xos.tenant().blessed_service_classes.length == 0) {\r
275         errors.push("no blessed service classes");\r
276     }\r
277     if (xos.tenant().blessed_flavors.length == 0) {\r
278         errors.push("no blessed flavors");\r
279     }\r
280     if (xos.tenant().blessed_images.length == 0) {\r
281         errors.push("no blessed images");\r
282     }\r
283     if (xos.tenant().blessed_sites.length == 0) {\r
284         errors.push("no blessed sites");\r
285     }\r
286 \r
287     if (errors.length > 0) {\r
288          $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));\r
289          return false;\r
290     }\r
291 \r
292     return true;\r
293 }\r
294 \r
295 XOSTenantApp.collectionLoadChange = function() {\r
296     stats = xos.getCollectionStatus();\r
297 \r
298     if (!XOSTenantApp.navigationStarted) {\r
299         if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {\r
300             if (XOSTenantApp.sanityCheck()) {\r
301                 XOSTenantApp.viewSlice(undefined);\r
302             }\r
303         } else {\r
304             $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");\r
305             $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});\r
306         }\r
307     }\r
308 };\r
309 \r
310 XOSTenantApp.on("start", function() {\r
311      XOSTenantApp.buildViews();
312
313      // fire it once to initially show the progress bar
314      XOSTenantApp.collectionLoadChange();
315
316      // fire it each time the collection load status is updated
317      Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
318 });
319
320 $(document).ready(function(){
321     XOSTenantApp.start();
322 });
323