humanReadableNames in logPanel
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xosHelper.js
index b8e0347..dc11670 100644 (file)
@@ -28,6 +28,23 @@ XOSApplication = Marionette.Application.extend({
     successTemplate: "#xos-success-template",
     logMessageCount: 0,
 
+    confirmDialog: function(view, event) {
+        $("#xos-confirm-dialog").dialog({
+           autoOpen: false,\r
+           modal: true,\r
+           buttons : {\r
+                "Confirm" : function() {\r
+                  $(this).dialog("close");\r
+                  view.trigger(event);\r
+                },\r
+                "Cancel" : function() {\r
+                  $(this).dialog("close");\r
+                }\r
+              }\r
+            });
+        $("#xos-confirm-dialog").dialog("open");
+    },
+
     hideError: function() {
         if (this.logWindowId) {
         } else {
@@ -37,7 +54,7 @@ XOSApplication = Marionette.Application.extend({
     },
 
     showSuccess: function(result) {
-         result["success"] = "success";
+         result["statusclass"] = "success";
          if (this.logTableId) {
              this.appendLogWindow(result);
          } else {
@@ -51,7 +68,7 @@ XOSApplication = Marionette.Application.extend({
     },
 
     showError: function(result) {
-         result["success"] = "failure";
+         result["statusclass"] = "failure";
          if (this.logTableId) {
              this.appendLogWindow(result);
          } else {
@@ -65,7 +82,7 @@ XOSApplication = Marionette.Application.extend({
     },
 
     showInformational: function(result) {
-         result["success"] = "information";
+         result["statusclass"] = "inprog";
          if (this.logTableId) {
              return this.appendLogWindow(result);
          } else {
@@ -99,6 +116,8 @@ XOSApplication = Marionette.Application.extend({
             $(this.statusMsgId).html( templateFromId("#xos-status-template")(result) );
         }
 
+        limitTableRows(this.logTableId, 5);
+
         return logMessageId;
     },
 
@@ -128,7 +147,7 @@ XOSApplication = Marionette.Application.extend({
         return function() {\r
             model = new xos[collection_name].model();\r
             detailViewClass = app[detailName];\r
-            detailView = new detailViewClass({model: model});\r
+            detailView = new detailViewClass({model: model, collection:xos[collection_name]});\r
             app[regionName].show(detailView);\r
             $("#xos-detail-button-box").show();\r
             $("#xos-listview-button-box").hide();\r
@@ -169,8 +188,13 @@ XOSDetailView = Marionette.ItemView.extend({
             events: {"click button.btn-xos-save-continue": "submitContinueClicked",
                      "click button.btn-xos-save-leave": "submitLeaveClicked",
                      "click button.btn-xos-save-another": "submitAddAnotherClicked",
+                     "click button.btn-xos-delete": "deleteClicked",
                      "change input": "inputChanged"},
 
+            initialize: function() {
+                this.on('deleteConfirmed', this.deleteConfirmed);
+            },
+
             /* inputChanged is watching the onChange events of the input controls. We
                do this to track when this view is 'dirty', so we can throw up a warning\r
                if the user tries to change his slices without saving first.\r
@@ -181,14 +205,27 @@ XOSDetailView = Marionette.ItemView.extend({
             },\r
 \r
             saveError: function(model, result, xhr, infoMsgId) {\r
-                result["what"] = "save " + model.__proto__.modelName;\r
+                result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName;\r
                 result["infoMsgId"] = infoMsgId;\r
                 this.app.showError(result);\r
             },\r
 \r
             saveSuccess: function(model, result, xhr, infoMsgId) {\r
                 result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText};\r
-                result["what"] = "save " + model.__proto__.modelName;\r
+                result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName;\r
+                result["infoMsgId"] = infoMsgId;\r
+                this.app.showSuccess(result);\r
+            },
+
+            destroyError: function(model, result, xhr, infoMsgId) {
+                result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName;\r
+                result["infoMsgId"] = infoMsgId;\r
+                this.app.showError(result);\r
+            },\r
+\r
+            destroySuccess: function(model, result, xhr, infoMsgId) {\r
+                result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText};\r
+                result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName;\r
                 result["infoMsgId"] = infoMsgId;\r
                 this.app.showSuccess(result);\r
             },
@@ -203,24 +240,51 @@ XOSDetailView = Marionette.ItemView.extend({
                 console.log("saveLeave");
                 e.preventDefault();
                 this.save();
+                this.app.navigate("list", this.model.modelName);
             },
 
             submitAddAnotherClicked: function(e) {
                 console.log("saveAnother");
                 e.preventDefault();
                 this.save();
+                this.app.navigate("add", this.model.modelName);
             },
 
             save: function() {
                 this.app.hideError();
-                var infoMsgId = this.app.showInformational( {what: "save " + this.model.__proto__.modelName, status: "", statusText: "in progress..."} );\r
+                var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );\r
                 var data = Backbone.Syphon.serialize(this);\r
                 var that = this;\r
+                var isNew = !this.model.id;\r
                 this.model.save(data, {error: function(model, result, xhr) { that.saveError(model,result,xhr,infoMsgId);},\r
                                        success: function(model, result, xhr) { that.saveSuccess(model,result,xhr,infoMsgId);}});\r
+                if (isNew) {\r
+                    console.log(this.model);\r
+                    this.collection.add(this.model);\r
+                    this.collection.sort();\r
+                }\r
                 this.dirty = false;\r
             },
 
+            destroyModel: function() {
+                 this.app.hideError();
+                 var infoMsgId = this.app.showInformational( {what: "destroy " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );
+                 var that = this;
+                 this.model.destroy({error: function(model, result, xhr) { that.destroyError(model,result,xhr,infoMsgId);},
+                                     success: function(model, result, xhr) { that.destroySuccess(model,result,xhr,infoMsgId);}});
+            },
+
+             deleteClicked: function(e) {
+                 e.preventDefault();
+\r                 this.app.confirmDialog(this, "deleteConfirmed");
+\r             },
+\r
+\r             deleteConfirmed: function() {
+\r                 modelName = this.model.modelName;
+\r                 this.destroyModel();
+\r                 this.app.navigate("list", modelName);
+\r             },
+\r
             tabClick: function(tabId, regionName) {
                     region = this.app[regionName];\r
                     if (this.currentTabRegion != undefined) {\r
@@ -318,16 +382,30 @@ XOSListView = Marionette.CompositeView.extend({
              childViewContainer: 'tbody',\r
 \r
              events: {"click button.btn-xos-add": "addClicked",\r
-                     },
-
+                      "click button.btn-xos-refresh": "refreshClicked",\r
+                     },\r
+\r
+             _fetchStateChange: function() {\r
+                 if (this.collection.fetching) {\r
+                    $("#xos-list-title-spinner").show();\r
+                 } else {\r
+                    $("#xos-list-title-spinner").hide();\r
+                 }\r
+             },\r
+\r
              addClicked: function(e) {
-                console.log("add");
                 e.preventDefault();
                 this.app.Router.navigate("add" + firstCharUpper(this.collection.modelName), {trigger: true});
              },
 \r
-             initialize: function() {\r
-                 this.listenTo(this.collection, 'change', this._renderChildren)
+\r             refreshClicked: function(e) {
+\r                 e.preventDefault();
+\r                 this.collection.refresh(refreshRelated=true);
+\r             },
+\r
+\r             initialize: function() {
+\r                 this.listenTo(this.collection, 'change', this._renderChildren)
+                 this.listenTo(this.collection, 'fetchStateChange', this._fetchStateChange);
 
                  // Because many of the templates use idToName(), we need to
                  // listen to the collections that hold the names for the ids