>From 7556ebf5f8bbc4477eb4601a359ce3ed5cc6af6f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Sat, 18 Feb 2017 08:58:25 +0100
Subject: [PATCH] Refactored pki-ui.js.

For clarity the non-UI code in the pki-ui.js has been moved into
pki.js.
---
 base/server/share/webapps/pki/js/pki-ui.js | 152 -------------------------
 base/server/share/webapps/pki/js/pki.js    | 172 +++++++++++++++++++++++++++++
 base/tps/shared/webapps/tps/ui/index.jsp   |   1 +
 3 files changed, 173 insertions(+), 152 deletions(-)
 create mode 100644 base/server/share/webapps/pki/js/pki.js

diff --git a/base/server/share/webapps/pki/js/pki-ui.js b/base/server/share/webapps/pki/js/pki-ui.js
index c4acdb9965c57c297f7eb748b493a54bdcb6a6da..005e8e689e6f35992ff39f22898d4209596ac751 100644
--- a/base/server/share/webapps/pki/js/pki-ui.js
+++ b/base/server/share/webapps/pki/js/pki-ui.js
@@ -19,158 +19,6 @@
  * @author Endi S. Dewata
  */
 
-var PKI = {
-    substitute: function(content, map) {
-
-        var newContent = "";
-
-        // substitute ${attribute} with attribute value
-        var pattern = /\${([^}]*)}/;
-
-        while (content.length) {
-            // search for ${attribute} pattern
-            var index = content.search(pattern);
-            if (index < 0) {
-                newContent += content;
-                break;
-            }
-
-            var name = RegExp.$1;
-            var value = map[name];
-
-            // replace pattern occurrence with attribute value
-            newContent += content.substring(0, index) + (value === undefined ? "" : value);
-
-            // process the remaining content
-            content = content.substring(index + name.length + 3);
-        }
-
-        return newContent;
-    },
-    logout: function(options) {
-        options = options || {};
-        if (window.crypto && typeof window.crypto.logout === "function") { // Firefox
-            window.crypto.logout();
-            if (options.success) options.success.call();
-
-        } else {
-            var result = document.execCommand("ClearAuthenticationCache", false);
-            if (result) { // IE
-                if (options.success) options.success.call();
-
-            } else { // logout not supported
-                if (options.error) options.error.call();
-            }
-        }
-    }
-};
-
-var Model = Backbone.Model.extend({
-    parseResponse: function(response) {
-        return response;
-    },
-    parse: function(response, options) {
-        return this.parseResponse(response);
-    },
-    createRequest: function(attributes) {
-        return attributes;
-    },
-    save: function(attributes, options) {
-        var self = this;
-        if (attributes == undefined) attributes = self.attributes;
-        // convert attributes into JSON request
-        var request = self.createRequest(attributes);
-        // remove old attributes
-        if (self.isNew()) self.clear();
-        // send JSON request
-        Model.__super__.save.call(self, request, options);
-    }
-});
-
-var Collection = Backbone.Collection.extend({
-    urlRoot: null,
-    initialize: function(models, options) {
-        var self = this;
-        Collection.__super__.initialize.call(self, models, options);
-
-        self.options = options;
-        self.links = {};
-        self.query({});
-    },
-    url: function() {
-        return this.currentURL;
-    },
-    parse: function(response) {
-        var self = this;
-
-        // get total entries
-        self.total = self.getTotal(response);
-
-        // parse links
-        var links = self.getLinks(response);
-        links = links == undefined ? [] : [].concat(links);
-        self.parseLinks(links);
-
-        // convert entries into models
-        var models = [];
-        var entries = self.getEntries(response);
-        entries = entries == undefined ? [] : [].concat(entries);
-
-        _(entries).each(function(entry) {
-            var model = self.parseEntry(entry);
-            models.push(model);
-        });
-
-        return models;
-    },
-    getTotal: function(response) {
-        return response.total;
-    },
-    getEntries: function(response) {
-        return null;
-    },
-    getLinks: function(response) {
-        return null;
-    },
-    parseEntry: function(entry) {
-        return null;
-    },
-    parseLinks: function(links) {
-        var self = this;
-        self.links = {};
-        _(links).each(function(link) {
-            var name = link.rel;
-            var href = link.href;
-            self.links[name] = href;
-        });
-    },
-    link: function(name) {
-        return this.links[name];
-    },
-    go: function(name) {
-        var self = this;
-        if (self.links[name] == undefined) return;
-        self.currentURL = self.links[name];
-    },
-    query: function(params) {
-        var self = this;
-
-        // add default options into the params
-        _.defaults(params, self.options);
-
-        // generate query string
-        var query = "";
-        _(params).each(function(value, name) {
-            // skip null or empty string, but don't skip 0
-            if (value === null || value === "") return;
-            query = query == "" ? "?" : query + "&";
-            query = query + name + "=" + encodeURIComponent(value);
-        });
-
-        self.currentURL = self.urlRoot + query;
-    }
-});
-
 var Page = Backbone.View.extend({
     initialize: function(options) {
         var self = this;
diff --git a/base/server/share/webapps/pki/js/pki.js b/base/server/share/webapps/pki/js/pki.js
new file mode 100644
index 0000000000000000000000000000000000000000..573d8819b35a7ba7bbab543026d940e6af843af4
--- /dev/null
+++ b/base/server/share/webapps/pki/js/pki.js
@@ -0,0 +1,172 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ *
+ * @author Endi S. Dewata
+ */
+
+var PKI = {
+    substitute: function(content, map) {
+
+        var newContent = "";
+
+        // substitute ${attribute} with attribute value
+        var pattern = /\${([^}]*)}/;
+
+        while (content.length) {
+            // search for ${attribute} pattern
+            var index = content.search(pattern);
+            if (index < 0) {
+                newContent += content;
+                break;
+            }
+
+            var name = RegExp.$1;
+            var value = map[name];
+
+            // replace pattern occurrence with attribute value
+            newContent += content.substring(0, index) + (value === undefined ? "" : value);
+
+            // process the remaining content
+            content = content.substring(index + name.length + 3);
+        }
+
+        return newContent;
+    },
+    logout: function(options) {
+        options = options || {};
+        if (window.crypto && typeof window.crypto.logout === "function") { // Firefox
+            window.crypto.logout();
+            if (options.success) options.success.call();
+
+        } else {
+            var result = document.execCommand("ClearAuthenticationCache", false);
+            if (result) { // IE
+                if (options.success) options.success.call();
+
+            } else { // logout not supported
+                if (options.error) options.error.call();
+            }
+        }
+    }
+};
+
+var Model = Backbone.Model.extend({
+    parseResponse: function(response) {
+        return response;
+    },
+    parse: function(response, options) {
+        return this.parseResponse(response);
+    },
+    createRequest: function(attributes) {
+        return attributes;
+    },
+    save: function(attributes, options) {
+        var self = this;
+        if (attributes == undefined) attributes = self.attributes;
+        // convert attributes into JSON request
+        var request = self.createRequest(attributes);
+        // remove old attributes
+        if (self.isNew()) self.clear();
+        // send JSON request
+        Model.__super__.save.call(self, request, options);
+    }
+});
+
+var Collection = Backbone.Collection.extend({
+    urlRoot: null,
+    initialize: function(models, options) {
+        var self = this;
+        Collection.__super__.initialize.call(self, models, options);
+
+        self.options = options;
+        self.links = {};
+        self.query({});
+    },
+    url: function() {
+        return this.currentURL;
+    },
+    parse: function(response) {
+        var self = this;
+
+        // get total entries
+        self.total = self.getTotal(response);
+
+        // parse links
+        var links = self.getLinks(response);
+        links = links == undefined ? [] : [].concat(links);
+        self.parseLinks(links);
+
+        // convert entries into models
+        var models = [];
+        var entries = self.getEntries(response);
+        entries = entries == undefined ? [] : [].concat(entries);
+
+        _(entries).each(function(entry) {
+            var model = self.parseEntry(entry);
+            models.push(model);
+        });
+
+        return models;
+    },
+    getTotal: function(response) {
+        return response.total;
+    },
+    getEntries: function(response) {
+        return null;
+    },
+    getLinks: function(response) {
+        return null;
+    },
+    parseEntry: function(entry) {
+        return null;
+    },
+    parseLinks: function(links) {
+        var self = this;
+        self.links = {};
+        _(links).each(function(link) {
+            var name = link.rel;
+            var href = link.href;
+            self.links[name] = href;
+        });
+    },
+    link: function(name) {
+        return this.links[name];
+    },
+    go: function(name) {
+        var self = this;
+        if (self.links[name] == undefined) return;
+        self.currentURL = self.links[name];
+    },
+    query: function(params) {
+        var self = this;
+
+        // add default options into the params
+        _.defaults(params, self.options);
+
+        // generate query string
+        var query = "";
+        _(params).each(function(value, name) {
+            // skip null or empty string, but don't skip 0
+            if (value === null || value === "") return;
+            query = query == "" ? "?" : query + "&";
+            query = query + name + "=" + encodeURIComponent(value);
+        });
+
+        self.currentURL = self.urlRoot + query;
+    }
+});
diff --git a/base/tps/shared/webapps/tps/ui/index.jsp b/base/tps/shared/webapps/tps/ui/index.jsp
index b7776c91cd2526a02401af2f5f0ca62d31a7a982..bae74ce8fcee7db8d8dda6fb3c17b760cc0c99d5 100644
--- a/base/tps/shared/webapps/tps/ui/index.jsp
+++ b/base/tps/shared/webapps/tps/ui/index.jsp
@@ -26,6 +26,7 @@
     <script src="/pki/js/backbone.js"></script>
     <script src="/pki/js/bootstrap.js"></script>
     <script src="/pki/js/patternfly.js"></script>
+    <script src="/pki/js/pki.js"></script>
     <script src="/pki/js/pki-ui.js"></script>
     <script src="/tps/js/tps.js"></script>
     <script src="/tps/js/account.js"></script>
-- 
2.5.5

