From d028607d614dac747f2c2e68a4a7cce0bf5e433a Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 18 Dec 2013 02:25:36 -0500
Subject: [PATCH] Fixed edit dialog to enable/disable TPS resources.

The edit dialogs for authenticators, connections, and profiles, have
been modified to show the available action (i.e. enable or disable)
based on the resource status.

Ticket #654
---
 base/server/share/webapps/pki/css/pki-ui.css       |  4 ++
 base/server/share/webapps/pki/js/pki-ui.js         |  5 +-
 .../shared/webapps/tps/authenticators.html         |  9 ++-
 .../tps-tomcat/shared/webapps/tps/connections.html |  9 ++-
 .../shared/webapps/tps/js/authenticator.js         | 73 ++++++++++++++++++++++
 .../tps-tomcat/shared/webapps/tps/js/connection.js | 73 ++++++++++++++++++++++
 base/tps-tomcat/shared/webapps/tps/js/profile.js   | 59 +++++++++++++++++
 base/tps-tomcat/shared/webapps/tps/profiles.html   |  7 +--
 8 files changed, 228 insertions(+), 11 deletions(-)

diff --git a/base/server/share/webapps/pki/css/pki-ui.css b/base/server/share/webapps/pki/css/pki-ui.css
index 4d79d531d420faf17ed80f923b06e8ad8cfc8cbd..3f1729f13c8bcfceab8f8e495936844d751c915a 100644
--- a/base/server/share/webapps/pki/css/pki-ui.css
+++ b/base/server/share/webapps/pki/css/pki-ui.css
@@ -34,6 +34,10 @@ body {
   display: none;
 }
 
+.rcue-dialog footer {
+  right: 0;
+}
+
 input[readonly="readonly"] {
   background-image: none;
   border: 1px solid #FFFFFF;
diff --git a/base/server/share/webapps/pki/js/pki-ui.js b/base/server/share/webapps/pki/js/pki-ui.js
index 0ac313952490b058f6169a3437149c4ecdd3b8ef..62bd2a2bfd76dcf1950f3fa57919bfcc8415c60e 100644
--- a/base/server/share/webapps/pki/js/pki-ui.js
+++ b/base/server/share/webapps/pki/js/pki-ui.js
@@ -328,6 +328,9 @@ var TableItemView = Backbone.View.extend({
                     click: function(e) {
                         var dialog = self.table.editDialog;
                         dialog.model = self.model;
+                        dialog.once("close", function(event) {
+                            self.render();
+                        });
                         dialog.open();
                         e.preventDefault();
                     }
@@ -357,7 +360,7 @@ var TableView = Backbone.View.extend({
         $("button[name=add]", self.thead).click(function(e) {
             var dialog = self.addDialog;
             dialog.model = new self.collection.model();
-            dialog.on("close", function(event) {
+            dialog.once("close", function(event) {
                 self.render();
             });
             dialog.open();
diff --git a/base/tps-tomcat/shared/webapps/tps/authenticators.html b/base/tps-tomcat/shared/webapps/tps/authenticators.html
index 81e2c53ec6af0c8200d90a4eb328abac97422e91..a13f21c6c8f7b82a3a81b1a9c6fc4a3c60d853a3 100644
--- a/base/tps-tomcat/shared/webapps/tps/authenticators.html
+++ b/base/tps-tomcat/shared/webapps/tps/authenticators.html
@@ -25,10 +25,10 @@
     <script src="/tps/js/authenticator.js"></script>
     <script>
 $(function() {
-    var editDialog = new Dialog({
+    var editDialog = new AuthenticatorDialog({
         el: $("#authenticator-dialog"),
         title: "Edit Authenticator",
-        readonly: ["id"]
+        readonly: ["id", "status"]
     });
 
     new TableView({
@@ -85,7 +85,10 @@ $(function() {
             <label>Status</label><input name="status" type="text"><br>
         </fieldset>
         <footer>
-            <button name="save" class="primary">Save</button>
+            <button name="approve">Approve</button>
+            <button name="reject">Reject</button>
+            <button name="enable" class="primary">Enable</button>
+            <button name="disable" class="primary">Disable</button>
             <button name="cancel">Cancel</button>
         </footer>
     </div>
diff --git a/base/tps-tomcat/shared/webapps/tps/connections.html b/base/tps-tomcat/shared/webapps/tps/connections.html
index 0f26e330110ddecb3cac36792bad785f12e9a782..ba5a45331ee0555cb7866f103ec346422ae5efce 100644
--- a/base/tps-tomcat/shared/webapps/tps/connections.html
+++ b/base/tps-tomcat/shared/webapps/tps/connections.html
@@ -25,10 +25,10 @@
     <script src="/tps/js/connection.js"></script>
     <script>
 $(function() {
-    var editDialog = new Dialog({
+    var editDialog = new ConnectionDialog({
         el: $("#connection-dialog"),
         title: "Edit Connection",
-        readonly: ["id"]
+        readonly: ["id", "status"]
     });
 
     new TableView({
@@ -85,7 +85,10 @@ $(function() {
             <label>Status</label><input name="status" type="text"><br>
         </fieldset>
         <footer>
-            <button name="save" class="primary">Save</button>
+            <button name="approve">Approve</button>
+            <button name="reject">Reject</button>
+            <button name="enable" class="primary">Enable</button>
+            <button name="disable" class="primary">Disable</button>
             <button name="cancel">Cancel</button>
         </footer>
     </div>
diff --git a/base/tps-tomcat/shared/webapps/tps/js/authenticator.js b/base/tps-tomcat/shared/webapps/tps/js/authenticator.js
index 89ebb282a84191adc37dac3157a6d58be75747b1..8e67a598314ace42e456104704fd2265690c6063 100644
--- a/base/tps-tomcat/shared/webapps/tps/js/authenticator.js
+++ b/base/tps-tomcat/shared/webapps/tps/js/authenticator.js
@@ -27,6 +27,15 @@ var AuthenticatorModel = Model.extend({
             status: response.Authenticator.Status
         };
     },
+    parseXML: function(data) {
+        var xml = $(data);
+        var entry = $("Authenticator", xml);
+        var status = $("Status", entry);
+        return {
+            id: entry.attr("id"),
+            status: status.text()
+        };
+    },
     createRequest: function(attributes) {
         return {
             Authenticator: {
@@ -34,6 +43,28 @@ var AuthenticatorModel = Model.extend({
                 Status: attributes.status
             }
         };
+    },
+    enable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=enable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
+    },
+    disable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=disable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
     }
 });
 
@@ -52,3 +83,45 @@ var AuthenticatorCollection = Collection.extend({
         });
     }
 });
+
+var AuthenticatorDialog = Dialog.extend({
+    render: function() {
+        var self = this;
+        var status = self.model.get("status");
+        if (status == "Enabled") {
+            self.actions = ["disable", "cancel"];
+        } else if (status == "Disabled") {
+            self.actions = ["enable", "cancel"];
+        } else {
+            alert("ERROR: Invalid status: " + status);
+        }
+        AuthenticatorDialog.__super__.render.call(self);
+    },
+    performAction: function(action) {
+        var self = this;
+
+        if (action == "enable") {
+            self.model.enable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
+
+        } else if (action == "disable") {
+            self.model.disable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
+
+        } else {
+            AuthenticatorDialog.__super__.performAction.call(self, action);
+        }
+    }
+});
diff --git a/base/tps-tomcat/shared/webapps/tps/js/connection.js b/base/tps-tomcat/shared/webapps/tps/js/connection.js
index bf97cc0c8ed50ce313d4742ee8b3ba1ce8438e8d..4ad7a98492d86b3b8de01200a97e84bbb867f1d3 100644
--- a/base/tps-tomcat/shared/webapps/tps/js/connection.js
+++ b/base/tps-tomcat/shared/webapps/tps/js/connection.js
@@ -27,6 +27,15 @@ var ConnectionModel = Model.extend({
             status: response.Connection.Status
         };
     },
+    parseXML: function(data) {
+        var xml = $(data);
+        var entry = $("Connection", xml);
+        var status = $("Status", entry);
+        return {
+            id: entry.attr("id"),
+            status: status.text()
+        };
+    },
     createRequest: function(attributes) {
         return {
             Connection: {
@@ -34,6 +43,28 @@ var ConnectionModel = Model.extend({
                 Status: attributes.status
             }
         };
+    },
+    enable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=enable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
+    },
+    disable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=disable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
     }
 });
 
@@ -52,3 +83,45 @@ var ConnectionCollection = Collection.extend({
         });
     }
 });
+
+var ConnectionDialog = Dialog.extend({
+    render: function() {
+        var self = this;
+        var status = self.model.get("status");
+        if (status == "Enabled") {
+            self.actions = ["disable", "cancel"];
+        } else if (status == "Disabled") {
+            self.actions = ["enable", "cancel"];
+        } else {
+            alert("ERROR: Invalid status: " + status);
+        }
+        ConnectionDialog.__super__.render.call(self);
+    },
+    performAction: function(action) {
+        var self = this;
+
+        if (action == "enable") {
+            self.model.enable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
+
+        } else if (action == "disable") {
+            self.model.disable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
+
+        } else {
+            ConnectionDialog.__super__.performAction.call(self, action);
+        }
+    }
+});
diff --git a/base/tps-tomcat/shared/webapps/tps/js/profile.js b/base/tps-tomcat/shared/webapps/tps/js/profile.js
index 25ed88110a02719654d210d13e2099ba360b4cd5..b847e12ecf29ecaa8338fb1efff53eabb32bbd3b 100644
--- a/base/tps-tomcat/shared/webapps/tps/js/profile.js
+++ b/base/tps-tomcat/shared/webapps/tps/js/profile.js
@@ -27,6 +27,15 @@ var ProfileModel = Model.extend({
             status: response.Profile.Status
         };
     },
+    parseXML: function(data) {
+        var xml = $(data);
+        var entry = $("Profile", xml);
+        var status = $("Status", entry);
+        return {
+            id: entry.attr("id"),
+            status: status.text()
+        };
+    },
     createRequest: function(attributes) {
         return {
             Profile: {
@@ -34,6 +43,28 @@ var ProfileModel = Model.extend({
                 Status: attributes.status
             }
         };
+    },
+    enable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=enable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
+    },
+    disable: function(options) {
+        var self = this;
+        $.post(self.url() + "?action=disable")
+            .done(function(data, textStatus, jqXHR) {
+                self.set(self.parseXML(data));
+                options.success.call(self, data, textStatus, jqXHR);
+            })
+            .fail(function(jqXHR, textStatus, errorThrown) {
+                options.error.call(self, jqXHR, textStatus, errorThrown);
+            });
     }
 });
 
@@ -54,12 +85,40 @@ var ProfileCollection = Collection.extend({
 });
 
 var ProfileDialog = Dialog.extend({
+    render: function() {
+        var self = this;
+        var status = self.model.get("status");
+        if (status == "Enabled") {
+            self.actions = ["disable", "cancel"];
+        } else if (status == "Disabled") {
+            self.actions = ["enable", "cancel"];
+        } else {
+            alert("ERROR: Invalid status: " + status);
+        }
+        ProfileDialog.__super__.render.call(self);
+    },
     performAction: function(action) {
         var self = this;
 
         if (action == "enable") {
+            self.model.enable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
 
         } else if (action == "disable") {
+            self.model.disable({
+                success: function(data,textStatus, jqXHR) {
+                    self.close();
+                },
+                error: function(jqXHR, textStatus, errorThrown) {
+                    alert("ERROR: " + textStatus);
+                }
+            });
 
         } else {
             ProfileDialog.__super__.performAction.call(self, action);
diff --git a/base/tps-tomcat/shared/webapps/tps/profiles.html b/base/tps-tomcat/shared/webapps/tps/profiles.html
index 9f00ac5436d35068df4e78f73fe5c633ee9f96dc..1ccc44346ab891258f006a238751339f5c3005d9 100644
--- a/base/tps-tomcat/shared/webapps/tps/profiles.html
+++ b/base/tps-tomcat/shared/webapps/tps/profiles.html
@@ -28,8 +28,7 @@ $(function() {
     var editDialog = new ProfileDialog({
         el: $("#profile-dialog"),
         title: "Edit Profile",
-        readonly: ["id", "status"],
-        actions: ["enable", "disable", "cancel"]
+        readonly: ["id", "status"]
     });
 
     new TableView({
@@ -88,8 +87,8 @@ $(function() {
         <footer>
             <button name="approve">Approve</button>
             <button name="reject">Reject</button>
-            <button name="enable">Enable</button>
-            <button name="disable">Disable</button>
+            <button name="enable" class="primary">Enable</button>
+            <button name="disable" class="primary">Disable</button>
             <button name="cancel">Cancel</button>
         </footer>
     </div>
-- 
1.8.4.2

