1 var renderTargetsReqVar;
\r
2 var renderOnRecordTargetsReqVar;
\r
4 function renderTargets(doRefresh)
\r
6 //console.log('rendering ' + renderWhileActiveclients);
\r
7 var sourcecomp = document.getElementById("pz2watch:activeclientsField");
\r
8 jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"});
\r
10 //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField');
\r
11 renderTargetsReqVar=setTimeout(
\r
13 //console.log('Making request for pz2watch:activeclientsField');
\r
14 jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"});
\r
18 //console.log("No further updates from server requested");
\r
22 function renderOnRecordTargets(doRefresh)
\r
24 console.log('rendering ' + renderWhileActiveclientsRecord);
\r
25 var sourcecomp = document.getElementById("pz2watch:activeclientsFieldRecord");
\r
26 jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclientsRecord});
\r
28 console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsFieldRecord');
\r
29 renderOnRecordTargetsReqVar=setTimeout(
\r
31 console.log('Making request for pz2watch:activeclientsFieldRecord');
\r
32 jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsFieldRecord"});
\r
36 //console.log("No further updates from server requested");
\r
41 function windowlocationhashListener () {
\r
43 //console.log("browser hash update detected");
\r
44 var stateKey = document.getElementById("pz2watch:windowlocationhash");
\r
45 if (window.location.hash != stateKey.value) {
\r
46 //console.log("updating stateKey with new browser hash: " + window.location.hash);
\r
47 stateKey.value = window.location.hash;
\r
48 if (! stateKey.value) window.location.hash = '#initial';
\r
49 stateKey.onchange();
\r
51 //console.log("State hash already has the value of the new browser hash - not updating state hash");
\r
56 function viewExpirationListener (data) {
\r
57 if (data.status === "success" && data.responseXML) {
\r
58 var errorElements = data.responseXML.getElementsByTagName("error-name");
\r
59 if (errorElements.length>0) {
\r
60 var errorname = errorElements.item(0).textContent || errorElements.item(0).text;
\r
61 if (errorname === "class javax.faces.application.ViewExpiredException") {
\r
62 var newloc = window.location.protocol + "//" + window.location.host + window.location.pathname.replace(/;jsessionid.*/,'');
\r
63 alert('Sorry, but this session has expired, will load a new one for you.');
\r
64 window.location.replace(newloc);
\r
71 function fieldUpdateListener (data) {
\r
72 if (data.status === "success") {
\r
73 var updates = data.responseXML.getElementsByTagName("update");
\r
74 for (var i=0, max=updates.length; i<max; i++) {
\r
75 var lsnri = fieldListeners.getListener(updates[i].getAttribute("id"));
\r
77 lsnri.invoke(updates[i]);
\r
83 var Pz2listeners = function () {
\r
85 this.addListener = function (key, lsnr) {
\r
88 this.getListener = function (key) {
\r
93 var fieldListeners = new Pz2listeners();
\r
95 var StateListener = function () {
\r
96 this.invoke = function (field) {
\r
97 var stateKeyDoc = StringtoXML(field.textContent || field.text);
\r
98 var stateKeyValue = stateKeyDoc.childNodes[0].getAttribute("value");
\r
99 //console.log('Application hash update detected. New value: ' + stateKeyValue);
\r
100 if (stateKeyValue !== window.location.hash) {
\r
101 window.location.hash = stateKeyValue;
\r
102 //console.log("Browsers hash updated accordingly.");
\r
104 //console.log("Browsers hash already has the value of the state hash. Not updating browser hash.");
\r
110 var ActiveclientsListener = function () {
\r
111 this.invoke = function (field) {
\r
112 var updateDoc = StringtoXML(field.textContent || field.text);
\r
113 var activeClientsValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);
\r
114 //console.log('Activeclients response detected: ' + activeClientsValue);
\r
115 clearTimeout(renderTargetsReqVar);
\r
116 if (activeClientsValue > '0') {
\r
117 renderTargets(true);
\r
119 renderTargets(false);
\r
124 var ActiveclientsRecordListener = function () {
\r
125 this.invoke = function (field) {
\r
126 var updateDoc = StringtoXML(field.textContent || field.text);
\r
127 var activeClientsRecordValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);
\r
128 console.log('Activeclients response for record detected: ' + activeClientsRecordValue);
\r
129 clearTimeout(renderOnRecordTargetsReqVar);
\r
130 if (activeClientsRecordValue > '0') {
\r
131 renderOnRecordTargets(true);
\r
133 console.log('Active clients is 0, final rendering');
\r
134 renderOnRecordTargets(false);
\r
140 jsf.ajax.addOnEvent(fieldUpdateListener);
\r
141 jsf.ajax.addOnEvent(viewExpirationListener);
\r
143 function StringtoXML(text){
\r
145 if (window.ActiveXObject){
\r
146 doc=new ActiveXObject('Microsoft.XMLDOM');
\r
150 var parser=new DOMParser();
\r
151 doc=parser.parseFromString(text,'text/xml');
\r
156 var setUpListeners = function () {
\r
157 //console.log("Starts tracking activeclientsField");
\r
158 fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener());
\r
159 fieldListeners.addListener("pz2watch:activeclientsFieldRecord", new ActiveclientsRecordListener());
\r
160 if (trackHistory) {
\r
161 //console.log("Starts tracking windowlocationhash field");
\r
162 fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener());
\r
163 //console.log("Setting listener for browser onhashchange");
\r
164 window.onload = window.onhashchange = windowlocationhashListener;
\r