@@ -370,8 +370,11 @@ this.xhr = false; if (window.XMLHttpRequest) this.xhr = new XMLHttpRequest(); else this.xhr = new ActiveXObject("Microsoft.XMLHTTP"); - this.hook = ''; // The ID of the HTML element in the page that will be - // replaced by result of executing the Ajax request. + this.hook = ''; /* The ID of the HTML element in the page that will be + replaced by result of executing the Ajax request. */ + this.onGet = ''; /* The name of a Javascript function to call once we + receive the result. */ + this.info = {}; /* An associative array for putting anything else. */ } function getAjaxChunk(pos) { @@ -391,15 +394,35 @@ var hookElem = document.getElementById(hook); if (hookElem && (xhrObjects[pos].xhr.status == 200)) { hookElem.innerHTML = xhrObjects[pos].xhr.responseText; + // Call a custom Javascript function if required + if (xhrObjects[pos].onGet) { + xhrObjects[pos].onGet(xhrObjects[pos], hookElem); + } } xhrObjects[pos].freed = 1; } } } - function askAjaxChunk(hook, url) { - // This function will ask to get a chunk of HTML on the server by - // triggering a XMLHttpRequest. + function askAjaxChunk(hook,mode,url,page,macro,params,beforeSend,onGet) { + /* This function will ask to get a chunk of HTML on the server through a + XMLHttpRequest. p_mode can be 'GET' or 'POST'. p_url is the URL of a + given server object. On this URL we will call the page "ajax.pt" that + will call a specific p_macro in a given p_page with some additional + p_params (must be an associative array) if required. + + p_hook is the ID of the HTML element that will be filled with the HTML + result from the server. + + p_beforeSend is a Javascript function to call before sending the request. + This function will get 2 args: the XMLHttpRequest object and the + p_params. This method can return, in a string, additional parameters to + send, ie: "¶m1=blabla¶m2=blabla". + + p_onGet is a Javascript function to call when we will receive the answer. + This function will get 2 args, too: the XMLHttpRequest object and the + HTML node element into which the result has been inserted. + */ // First, get a non-busy XMLHttpRequest object. var pos = -1; for (var i=0; i < xhrObjects.length; i++) { @@ -410,16 +433,86 @@ xhrObjects[pos] = new XhrObject(); } xhrObjects[pos].hook = hook; + xhrObjects[pos].onGet = onGet; if (xhrObjects[pos].xhr) { - xhrObjects[pos].freed = 0; - // Perform the asynchronous HTTP GET - xhrObjects[pos].xhr.open('GET', url, true); - xhrObjects[pos].xhr.onreadystatechange = function() { getAjaxChunk(pos); } - if (window.XMLHttpRequest) { xhrObjects[pos].xhr.send(null); } - else if (window.ActiveXObject) { xhrObjects[pos].xhr.send(); } + var rq = xhrObjects[pos]; + rq.freed = 0; + // Construct parameters + var paramsFull = 'page=' + page + '¯o=' + macro; + if (params) { + for (var paramName in params) + paramsFull = paramsFull + '&' + paramName + '=' + params[paramName]; + } + // Call beforeSend if required + if (beforeSend) { + var res = beforeSend(rq, params); + if (res) paramsFull = paramsFull + res; + } + // Construct the URL to call + var urlFull = url + '/skyn/ajax'; + if (mode == 'GET') { + urlFull = urlFull + '?' + paramsFull; + } + // Perform the asynchronous HTTP GET or POST + rq.xhr.open(mode, urlFull, true); + if (mode == 'POST') { + // Set the correct HTTP headers + rq.xhr.setRequestHeader( + "Content-Type", "application/x-www-form-urlencoded"); + rq.xhr.setRequestHeader("Content-length", paramsFull.length); + rq.xhr.setRequestHeader("Connection", "close"); + rq.xhr.onreadystatechange = function(){ getAjaxChunk(pos); } + rq.xhr.send(paramsFull); + } + else if (mode == 'GET') { + rq.xhr.onreadystatechange = function() { getAjaxChunk(pos); } + if (window.XMLHttpRequest) { rq.xhr.send(null); } + else if (window.ActiveXObject) { rq.xhr.send(); } + } } } + /* The functions below wrap askAjaxChunk for getting specific content through + an Ajax request. */ + function askQueryResult(hookId, objectUrl, contentType, flavourNumber, + searchName, startNumber, sortKey, sortOrder, filterKey) { + // Sends an Ajax request for getting the result of a query. + var params = {'type_name': contentType, 'flavourNumber': flavourNumber, + 'search': searchName, 'startNumber': startNumber}; + if (sortKey) params['sortKey'] = sortKey; + if (sortOrder) params['sortOrder'] = sortOrder; + if (filterKey) { + var filterWidget = document.getElementById(hookId + '_' + filterKey); + if (filterWidget && filterWidget.value) { + params['filterKey'] = filterKey; + params['filterValue'] = filterWidget.value; + } + } + askAjaxChunk(hookId,'GET',objectUrl,'macros','queryResult',params); + } + + function askObjectHistory(hookId, objectUrl, startNumber) { + // Sends an Ajax request for getting the history of an object + var params = {'startNumber': startNumber}; + askAjaxChunk(hookId, 'GET', objectUrl, 'macros', 'history', params); + } + + function askRefField(hookId, objectUrl, fieldName, isBack, innerRef, labelId, + descrId, startNumber, action, actionParams){ + // Sends an Ajax request for getting the content of a reference field. + var startKey = hookId + '_startNumber'; + var params = {'fieldName': fieldName, 'isBack': isBack, + 'innerRef': innerRef, 'labelId': labelId, + 'descrId': descrId }; + params[startKey] = startNumber; + if (action) params['action'] = action; + if (actionParams) { + for (key in actionParams) { params[key] = actionParams[key]; }; + } + askAjaxChunk(hookId, 'GET', objectUrl, 'ref', 'showReferenceContent', + params); + } + // Function used by checkbox widgets for having radio-button-like behaviour function toggleCheckbox(visibleCheckbox, hiddenBoolean) { vis = document.getElementById(visibleCheckbox); @@ -633,10 +726,9 @@ |
-
-
@@ -655,7 +747,7 @@
|
---|
- | - | - - - // - - | -- | - |