X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=www%2Fprive%2Fjavascript%2FajaxCallback.js;fp=www%2Fprive%2Fjavascript%2FajaxCallback.js;h=7dfbe6b9696d10bf0b7a43388ba7927e5c3b30c2;hb=80b4d3e85f78d402ed2e73f8f5d1bf4c19962eed;hp=0000000000000000000000000000000000000000;hpb=aaf970bf4cdaf76689ecc10609048e18d073820c;p=velocampus%2Fweb%2Fwww.git diff --git a/www/prive/javascript/ajaxCallback.js b/www/prive/javascript/ajaxCallback.js new file mode 100644 index 0000000..7dfbe6b --- /dev/null +++ b/www/prive/javascript/ajaxCallback.js @@ -0,0 +1,327 @@ +// A plugin that wraps all ajax calls introducing a fixed callback function on ajax complete +if(!jQuery.load_handlers) { + jQuery.load_handlers = new Array(); + // + // Add a function to the list of those to be executed on ajax load complete + // + function onAjaxLoad(f) { + jQuery.load_handlers.push(f); + }; + + // + // Call the functions that have been added to onAjaxLoad + // + function triggerAjaxLoad(root) { + for ( var i = 0; i < jQuery.load_handlers.length; i++ ) + jQuery.load_handlers[i].apply( root ); + }; + + // jQuery uses _load, we use _ACBload + jQuery.fn._ACBload = jQuery.fn.load; + + jQuery.fn.load = function( url, params, callback ) { + + callback = callback || function(){}; + + // If the second parameter was provided + if ( params ) { + // If it's a function + if ( params.constructor == Function ) { + // We assume that it's the callback + callback = params; + params = null; + } + } + var callback2 = function(res,status) {triggerAjaxLoad(this);callback.call(this,res,status);}; + + return this._ACBload( url, params, callback2 ); + }; + + jQuery._ACBajax = jQuery.ajax; + + jQuery.ajax = function(type) { + var s = jQuery.extend(true, {}, jQuery.ajaxSettings, type); + var callbackContext = s.context || s; + //If called by _load exit now because the callback has already been set + if (jQuery.ajax.caller==jQuery.fn._load) return jQuery._ACBajax( type); + var orig_complete = s.complete || function() {}; + type.complete = function(res,status) { + // Do not fire OnAjaxLoad if the dataType is not html + var dataType = type.dataType; + var ct = (res && (typeof res.getResponseHeader == 'function')) + ? res.getResponseHeader("content-type"): ''; + var xml = !dataType && ct && ct.indexOf("xml") >= 0; + orig_complete.call( callbackContext, res, status); + if(!dataType && !xml || dataType == "html") triggerAjaxLoad(document); + }; + return jQuery._ACBajax(type); + }; + +} + +// animation du bloc cible pour faire patienter +jQuery.fn.animeajax = function(end) { + this.children().css('opacity', 0.5); + if (typeof ajax_image_searching != 'undefined'){ + var i = (this).find('.image_loading'); + if (i.length) i.eq(0).html(ajax_image_searching); + else this.prepend(''+ajax_image_searching+''); + } + return this; // don't break the chain +} + +// s'il n'est pas totalement visible, scroller pour positionner +// le bloc cible en haut de l'ecran +// si force = true, scroller dans tous les cas +jQuery.fn.positionner = function(force) { + var offset = jQuery(this).offset(); + var hauteur = parseInt(jQuery(this).css('height')); + var scrolltop = self['pageYOffset'] || + jQuery.boxModel && document.documentElement[ 'scrollTop' ] || + document.body[ 'scrollTop' ]; + var h = jQuery(window).height(); + var scroll=0; + + if (force || offset['top'] - 5 <= scrolltop) + scroll = offset['top'] - 5; + else if (offset['top'] + hauteur - h + 5 > scrolltop) + scroll = Math.min(offset['top'] - 5, offset['top'] + hauteur - h + 15); + if (scroll) + jQuery('html,body') + .animate({scrollTop: scroll}, 300); + + // positionner le curseur dans la premiere zone de saisie + jQuery(jQuery('*', this).filter('input[type=text],textarea')[0]).focus(); + return this; // don't break the chain +} + +// deux fonctions pour rendre l'ajax compatible Jaws +var virtualbuffer_id='spip_virtualbufferupdate'; +function initReaderBuffer(){ + if (jQuery('#'+virtualbuffer_id).length) return; + jQuery('body').append('
'); +} +function updateReaderBuffer(){ + var i = jQuery('#'+virtualbuffer_id); + if (!i.length) return; + // incrementons l'input hidden, ce qui a pour effet de forcer le rafraichissement du + // buffer du lecteur d'ecran (au moins dans Jaws) + i.attr('value',parseInt(i.attr('value'))+1); +} + +// rechargement ajax d'un formulaire dynamique implemente par formulaires/xxx.html +jQuery.fn.formulaire_dyn_ajax = function(target) { + if (this.length) + initReaderBuffer(); + return this.each(function() { + var cible = target || this; + jQuery('form:not(.noajax,.bouton_action_post)', this).each(function(){ + var leform = this; + var leclk,leclk_x,leclk_y; + jQuery(this).prepend("") + .ajaxForm({ + beforeSubmit: function(){ + // memoriser le bouton clique, en cas de repost non ajax + leclk = leform.clk; + if (leclk) { + var n = leclk.name; + if (n && !leclk.disabled && leclk.type == "image") { + leclk_x = leform.clk_x; + leclk_y = leform.clk_y; + } + } + jQuery(cible).addClass('loading').animeajax(); + }, + success: function(c){ + if (c=='noajax'){ + // le serveur ne veut pas traiter ce formulaire en ajax + // on resubmit sans ajax + jQuery("input[name=var_ajax]",leform).remove(); + // si on a memorise le nom et la valeur du bouton clique + // les reinjecter dans le dom sous forme de input hidden + // pour que le serveur les recoive + if (leclk){ + var n = leclk.name; + if (n && !leclk.disabled) { + jQuery(leform).prepend(""); + if (leclk.type == "image") { + jQuery(leform).prepend(""); + jQuery(leform).prepend(""); + } + } + } + jQuery(leform).ajaxFormUnbind().submit(); + } + else { + var recu = jQuery('