'From Squeak3.9alpha of 4 July 2005 [latest update: #7051] on 27 October 2006 at 7:05:40 pm'!
!WAStandardScripts methodsFor: '*seasideasync' stamp: 'RJT 10/27/2006 19:04'!
ajax
^ '
function liveUpdater(uriFunc, ret)
{
var request = false;
function xmlUpdater()
{
return sendHttpCallback(uriFunc(), ret);
}
return xmlUpdater;
// Build and send HTTP request to Seaside
function sendHttpCallback(uri, ret, respFunc)
{
if (request && request.readyState < 4)
request.abort();
request = newHttpRequest();
if (!!request)
return ret;
if (!!respFunc)
respFunc = processRequestChange;
request.onreadystatechange = respFunc;
// Resolve xml entity before issuing the request
var uri = addParameter(uri, "timestamp", (new Date()).getTime().toString());
var regex = new RegExp("&", "g");
uri = uri.replace(regex,"&");
request.open("GET", uri, true);
request.send(null);
return ret;
}
// Parse HTTP response from the Seaside live callbacks as XML and update the document
function processRequestChange()
{
if (request.readyState == 4) {
var doc = request.responseXML;
var replacements = [];
var anyReplacement = false;
// alert( request.responseText);
// alert( request.status);
// alert( request.statusText);
// alert( request.getAllResponseHeaders() );
// alert( request.responseXML.parseError.errorCode);
// alert( request.responseXML.parseError.reason);
if (doc !!= null) {
var elements = doc.getElementsByTagName("*");
if (elements.length == 0) {
// IE does not support the above, use an alternative hard-coded technique
elements = getAllElements(doc)
}
// Find html elements with the id= attribute
for ( var x = 1 ; x <= elements.length ; x++ ) {
var elem = elements[x-1];
if (elem.tagName == "body") {
var body = xmlString(elem);
}
var attributes = elem.attributes;
if (attributes.length > 0) {
for ( var ax = 1 ; ax <= attributes.length ; ax++ ) {
var attr = attributes[ax-1];
var attrName = attr.name;
if (attrName == "id") {
// Find the html element in the document that has a matching id and replace it
var docElement = document.getElementById(attr.value);
if (docElement) {
if (!!includes(replacements,elem)) {
// HTML is only updated with the parent at highest level
// Resolve IE and Opera issue where
skips two lines
var regex = new RegExp("
", "g");
var replacement = xmlString(elem).replace(regex,"
");
try {
docElement.innerHTML = replacement;
anyReplacement = true;
replacements[replacements.length] = attr.value;
} catch (e) {
// Some browsers (like Safari) have read-only elements
}
}
}
}
}
}
}
// Find html