Adds Ajax listener to detect view expirations
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 11 Apr 2013 15:56:55 +0000 (11:56 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 11 Apr 2013 15:56:55 +0000 (11:56 -0400)
.. and reload page, stripped of its jsessionid and hash content

src/META-INF/resources/pz2utils/fieldlisteners.js [deleted file]
src/META-INF/resources/pz2utils/listeners.js [new file with mode: 0644]
src/META-INF/resources/pz2utils/pz2watch.xhtml

diff --git a/src/META-INF/resources/pz2utils/fieldlisteners.js b/src/META-INF/resources/pz2utils/fieldlisteners.js
deleted file mode 100644 (file)
index b0a3e6a..0000000
+++ /dev/null
@@ -1,150 +0,0 @@
-  var renderTargetsReqVar;\r
-  var renderOnRecordTargetsReqVar;\r
-    \r
-  function renderTargets(doRefresh)\r
-  {\r
-       //console.log('rendering ' + renderWhileActiveclients);\r
-       var sourcecomp = document.getElementById("pz2watch:activeclientsField");\r
-    jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"});\r
-    if (doRefresh) {\r
-               //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField');  \r
-           renderTargetsReqVar=setTimeout(\r
-            function() {              \r
-              //console.log('Making request for pz2watch:activeclientsField');\r
-              jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"});       \r
-            }\r
-            ,500);\r
-    } else {\r
-       //console.log("No further updates from server requested");\r
-    }\r
-  }\r
-  \r
-  function renderOnRecordTargets(doRefresh)\r
-  {\r
-       console.log('rendering ' + renderWhileActiveclientsRecord);\r
-       var sourcecomp = document.getElementById("pz2watch:activeclientsFieldRecord");\r
-    jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclientsRecord});\r
-    if (doRefresh) {\r
-               console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsFieldRecord');  \r
-           renderOnRecordTargetsReqVar=setTimeout(\r
-            function() {              \r
-              console.log('Making request for pz2watch:activeclientsFieldRecord');\r
-              jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsFieldRecord"});       \r
-            }\r
-            ,1000);\r
-    } else {\r
-       //console.log("No further updates from server requested");\r
-    }\r
-  }\r
-\r
-\r
-  function windowlocationhashListener () {\r
-         if (trackHistory) {\r
-             //console.log("browser hash update detected");\r
-             var stateKey = document.getElementById("pz2watch:windowlocationhash");\r
-             if (window.location.hash != stateKey.value) {\r
-               //console.log("updating stateKey with new browser hash: " + window.location.hash);\r
-               stateKey.value = window.location.hash;\r
-               if (! stateKey.value) window.location.hash = '#initial';\r
-               stateKey.onchange();\r
-             } else {\r
-               //console.log("State hash already has the value of the new browser hash - not updating state hash");\r
-             }           \r
-         }       \r
-  }      \r
-\r
-  function fieldUpdateListener (data) {\r
-         if (data.status === "success") {\r
-               var updates = data.responseXML.getElementsByTagName("update");\r
-               for (var i=0, max=updates.length; i<max; i++) {\r
-                       var lsnri = fieldListeners.getListener(updates[i].getAttribute("id"));\r
-                       if (lsnri) {\r
-                               lsnri.invoke(updates[i]);\r
-                       }\r
-               }\r
-         }\r
-  }\r
-        \r
-  var Pz2listeners = function () {\r
-       var lsnrs = {};\r
-       this.addListener = function (key, lsnr) {\r
-               lsnrs[key] =lsnr;\r
-       };\r
-       this.getListener = function (key) {\r
-               return lsnrs[key];\r
-       };\r
-  };\r
-\r
-  var fieldListeners = new Pz2listeners();\r
-\r
-  var StateListener = function () {\r
-       this.invoke = function (field) {\r
-      var stateKeyDoc = StringtoXML(field.textContent || field.text);\r
-      var stateKeyValue = stateKeyDoc.childNodes[0].getAttribute("value");\r
-      //console.log('Application hash update detected. New value: ' + stateKeyValue);\r
-      if (stateKeyValue !== window.location.hash) {\r
-        window.location.hash = stateKeyValue;\r
-        //console.log("Browsers hash updated accordingly.");\r
-      } else {\r
-        //console.log("Browsers hash already has the value of the state hash. Not updating browser hash."); \r
-      }\r
-\r
-       };\r
-  };\r
-  \r
-  var ActiveclientsListener = function () {\r
-    this.invoke = function (field) {\r
-      var updateDoc = StringtoXML(field.textContent || field.text);\r
-      var activeClientsValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);\r
-      //console.log('Activeclients response detected: ' + activeClientsValue);\r
-      clearTimeout(renderTargetsReqVar);\r
-      if (activeClientsValue > '0') {\r
-        renderTargets(true);\r
-      } else {\r
-       renderTargets(false);\r
-      }\r
-       };\r
-  };\r
-  \r
-  var ActiveclientsRecordListener = function () {\r
-           this.invoke = function (field) {\r
-             var updateDoc = StringtoXML(field.textContent || field.text);\r
-             var activeClientsRecordValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);\r
-             console.log('Activeclients response for record detected: ' + activeClientsRecordValue);\r
-             clearTimeout(renderOnRecordTargetsReqVar);\r
-             if (activeClientsRecordValue > '0') {\r
-               renderOnRecordTargets(true);\r
-             } else {\r
-               console.log('Active clients is 0, final rendering');\r
-               renderOnRecordTargets(false);\r
-             }\r
-               };\r
-         };\r
-\r
-    \r
-  jsf.ajax.addOnEvent(fieldUpdateListener);\r
-  \r
-  function StringtoXML(text){\r
-               var doc;\r
-           if (window.ActiveXObject){\r
-             doc=new ActiveXObject('Microsoft.XMLDOM');\r
-             doc.async=false;\r
-             doc.loadXML(text);\r
-           } else {\r
-             var parser=new DOMParser();\r
-             doc=parser.parseFromString(text,'text/xml');\r
-           }\r
-           return doc;\r
-  }\r
-\r
-  var setUpListeners = function () {\r
-    //console.log("Starts tracking activeclientsField");\r
-    fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener());\r
-    fieldListeners.addListener("pz2watch:activeclientsFieldRecord", new ActiveclientsRecordListener());\r
-    if (trackHistory) {\r
-        //console.log("Starts tracking windowlocationhash field");\r
-        fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener());\r
-        //console.log("Setting listener for browser onhashchange");\r
-        window.onload = window.onhashchange = windowlocationhashListener;\r
-    }\r
-  };
\ No newline at end of file
diff --git a/src/META-INF/resources/pz2utils/listeners.js b/src/META-INF/resources/pz2utils/listeners.js
new file mode 100644 (file)
index 0000000..80e1bc5
--- /dev/null
@@ -0,0 +1,166 @@
+  var renderTargetsReqVar;\r
+  var renderOnRecordTargetsReqVar;\r
+    \r
+  function renderTargets(doRefresh)\r
+  {\r
+       //console.log('rendering ' + renderWhileActiveclients);\r
+       var sourcecomp = document.getElementById("pz2watch:activeclientsField");\r
+    jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"});\r
+    if (doRefresh) {\r
+               //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField');  \r
+           renderTargetsReqVar=setTimeout(\r
+            function() {              \r
+              //console.log('Making request for pz2watch:activeclientsField');\r
+              jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"});       \r
+            }\r
+            ,500);\r
+    } else {\r
+       //console.log("No further updates from server requested");\r
+    }\r
+  }\r
+  \r
+  function renderOnRecordTargets(doRefresh)\r
+  {\r
+       console.log('rendering ' + renderWhileActiveclientsRecord);\r
+       var sourcecomp = document.getElementById("pz2watch:activeclientsFieldRecord");\r
+    jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclientsRecord});\r
+    if (doRefresh) {\r
+               console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsFieldRecord');  \r
+           renderOnRecordTargetsReqVar=setTimeout(\r
+            function() {              \r
+              console.log('Making request for pz2watch:activeclientsFieldRecord');\r
+              jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsFieldRecord"});       \r
+            }\r
+            ,1000);\r
+    } else {\r
+       //console.log("No further updates from server requested");\r
+    }\r
+  }\r
+\r
+\r
+  function windowlocationhashListener () {\r
+         if (trackHistory) {\r
+             //console.log("browser hash update detected");\r
+             var stateKey = document.getElementById("pz2watch:windowlocationhash");\r
+             if (window.location.hash != stateKey.value) {\r
+               //console.log("updating stateKey with new browser hash: " + window.location.hash);\r
+               stateKey.value = window.location.hash;\r
+               if (! stateKey.value) window.location.hash = '#initial';\r
+               stateKey.onchange();\r
+             } else {\r
+               //console.log("State hash already has the value of the new browser hash - not updating state hash");\r
+             }           \r
+         }       \r
+  }\r
+  \r
+  function viewExpirationListener (data) {\r
+         if (data.status === "success" && data.responseXML) {  \r
+                 var errorElements = data.responseXML.getElementsByTagName("error-name");\r
+                 if (errorElements.length>0) {\r
+                         var errorname = errorElements.item(0).textContent || errorElements.item(0).text;\r
+                         if (errorname === "class javax.faces.application.ViewExpiredException") {\r
+                                 var newloc = window.location.protocol + "//" + window.location.host + window.location.pathname.replace(/;jsessionid.*/,'');\r
+                                 alert('Sorry, but this session has expired, will load a new one for you.');\r
+                                 window.location.replace(newloc);\r
+                         }                       \r
+                 }\r
+         }\r
+\r
+  }\r
+\r
+  function fieldUpdateListener (data) {\r
+         if (data.status === "success") {\r
+               var updates = data.responseXML.getElementsByTagName("update");\r
+               for (var i=0, max=updates.length; i<max; i++) {\r
+                       var lsnri = fieldListeners.getListener(updates[i].getAttribute("id"));\r
+                       if (lsnri) {\r
+                               lsnri.invoke(updates[i]);\r
+                       }\r
+               }\r
+         } \r
+  }\r
+        \r
+  var Pz2listeners = function () {\r
+       var lsnrs = {};\r
+       this.addListener = function (key, lsnr) {\r
+               lsnrs[key] =lsnr;\r
+       };\r
+       this.getListener = function (key) {\r
+               return lsnrs[key];\r
+       };\r
+  };\r
+\r
+  var fieldListeners = new Pz2listeners();\r
+\r
+  var StateListener = function () {\r
+       this.invoke = function (field) {\r
+      var stateKeyDoc = StringtoXML(field.textContent || field.text);\r
+      var stateKeyValue = stateKeyDoc.childNodes[0].getAttribute("value");\r
+      //console.log('Application hash update detected. New value: ' + stateKeyValue);\r
+      if (stateKeyValue !== window.location.hash) {\r
+        window.location.hash = stateKeyValue;\r
+        //console.log("Browsers hash updated accordingly.");\r
+      } else {\r
+        //console.log("Browsers hash already has the value of the state hash. Not updating browser hash."); \r
+      }\r
+\r
+       };\r
+  };\r
+  \r
+  var ActiveclientsListener = function () {\r
+    this.invoke = function (field) {\r
+      var updateDoc = StringtoXML(field.textContent || field.text);\r
+      var activeClientsValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);\r
+      //console.log('Activeclients response detected: ' + activeClientsValue);\r
+      clearTimeout(renderTargetsReqVar);\r
+      if (activeClientsValue > '0') {\r
+        renderTargets(true);\r
+      } else {\r
+       renderTargets(false);\r
+      }\r
+       };\r
+  };\r
+  \r
+  var ActiveclientsRecordListener = function () {\r
+           this.invoke = function (field) {\r
+             var updateDoc = StringtoXML(field.textContent || field.text);\r
+             var activeClientsRecordValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);\r
+             console.log('Activeclients response for record detected: ' + activeClientsRecordValue);\r
+             clearTimeout(renderOnRecordTargetsReqVar);\r
+             if (activeClientsRecordValue > '0') {\r
+               renderOnRecordTargets(true);\r
+             } else {\r
+               console.log('Active clients is 0, final rendering');\r
+               renderOnRecordTargets(false);\r
+             }\r
+               };\r
+         };\r
+\r
+    \r
+  jsf.ajax.addOnEvent(fieldUpdateListener);\r
+  jsf.ajax.addOnEvent(viewExpirationListener);\r
+  \r
+  function StringtoXML(text){\r
+               var doc;\r
+           if (window.ActiveXObject){\r
+             doc=new ActiveXObject('Microsoft.XMLDOM');\r
+             doc.async=false;\r
+             doc.loadXML(text);\r
+           } else {\r
+             var parser=new DOMParser();\r
+             doc=parser.parseFromString(text,'text/xml');\r
+           }\r
+           return doc;\r
+  }\r
+\r
+  var setUpListeners = function () {\r
+    //console.log("Starts tracking activeclientsField");\r
+    fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener());\r
+    fieldListeners.addListener("pz2watch:activeclientsFieldRecord", new ActiveclientsRecordListener());\r
+    if (trackHistory) {\r
+        //console.log("Starts tracking windowlocationhash field");\r
+        fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener());\r
+        //console.log("Setting listener for browser onhashchange");\r
+        window.onload = window.onhashchange = windowlocationhashListener;\r
+    }\r
+  };
\ No newline at end of file
index 39706dd..fbdd66d 100644 (file)
@@ -21,9 +21,9 @@
 \r
   <cc:implementation>\r
     <h:outputScript name="jsf.js" library="javax.faces" target="head" />\r
-    <h:outputScript name="fieldlisteners.js" library="pz2utils" target="head" />\r
+    <h:outputScript name="listeners.js" library="pz2utils" target="head" />\r
     \r
-    <!-- Initiate Ajax update listeners -->  \r
+    <!-- Initiate Ajax listeners -->  \r
     <script>\r
      //<![CDATA[\r
        var renderWhileActiveclients = "${cc.attrs.renderWhileActiveclients}";\r