X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=www%2Fprive%2Fjavascript%2FajaxCallback.js;h=777a0cbabdb06a01483338c771c8f333efa3e902;hb=4f443dce95ff6f8221c189880a70c74ce1c1f238;hp=3df5c8847ef17b9937fa9618f9704fd005e8533e;hpb=4a628e9b277d3617535f99d663ca79fa2e891177;p=lhc%2Fweb%2Fwww.git diff --git a/www/prive/javascript/ajaxCallback.js b/www/prive/javascript/ajaxCallback.js index 3df5c884..777a0cba 100644 --- a/www/prive/javascript/ajaxCallback.js +++ b/www/prive/javascript/ajaxCallback.js @@ -37,6 +37,9 @@ if(!jQuery.spip.load_handlers) { // intercept jQuery.fn.load jQuery.spip.intercepted.load = jQuery.fn.load; jQuery.fn.load = function( url, params, callback ) { + if ( typeof url !== "string") { + return jQuery.spip.intercepted.load.apply( this, arguments ); + } callback = callback || function(){}; @@ -49,6 +52,7 @@ if(!jQuery.spip.load_handlers) { params = null; } } + params = jQuery.extend(params, {triggerAjaxLoad:false}); // prevent $.ajax to triggerAjaxLoad var callback2 = function() {jQuery.spip.log('jQuery.load');jQuery.spip.triggerAjaxLoad(this);callback.apply(this,arguments);}; return jQuery.spip.intercepted.load.apply(this,[url, params, callback2]); }; @@ -75,33 +79,95 @@ if(!jQuery.spip.load_handlers) { // intercept jQuery.ajax jQuery.spip.intercepted.ajax = jQuery.ajax; - jQuery.ajax = function(type) { - var s = jQuery.extend(true, {}, jQuery.ajaxSettings, type); + jQuery.ajax = function(url, settings) { + if (typeof settings == 'undefined') { + settings = {}; + if (typeof url == 'object') { + settings = url; + url = null; + } + } + if (typeof url == 'string') { + settings['url'] = url; + } + // if triggerAjaxLoad is prevented, finish without faking callback + if (settings.data && settings.data['triggerAjaxLoad'] === false) { + settings.data['triggerAjaxLoad'] = null; + return jQuery.spip.intercepted.ajax(settings); + } + var s = jQuery.extend(true, {}, jQuery.ajaxSettings, settings); var callbackContext = s.context || s; try { if (jQuery.ajax.caller==jQuery.spip.intercepted.load || jQuery.ajax.caller==jQuery.spip.intercepted.ajaxSubmit) - return jQuery.spip.intercepted.ajax(type); + return jQuery.spip.intercepted.ajax(settings); } catch (err){} var orig_complete = s.complete || function() {}; - type.complete = function(res,status) { + settings.complete = function(res,status) { // Do not fire OnAjaxLoad if the dataType is not html - var dataType = type.dataType; + var dataType = settings.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") { + if((!dataType && !xml) || dataType == "html") { jQuery.spip.log('jQuery.ajax'); if (typeof s.onAjaxLoad=="undefined" || s.onAjaxLoad!=false) jQuery.spip.triggerAjaxLoad(s.ajaxTarget?s.ajaxTarget:document); } }; - return jQuery.spip.intercepted.ajax(type); + return jQuery.spip.intercepted.ajax(settings); }; } +/* jQuery.browser */ +jQuery.uaMatch = function( ua ) { + ua = ua.toLowerCase(); + + var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) || + /(webkit)[ \/]([\w.]+)/.exec( ua ) || + /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) || + /(msie) ([\w.]+)/.exec( ua ) || + ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || + []; + + return { + browser: match[ 1 ] || "", + version: match[ 2 ] || "0" + }; +}; + +// Don't clobber any existing jQuery.browser in case it's different +if ( !jQuery.browser ) { + matched = jQuery.uaMatch( navigator.userAgent ); + browser = {}; + + if ( matched.browser ) { + browser[ matched.browser ] = true; + browser.version = matched.version; + } + + // Chrome is Webkit, but Webkit is also Safari. + if ( browser.chrome ) { + browser.webkit = true; + } else if ( browser.webkit ) { + browser.safari = true; + } + + jQuery.browser = browser; +} + +// jQuery.getScript cache par defaut +jQuery.getScript = function(url,callback){ + return $.ajax({ + url: url, + dataType: "script", + success: callback, + cache: true + }); +} + /** * if not fully visible, scroll the page to position * target block at the top of page @@ -143,7 +209,7 @@ jQuery.spip.updateReaderBuffer = function(){ 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); + i.val(parseInt(i.val())+1); } jQuery.fn.formulaire_setARIA = function(){ @@ -164,33 +230,44 @@ jQuery.fn.formulaire_setARIA = function(){ jQuery.fn.formulaire_dyn_ajax = function(target) { if (this.length) jQuery.spip.initReaderBuffer(); - return this.each(function() { - var scrollwhensubmit = !jQuery(this).is('.noscroll'); + return this.each(function() { + var scrollwhensubmit = !jQuery(this).is('.noscroll'); var cible = target || this; - jQuery(cible).formulaire_setARIA(); + jQuery(cible).formulaire_setARIA(); jQuery('form:not(.noajax):not(.bouton_action_post)', this).each(function(){ var leform = this; var leclk,leclk_x,leclk_y; + var onError = function(xhr, status, error, $form){ + jQuery(leform).ajaxFormUnbind().find('input[name="var_ajax"]').remove(); + var msg = "Erreur"; + if (typeof(error_on_ajaxform)!=="undefined") msg = error_on_ajaxform; + jQuery(leform).prepend("

"+msg+"

").find('.ajax-error').show('fast'); + jQuery(cible).closest('.ariaformprop').endLoading(true); + } 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; - } - } + var scrollwhensubmit_button = true; + if (leclk) { + scrollwhensubmit_button = !jQuery(leclk).is('.noscroll'); + var n = leclk.name; + if (n && !leclk.disabled && leclk.type == "image") { + leclk_x = leform.clk_x; + leclk_y = leform.clk_y; + } + } jQuery(cible).wrap('
'); cible = jQuery(cible).parent(); jQuery(cible).closest('.ariaformprop').animateLoading(); - if (scrollwhensubmit) + if (scrollwhensubmit && scrollwhensubmit_button) { jQuery(cible).positionner(false,false); + } }, - success: function(c){ - if (c=='noajax'){ + error: onError, + success: function(c, status, xhr , $form){ + if (c.match(/^\s*noajax\s*$/)){ // le serveur ne veut pas traiter ce formulaire en ajax // on resubmit sans ajax jQuery("input[name=var_ajax]",leform).remove(); @@ -198,8 +275,8 @@ jQuery.fn.formulaire_dyn_ajax = function(target) { // 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) { + var n = leclk.name; + if (n && !leclk.disabled) { jQuery(leform).prepend(""); if (leclk.type == "image") { jQuery(leform).prepend(""); @@ -210,6 +287,8 @@ jQuery.fn.formulaire_dyn_ajax = function(target) { jQuery(leform).ajaxFormUnbind().submit(); } else { + if (!c.length || c.indexOf("ajax-form-is-ok")==-1) + return onError.apply(this,[status, xhr , $form]); // commencons par vider le cache des urls, si jamais un js au retour // essaye tout de suite de suivre un lien en cache // dans le doute sur la validite du cache il vaut mieux l'invalider @@ -273,13 +352,12 @@ jQuery.fn.formulaire_dyn_ajax = function(target) { // a supprimer ? jQuery.spip.updateReaderBuffer(); } - }, - iframe: jQuery.browser.msie + }/*, + iframe: jQuery.browser.msie*/ }) // previent qu'on n'ajaxera pas deux fois le meme formulaire en cas de ajaxload // mais le marquer comme ayant l'ajax au cas ou on reinjecte du contenu ajax dedans - .addClass('noajax hasajax') - ; + .addClass('noajax hasajax'); }); }); } @@ -321,7 +399,7 @@ jQuery.fn.formulaire_activer_verif_auto = function(callback){ .find('form') .attr('data-verifjson','on') .find('input,select,textarea') - .bind('change',check); + .on('change', check); } } jQuery(activer); @@ -555,7 +633,7 @@ jQuery.spip.makeAjaxUrl = function(href,ajax_env,origin){ if (p!==-1) arg = arg.substring(0,p); val = parametre_url(href,arg); - if (typeof val=="undefined") + if (typeof val=="undefined" || val==null) url[0] = url[0] + '&' + arg + '='; } } @@ -582,7 +660,7 @@ jQuery.spip.ajaxReload = function(blocfrag, options){ if (!ajax_env || ajax_env==undefined) return; var href = options.href || blocfrag.attr('data-url') || blocfrag.attr('data-origin'); if (href && typeof href != undefined){ - options == options || {}; + options = options || {}; var callback=options.callback || null; var history=options.history || false; var args = options.args || {}; @@ -635,26 +713,26 @@ jQuery.fn.ajaxbloc = function() { if (ajaxbloc_selecteur==undefined) ajaxbloc_selecteur = '.pagination a,a.ajax'; - return this.each(function() { - // traiter les enfants d'abord : - // un lien ajax provoque le rechargement - // du plus petit bloc ajax le contenant - jQuery('div.ajaxbloc',this).ajaxbloc(); + return this.each(function() { + // traiter les enfants d'abord : + // un lien ajax provoque le rechargement + // du plus petit bloc ajax le contenant + jQuery('div.ajaxbloc',this).ajaxbloc(); var blocfrag = jQuery(this); var ajax_env = blocfrag.attr('data-ajax-env'); if (!ajax_env || ajax_env==undefined) return; - blocfrag.not('.bind-ajaxReload').bind('ajaxReload',function(event, options){ - if (jQuery.spip.ajaxReload(blocfrag,options)) + blocfrag.not('.bind-ajaxReload').on('ajaxReload', function(event, options){ + if (jQuery.spip.ajaxReload(blocfrag,options)) // don't trig reload of parent blocks event.stopPropagation(); - }).addClass('bind-ajaxReload') - .attr('aria-live','polite').attr('aria-atomic','true'); + }).addClass('bind-ajaxReload') + .attr('aria-live','polite').attr('aria-atomic','true'); // dans un formulaire, le screen reader relit tout a chaque saisie d'un caractere si on est en aria-live - // mettre un aria-live="off" sur les forms inclus dans ce bloc aria-live="polite" - jQuery('form',this).not('[aria-live]').attr('aria-live','off'); + // mettre un aria-live="off" sur les forms inclus dans ce bloc aria-live="polite" + jQuery('form',this).not('[aria-live]').attr('aria-live','off'); jQuery(ajaxbloc_selecteur,this).not('.noajax,.bind-ajax') .click(function(){return jQuery.spip.ajaxClick(blocfrag,this.href,{force:jQuery(this).is('.nocache'),history:!(jQuery(this).is('.nohistory')||jQuery(this).closest('.box_modalbox').length)});}) @@ -673,21 +751,24 @@ jQuery.fn.ajaxbloc = function() { jQuery('form.bouton_action_post.ajax', this).not('.noajax,.bind-ajax').each(function(){ var leform = this; var url = jQuery(this).attr('action').split('#'); + var scrollwhensubmit = (!jQuery(this).is('.noscroll') && !jQuery('.submit',this).is('.noscroll')); jQuery(this) .prepend(""+(url[1]?"":"")) .ajaxForm({ beforeSubmit: function(){ - jQuery(blocfrag).animateLoading().positionner(false); + jQuery(blocfrag).animateLoading(); + if (scrollwhensubmit) { + jQuery(blocfrag).positionner(false); + } }, onAjaxLoad:false, success: function(c){ jQuery.spip.on_ajax_loaded(blocfrag,c); jQuery.spip.preloaded_urls = {}; // on vide le cache des urls car on a fait une action en bdd - }, - iframe: jQuery.browser.msie + }/*, + iframe: jQuery.browser.msie*/ }) - .addClass('bind-ajax') // previent qu'on n'ajaxera pas deux fois le meme formulaire en cas de ajaxload - ; + .addClass('bind-ajax'); // previent qu'on n'ajaxera pas deux fois le meme formulaire en cas de ajaxload }); }); }; @@ -786,7 +867,7 @@ jQuery.fn.animateRemove = function(callback){ // if target is a tr, include td childrens cause background color on tr doesn't works in a lot of browsers if (sel.is('tr')) sel = sel.add('>td',sel); - sel.addClass('remove').css({backgroundColor: color}).animate({opacity: "0.0"}, 'fast',function(){ + sel.addClass('remove').css({backgroundColor: color}).animate({opacity: "0.0"}, 'fast', function(){ sel.removeClass('remove').css({backgroundColor: ''}); if (callback) callback.apply(me); @@ -795,6 +876,7 @@ jQuery.fn.animateRemove = function(callback){ return this; // don't break the chain } + /** * animation d'un item que l'on ajoute : * ajout de la classe append @@ -851,7 +933,7 @@ function parametre_url(url,c,v,sep,force_vide){ if (typeof(url) == 'undefined'){ url = ''; } - + var p; // lever l'#ancre var ancre=''; @@ -905,8 +987,8 @@ function parametre_url(url,c,v,sep,force_vide){ na.push(r[1]+'='+u); ajouts.push(r[1]); } - /* Pour les tableaux ont laisse tomber les valeurs de départ, on - remplira à l'étape suivante */ + /* Pour les tableaux ont laisse tomber les valeurs de départ, on + remplira à l'étape suivante */ // else na.push(args[n]); } else