X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2FMETA-INF%2Fresources%2Fpz2utils%2Flisteners.js;fp=src%2FMETA-INF%2Fresources%2Fpz2utils%2Flisteners.js;h=80e1bc59731771ac8cc02a9c46314217eaaad987;hb=1c8dd02f257be7e640560b5a339b87c69a370b56;hp=0000000000000000000000000000000000000000;hpb=a81f30fcf2c5f425fa9de23d0e4eece3694b16f4;p=mkjsf-moved-to-github.git 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