From af23bd295d712ce8ba0bd16e78600c8d3ed3001e Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Tue, 2 Dec 2014 12:13:49 -0800 Subject: [PATCH] move some code around --- .../core/xoslib/static/js/xoslib/xosHelper.js | 264 ++++++++++-------- 1 file changed, 146 insertions(+), 118 deletions(-) diff --git a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js index 28f85fd..d9e23a6 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xosHelper.js +++ b/planetstack/core/xoslib/static/js/xoslib/xosHelper.js @@ -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, modal: true, buttons : { "Confirm" : function() { $(this).dialog("close"); - view.trigger(event); + if (event) { + view.trigger(event); + } + if (callback) { + callback(); + } }, "Cancel" : function() { $(this).dialog("close"); @@ -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) { + this["linkedObjs" + (index+1)].empty(); + index = index + 1; + } + }, + + listViewShower: function(listViewName, collection_name, regionName, title) { + var app=this; + return function() { + app[regionName].show(new app[listViewName]); + app.hideLinkedItems(); + $("#contentTitle").html(templateFromId("#xos-title-list")({"title": title})); + $("#detail").show(); + $("#xos-listview-button-box").show(); + $("#tabs").hide(); + $("#xos-detail-button-box").hide(); + } + }, + + addShower: function(detailName, collection_name, regionName, title) { + var app=this; + return function() { + model = new xos[collection_name].model(); + detailViewClass = app[detailName]; + detailView = new detailViewClass({model: model, collection:xos[collection_name]}); + app[regionName].show(detailView); + $("#xos-detail-button-box").show(); + $("#xos-listview-button-box").hide(); + } + }, + + detailShower: function(detailName, collection_name, regionName, title) { + var app=this; + showModelId = function(model_id) { + $("#contentTitle").html(templateFromId("#xos-title-detail")({"title": title})); + + collection = xos[collection_name]; + model = collection.get(model_id); + if (model == undefined) { + app[regionName].show(new HTMLView({html: "failed to load object " + model_id + " from collection " + collection_name})); + } else { + detailViewClass = app[detailName]; + detailView = new detailViewClass({model: model}); + app[regionName].show(detailView); + detailView.showLinkedItems(); + $("#xos-detail-button-box").show(); + $("#xos-listview-button-box").hide(); + } + } + return showModelId; + }, + + /* error handling callbacks */ + + 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(); + }); + } + }, + + showError: function(result) { result["statusclass"] = "failure"; if (this.logTableId) { this.appendLogWindow(result); @@ -105,9 +166,9 @@ XOSApplication = Marionette.Application.extend({ $(that.errorBoxId).hide(); }); } - }, - - showInformational: function(result) { + }, + + showInformational: function(result) { 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) { - this["linkedObjs" + (index+1)].empty(); - index = index + 1; - } }, - listViewShower: function(listViewName, collection_name, regionName, title) { - var app=this; - return function() { - app[regionName].show(new app[listViewName]); - app.hideLinkedItems(); - $("#contentTitle").html(templateFromId("#xos-title-list")({"title": title})); - $("#detail").show(); - $("#xos-listview-button-box").show(); - $("#tabs").hide(); - $("#xos-detail-button-box").hide(); - } + saveError: function(model, result, xhr, infoMsgId) { + console.log("saveError"); + result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName; + result["infoMsgId"] = infoMsgId; + this.showError(result); }, - addShower: function(detailName, collection_name, regionName, title) { - var app=this; - return function() { - model = new xos[collection_name].model(); - detailViewClass = app[detailName]; - detailView = new detailViewClass({model: model, collection:xos[collection_name]}); - app[regionName].show(detailView); - $("#xos-detail-button-box").show(); - $("#xos-listview-button-box").hide(); + saveSuccess: function(model, result, xhr, infoMsgId, addToCollection) { + console.log("saveSuccess"); + if (addToCollection) { + addToCollection.add(model); + addToCollection.sort(); } + result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText}; + result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName; + result["infoMsgId"] = infoMsgId; + this.showSuccess(result); + }, + + destroyError: function(model, result, xhr, infoMsgId) { + result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName; + result["infoMsgId"] = infoMsgId; + this.showError(result); }, - detailShower: function(detailName, collection_name, regionName, title) { - var app=this; - showModelId = function(model_id) { - $("#contentTitle").html(templateFromId("#xos-title-detail")({"title": title})); - - collection = xos[collection_name]; - model = collection.get(model_id); - if (model == undefined) { - app[regionName].show(new HTMLView({html: "failed to load object " + model_id + " from collection " + collection_name})); - } else { - detailViewClass = app[detailName]; - detailView = new detailViewClass({model: model}); - app[regionName].show(detailView); - detailView.showLinkedItems(); - $("#xos-detail-button-box").show(); - $("#xos-listview-button-box").hide(); - } - } - return showModelId; + destroySuccess: function(model, result, xhr, infoMsgId) { + result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText}; + result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName; + result["infoMsgId"] = infoMsgId; + this.showSuccess(result); }, -}); + + /* end error handling callbacks */ + + destroyModel: function(model) { + 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); + } + + }); + }, +}); + /* XOSDetailView extend with: app - MarionetteApplication @@ -230,39 +291,7 @@ XOSDetailView = Marionette.ItemView.extend({ this.dirty = true; }, - saveError: function(model, result, xhr, infoMsgId) { - console.log("saveError"); - result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName; - result["infoMsgId"] = infoMsgId; - this.app.showError(result); - }, - - saveSuccess: function(model, result, xhr, infoMsgId, isNew) { - console.log("saveSuccess"); - if (isNew) { - this.collection.add(model); - this.collection.sort(); - } - result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText}; - result["what"] = "save " + model.modelName + " " + model.attributes.humanReadableName; - result["infoMsgId"] = infoMsgId; - this.app.showSuccess(result); - }, - - destroyError: function(model, result, xhr, infoMsgId) { - result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName; - result["infoMsgId"] = infoMsgId; - this.app.showError(result); - }, - - destroySuccess: function(model, result, xhr, infoMsgId) { - result = {status: xhr.xhr.status, statusText: xhr.xhr.statusText}; - result["what"] = "destroy " + model.modelName + " " + model.attributes.humanReadableName; - result["infoMsgId"] = infoMsgId; - this.app.showSuccess(result); - }, - - submitContinueClicked: function(e) { + submitContinueClicked: function(e) { console.log("saveContinue"); e.preventDefault(); this.save(); @@ -302,16 +331,15 @@ XOSDetailView = Marionette.ItemView.extend({ if (isNew) { this.model.attributes.humanReadableName = "new " + model.modelName; + this.model.addToCollection = this.collection; + } else { + this.model.addToCollection = undefined; } var infoMsgId = this.app.showInformational( {what: "save " + model.modelName + " " + model.attributes.humanReadableName, status: "", statusText: "in progress..."} ); - this.model.save(data, {error: function(model, result, xhr) { that.saveError(model,result,xhr,infoMsgId);}, - success: function(model, result, xhr) { that.saveSuccess(model,result,xhr,infoMsgId, isNew);}}); - /*if (isNew) { - this.collection.add(this.model); - this.collection.sort(); - }*/ + 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);}}); this.dirty = false; }, @@ -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) { -- 2.43.0