--- /dev/null
+<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
--- /dev/null
+ 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
--- /dev/null
+<?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