From e36fb2558e0a402ffb6caa83cadbe9c50cd76944 Mon Sep 17 00:00:00 2001 From: Thomas Gelf <thomas@gelf.net> Date: Tue, 25 Mar 2014 12:11:39 +0000 Subject: [PATCH] Add JS helper extending url params --- public/js/icinga/utils.js | 41 ++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/public/js/icinga/utils.js b/public/js/icinga/utils.js index 5e9714c8f..2e627c64d 100644 --- a/public/js/icinga/utils.js +++ b/public/js/icinga/utils.js @@ -53,16 +53,20 @@ return hours + ':' + minutes + ':' + seconds; }, + getUrlHelper: function () { + if (this.urlHelper === null) { + this.urlHelper = document.createElement('a'); + } + + return this.urlHelper; + }, + /** * Parse a given Url and return an object */ parseUrl: function (url) { - if (this.urlHelper === null) { - this.urlHelper = document.createElement('a'); - } - - var a = this.urlHelper; + var a = this.getUrlHelper(); a.href = url; var result = { @@ -83,6 +87,33 @@ return result; }, + // Local URLs only + addUrlParams: function (url, params) { + var parts = this.parseUrl(url); + var result = parts.path; + var newparams = parts.params; + var idx, p; + $.each(params, function (idx, p) { + // We overwrite existing params + newparams[p.name] = p.value; + }); + + if (Object.keys(newparams).length > 0) { + var queryString = '?'; + $.each(newparams, function (key, value) { + if (queryString !== '?') { + queryString += '&'; + } + queryString += encodeURIComponent(key) + '=' + encodeURIComponent(value); + }); + result += queryString; + } + if (parts.hash.length > 0) { + result += '#' + parts.hash; + } + return result; + }, + /** * Parse url params */