diff options
| author | Endi S. Dewata <edewata@redhat.com> | 2017-02-18 08:58:25 +0100 |
|---|---|---|
| committer | Endi S. Dewata <edewata@redhat.com> | 2017-02-20 17:31:23 +0100 |
| commit | 7556ebf5f8bbc4477eb4601a359ce3ed5cc6af6f (patch) | |
| tree | fca2a25743eb008ebcf9cf97c702178d5a950b98 /base | |
| parent | 220dd64ce23b05bcf84df8ca123ee2f27589ab91 (diff) | |
| download | pki-7556ebf5f8bbc4477eb4601a359ce3ed5cc6af6f.tar.gz pki-7556ebf5f8bbc4477eb4601a359ce3ed5cc6af6f.tar.xz pki-7556ebf5f8bbc4477eb4601a359ce3ed5cc6af6f.zip | |
Refactored pki-ui.js.
For clarity the non-UI code in the pki-ui.js has been moved into
pki.js.
Diffstat (limited to 'base')
| -rw-r--r-- | base/server/share/webapps/pki/js/pki-ui.js | 152 | ||||
| -rw-r--r-- | base/server/share/webapps/pki/js/pki.js | 172 | ||||
| -rw-r--r-- | base/tps/shared/webapps/tps/ui/index.jsp | 1 |
3 files changed, 173 insertions, 152 deletions
diff --git a/base/server/share/webapps/pki/js/pki-ui.js b/base/server/share/webapps/pki/js/pki-ui.js index c4acdb996..005e8e689 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 000000000..573d8819b --- /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 b7776c91c..bae74ce8f 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> |
