Packaging pazpar2/io error messages in response objects
[mkjsf-moved-to-github.git] / src / META-INF / resources / pz2utils / fieldlisteners.js
1   var renderTargetsReqVar;\r
2     \r
3   function renderTargets(doRefresh)\r
4   {\r
5         //console.log('rendering ' + renderWhileActiveclients);\r
6         var sourcecomp = document.getElementById("pz2watch:activeclientsField");\r
7     jsf.ajax.request(sourcecomp, null,{render: renderWhileActiveclients + " pz2watch:errorMessages"});\r
8     if (doRefresh) {\r
9                 //console.log('Will do another ajax request after a timeout in order to render: pz2watch:activeclientsField');  \r
10             renderTargetsReqVar=setTimeout(\r
11              function() {              \r
12                //console.log('Making request for pz2watch:activeclientsField');\r
13                jsf.ajax.request(sourcecomp, null,{render: "pz2watch:activeclientsField"});       \r
14              }\r
15              ,500);\r
16     } else {\r
17         //console.log("No further updates from server requested");\r
18     }\r
19   }\r
20 \r
21   function windowlocationhashListener () {\r
22           if (trackHistory) {\r
23               //console.log("browser hash update detected");\r
24               var stateKey = document.getElementById("pz2watch:windowlocationhash");\r
25               if (window.location.hash != stateKey.value) {\r
26                 //console.log("updating stateKey with new browser hash: " + window.location.hash);\r
27                 stateKey.value = window.location.hash;\r
28                 if (! stateKey.value) window.location.hash = '#initial';\r
29                 stateKey.onchange();\r
30               } else {\r
31                 //console.log("State hash already has the value of the new browser hash - not updating state hash");\r
32               }           \r
33           }       \r
34   }      \r
35 \r
36   function fieldUpdateListener (data) {\r
37           if (data.status === "success") {\r
38                 var updates = data.responseXML.getElementsByTagName("update");\r
39                 for (var i=0, max=updates.length; i<max; i++) {\r
40                         var lsnri = fieldListeners.getListener(updates[i].getAttribute("id"));\r
41                         if (lsnri) {\r
42                                 lsnri.invoke(updates[i]);\r
43                         }\r
44                 }\r
45           }\r
46   }\r
47         \r
48   var Pz2listeners = function () {\r
49         var lsnrs = {};\r
50         this.addListener = function (key, lsnr) {\r
51                 lsnrs[key] =lsnr;\r
52         };\r
53         this.getListener = function (key) {\r
54                 return lsnrs[key];\r
55         };\r
56   };\r
57 \r
58   var fieldListeners = new Pz2listeners();\r
59 \r
60   var StateListener = function () {\r
61         this.invoke = function (field) {\r
62       var stateKeyDoc = StringtoXML(field.textContent || field.text);\r
63       var stateKeyValue = stateKeyDoc.childNodes[0].getAttribute("value");\r
64       //console.log('Application hash update detected. New value: ' + stateKeyValue);\r
65       if (stateKeyValue != window.location.hash) {\r
66         window.location.hash = stateKeyValue;\r
67         //console.log("Browsers hash updated accordingly.");\r
68       } else {\r
69         //console.log("Browsers hash already has the value of the state hash. Not updating browser hash."); \r
70       }\r
71 \r
72         };\r
73   };\r
74   \r
75   var ActiveclientsListener = function () {\r
76     this.invoke = function (field) {\r
77       var updateDoc = StringtoXML(field.textContent || field.text);\r
78       var activeClientsValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text);\r
79       //console.log('Activeclients response detected: ' + activeClientsValue);\r
80       clearTimeout(renderTargetsReqVar);\r
81       if (activeClientsValue > '0') {\r
82         renderTargets(true);\r
83       } else {\r
84         renderTargets(false);\r
85       }\r
86         };\r
87   };\r
88     \r
89   jsf.ajax.addOnEvent(fieldUpdateListener);\r
90   \r
91   function StringtoXML(text){\r
92                 var doc;\r
93             if (window.ActiveXObject){\r
94               doc=new ActiveXObject('Microsoft.XMLDOM');\r
95               doc.async=false;\r
96               doc.loadXML(text);\r
97             } else {\r
98               var parser=new DOMParser();\r
99               doc=parser.parseFromString(text,'text/xml');\r
100             }\r
101             return doc;\r
102   }\r
103 \r
104   var setUpListeners = function () {\r
105     //console.log("Starts tracking activeclientsField");\r
106     fieldListeners.addListener("pz2watch:activeclientsField", new ActiveclientsListener());\r
107     if (trackHistory) {\r
108         //console.log("Starts tracking windowlocationhash field");\r
109         fieldListeners.addListener("pz2watch:windowlocationhash", new StateListener());\r
110         //console.log("Setting listener for browser onhashchange");\r
111         window.onload = window.onhashchange = windowlocationhashListener;\r
112     }\r
113   };