EXTCDI-284 add support for already existing window.name value

This will enable
* href target=
* multi-framework apps
* copy&paste between different tabs
* opening a bookmark on an existing page


git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/cdi/trunk@1338175 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/ClientSideWindowHandler.java b/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/ClientSideWindowHandler.java
index d71cd65..76fc935 100644
--- a/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/ClientSideWindowHandler.java
+++ b/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/scope/conversation/ClientSideWindowHandler.java
@@ -179,8 +179,8 @@
             windowHandlerHtml = windowHandlerHtml.replace(WINDOW_ID_REPLACE_PATTERN, windowId);
 
             // set the noscript-URL for users with no JavaScript
-            windowHandlerHtml = windowHandlerHtml.replace(
-                    NOSCRIPT_URL_REPLACE_PATTERN, getNoscriptUrl(externalContext));
+            windowHandlerHtml =
+                    windowHandlerHtml.replace(NOSCRIPT_URL_REPLACE_PATTERN, getNoscriptUrl(externalContext));
 
             OutputStream os = httpResponse.getOutputStream();
             try
diff --git a/jee-modules/jsf-module/impl/src/main/resources/META-INF/resources/js/windowhandler.js b/jee-modules/jsf-module/impl/src/main/resources/META-INF/resources/js/windowhandler.js
index 85d8279..a6e929e 100644
--- a/jee-modules/jsf-module/impl/src/main/resources/META-INF/resources/js/windowhandler.js
+++ b/jee-modules/jsf-module/impl/src/main/resources/META-INF/resources/js/windowhandler.js
@@ -78,9 +78,8 @@
         if (!links[i].onclick) {
             links[i].onclick = function() {storeWindowTree(); return true;};
         } else {
-            //the function wrapper is important
-            //otherwise the last onclick handler would be assigned
-            //to oldonclick
+            //the function wrapper is important otherwise the
+            //last onclick handler would be assigned to oldonclick
             (function storeEvent() {
                 var oldonclick = links[i].onclick;
                 links[i].onclick = function(evt) {
@@ -95,11 +94,26 @@
 }
 
 function assertWindowId() {
-    var freshWindow = window.name.length < 1;
-    if (freshWindow) {
-        var url = urlWithoutWindowId(window.location.href);
-        window.name = "window";
-        window.location = url;
+    var url = window.location.href;
+    var vars = url.split(/&|\?/g);
+    var newUrl = "";
+    var iParam = 0;
+    for (var i=0; vars != null && i < vars.length; i++) {
+        var pair = vars[i].split("=");
+        if (pair.length == 1) {
+            newUrl = pair[0];
+        } else {
+            if (pair[0] == "windowId") { //
+                pair[1] = window.name ? window.name : "";
+            }
+            var amp = iParam++ > 0 ? "&" : "?";
+            newUrl =  newUrl + amp + pair[0] + "=" + pair[1];
+        }
+    }
+
+    if (newUrl != window.location.href) {
+        window.name = "tempWindowId";
+        window.location = newUrl;
     }
 }
 
@@ -112,7 +126,7 @@
         if (isHtml5()) {
             applyOnClick();
         }
-        // this ensures that even without the ClientSideWindowHandler
+        // this ensures that even without the ClientSideWindowHandler a new windowId gets issued on a new tab
         assertWindowId();
     }
 }
diff --git a/jee-modules/jsf-module/impl/src/main/resources/static/windowhandler.html b/jee-modules/jsf-module/impl/src/main/resources/static/windowhandler.html
index ec2c134..d7f0d8a 100644
--- a/jee-modules/jsf-module/impl/src/main/resources/static/windowhandler.html
+++ b/jee-modules/jsf-module/impl/src/main/resources/static/windowhandler.html
@@ -29,16 +29,11 @@
 
 <script type="text/javascript" >
 function isHtml5() {
-    try {
-        return !!localStorage.getItem;
-    } catch(e) {
-        return false;
-    }
+    try { return !!localStorage.getItem;
+    } catch(e) { return false; }
 }
 function unstringify(serialized) {
-    if (JSON) {
-        return JSON.parse(serialized);
-    }
+    if (JSON) { return JSON.parse(serialized); }
     return serialized.split("|||");
 }
 function getOldBody() {
@@ -78,7 +73,6 @@
             localStorage.removeItem(window.name + '_css');
         }
     }
-
 }
 
 function replaceContent() {
@@ -120,8 +114,7 @@
         var pair = vars[i].split("=");
         if (pair.length == 1) {
             newQuery = pair[0];
-        }
-        else {
+        } else {
             if (pair[0] != paramName) {
                 var amp = iParam++ > 0 ? "&" : "?";
                 newQuery =  newQuery + amp + pair[0] + "=" + pair[1];
@@ -132,7 +125,6 @@
             }
         }
     }
-
     if (!paramFound) {
         var amp = iParam++ > 0 ? "&" : "?";
         newQuery =  newQuery + amp + paramName + "=" + paramValue;
@@ -149,7 +141,7 @@
     if (windowId == 'uninitializedWindowId') {
         windowId = window.name
     }
-    if (!windowId || windowId.length < 3) {
+    if (!windowId || windowId.length < 1) {
         // request a new windowId
         windowId = 'automatedEntryPoint';
     }