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