repair the sliver filter logic in xosAdminDashboard
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xosHelper.js
index 07ae9a9..a055093 100644 (file)
@@ -4,6 +4,21 @@ HTMLView = Marionette.ItemView.extend({
   },
 });
 
+FilteredCompositeView = Marionette.CompositeView.extend( {
+    showCollection: function() {
+      var ChildView;
+      this.collection.each(function(child, index) {
+        filterFunc = this.options.filter || this.filter;
+        if (filterFunc && !filterFunc(child)) {
+            return;
+        }
+        ChildView = this.getChildView(child);
+        this.addChild(child, ChildView, index);
+      }, this);
+
+    },
+});
+
 SliceSelectorOption = Marionette.ItemView.extend({
     template: "#xos-sliceselector-option",
     tagName: "option",
@@ -16,7 +31,7 @@ SliceSelectorOption = Marionette.ItemView.extend({
     },
 });
 
-SliceSelectorView = Marionette.CompositeView.extend({
+SliceSelectorView = FilteredCompositeView.extend({
     template: "#xos-sliceselector-select",
     childViewContainer: "select",
     childView: SliceSelectorOption,
@@ -39,20 +54,6 @@ SliceSelectorView = Marionette.CompositeView.extend({
     templateHelpers: function() { return {caption: this.options.caption || this.caption }; },
 });
 
-FilteredCompositeView = Marionette.CompositeView.extend( {
-    showCollection: function() {
-      var ChildView;
-      this.collection.each(function(child, index) {
-        if (this.filter && !this.filter(child)) {
-            return;
-        }
-        ChildView = this.getChildView(child);
-        this.addChild(child, ChildView, index);
-      }, this);
-
-    },
-});
-
 XOSRouter = Marionette.AppRouter.extend({
         initialize: function() {\r
             this.routeStack=[];\r
@@ -146,10 +147,18 @@ XOSApplication = Marionette.Application.extend({
         }
         console.log(responseText);
         console.log(parsed_error);
-        if (parsed_error) {
+
+        if (parsed_error && ("error" in parsed_error)) {
+            // this error comes from genapi views
+            $("#xos-error-dialog").html(templateFromId("#xos-error-response")(parsed_error));
+        } else if (parsed_error && ("detail" in parsed_error)) {
+            // this error response comes from rest_framework APIException
+            parsed_error["error"] = "API Error";
+            parsed_error["specific_error"] = parsed_error["detail"];
+            parsed_error["reasons"] = [];
             $("#xos-error-dialog").html(templateFromId("#xos-error-response")(parsed_error));
         } else {
-            $("#xos-error-dialog").html(templateFromId("#xos-error-rawresponse")({responseText: responseText}))
+            $("#xos-error-dialog").html(templateFromId("#xos-error-rawresponse")({responseText: strip_scripts(responseText)}))
         }
 
         $("#xos-error-dialog").dialog({
@@ -475,7 +484,7 @@ XOSDetailView = Marionette.ItemView.extend({
             */
 
             initialize: function() {
-                this.on("saveSuccess", this.onAfterSave);
+                this.on("saveSuccess", this.onSaveSuccess);
                 this.synchronous = false;
             },
 
@@ -485,11 +494,20 @@ XOSDetailView = Marionette.ItemView.extend({
                 });
             },
 
+            saveSuccess: function(e) {
+                // always called after a save succeeds
+            },
+
             afterSave: function(e) {
+                // if this.synchronous, then called after the save succeeds
+                // if !this.synchronous, then called after save is initiated
             },
 
-            onAfterSave: function(e) {
-                this.afterSave(e);
+            onSaveSuccess: function(e) {
+                this.saveSuccess(e);
+                if (this.synchronous) {
+                    this.afterSave(e);
+                }
             },
 
             inputChanged: function(e) {
@@ -559,9 +577,7 @@ XOSDetailView = Marionette.ItemView.extend({
 
                 this.model.save(data, {error: function(model, result, xhr) { that.app.saveError(model,result,xhr,infoMsgId);},
                                        success: function(model, result, xhr) { that.app.saveSuccess(model,result,xhr,infoMsgId);
-                                                                               if (that.synchronous) {
-                                                                                   that.trigger("saveSuccess");
-                                                                               }
+                                                                               that.trigger("saveSuccess");
                                                                              }});
                 this.dirty = false;
 
@@ -665,12 +681,13 @@ XOSDetailView = Marionette.ItemView.extend({
                                                     model: this.model,
                                                     detailView: this,
                                                     choices: this.options.choices || this.choices || this.model.choices || {},
+                                                    helpText: this.options.helpText || this.helpText || this.model.helpText || {},
                                          }},
 });
 
 XOSDetailView_sliver = XOSDetailView.extend( {
     events: $.extend(XOSDetailView.events,
-        {"change #field_deploymentNetwork": "onDeploymentNetworkChange"}
+        {"change #field_deployment": "onDeploymentChange"}
     ),
 
     onShow: function() {
@@ -678,16 +695,23 @@ XOSDetailView_sliver = XOSDetailView.extend( {
         // first time was when the template was originally invoked, and the
         // selects will all have the full unfiltered set of candidates. Then
         // onShow will fire, and we'll update them with the filtered values.
-        this.onDeploymentNetworkChange();
+        this.onDeploymentChange();
     },
 
-    onDeploymentNetworkChange: function(e) {
-        var deploymentID = this.$el.find("#field_deploymentNetwork").val();
+    onDeploymentChange: function(e) {
+        var deploymentID = this.$el.find("#field_deployment").val();
 
-        console.log("onDeploymentNetworkChange");
-        console.log(deploymentID);
+        //console.log("onDeploymentChange");
 
-        filterFunc = function(model) { return (model.attributes.deployment==deploymentID); }
+        filterFunc = function(model) { for (index in xos.siteDeployments.models) {
+                                           site_deployment = xos.siteDeployments.models[index];
+                                           if (site_deployment.attributes.id == model.attributes.site_deployment) {
+                                               return (site_deployment.attributes.deployment == deploymentID);
+                                           }
+                                        }
+                                        return false;
+                                        // return (model.attributes.deployment==deploymentID); }
+                                      };
         newSelect = idToSelect("node",
                                this.model.attributes.node,
                                this.model.foreignFields["node"],
@@ -697,8 +721,7 @@ XOSDetailView_sliver = XOSDetailView.extend( {
         this.$el.find("#field_node").html(newSelect);
 
         filterFunc = function(model) { for (index in model.attributes.deployments) {
-                                          item=model.attributes.deployments[index];
-                                          if (item.toString()==deploymentID.toString()) return true;
+                                          if (model.attributes.deployments[index] == deploymentID) return true;
                                         };
                                         return false;
                                      }
@@ -710,12 +733,9 @@ XOSDetailView_sliver = XOSDetailView.extend( {
                                filterFunc);
         this.$el.find("#field_flavor").html(newSelect);
 
-        filterFunc = function(model) { for (index in xos.imageDeployments.models) {
-                                           imageDeployment = xos.imageDeployments.models[index];
-                                           if ((imageDeployment.attributes.deployment == deploymentID) && (imageDeployment.attributes.image == model.id)) {
-                                               return true;
-                                           }
-                                       }
+        filterFunc = function(model) { for (index in model.attributes.deployments) {
+                                           if (model.attributes.deployments[index] == deploymentID) return true;
+                                       };
                                        return false;
                                      };
         newSelect = idToSelect("image",
@@ -875,6 +895,7 @@ XOSDataTableView = Marionette.View.extend( {
 
     render: function() {
         var view = this;
+        var fieldDisplayNames = view.options.fieldDisplayNames || view.fieldDisplayNames || {};
 
         view.columnsByIndex = [];
         view.columnsByFieldName = {};
@@ -882,7 +903,7 @@ XOSDataTableView = Marionette.View.extend( {
             inputType = view.options.inputType || view.inputType || {};
             mRender = undefined;
             mSearchText = undefined;
-            sTitle = fieldNameToHumanReadable(fieldName);
+            sTitle = fieldName in fieldDisplayNames ? fieldDisplayNames[fieldName] : fieldNameToHumanReadable(fieldName);
             bSortable = true;
             if (fieldName=="backend_status") {
                 mRender = function(x,y,z) { return xosBackendStatusIconTemplate(z); };
@@ -913,6 +934,8 @@ XOSDataTableView = Marionette.View.extend( {
             "bJQueryUI": true,
             "bStateSave": true,
             "bServerSide": true,
+            "bFilter": ! (view.options.disableFilter || view.disableFilter),
+            "bPaginate": ! (view.options.disablePaginate || view.disablePaginate),
             "aoColumns": view.columnsByIndex,
 
             fnServerData: function(sSource, aoData, fnCallback, settings) {
@@ -1005,7 +1028,9 @@ XOSDataTableView = Marionette.View.extend( {
                   aaData.sort(function(a,b) { return compareColumns(sortCols, sortDirs, a, b); });\r
 \r
                   // slice it for pagination\r
-                  aaData = aaData.slice(iDisplayStart, iDisplayStart+iDisplayLength);\r
+                  if (iDisplayLength >= 0) {\r
+                      aaData = aaData.slice(iDisplayStart, iDisplayStart+iDisplayLength);\r
+                  }\r
 \r
                   return fnCallback({iTotalRecords: totalSize,\r
                          iTotalDisplayRecords: filteredSize,\r