[gen] Ajax machinery: allow to specify different pre-loaders when calling askAjaxChunk.

This commit is contained in:
Gaetan Delannay 2015-03-11 14:24:19 +01:00
parent d54455baa3
commit 826664b798
2 changed files with 32 additions and 18 deletions

View file

@ -1,24 +1,24 @@
var wrongTextInput = '#F9EDBE none'; var wrongTextInput = '#F9EDBE none';
var loadingLink = '<img src="ui/loading.gif"/>'; var loadingLink = '<img src="ui/loading.gif"/>';
var loadingButton = '<img align="center" src="ui/loadingBtn.gif"/>'; var loadingButton = '<img align="center" src="ui/loadingBtn.gif"/>';
var loadingZone = '<div align="center"><img src="ui/loadingBig.gif"/></div>'; var loadingZone = '<div align="center"><img src="ui/?.gif"/></div>';
var lsTimeout; // Timout for the live search var lsTimeout; // Timout for the live search
var podTimeout; // Timeout for checking status of pod downloads var podTimeout; // Timeout for checking status of pod downloads
// Functions related to user authentication // Functions related to user authentication
function cookiesAreEnabled() { function cookiesAreEnabled() {
// Test whether cookies are enabled by attempting to set a cookie and then /* Test whether cookies are enabled by attempting to set a cookie and then
// change its value change its value. */
var c = "areYourCookiesEnabled=0"; var c = "areYourCookiesEnabled=0";
document.cookie = c; document.cookie = c;
var dc = document.cookie; var dc = document.cookie;
// Cookie not set? Fail // Cookie not set? Fail.
if (dc.indexOf(c) == -1) return 0; if (dc.indexOf(c) == -1) return 0;
// Change test cookie // Change test cookie
c = "areYourCookiesEnabled=1"; c = "areYourCookiesEnabled=1";
document.cookie = c; document.cookie = c;
dc = document.cookie; dc = document.cookie;
// Cookie not changed? fail // Cookie not changed? Fail.
if (dc.indexOf(c) == -1) return 0; if (dc.indexOf(c) == -1) return 0;
// Delete cookie // Delete cookie
document.cookie = "areYourCookiesEnabled=; expires=Thu, 01-Jan-70 00:00:01 GMT"; document.cookie = "areYourCookiesEnabled=; expires=Thu, 01-Jan-70 00:00:01 GMT";
@ -30,9 +30,10 @@ function setLoginVars() {
document.getElementById('js_enabled').value = 1; document.getElementById('js_enabled').value = 1;
// Indicate if cookies are enabled // Indicate if cookies are enabled
document.getElementById('cookies_enabled').value = cookiesAreEnabled(); document.getElementById('cookies_enabled').value = cookiesAreEnabled();
// Copy login and password length to alternative vars since current vars will /* Copy login and password length to alternative vars since current vars will
// be removed from the request by zope's authentication mechanism. be removed from the request by zope's authentication mechanism. */
document.getElementById('login_name').value = document.getElementById('__ac_name').value; var v = document.getElementById('__ac_name').value;
document.getElementById('login_name').value = v;
password = document.getElementById('__ac_password'); password = document.getElementById('__ac_password');
emptyPassword = document.getElementById('pwd_empty'); emptyPassword = document.getElementById('pwd_empty');
if (password.value.length==0) emptyPassword.value = '1'; if (password.value.length==0) emptyPassword.value = '1';
@ -40,10 +41,10 @@ function setLoginVars() {
} }
function showLoginForm() { function showLoginForm() {
// Hide the login link. // Hide the login link
var loginLink = document.getElementById('loginLink'); var loginLink = document.getElementById('loginLink');
loginLink.style.display = "none"; loginLink.style.display = "none";
// Displays the login form. // Displays the login form
var loginFields = document.getElementById('loginFields'); var loginFields = document.getElementById('loginFields');
loginFields.style.display = "inline"; loginFields.style.display = "inline";
} }
@ -160,7 +161,17 @@ function getAjaxChunk(pos) {
} }
} }
function askAjaxChunk(hook, mode, url, px, params, beforeSend, onGet) { // Displays the waiting icon when an ajax chunk is asked
function showPreloader(hook, waiting) {
/* p_hook may be null if the ajax result would be the same as what is
currently shown, as when inline-editing a rich text field). */
if (!hook || (waiting == 'none')) return;
// What waiting icon to show?
if (!waiting) waiting = 'loadingBig';
injectChunk(getAjaxHook(hook), loadingZone.replace('?', waiting), true);
}
function askAjaxChunk(hook, mode, url, px, params, beforeSend, onGet, waiting) {
/* This function will ask to get a chunk of XHTML on the server through a /* This function will ask to get a chunk of XHTML on the server through a
XMLHttpRequest. p_mode can be 'GET' or 'POST'. p_url is the URL of a XMLHttpRequest. p_mode can be 'GET' or 'POST'. p_url is the URL of a
given server object. On this object we will call method "ajax" that will given server object. On this object we will call method "ajax" that will
@ -181,6 +192,11 @@ function askAjaxChunk(hook, mode, url, px, params, beforeSend, onGet) {
p_onGet is a Javascript function to call when we will receive the answer. 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 This function will get 2 args, too: the XMLHttpRequest object and the
HTML node element into which the result has been inserted. HTML node element into which the result has been inserted.
p_waiting is the name of the animated icon that will be shown while waiting
for the ajax result. If null, it will be loadingBig.gif. Other values can
be "loading", "loadingBtn" or "loadingPod" (the .gif must be omitted).
If "none", there will be no icon at all.
*/ */
// First, get a non-busy XMLHttpRequest object. // First, get a non-busy XMLHttpRequest object.
var pos = -1; var pos = -1;
@ -212,10 +228,7 @@ function askAjaxChunk(hook, mode, url, px, params, beforeSend, onGet) {
if (mode == 'GET') { if (mode == 'GET') {
urlFull = urlFull + '?' + paramsFull; urlFull = urlFull + '?' + paramsFull;
} }
/* Display the preloader (rq.hook may be null if the ajax result would be showPreloader(rq.hook, waiting); // Display the pre-loader
the same as what is currently shown, as when inline-editing a rich text
field). */
if (rq.hook) injectChunk(getAjaxHook(rq.hook), loadingZone, true);
// Perform the asynchronous HTTP GET or POST // Perform the asynchronous HTTP GET or POST
rq.xhr.open(mode, urlFull, true); rq.xhr.open(mode, urlFull, true);
if (mode == 'POST') { if (mode == 'POST') {
@ -252,7 +265,7 @@ function AjaxData(hook, px, params, parentHook, url, mode, beforeSend, onGet) {
getAjaxHook(hook, true)['ajax'] = this; getAjaxHook(hook, true)['ajax'] = this;
} }
function askAjax(hook, form, params) { function askAjax(hook, form, params, waiting) {
/* Call askAjaxChunk by getting an AjaxData instance from p_hook, a /* Call askAjaxChunk by getting an AjaxData instance from p_hook, a
potential action from p_form and additional parameters from p_param. */ potential action from p_form and additional parameters from p_param. */
var d = getAjaxHook(hook)['ajax']; var d = getAjaxHook(hook)['ajax'];
@ -298,7 +311,8 @@ function askAjax(hook, form, params) {
if (params && ('mode' in params)) { if (params && ('mode' in params)) {
mode = params['mode']; delete params['mode'] } mode = params['mode']; delete params['mode'] }
if (params) { for (var key in params) d.params[key] = params[key]; } if (params) { for (var key in params) d.params[key] = params[key]; }
askAjaxChunk(hook,mode,d.url,d.px,d.params,d.beforeSend,evalInnerScripts); askAjaxChunk(hook, mode, d.url, d.px, d.params, d.beforeSend,
evalInnerScripts, waiting);
} }
function askBunch(hookId, startNumber) { function askBunch(hookId, startNumber) {

View file

@ -172,6 +172,6 @@ function onCheckCbCell(cb, hook, totalRows) {
if (totalRows) { if (totalRows) {
var params = getValidationStatus(hook); var params = getValidationStatus(hook);
params['mode'] = 'POST'; params['mode'] = 'POST';
askAjax(hook + '_trs', null, params); askAjax(hook + '_trs', null, params, 'loadingPod');
} }
} }