edit users button in tenant view
diff --git a/planetstack/core/xoslib/static/js/xosTenant.js b/planetstack/core/xoslib/static/js/xosTenant.js
index 8bf2442..fc510f3 100644
--- a/planetstack/core/xoslib/static/js/xosTenant.js
+++ b/planetstack/core/xoslib/static/js/xosTenant.js
@@ -9,7 +9,7 @@
     modelName: "tenantSite",
     collectionName: "tenantSites",
 
-    updateFromSlice: function(slice) {
+    getFromSlice: function(slice) {
         var tenantSites = [];
         var id = 0;
         for (siteName in slice.attributes.site_allocation) {
@@ -26,8 +26,31 @@
         }
         this.set(tenantSites);
     },
+
+    putToSlice: function(slice) {
+        slice.attributes.site_allocation = {};
+        for (index in this.models) {
+            model = this.models[index];
+            slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
+        }
+    },
 });
 
+XOSEditUsersView = Marionette.ItemView.extend({
+            template: "#tenant-edit-users",
+            viewInitializers: [],
+
+            onShow: function() {
+                _.each(this.viewInitializers, function(initializer) {
+                    initializer();
+                });
+            },
+
+            templateHelpers: function() { return { detailView: this, model: this.model }; },
+
+            });
+
+
 XOSTenantButtonView = Marionette.ItemView.extend({
             template: "#xos-tenant-buttons-template",
 
@@ -46,9 +69,12 @@
                      },
 
             addUserClicked: function(e) {
+                     XOSTenantApp.editUsers(this.options.linkedView.model);
                      },
 
             saveClicked: function(e) {
+                     model = this.options.linkedView.model;
+                     model.tenantSiteCollection.putToSlice(model);
                      this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
                      },
             });
@@ -66,6 +92,7 @@
     tenantSiteList: "#tenantSiteList",
     tenantButtons: "#tenantButtons",
     tenantAddSliceInterior: "#tenant-addslice-interior",
+    tenantEditUsersInterior: "#tenant-edit-users-interior",
 });
 
 XOSTenantApp.buildViews = function() {
@@ -73,7 +100,9 @@
 

      tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",

                                                 app: XOSTenantApp,

-                                                detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"]});

+                                                detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],

+                                                fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image"},

+                                                });

 

      XOSTenantApp.tenantSummaryView = tenantSummaryClass;

 

@@ -138,10 +167,14 @@
 

 XOSTenantApp.addSlice = function() {

     var app=this;

-    model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id});

+    model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,

+                                      name: xos.tenant().current_user_login_base + "_"});

     console.log(model);

-    var detailView = new XOSTenantApp.tenantAddView({model: model, collection: xos.slicesPlus});

-    detailView.dialog = $("tenant-addslice-dialog");

+    var detailView = new XOSTenantApp.tenantAddView({model: model,

+                                                    collection: xos.slicesPlus,

+                                                    noSubmitButton: true,

+                                                    });

+    detailView.dialog = $("#tenant-addslice-dialog");

     app.tenantAddSliceInterior.show(detailView);

     $("#tenant-addslice-dialog").dialog({

        autoOpen: false,
@@ -150,6 +183,7 @@
        buttons : {
             "Save" : function() {
               var addDialog = this;
+              console.log("SAVE!!!");
               detailView.synchronous = true;
               detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
               detailView.save();
@@ -162,6 +196,31 @@
     $("#tenant-addslice-dialog").dialog("open");

 };

 

+XOSTenantApp.editUsers = function(model) {

+    var app=this;

+    var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});

+    detailView.dialog = $("#tenant-edit-users-dialog");

+    app.tenantEditUsersInterior.show(detailView);

+    $("#tenant-edit-users-dialog").dialog({

+       autoOpen: false,
+       modal: true,
+       width: 640,
+       buttons : {
+            "Save" : 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;
+              $(editDialog).dialog("close");
+            },
+            "Cancel" : function() {
+              $(this).dialog("close");
+            }
+          }
+        });
+    $("#tenant-edit-users-dialog").dialog("open");

+};

+

 XOSTenantApp.deleteSlice = function(model) {

     var app=this;

     app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });

@@ -181,13 +240,14 @@
     tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,

                                                         choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),

                                                                   serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),

-                                                                  default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_image_ids),

-                                                                  default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavor_ids),},

+                                                                  default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),

+                                                                  default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},

                                                        });

     XOSTenantApp.tenantSummary.show(tenantSummary);

 

     tenantSites = new XOSTenantSiteCollection();

-    tenantSites.updateFromSlice(model);

+    tenantSites.getFromSlice(model);

+    model.tenantSiteCollection = tenantSites;

     XOSTenantApp.tenantSites = tenantSites;

 

     tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });

diff --git a/planetstack/core/xoslib/static/js/xoslib/xos-util.js b/planetstack/core/xoslib/static/js/xoslib/xos-util.js
index 3fd597b..1d135f4 100644
--- a/planetstack/core/xoslib/static/js/xoslib/xos-util.js
+++ b/planetstack/core/xoslib/static/js/xoslib/xos-util.js
@@ -79,3 +79,35 @@
 
     return true;
 }
+
+function array_diff(a1, a2)
+{

+  var a=[], diff=[];

+  for(var i=0;i<a1.length;i++)

+    a[a1[i]]=true;

+  for(var i=0;i<a2.length;i++)

+    if(a[a2[i]]) delete a[a2[i]];

+    else a[a2[i]]=true;

+  for(var k in a)

+    diff.push(k);

+  return diff;

+}
+
+function array_pair_lookup(x, names, values)
+{
+    for (index in values) {
+        if (values[index] == x) {
+            return names[index];
+        }
+    }
+    return undefined;
+}
+
+function all_options(selector) {
+    el = $(selector);
+    result = [];
+    _.each(el.find("option"), function(option) {
+        result.push($(option).val());
+    });
+    return result;
+}
diff --git a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js
index 9f9964c..e8c1b10 100644
--- a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js
+++ b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js
@@ -80,8 +80,13 @@
             Marionette.AppRouter.prototype.navigate.call(this, href, options);
         },
     });

-
-Backbone.Syphon.InputReaders.register('select', function(el) {
+

+// XXX - We import backbone multiple times (BAD!) since the import happens

+//   inside of the view's html. The second time it's imported (developer

+//   view), it wipes out Backbone.Syphon. So, save it as Backbone_Syphon for

+//   now.

+Backbone_Syphon = Backbone.Syphon

+Backbone_Syphon.InputReaders.register('select', function(el) {

     // Modify syphon so that if a select has "syphonall" in the class, then
     // the value of every option will be returned, regardless of whether of
     // not it is selected.
@@ -498,6 +503,9 @@
             submitLeaveClicked: function(e) {
                 console.log("saveLeave");
                 e.preventDefault();
+                if (this.options.noSubmitButton || this.noSubmitButton) {
+                    return;
+                }
                 var that=this;
                 this.afterSave = function() {
                     that.app.navigate("list", that.model.modelName);
@@ -519,7 +527,7 @@
 
             save: function() {
                 this.app.hideError();
-                var data = Backbone.Syphon.serialize(this);
+                var data = Backbone_Syphon.serialize(this);
                 var that = this;
                 var isNew = !this.model.id;
 
@@ -647,6 +655,7 @@
                                                     addFields: this.model.addFields,
                                                     listFields: this.model.listFields,
                                                     detailFields: this.options.detailFields || this.detailFields || this.model.detailFields,
+                                                    fieldDisplayNames: this.options.fieldDisplayNames || this.fieldDisplayNames || this.model.fieldDisplayNames || {},
                                                     foreignFields: this.model.foreignFields,
                                                     detailLinkFields: this.model.detailLinkFields,
                                                     inputType: this.model.inputType,