From: Niels Erik G. Nielsen Date: Thu, 11 Apr 2013 15:56:55 +0000 (-0400) Subject: Adds Ajax listener to detect view expirations X-Git-Tag: v0.0.7~179 X-Git-Url: http://git.indexdata.com/?a=commitdiff_plain;h=1c8dd02f257be7e640560b5a339b87c69a370b56;hp=a81f30fcf2c5f425fa9de23d0e4eece3694b16f4;p=mkjsf-moved-to-github.git Adds Ajax listener to detect view expirations .. and reload page, stripped of its jsessionid and hash content --- diff --git a/src/META-INF/resources/pz2utils/fieldlisteners.js b/src/META-INF/resources/pz2utils/fieldlisteners.js deleted file mode 100644 index b0a3e6a..0000000 --- a/src/META-INF/resources/pz2utils/fieldlisteners.js +++ /dev/null @@ -1,150 +0,0 @@ - var renderTargetsReqVar; - var renderOnRecordTargetsReqVar; - - function renderTargets(doRefresh) - { - //console.log('rendering ' + renderWhileActiveclients); - var sourcecomp = document.getElementById("pz2watch:activeclientsField"); - jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"}); - if (doRefresh) { - //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField'); - renderTargetsReqVar=setTimeout( - function() { - //console.log('Making request for pz2watch:activeclientsField'); - jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"}); - } - ,500); - } else { - //console.log("No further updates from server requested"); - } - } - - function renderOnRecordTargets(doRefresh) - { - console.log('rendering ' + renderWhileActiveclientsRecord); - var sourcecomp = document.getElementById("pz2watch:activeclientsFieldRecord"); - jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclientsRecord}); - if (doRefresh) { - console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsFieldRecord'); - renderOnRecordTargetsReqVar=setTimeout( - function() { - console.log('Making request for pz2watch:activeclientsFieldRecord'); - jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsFieldRecord"}); - } - ,1000); - } else { - //console.log("No further updates from server requested"); - } - } - - - function windowlocationhashListener () { - if (trackHistory) { - //console.log("browser hash update detected"); - var stateKey = document.getElementById("pz2watch:windowlocationhash"); - if (window.location.hash != stateKey.value) { - //console.log("updating stateKey with new browser hash: " + window.location.hash); - stateKey.value = window.location.hash; - if (! stateKey.value) window.location.hash = '#initial'; - stateKey.onchange(); - } else { - //console.log("State hash already has the value of the new browser hash - not updating state hash"); - } - } - } - - function fieldUpdateListener (data) { - if (data.status === "success") { - var updates = data.responseXML.getElementsByTagName("update"); - for (var i=0, max=updates.length; i '0') { - renderTargets(true); - } else { - renderTargets(false); - } - }; - }; - - var ActiveclientsRecordListener = function () { - this.invoke = function (field) { - var updateDoc = StringtoXML(field.textContent || field.text); - var activeClientsRecordValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text); - console.log('Activeclients response for record detected: ' + activeClientsRecordValue); - clearTimeout(renderOnRecordTargetsReqVar); - if (activeClientsRecordValue > '0') { - renderOnRecordTargets(true); - } else { - console.log('Active clients is 0, final rendering'); - renderOnRecordTargets(false); - } - }; - }; - - - jsf.ajax.addOnEvent(fieldUpdateListener); - - function StringtoXML(text){ - var doc; - if (window.ActiveXObject){ - doc=new ActiveXObject('Microsoft.XMLDOM'); - doc.async=false; - doc.loadXML(text); - } else { - var parser=new DOMParser(); - doc=parser.parseFromString(text,'text/xml'); - } - return doc; - } - - var setUpListeners = function () { - //console.log("Starts tracking activeclientsField"); - fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener()); - fieldListeners.addListener("pz2watch:activeclientsFieldRecord", new ActiveclientsRecordListener()); - if (trackHistory) { - //console.log("Starts tracking windowlocationhash field"); - fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener()); - //console.log("Setting listener for browser onhashchange"); - window.onload = window.onhashchange = windowlocationhashListener; - } - }; \ 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 index 0000000..80e1bc5 --- /dev/null +++ b/src/META-INF/resources/pz2utils/listeners.js @@ -0,0 +1,166 @@ + var renderTargetsReqVar; + var renderOnRecordTargetsReqVar; + + function renderTargets(doRefresh) + { + //console.log('rendering ' + renderWhileActiveclients); + var sourcecomp = document.getElementById("pz2watch:activeclientsField"); + jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"}); + if (doRefresh) { + //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField'); + renderTargetsReqVar=setTimeout( + function() { + //console.log('Making request for pz2watch:activeclientsField'); + jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"}); + } + ,500); + } else { + //console.log("No further updates from server requested"); + } + } + + function renderOnRecordTargets(doRefresh) + { + console.log('rendering ' + renderWhileActiveclientsRecord); + var sourcecomp = document.getElementById("pz2watch:activeclientsFieldRecord"); + jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclientsRecord}); + if (doRefresh) { + console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsFieldRecord'); + renderOnRecordTargetsReqVar=setTimeout( + function() { + console.log('Making request for pz2watch:activeclientsFieldRecord'); + jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsFieldRecord"}); + } + ,1000); + } else { + //console.log("No further updates from server requested"); + } + } + + + function windowlocationhashListener () { + if (trackHistory) { + //console.log("browser hash update detected"); + var stateKey = document.getElementById("pz2watch:windowlocationhash"); + if (window.location.hash != stateKey.value) { + //console.log("updating stateKey with new browser hash: " + window.location.hash); + stateKey.value = window.location.hash; + if (! stateKey.value) window.location.hash = '#initial'; + stateKey.onchange(); + } else { + //console.log("State hash already has the value of the new browser hash - not updating state hash"); + } + } + } + + function viewExpirationListener (data) { + if (data.status === "success" && data.responseXML) { + var errorElements = data.responseXML.getElementsByTagName("error-name"); + if (errorElements.length>0) { + var errorname = errorElements.item(0).textContent || errorElements.item(0).text; + if (errorname === "class javax.faces.application.ViewExpiredException") { + var newloc = window.location.protocol + "//" + window.location.host + window.location.pathname.replace(/;jsessionid.*/,''); + alert('Sorry, but this session has expired, will load a new one for you.'); + window.location.replace(newloc); + } + } + } + + } + + function fieldUpdateListener (data) { + if (data.status === "success") { + var updates = data.responseXML.getElementsByTagName("update"); + for (var i=0, max=updates.length; i '0') { + renderTargets(true); + } else { + renderTargets(false); + } + }; + }; + + var ActiveclientsRecordListener = function () { + this.invoke = function (field) { + var updateDoc = StringtoXML(field.textContent || field.text); + var activeClientsRecordValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text); + console.log('Activeclients response for record detected: ' + activeClientsRecordValue); + clearTimeout(renderOnRecordTargetsReqVar); + if (activeClientsRecordValue > '0') { + renderOnRecordTargets(true); + } else { + console.log('Active clients is 0, final rendering'); + renderOnRecordTargets(false); + } + }; + }; + + + jsf.ajax.addOnEvent(fieldUpdateListener); + jsf.ajax.addOnEvent(viewExpirationListener); + + function StringtoXML(text){ + var doc; + if (window.ActiveXObject){ + doc=new ActiveXObject('Microsoft.XMLDOM'); + doc.async=false; + doc.loadXML(text); + } else { + var parser=new DOMParser(); + doc=parser.parseFromString(text,'text/xml'); + } + return doc; + } + + var setUpListeners = function () { + //console.log("Starts tracking activeclientsField"); + fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener()); + fieldListeners.addListener("pz2watch:activeclientsFieldRecord", new ActiveclientsRecordListener()); + if (trackHistory) { + //console.log("Starts tracking windowlocationhash field"); + fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener()); + //console.log("Setting listener for browser onhashchange"); + window.onload = window.onhashchange = windowlocationhashListener; + } + }; \ No newline at end of file diff --git a/src/META-INF/resources/pz2utils/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml index 39706dd..fbdd66d 100644 --- a/src/META-INF/resources/pz2utils/pz2watch.xhtml +++ b/src/META-INF/resources/pz2utils/pz2watch.xhtml @@ -21,9 +21,9 @@ - + - +