From 393ec4774e5256464e79d8d99efba6b44ac28d7d Mon Sep 17 00:00:00 2001 From: Chinmay Zanpure Date: Fri, 19 Feb 2016 12:07:09 -0800 Subject: [PATCH] Partial-Bug: #1537225 Fixed delete in config db using model to show proper errors. Change-Id: I69bdadac08fa845b72f8b5522a1688ee91e5c046 --- webroot/common/ui/js/controller.labels.js | 6 ++ .../ui/js/models/ConfigDatabaseModel.js | 41 +++++++++ .../ui/js/views/ConfigDatabaseActionView.js | 55 ++++++++++++ .../ui/js/views/ConfigDatabaseView.js | 90 ++++++------------- 4 files changed, 130 insertions(+), 62 deletions(-) create mode 100644 webroot/setting/configdb/ui/js/models/ConfigDatabaseModel.js create mode 100644 webroot/setting/configdb/ui/js/views/ConfigDatabaseActionView.js diff --git a/webroot/common/ui/js/controller.labels.js b/webroot/common/ui/js/controller.labels.js index 0845a3eef..6d027f6d6 100644 --- a/webroot/common/ui/js/controller.labels.js +++ b/webroot/common/ui/js/controller.labels.js @@ -973,6 +973,12 @@ define([ this.CDB_TITLE_FQ_TABLE = "FQ Name Table"; this.CDB_TITLE_UUID_TABLE = "UUID Name Table"; this.CDB_TITLE_SHARED_TABLE = "Shared Name Table"; + this.CDB_TITLE_DELETE_RECORD = "Delete Record"; + this.CDB_TMPL_DELETE_RECORD = "cdb-delete-template"; + this.CDB_DELETE_MODAL_ID_ = "delete-cdb"; + + this.CDB_LABEL_KEY_VALUES = "keyvalues"; + this.CDB_LABEL_KEY = "keys"; //Config DB Labels - End /* Service Appliance */ diff --git a/webroot/setting/configdb/ui/js/models/ConfigDatabaseModel.js b/webroot/setting/configdb/ui/js/models/ConfigDatabaseModel.js new file mode 100644 index 000000000..a5cb07dfe --- /dev/null +++ b/webroot/setting/configdb/ui/js/models/ConfigDatabaseModel.js @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. + */ + +define([ + 'underscore', + 'contrail-model' +], function (_, ContrailModel) { + var ConfigDatabaseModel = ContrailModel.extend({ + + deleteRecord: function (checkedRow, callbackObj, type) { + var ajaxConfig = {}, that = this, url; + + if (type == ctwc.DELETE_KEY_TYPE) { + url = "/api/query/cassandra/key/" + checkedRow.table + "/" + checkedRow.key; + } else if (type == ctwc.DELETE_KEY_VALUE_TYPE) { + url = "/api/query/cassandra/value/" + checkedRow.table + "/" + checkedRow.key + "/" + checkedRow.keyvalue; + } + + ajaxConfig.type = "DELETE"; + ajaxConfig.url = url; + + contrail.ajaxHandler(ajaxConfig, function () { + if (contrail.checkIfFunction(callbackObj.init)) { + callbackObj.init(); + } + }, function (response) { + if (contrail.checkIfFunction(callbackObj.success)) { + callbackObj.success(); + } + }, function (error) { + console.log(error); + if (contrail.checkIfFunction(callbackObj.error)) { + callbackObj.error(error); + } + }); + }, + validations : {} + }); + return ConfigDatabaseModel; +}); \ No newline at end of file diff --git a/webroot/setting/configdb/ui/js/views/ConfigDatabaseActionView.js b/webroot/setting/configdb/ui/js/views/ConfigDatabaseActionView.js new file mode 100644 index 000000000..1dbc14213 --- /dev/null +++ b/webroot/setting/configdb/ui/js/views/ConfigDatabaseActionView.js @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. + */ + +define([ + 'underscore', + 'contrail-view', + 'knockback' +], function (_, ContrailView, Knockback) { + + var modalId = ctwl.CDB_DELETE_MODAL_ID_, + ConfigDatabaseActionView = ContrailView.extend({ + + renderDeleteRecord: function (options) { + var textTemplate = contrail.getTemplate4Id(ctwl.CDB_TMPL_DELETE_RECORD), + elId = 'deleteRecord', + self = this, + checkedRows = options['checkedRows'], + recordsToBeDeleted = {'elementId': elId, 'checkedRows': checkedRows}; + + cowu.createModal({ + 'modalId' : modalId, + 'className': 'modal-700', + 'title' : options['title'], + 'btnName' : 'Confirm', + 'body' : textTemplate(recordsToBeDeleted), + 'onSave' : function () { + self.model.deleteRecord(options['checkedRows'], { + init : function () { + self.model.showErrorAttr(elId, false); + cowu.enableModalLoading(modalId); + }, + success: function () { + options['callback'](); + $("#" + modalId).modal('hide'); + }, + error : function (error) { + cowu.disableModalLoading(modalId, function () { + self.model.showErrorAttr(elId, error.responseText); + }); + } + }, options['type']); + }, + 'onCancel' : function () { + $("#" + modalId).modal('hide'); + } + }); + + self.model.showErrorAttr(elId, false); + Knockback.applyBindings(this.model, document.getElementById(modalId)); + kbValidation.bind(this); + }, + }); + return ConfigDatabaseActionView; +}); \ No newline at end of file diff --git a/webroot/setting/configdb/ui/js/views/ConfigDatabaseView.js b/webroot/setting/configdb/ui/js/views/ConfigDatabaseView.js index 23c97aaeb..5756a6676 100644 --- a/webroot/setting/configdb/ui/js/views/ConfigDatabaseView.js +++ b/webroot/setting/configdb/ui/js/views/ConfigDatabaseView.js @@ -4,8 +4,10 @@ define([ 'underscore', - 'contrail-view' -], function (_, ContrailView) { + 'contrail-view', + 'setting/configdb/ui/js/models/ConfigDatabaseModel', + 'setting/configdb/ui/js/views/ConfigDatabaseActionView' +], function (_, ContrailView, ConfigDatabaseModel, ConfigDatabaseActionView) { var ConfigDatabaseView = ContrailView.extend({ el: $(contentContainer), @@ -345,7 +347,7 @@ define([ forceFitColumns: true, actionCell: function (dc) { if (gridConfig.actionCell && globalObj['configDBEditEnabled']) { - return getActionCog(gridConfig.columnName, gridId); + return getRowActionConfig(gridConfig.columnName, gridId); } else { return []; } @@ -385,66 +387,30 @@ define([ } }; - function getActionCog(columnName, gridId){ - return [{ - title: 'Delete', - iconClass: 'icon-trash', - onClick: function(rowIndex){ - var selectedRow = $('#' + gridId).data('contrailGrid')._dataView.getItem(rowIndex); - if(columnName === "keys"){ - createModalForDelete(selectedRow, "delete-key"); - }else if(columnName === "keyvalues"){ - createModalForDelete(selectedRow, "delete-key-value"); - } - } - }]; - }; - - function createModalForDelete (selectedRow, type){ - var textTemplate = '', modalId = "delete-cdb", - callbackObj = { - init: function () { - cowu.enableModalLoading(modalId); - }, - success: function () { - $("#" + modalId).modal('hide'); - }, - error: function (error) { - cowu.disableModalLoading(modalId, function () {}); - } - }; - - if( (contrail.checkIfExist(selectedRow.keyvalue)) && (type == ctwc.DELETE_KEY_VALUE_TYPE) ){ - textTemplate = '
Are you sure you want to delete ' + selectedRow.keyvalue + ' ?
'; - } else if ((contrail.checkIfExist(selectedRow.key)) && (type == ctwc.DELETE_KEY_TYPE)) { - textTemplate = '
Are you sure you want to delete ' + selectedRow.key + ' ?
'; - } + function getRowActionConfig (columnName, gridId) { + var type = '', + rowActionConfig = [ + ctwgc.getDeleteAction(function (rowIndex) { + var dataItem = $('#' + gridId).data('contrailGrid')._dataView.getItem(rowIndex), + configDatabaseModel = new ConfigDatabaseModel(dataItem), + checkedRow = dataItem, title = ctwl.CDB_TITLE_DELETE_RECORD, + configDatabaseActionView = new ConfigDatabaseActionView(); - cowu.createModal({'modalId': modalId , 'className': 'modal-700', 'title': "Delete ", 'btnName': 'Confirm', 'body': textTemplate, 'onSave': function () { - var url, ajaxConfig = {}; - - if (type == ctwc.DELETE_KEY_TYPE) { - url = "/api/query/cassandra/key/" + selectedRow.table + "/" + selectedRow.key; - } else if (type == ctwc.DELETE_KEY_VALUE_TYPE) { - url = "/api/query/cassandra/value/" + selectedRow.table + "/" + selectedRow.key + "/" + selectedRow.keyvalue; - } - - ajaxConfig.type = "DELETE"; - ajaxConfig.url = url; - - contrail.ajaxHandler(ajaxConfig, function (response) { - if (contrail.checkIfFunction(callbackObj.success)) { - callbackObj.success(); - } - }, function (error) { - if (contrail.checkIfFunction(callbackObj.error)) { - callbackObj.error(error); - } - }); - - }, 'onCancel': function () { - $("#" + modalId).modal('hide'); - }}); + configDatabaseActionView.model = configDatabaseModel; + if (columnName === ctwl.CDB_LABEL_KEY) { + type = "delete-key"; + } else if (columnName === ctwl.CDB_LABEL_KEY_VALUES) { + type = "delete-key-value"; + } + configDatabaseActionView.renderDeleteRecord({ + "title": title, "type": type, checkedRows: checkedRow, callback: function () { + var dataView = $('#' + gridId).data("contrailGrid")._dataView; + dataView.refreshData(); + } + }); + }) + ]; + return rowActionConfig; }; return ConfigDatabaseView;