Adds pazpar2 listeners
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 28 Feb 2013 02:12:45 +0000 (21:12 -0500)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Thu, 28 Feb 2013 02:12:45 +0000 (21:12 -0500)
.gitignore [new file with mode: 0644]
pom.xml [new file with mode: 0644]
src/META-INF/MANIFEST.MF [new file with mode: 0644]
src/META-INF/resources/pz2utils/fieldlisteners.js [new file with mode: 0644]
src/META-INF/resources/pz2utils/pz2watch.xhtml [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..bf98bbd
--- /dev/null
@@ -0,0 +1,5 @@
+.classpath\r
+.project\r
+.settings/\r
+build/\r
+target/\r
diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..7d6dd9c
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,58 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\r
+  <modelVersion>4.0.0</modelVersion>\r
+  <groupId>com.indexdata</groupId>\r
+  <artifactId>pz2utils4jsf</artifactId>\r
+  <version>0.0.1-SNAPSHOT</version>\r
+  <name>pz2utils4jsf</name>\r
+  <packaging>jar</packaging>\r
+  <description>Components for building Pazpar2 application using JSF</description>\r
+    <dependencies>\r
+    <dependency>\r
+      <groupId>javax</groupId>\r
+      <artifactId>javaee-api</artifactId>\r
+      <version>6.0</version>\r
+      <scope>provided</scope>\r
+    </dependency>\r
+\r
+    <!-- JSF 2 -->\r
+    <dependency>\r
+      <groupId>org.glassfish</groupId>\r
+      <artifactId>javax.faces</artifactId>\r
+      <version>2.1.16</version>\r
+      <scope>compile</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>javax.servlet.jsp.jstl</groupId>\r
+      <artifactId>javax.servlet.jsp.jstl-api</artifactId>\r
+      <version>1.2.1</version>\r
+      <scope>compile</scope>\r
+    </dependency>\r
+    <dependency>\r
+      <groupId>com.indexdata</groupId>\r
+      <artifactId>masterkey-common</artifactId>\r
+      <version>0.1.9-SNAPSHOT</version>\r
+    </dependency>\r
+  </dependencies>\r
+\r
+  <build>\r
+    <sourceDirectory>src</sourceDirectory>\r
+    <resources>\r
+      <resource>\r
+        <directory>src</directory>\r
+        <excludes>\r
+          <exclude>**/*.java</exclude>\r
+        </excludes>\r
+      </resource>\r
+    </resources>\r
+    <plugins>\r
+      <plugin>\r
+        <artifactId>maven-compiler-plugin</artifactId>\r
+        <version>2.3.2</version>\r
+        <configuration>\r
+          <source>1.7</source>\r
+          <target>1.7</target>\r
+        </configuration>\r
+      </plugin>\r
+    </plugins>\r
+  </build>\r
+</project>
\ No newline at end of file
diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..5e94951
--- /dev/null
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0\r
+Class-Path: \r
+\r
diff --git a/src/META-INF/resources/pz2utils/fieldlisteners.js b/src/META-INF/resources/pz2utils/fieldlisteners.js
new file mode 100644 (file)
index 0000000..c5cf63a
--- /dev/null
@@ -0,0 +1,113 @@
+  var renderTargetsReqVar;\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});\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 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
+  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
+    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/pz2watch.xhtml b/src/META-INF/resources/pz2utils/pz2watch.xhtml
new file mode 100644 (file)
index 0000000..511aae1
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8" ?>\r
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r
+<h:html xmlns="http://www.w3.org/1999/xhtml"\r
+      xmlns:f="http://java.sun.com/jsf/core"\r
+      xmlns:h="http://java.sun.com/jsf/html"\r
+      xmlns:ui="http://java.sun.com/jsf/facelets"\r
+      xmlns:cc="http://java.sun.com/jsf/composite">\r
+\r
+<head>\r
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\r
+</head>\r
+<body>\r
+\r
+  <cc:interface>\r
+    <cc:attribute name="renderWhileActiveclients"/>\r
+    <cc:attribute name="trackHistory"/>\r
+    <cc:attribute name="renderOnHitoryChange"/> \r
+    <cc:attribute name="debug"/>   \r
+  </cc:interface>\r
+\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
+    \r
+    <!-- Initiate Ajax update listeners -->  \r
+    <script>\r
+     //<![CDATA[\r
+       var renderWhileActiveclients = "${cc.attrs.renderWhileActiveclients}";\r
+       var renderOnHistoryChange = "${cc.attrs.renderOnHistoryChange}";\r
+       var trackHistory = ("${cc.attrs.trackHistory}" == "true");  \r
+       setUpListeners();\r
+     //]]>\r
+    </script>  \r
+\r
+    <h:form id="activeclientsForm" prependId="false" style="${cc.attrs.debug == 'true' ? '' : 'display:none;'}">\r
+      Active clients: <h:outputText id="activeclientsField" value="${pz2.update()}" />\r
+    </h:form>\r
+\r
+    <h:form id="stateForm" prependId="false" rendered="${cc.attrs.trackHistory == 'true'}" style="${cc.attrs.debug == 'true' ? '' : 'display:none;'}">\r
+      State: \r
+      <h:inputText id="windowlocationhash" value="#{pz2.currentStateKey}" size="100">\r
+        <f:ajax event="change" render="#{cc.attrs.renderOnHistoryChange} #{pz2watch.activeclients}"/>\r
+      </h:inputText>\r
+    </h:form>\r
+  </cc:implementation>\r
+  \r
+</body>\r
+</h:html>
\ No newline at end of file