X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2FMETA-INF%2Fresources%2Fpz2utils%2Flisteners.js;h=b46014e7e4a3e1888e84220169d3d97b3b178640;hb=f969f221326787617bd4a8766bed1942d010eae0;hp=8c1f46670f7d1e8a49ef8ee5923bef088c86cb4e;hpb=906df9a7a07c860de28e13cf3769361d4aaa2742;p=mkjsf-moved-to-github.git diff --git a/src/META-INF/resources/pz2utils/listeners.js b/src/META-INF/resources/pz2utils/listeners.js index 8c1f466..b46014e 100644 --- a/src/META-INF/resources/pz2utils/listeners.js +++ b/src/META-INF/resources/pz2utils/listeners.js @@ -1,180 +1,229 @@ - var renderTargetsReqVar; - var renderOnRecordTargetsReqVar; - - // Renders UI elements listed in 'renderWhileActiveclients', optionally doing - // another update round-trip to Pazpar2 when done. - 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"); - } +var renderTargetsReqVar; +var renderOnRecordTargetsReqVar; + +// Renders UI elements listed in 'renderWhileActiveclients', optionally doing +// another update round-trip to Pazpar2 when done. +function renderTargets(doRefresh) { + // console.log('rendering ' + renderWhileActiveclients); + var sourcecomp = document.getElementById("pz2watch:activeclientsField"); + jsf.ajax.request(sourcecomp, null, { + render : renderWhileActiveclients + }); + 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"); + } +} + +// Renders UI elements listed in 'renderWhileActiveclientsRecord', optionally +// doing +// another record request when done. +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"); } - - // Renders UI elements listed in 'renderWhileActiveclientsRecord', optionally doing - // another record request when done. - 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); +} + +// Listens for browser initiated changes to 'window.location.hash' and sends +// the hash key changes to the back-end, to have the back-end pull up a previous +// Pazpar2 state. +// +// See also: The field in pz2watch.xhtml, the StateListener function below, +// the method Pz2Bean.handleQueryStateChanges(), and the classes +// Pazpar2State and StateManager for a complete picture of browser history +// handling. +function windowlocationhashListener() { + if (trackHistory) { + + var stateKey = document.getElementById("pz2watch:windowlocationhash"); + // console.log("browser hash update response detected."); + // console.log("pz2watch:windowlocationhash: [" + stateKey.value + "]"); + // console.log("window.location.hash: [" + window.location.hash + "]"); + if (window.location.hash != stateKey.value) { + if (window.location.hash) { + //console.log("updating pz2watch:windowlocationhash with new window.location.hash [" + window.location.hash + "]"); + stateKey.value = window.location.hash; + //console.log("firing pz2watch:windowlocationhash onChange"); + stateKey.onchange(); + } else if (stateKey.value) { + //console.log("updating window.location.hash with pz2watch:windowlocationhash [" + stateKey.value + "]"); + window.location.hash = stateKey.value; + //console.log("firing pz2watch:windowlocationhash onChange"); + stateKey.onchange(); + } } else { - //console.log("No further updates from server requested"); + //console.log("State hash already has the value of the new browser hash - not updating state hash"); } } +} - // Listens for browser initiated changes to 'window.location.hash' and sends the hash - // changes to the back-end (to have the back-end pull up a previous Pazpar2 state) - 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 = '#1'; - stateKey.onchange(); - } else { - //console.log("State hash already has the value of the new browser hash - not updating state hash"); - } - } +// Listens for ViewExpiredException message. Reloads the current page, stripped +// of +// it's jsessionid and hash content +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, this session has expired. A new one will be loaded.'); + window.location.replace(newloc); + } + } } - - // Listens for ViewExpiredException message. Reloads the current page, stripped of - // it's jsessionid and hash content - 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, this session has expired. A new one will be loaded.'); - window.location.replace(newloc); - } - } - } - + +} + +function errorListener(data) { + // alert(data.toSource()); + if (data.source.name === "pz2watch:activeclientsField") { + alert("This occurred when polling: [" + + data.description + + "]."); + } else { + var pattern = /viewId.*could not be restored./; + if (pattern.test(data.description)) { + // ignore - caught by view expiration listener + } else { + alert("An error [" + + data.errorName + ": " +data.description + + "] was triggered by [" + + data.source.name + + "]."); + } } +} - // Composite listener, invoking all field update listeners - 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); - } - }; +}; + +// Listens for updates to general 'activeclients' field, then invokes +// renderTargets +var ActiveclientsListener = function() { + this.invoke = function(field) { + var updateDoc = StringtoXML(field.textContent || field.text); + var activeClientsValue = (updateDoc.childNodes[0].textContent || updateDoc.childNodes[0].text); + // console.log('Activeclients response detected: ' + activeClientsValue); + clearTimeout(renderTargetsReqVar); + if (activeClientsValue > '0') { + renderTargets(true); + } else { + renderTargets(false); + } }; - - // Listens for updates to record 'activeclients' field, then invokes renderOnRecordTargets - 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); - } - }; - }; +}; - - // Inserts field update listeners, state listeners, view expired listener - // into Ajax response handling - 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; +// Listens for updates to record 'activeclients' field, then invokes +// renderOnRecordTargets +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); + } + }; +}; + +// Inserts field update listeners, state listeners, view expired listener +// into Ajax response handling +jsf.ajax.addOnEvent(fieldUpdateListener); +jsf.ajax.addOnEvent(viewExpirationListener); +jsf.ajax.addOnError(errorListener); + +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; +} - // Sets up field update listeners - 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 +// Sets up field update listeners +var setUpListeners = function() { + // console.log("Starts tracking activeclientsField, for 'show' and 'record'"); + 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