move some code around
authorScott Baker <smbaker@gmail.com>
Tue, 2 Dec 2014 20:13:49 +0000 (12:13 -0800)
committerScott Baker <smbaker@gmail.com>
Tue, 2 Dec 2014 20:13:49 +0000 (12:13 -0800)
planetstack/core/xoslib/static/js/xoslib/xosHelper.js

index 28f85fd..d9e23a6 100644 (file)
@@ -28,14 +28,19 @@ XOSApplication = Marionette.Application.extend({
     successTemplate: "#xos-success-template",
     logMessageCount: 0,
 
-    confirmDialog: function(view, event) {
+    confirmDialog: function(view, event, callback) {
         $("#xos-confirm-dialog").dialog({
            autoOpen: false,\r
            modal: true,\r
            buttons : {\r
                 "Confirm" : function() {\r
                   $(this).dialog("close");\r
-                  view.trigger(event);\r
+                  if (event) {\r
+                      view.trigger(event);\r
+                  }\r
+                  if (callback) {\r
+                      callback();\r
+                  }\r
                 },\r
                 "Cancel" : function() {\r
                   $(this).dialog("close");\r
@@ -45,28 +50,6 @@ XOSApplication = Marionette.Application.extend({
         $("#xos-confirm-dialog").dialog("open");
     },
 
-    hideError: function() {
-        if (this.logWindowId) {
-        } else {
-            $(this.errorBoxId).hide();
-            $(this.successBoxId).hide();
-        }
-    },
-
-    showSuccess: function(result) {
-         result["statusclass"] = "success";
-         if (this.logTableId) {
-             this.appendLogWindow(result);
-         } else {
-             $(this.successBoxId).show();
-             $(this.successBoxId).html(_.template($(this.successTemplate).html())(result));
-             var that=this;
-             $(this.successCloseButtonId).unbind().bind('click', function() {
-                 $(that.successBoxId).hide();
-             });
-         }
-    },
-
     popupErrorDialog: function(responseText) {
         try {
             parsed_error=$.parseJSON(responseText);
@@ -77,7 +60,7 @@ XOSApplication = Marionette.Application.extend({
             width=640;    // django stacktraces like wide width
         }
         if (parsed_error) {
-            $("#xos-error-dialog").html(templateFromId("#xos-error-response")(json));
+            $("#xos-error-dialog").html(templateFromId("#xos-error-response")(parsed_error));
         } else {
             $("#xos-error-dialog").html(templateFromId("#xos-error-rawresponse")({responseText: responseText}))
         }
@@ -91,7 +74,85 @@ XOSApplication = Marionette.Application.extend({
         });
     },
 
-    showError: function(result) {
+    hideLinkedItems: function(result) {
+        var index=0;
+        while (index<4) {\r
+            this["linkedObjs" + (index+1)].empty();\r
+            index = index + 1;\r
+        }\r
+    },\r
+\r
+    listViewShower: function(listViewName, collection_name, regionName, title) {\r
+        var app=this;\r
+        return function() {\r
+            app[regionName].show(new app[listViewName]);\r
+            app.hideLinkedItems();\r
+            $("#contentTitle").html(templateFromId("#xos-title-list")({"title": title}));\r
+            $("#detail").show();\r
+            $("#xos-listview-button-box").show();\r
+            $("#tabs").hide();\r
+            $("#xos-detail-button-box").hide();\r
+        }\r
+    },\r
+\r
+    addShower: function(detailName, collection_name, regionName, title) {\r
+        var app=this;\r
+        return function() {\r
+            model = new xos[collection_name].model();\r
+            detailViewClass = app[detailName];\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
+        }\r
+    },\r
+\r
+    detailShower: function(detailName, collection_name, regionName, title) {\r
+        var app=this;\r
+        showModelId = function(model_id) {\r
+            $("#contentTitle").html(templateFromId("#xos-title-detail")({"title": title}));\r
+\r
+            collection = xos[collection_name];\r
+            model = collection.get(model_id);\r
+            if (model == undefined) {\r
+                app[regionName].show(new HTMLView({html: "failed to load object " + model_id + " from collection " + collection_name}));\r
+            } else {\r
+                detailViewClass = app[detailName];\r
+                detailView = new detailViewClass({model: model});\r
+                app[regionName].show(detailView);\r
+                detailView.showLinkedItems();\r
+                $("#xos-detail-button-box").show();\r
+                $("#xos-listview-button-box").hide();\r
+            }\r
+        }\r
+        return showModelId;\r
+    },\r
+\r
+    /* error handling callbacks */\r
+\r
+    hideError: function() {\r
+        if (this.logWindowId) {
+        } else {
+            $(this.errorBoxId).hide();
+            $(this.successBoxId).hide();
+        }
+    },
+
+    showSuccess: function(result) {
+         result["statusclass"] = "success";
+         if (this.logTableId) {
+             this.appendLogWindow(result);
+         } else {
+             $(this.successBoxId).show();
+             $(this.successBoxId).html(_.template($(this.successTemplate).html())(result));
+             var that=this;
+             $(this.successCloseButtonId).unbind().bind('click', function() {
+                 $(that.successBoxId).hide();
+             });
+         }
+    },\r
+\r
+    showError: function(result) {\r
          result["statusclass"] = "failure";
          if (this.logTableId) {
              this.appendLogWindow(result);
@@ -105,9 +166,9 @@ XOSApplication = Marionette.Application.extend({
                  $(that.errorBoxId).hide();
              });
          }
-    },
-
-    showInformational: function(result) {
+    },\r
+\r
+    showInformational: function(result) {\r
          result["statusclass"] = "inprog";
          if (this.logTableId) {
              return this.appendLogWindow(result);
@@ -145,63 +206,63 @@ XOSApplication = Marionette.Application.extend({
         limitTableRows(this.logTableId, 5);
 
         return logMessageId;
-    },
-
-    hideLinkedItems: function(result) {
-        var index=0;
-        while (index<4) {\r
-            this["linkedObjs" + (index+1)].empty();\r
-            index = index + 1;\r
-        }\r
     },\r
 \r
-    listViewShower: function(listViewName, collection_name, regionName, title) {\r
-        var app=this;\r
-        return function() {\r
-            app[regionName].show(new app[listViewName]);\r
-            app.hideLinkedItems();\r
-            $("#contentTitle").html(templateFromId("#xos-title-list")({"title": title}));\r
-            $("#detail").show();\r
-            $("#xos-listview-button-box").show();\r
-            $("#tabs").hide();\r
-            $("#xos-detail-button-box").hide();\r
-        }\r
+    saveError: function(model, result, xhr, infoMsgId) {\r
+        console.log("saveError");\r
+        result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName;\r
+        result["infoMsgId"] = infoMsgId;\r
+        this.showError(result);\r
     },\r
 \r
-    addShower: function(detailName, collection_name, regionName, title) {\r
-        var app=this;\r
-        return function() {\r
-            model = new xos[collection_name].model();\r
-            detailViewClass = app[detailName];\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
+    saveSuccess: function(model, result, xhr, infoMsgId, addToCollection) {\r
+        console.log("saveSuccess");\r
+        if (addToCollection) {\r
+            addToCollection.add(model);\r
+            addToCollection.sort();\r
         }\r
+        result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText};\r
+        result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName;\r
+        result["infoMsgId"] = infoMsgId;\r
+        this.showSuccess(result);\r
+    },
+
+    destroyError: function(model, result, xhr, infoMsgId) {
+        result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName;\r
+        result["infoMsgId"] = infoMsgId;\r
+        this.showError(result);\r
     },\r
 \r
-    detailShower: function(detailName, collection_name, regionName, title) {\r
-        var app=this;\r
-        showModelId = function(model_id) {\r
-            $("#contentTitle").html(templateFromId("#xos-title-detail")({"title": title}));\r
-\r
-            collection = xos[collection_name];\r
-            model = collection.get(model_id);\r
-            if (model == undefined) {\r
-                app[regionName].show(new HTMLView({html: "failed to load object " + model_id + " from collection " + collection_name}));\r
-            } else {\r
-                detailViewClass = app[detailName];\r
-                detailView = new detailViewClass({model: model});\r
-                app[regionName].show(detailView);\r
-                detailView.showLinkedItems();\r
-                $("#xos-detail-button-box").show();\r
-                $("#xos-listview-button-box").hide();\r
-            }\r
-        }\r
-        return showModelId;\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.showSuccess(result);\r
     },\r
-});
+\r
+    /* end error handling callbacks */\r
+\r
+    destroyModel: function(model) {\r
+         this.hideError();
+         var infoMsgId = this.showInformational( {what: "destroy " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );
+         var that = 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);}});
+    },
 
+    deleteDialog: function(model, navToListAfterDelete) {
+        var that=this;
+        this.confirmDialog(this, callback=function() {
+            modelName = model.modelName;
+            that.destroyModel(model);
+            if (navToListAfterDelete) {
+                that.navigate("list", modelName);
+            }
+
+        });
+    },
+});
+\r
 /* XOSDetailView
       extend with:
          app - MarionetteApplication
@@ -230,39 +291,7 @@ XOSDetailView = Marionette.ItemView.extend({
                 this.dirty = true;\r
             },\r
 \r
-            saveError: function(model, result, xhr, infoMsgId) {\r
-                console.log("saveError");\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, isNew) {\r
-                console.log("saveSuccess");\r
-                if (isNew) {\r
-                    this.collection.add(model);\r
-                    this.collection.sort();\r
-                }\r
-                result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText};\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
-            },
-
-            submitContinueClicked: function(e) {
+            submitContinueClicked: function(e) {\r
                 console.log("saveContinue");
                 e.preventDefault();
                 this.save();
@@ -302,16 +331,15 @@ XOSDetailView = Marionette.ItemView.extend({
 \r
                 if (isNew) {\r
                     this.model.attributes.humanReadableName = "new " + model.modelName;\r
+                    this.model.addToCollection = this.collection;\r
+                } else {\r
+                    this.model.addToCollection = undefined;\r
                 }\r
 \r
                 var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} );\r
 \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, isNew);}});\r
-                /*if (isNew) {\r
-                    this.collection.add(this.model);\r
-                    this.collection.sort();\r
-                }*/\r
+                this.model.save(data, {error: function(model, result, xhr) { that.app.saveError(model,result,xhr,infoMsgId);},\r
+                                       success: function(model, result, xhr) { that.app.saveSuccess(model,result,xhr,infoMsgId);}});\r
                 this.dirty = false;\r
             },
 
@@ -319,8 +347,8 @@ XOSDetailView = Marionette.ItemView.extend({
                  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);}});
+                 this.model.destroy({error: function(model, result, xhr) { that.app.destroyError(model,result,xhr,infoMsgId);},
+                                     success: function(model, result, xhr) { that.app.destroySuccess(model,result,xhr,infoMsgId);}});
             },
 
              deleteClicked: function(e) {