make tenantview method reslient of users with no site, catch users with no site in...
[plstackapi.git] / planetstack / core / xoslib / static / js / xosTenant.js
index fc510f3..9c45460 100644 (file)
@@ -30,7 +30,7 @@ XOSTenantSiteCollection = XOSCollection.extend( {
     putToSlice: function(slice) {
         slice.attributes.site_allocation = {};
         for (index in this.models) {
-            model = this.models[index];
+            var model = this.models[index];
             slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
         }
     },
@@ -75,6 +75,7 @@ XOSTenantButtonView = Marionette.ItemView.extend({
             saveClicked: function(e) {
                      model = this.options.linkedView.model;
                      model.tenantSiteCollection.putToSlice(model);
+                     model.attributes.users = model.usersBuffer;
                      this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
                      },
             });
@@ -101,7 +102,12 @@ XOSTenantApp.buildViews = function() {
      tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",\r
                                                 app: XOSTenantApp,\r
                                                 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],\r
-                                                fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image"},\r
+                                                fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},\r
+\r
+                                                onShow: function() {\r
+                                                    // the slice selector is in a different table, so make every label cell the maximal width\r
+                                                    make_same_width("#xos-tenant-view-panel", ".xos-label-cell");\r
+                                                },\r
                                                 });\r
 \r
      XOSTenantApp.tenantSummaryView = tenantSummaryClass;\r
@@ -124,6 +130,9 @@ XOSTenantApp.buildViews = function() {
                                                title: "sites",\r
                                                inputType: {allocated: "spinner"},\r
                                                noDeleteColumn: true,\r
+                                               disablePaginate: true,\r
+                                               disableFilter: true,\r
+                                               fieldDisplayNames: {"name": "Site"},\r
                                                });\r
 \r
      XOSTenantApp.tenantSiteListView = tenantSiteListClass;\r
@@ -167,6 +176,12 @@ XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amo
 \r
 XOSTenantApp.addSlice = function() {\r
     var app=this;\r
+\r
+    if (!xos.tenant().current_user_can_create_slice) {\r
+        window.alert("You do not have sufficient rights to create a slice on your site");\r
+        return;\r
+    }\r
+\r
     model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,\r
                                       name: xos.tenant().current_user_login_base + "_"});\r
     console.log(model);\r
@@ -181,7 +196,7 @@ XOSTenantApp.addSlice = function() {
        modal: true,
        width: 640,
        buttons : {
-            "Save" : function() {
+            "Create Slice" : function() {
               var addDialog = this;
               console.log("SAVE!!!");
               detailView.synchronous = true;
@@ -206,11 +221,11 @@ XOSTenantApp.editUsers = function(model) {
        modal: true,
        width: 640,
        buttons : {
-            "Save" : function() {
+            "Ok" : function() {
               var editDialog = this;
               user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
               user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
-              model.attributes.users = user_ids;
+              model.usersBuffer = user_ids;
               $(editDialog).dialog("close");
             },
             "Cancel" : function() {
@@ -231,31 +246,43 @@ XOSTenantApp.viewSlice = function(model) {
         model = xos.slicesPlus.models[0];\r
     }\r
 \r
-    sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,\r
-                                                              selectedID: model.id,\r
-                                                             } );\r
-    XOSTenantApp.sliceSelector = sliceSelector;\r
-    XOSTenantApp.tenantSliceSelector.show(sliceSelector);\r
+    if (model) {\r
+        sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,\r
+                                                                  selectedID: model ? model.id : null,\r
+                                                                 } );\r
+        XOSTenantApp.sliceSelector = sliceSelector;\r
+        XOSTenantApp.tenantSliceSelector.show(sliceSelector);\r
 \r
-    tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,\r
-                                                        choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),\r
-                                                                  serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),\r
-                                                                  default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),\r
-                                                                  default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},\r
-                                                       });\r
-    XOSTenantApp.tenantSummary.show(tenantSummary);\r
+        tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,\r
+                                                            choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),\r
+                                                                      serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),\r
+                                                                      default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),\r
+                                                                      default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},\r
+                                                           });\r
+        XOSTenantApp.tenantSummary.show(tenantSummary);\r
 \r
-    tenantSites = new XOSTenantSiteCollection();\r
-    tenantSites.getFromSlice(model);\r
-    model.tenantSiteCollection = tenantSites;\r
-    XOSTenantApp.tenantSites = tenantSites;\r
+        tenantSites = new XOSTenantSiteCollection();\r
+        tenantSites.getFromSlice(model);\r
+        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
+        model.usersOrig = model.attributes.users;   /* save an immutable copy that we'll use for username lookups */\r
+        model.user_namesOrig = model.attributes.user_names;\r
+        model.tenantSiteCollection = tenantSites;\r
+        XOSTenantApp.tenantSites = tenantSites;\r
 \r
-    tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });\r
-    XOSTenantApp.tenantSiteList.show(tenantSiteList);\r
-    // on xos.slicePlus.sort, need to update xostenantapp.tenantSites\r
+        tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });\r
+        XOSTenantApp.tenantSiteList.show(tenantSiteList);\r
+        // on xos.slicePlus.sort, need to update xostenantapp.tenantSites\r
 \r
-    XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
-                                                                linkedView: tenantSummary } ) );\r
+        XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
+                                                                    linkedView: tenantSummary } ) );\r
+    } else {\r
+        XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));\r
+        XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));\r
+        XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));\r
+        XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",\r
+                                                                    app: XOSTenantApp,\r
+                                                                    linkedView: tenantSummary } ) );\r
+    }\r
 };\r
 \r
 XOSTenantApp.sanityCheck = function() {\r
@@ -272,6 +299,9 @@ XOSTenantApp.sanityCheck = function() {
     if (xos.tenant().blessed_sites.length == 0) {\r
         errors.push("no blessed sites");\r
     }\r
+    if (xos.tenant().current_user_site_id == null) {\r
+        errors.push("current user does not have a site");\r
+    }\r
 \r
     if (errors.length > 0) {\r
          $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));\r