generic applyConstraints, before abandoning
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xosHelper.js
index 5e4b1cc..1141dcb 100644 (file)
@@ -595,6 +595,34 @@ XOSDetailView = Marionette.ItemView.extend({
                                                     model: this.model,
                                          }},
 
+             applyConstraints: function() {
+                 for (constraint in this.model.constraints) {
+                     // constraint syntax: "operator,destField.destSubField,srcField.srcSubField"
+                     //   i.e. equal,node.deployment,deploymentNetwork.id
+                     parts = constraints.split(",");
+                     operator = parts[0];
+                     parts1 = parts[1].split(".");
+                     destField = parts1[0];
+                     destSubField = parts1[1];
+                     parts2 = parts[2].split(".");
+                     srcField = parts2[0];
+                     srcID = this.$el.find(srcModel).val();
+                     if (operator == "equal"):
+                         filterMaker = function makeFilter(destSubField,srcID) { return function(linkedObj) { return (linkedObj.attributes[destSubField] == srcID); } };
+                         filterFunc = filterMaker(destSubField, srcID);
+                     else:
+                         continue;
+
+                     newSelect = idToSelect(destField,
+                                            model.attributes[destField],
+                                            model.foreignFields[destField],
+                                            "humanReadableName",
+                                            false,
+                                            filterFunc);
+
+                     this.$el.find(destFieldName).html(newSelect);
+                 },
+
 });
 
 /* XOSItemView
@@ -702,47 +730,44 @@ XOSListView = FilteredCompositeView.extend({
              },
 });
 
-XOSDataTableViewOld = Marionette.View.extend( {
-    //tagName: "table",
-    el: "<div><table></table></div>",
-
-    initialize: function() {
-         //$(this.el).html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="dynamicusersliceinfo' + dtcounter + '"></table><p>scott was here</p>' );
-    },
+XOSDataTableView = Marionette.View.extend( {
+    el: '<div style="overflow: hidden">' +
+        '<h3 class="xos-list-title title_placeholder"></h3>' +
+        '<div class="header_placeholder"></div>' +
+        '<table></table>' +
+        '<div class="footer_placeholder"></div>' +
+        '</div>',
 
-    render: function() {
-        //this.el = "<div><table></table></div>";
-        console.log(this.el);
-        console.log(this.$el);
-        console.log("render!");
-        actualEntries = [];
-        actualEntries.push(["foo", "bar", "1", "2"]);
-        actualEntries.push(["zoo", "zar", "0", "9"]);
+    filter: undefined,
 
-        oTable = $(this.el).find("table").dataTable( {
-            "bJQueryUI": true,
-            "aaData":  actualEntries,
-            "bStateSave": true,
-            "aoColumns": [
-                { "sTitle": "Slice" },
-                { "sTitle": "Privilege" , sClass: "alignCenter"},
-                { "sTitle": "Number of Slivers" , sClass: "alignCenter"},
-                { "sTitle": "Number of Sites" , sClass: "alignCenter"},
-            ]
-        } );
-        dtcounter = dtcounter + 1;
+     events: {"click button.btn-xos-add": "addClicked",
+              "click button.btn-xos-refresh": "refreshClicked",
+             },
 
-        return this;
-    },
+     _fetchStateChange: function() {
+         if (this.collection.fetching) {
+            $("#xos-list-title-spinner").show();
+         } else {
+            $("#xos-list-title-spinner").hide();
+         }
+     },
 
-});
+     addClicked: function(e) {
+        e.preventDefault();
+        this.app.Router.navigate("add" + firstCharUpper(this.collection.modelName), {trigger: true});
+     },
 
-XOSDataTableView = Marionette.View.extend( {
-    el: '<div style="overflow: hidden"><table></table></div>',
+     refreshClicked: function(e) {
+         e.preventDefault();
+         this.collection.refresh(refreshRelated=true);
+     },
 
-    filter: undefined,
 
     initialize: function() {
+        $(this.el).find(".footer_placeholder").html( xosListFooterTemplate({addChildHash: this.getAddChildHash()}) );
+        $(this.el).find(".header_placeholder").html( xosListHeaderTemplate() );
+
+        this.listenTo(this.collection, 'fetchStateChange', this._fetchStateChange);
     },
 
     render: function() {
@@ -753,7 +778,13 @@ XOSDataTableView = Marionette.View.extend( {
         _.each(this.collection.listFields, function(fieldName) {
             mRender = undefined;
             mSearchText = undefined;
-            if (fieldName in view.collection.foreignFields) {
+            sTitle = fieldNameToHumanReadable(fieldName);
+            bSortable = true;
+            if (fieldName=="backend_status") {
+                mRender = function(x,y,z) { return xosBackendStatusIconTemplate(z); };
+                sTitle = "";
+                bSortable = false;
+            } else if (fieldName in view.collection.foreignFields) {
                 var foreignCollection = view.collection.foreignFields[fieldName];
                 mSearchText = function(x) { return idToName(x, foreignCollection, "humanReadableName"); };
             }
@@ -761,11 +792,15 @@ XOSDataTableView = Marionette.View.extend( {
                 var collectionName = view.collection.collectionName;
                 mRender = function(x,y,z) { return '<a href="#' + collectionName + '/' + z.id + '">' + x + '</a>'; };
             }
-            thisColumn = {sTitle: fieldNameToHumanReadable(fieldName), mData: fieldName, mRender: mRender, mSearchText: mSearchText};
+            thisColumn = {sTitle: sTitle, bSortable: bSortable, mData: fieldName, mRender: mRender, mSearchText: mSearchText};
             view.columnsByIndex.push( thisColumn );
             view.columnsByFieldName[fieldName] = thisColumn;
         });
 
+        deleteColumn = {sTitle: "", bSortable: false, mRender: function(x,y,z) { return xosDeleteButtonTemplate({modelName: view.collection.modelName, id: z.id}); }, mData: function() { return "delete"; }};
+        view.columnsByIndex.push(deleteColumn);
+        view.columnsByFieldName["delete"] = deleteColumn;
+
         oTable = $(this.el).find("table").dataTable( {
             "bJQueryUI": true,
             "bStateSave": true,
@@ -799,12 +834,14 @@ XOSDataTableView = Marionette.View.extend( {
                     return false;
                 };
 
-                console.log(aoData);
+                //console.log(aoData);
 \r
                 // function used to populate the DataTable with the current\r
                 // content of the collection\r
                 var populateTable = function()\r
                 {\r
+                  console.log("populatetable!");\r
+\r
                   // clear out old row views\r
                   rows = [];\r
 \r
@@ -869,12 +906,35 @@ XOSDataTableView = Marionette.View.extend( {
 \r
                 aoData.shift(); // ignore sEcho
                 populateTable();
+
+                view.listenTo(view.collection, 'change', populateTable);
+                view.listenTo(view.collection, 'add', populateTable);
+                view.listenTo(view.collection, 'remove', populateTable);
             },
         } );
 
         return this;
     },
 
+     getAddChildHash: function() {
+        if (this.parentModel) {
+            parentFieldName = this.parentModel.relatedCollections[this.collection.collectionName];
+            parentFieldName = parentFieldName || "unknown";
+
+            /*parentFieldName = "unknown";
+
+            for (fieldName in this.collection.foreignFields) {
+                cname = this.collection.foreignFields[fieldName];
+                if (cname = this.collection.collectionName) {
+                    parentFieldName = fieldName;
+                }
+            }*/
+            return "#addChild" + firstCharUpper(this.collection.modelName) + "/" + this.parentModel.modelName + "/" + parentFieldName + "/" + this.parentModel.id; // modelName, fieldName, id
+        } else {
+            return null;
+        }
+     },
+
 });
 
 idToName = function(id, collectionName, fieldName) {
@@ -888,7 +948,7 @@ idToName = function(id, collectionName, fieldName) {
    fieldName = name of field within models of collection that will be displayed
 */
 
-idToOptions = function(selectedId, collectionName, fieldName) {
+idToOptions = function(selectedId, collectionName, fieldName, filterFunc) {
     result=""
     for (index in xos[collectionName].models) {
         linkedObject = xos[collectionName].models[index];
@@ -899,6 +959,9 @@ idToOptions = function(selectedId, collectionName, fieldName) {
         } else {
             selected = "";
         }
+        if ((filterFunc) && (!filterFunc(linkedObject)) {
+            continue;
+        }
         result = result + '<option value="' + linkedId + '"' + selected + '>' + linkedName + '</option>';
     }
     return result;
@@ -912,14 +975,14 @@ idToOptions = function(selectedId, collectionName, fieldName) {
    fieldName = name of field within models of collection that will be displayed
 */
 
-idToSelect = function(variable, selectedId, collectionName, fieldName, readOnly) {
+idToSelect = function(variable, selectedId, collectionName, fieldName, readOnly. filterFunc) {
     if (readOnly) {
         readOnly = " readonly";
     } else {
         readOnly = "";
     }
     result = '<select name="' + variable + '"' + readOnly + '>' +
-             idToOptions(selectedId, collectionName, fieldName) +
+             idToOptions(selectedId, collectionName, fieldName, filterFunc) +
              '</select>';
     return result;
 }