From fa5c499733d1c8849ff65e21bd4e32571d2ca80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Wed, 19 Jun 2013 13:17:43 +0200 Subject: [PATCH] Allow container-based url in history api When a url is now loaded for an non-main container, the url for the container is appended to the GET part of the URL refs #4303 --- public/js/icinga/icinga.js | 1 - public/js/icinga/util/async.js | 37 ++++++++++++++++--- test/frontend/cases/historyApiTest.js | 12 ++++-- .../static/fragments/testFragment1.html | 2 +- test/frontend/static/public | 1 + 5 files changed, 41 insertions(+), 12 deletions(-) create mode 120000 test/frontend/static/public diff --git a/public/js/icinga/icinga.js b/public/js/icinga/icinga.js index 31f78dcae..143593942 100755 --- a/public/js/icinga/icinga.js +++ b/public/js/icinga/icinga.js @@ -71,7 +71,6 @@ define([ loadUrl: function(url, target, params) { target = target || "icinga-main"; async.loadToTarget(target, url, params); - History.pushState(params, document.title, url); }, getFailedModules: function() { diff --git a/public/js/icinga/util/async.js b/public/js/icinga/util/async.js index 27e56cb7d..80322964a 100644 --- a/public/js/icinga/util/async.js +++ b/public/js/icinga/util/async.js @@ -9,7 +9,33 @@ var pending = { }; - + + var getCurrentGETParameters = function() { + var currentGET = window.location.search.substring(1).split("&"); + var params = {}; + if(currentGET.length > 0) { + $.each(currentGET, function(idx, elem) { + var keyVal = elem.split("="); + params[encodeURIComponent(keyVal[0])] = encodeURIComponent(keyVal[1]); + }); + } + return params; + } +; + var pushGet = function(param, value, url) { + url = url || (window.location.origin+window.location.pathname); + var params = getCurrentGETParameters(); + params[encodeURIComponent(param)] = encodeURIComponent(value); + var search = "?"; + for (var name in params) { + if(search != "?") + search += "&"; + search += name+"="+params[name]; + } + + return url+search+"#"+window.location.hash; + }; + var getDOMForDestination = function(destination) { var target = destination; if(typeof destination === "string") { @@ -96,14 +122,13 @@ if (destination == "icinga-main") { History.pushState(data, document.title, url); } else { - data = data || {}; - data[destination] = url; - History.pushState(data, document.title, document.location.href); + url = pushGet("c["+destination+"]", url); + History.pushState(data, document.title, url); } + console.log("New url: ", url); return req; - }; - + this.loadCSS = function(name) { }; diff --git a/test/frontend/cases/historyApiTest.js b/test/frontend/cases/historyApiTest.js index bb99a1c4c..a346447a9 100644 --- a/test/frontend/cases/historyApiTest.js +++ b/test/frontend/cases/historyApiTest.js @@ -44,12 +44,16 @@ var onSecondLink = function() { casper.page.evaluate(function() { requirejs(["icinga/icinga"], function(icinga) { icinga.loadUrl("/fragments/testFragment3.html?this=is_a_param", "icinga-detail"); - console.log(document.location.href); + }); }); - console.log(casper.page.evaluate(function() { - return document.location.href; - })); + this.wait(400, function() { + console.log(casper.page.evaluate(function() { + return window.location.href; + })); + + this.test.assertUrlMatch(/testFragment2.html.*testFragment3.html/); + }); }; casper.run(function() { diff --git a/test/frontend/static/fragments/testFragment1.html b/test/frontend/static/fragments/testFragment1.html index 926a2c392..2d6ef70db 100644 --- a/test/frontend/static/fragments/testFragment1.html +++ b/test/frontend/static/fragments/testFragment1.html @@ -1,6 +1,6 @@

Test fragment

- Fragment 2 + Fragment 2 Fragment 3
diff --git a/test/frontend/static/public b/test/frontend/static/public new file mode 120000 index 000000000..07a4824dc --- /dev/null +++ b/test/frontend/static/public @@ -0,0 +1 @@ +/Users/moja/git/icinga2-web/test/frontend/../../public \ No newline at end of file