~maj v3.0.19-->v3.0.21
[ptitvelo/web/www.git] / www / prive / javascript / ajaxCallback.js
index 27bba46..3df5c88 100644 (file)
@@ -78,8 +78,11 @@ if(!jQuery.spip.load_handlers) {
        jQuery.ajax = function(type) {
                var s = jQuery.extend(true, {}, jQuery.ajaxSettings, type);
                var callbackContext = s.context || s;
-               if (jQuery.ajax.caller==jQuery.spip.intercepted.load || jQuery.ajax.caller==jQuery.spip.intercepted.ajaxSubmit)
-                       return jQuery.spip.intercepted.ajax(type);
+               try {
+                       if (jQuery.ajax.caller==jQuery.spip.intercepted.load || jQuery.ajax.caller==jQuery.spip.intercepted.ajaxSubmit)
+                               return jQuery.spip.intercepted.ajax(type);
+               }
+               catch (err){}
                var orig_complete = s.complete || function() {};
                type.complete = function(res,status) {
                        // Do not fire OnAjaxLoad if the dataType is not html
@@ -133,7 +136,7 @@ jQuery.fn.positionner = function(force, setfocus) {
 jQuery.spip.virtualbuffer_id='spip_virtualbufferupdate';
 jQuery.spip.initReaderBuffer = function(){
        if (jQuery('#'+jQuery.spip.virtualbuffer_id).length) return;
-       jQuery('body').append('<p style="float:left;width:0;height:0;position:absolute;left:-5000;top:-5000;"><input type="hidden" name="'+jQuery.spip.virtualbuffer_id+'" id="'+jQuery.spip.virtualbuffer_id+'" value="0" /></p>');
+       jQuery('body').append('<p style="float:left;width:0;height:0;position:absolute;left:-5000px;top:-5000px;"><input type="hidden" name="'+jQuery.spip.virtualbuffer_id+'" id="'+jQuery.spip.virtualbuffer_id+'" value="0" /></p>');
 }
 jQuery.spip.updateReaderBuffer = function(){
        var i = jQuery('#'+jQuery.spip.virtualbuffer_id);
@@ -148,7 +151,9 @@ jQuery.fn.formulaire_setARIA = function(){
                // eviter une double execution du js au moment de sa reinsertion dans le DOM par wrap()
                // cf http://bugs.jquery.com/ticket/7447
                this.find('script').remove();
-               this.wrap('<div class="ariaformprop" aria-live="assertive" aria-atomic="true"></div>');
+               this.wrap('<div class="ariaformprop" aria-live="polite" aria-atomic="true" aria-relevant="additions"></div>');
+               // dans un formulaire, le screen reader relit tout a chaque saisie d'un caractere si on est en aria-live
+               jQuery('form',this).not('[aria-live]').attr('aria-live','off');
        }
        return this;
 }
@@ -383,10 +388,17 @@ jQuery.spip.on_ajax_loaded = function(blocfrag,c,href,history) {
                history = false;
        if (history)
                jQuery.spip.setHistoryState(blocfrag);
-       
-       jQuery(blocfrag)
-       .html(c)
-       .endLoading();
+
+       if (jQuery(blocfrag).attr('data-loaded-callback')){
+               var callback = eval(jQuery(blocfrag).attr('data-loaded-callback'));
+               callback.call(blocfrag, c, href, history);
+       }
+       else {
+               jQuery(blocfrag)
+               .html(c)
+               .endLoading();
+       }
+
        if (typeof href != undefined)
                jQuery(blocfrag).attr('data-url',href);
        if (history) {
@@ -427,6 +439,9 @@ jQuery.spip.setHistoryState = function(blocfrag){
                id:blocfrag.attr('id'),
                href: href
        };
+       var ajaxid = blocfrag.attr('class').match(/\bajax-id-[\w-]+\b/);
+       if (ajaxid && ajaxid.length)
+               state["ajaxid"] = ajaxid[0];
        // on remplace la variable qui decrit l'etat courant
        // initialement vide
        // -> elle servira a revenir dans l'etat courant
@@ -440,12 +455,23 @@ jQuery.spip.pushHistoryState = function(href, title){
 }
 
 window.onpopstate = function(popState){
-       if (popState.state && popState.state.id){
-               var blocfrag=jQuery('#'+popState.state.id);
-               if (blocfrag.length && popState.state.href) {
+       if (popState.state && popState.state.href){
+               var blocfrag=false;
+               if (popState.state.id){
+                       blocfrag=jQuery('#'+popState.state.id);
+               }
+               if ((!blocfrag || !blocfrag.length) && popState.state.ajaxid){
+                       blocfrag=jQuery('.ajaxbloc.'+popState.state.ajaxid);
+               }
+               if (blocfrag && blocfrag.length==1) {
                        jQuery.spip.ajaxClick(blocfrag,popState.state.href,{history:false});
                        return true;
                }
+               // si on revient apres avoir rompu la chaine ajax, on a pu perdre l'id #ghsidxx ajoute en JS
+               // dans ce cas on redirige hors ajax
+               else {
+                       window.location.href = popState.state.href;
+               }
        }
 }
 
@@ -465,7 +491,13 @@ window.onpopstate = function(popState){
  */
 jQuery.spip.loadAjax = function(blocfrag,url, href, options){
        var force = options.force || false;
-       jQuery(blocfrag).animateLoading();
+       if (jQuery(blocfrag).attr('data-loading-callback')){
+               var callback = eval(jQuery(blocfrag).attr('data-loading-callback'));
+               callback.call(blocfrag,url,href,options);
+       }
+       else {
+               jQuery(blocfrag).animateLoading();
+       }
        if (jQuery.spip.preloaded_urls[url] && !force) {
                // si on est deja en train de charger ce fragment, revenir plus tard
                if (jQuery.spip.preloaded_urls[url]=="<!--loading-->"){
@@ -503,10 +535,32 @@ jQuery.spip.loadAjax = function(blocfrag,url, href, options){
  * @param string href
  * @param string ajax_env
  */
-jQuery.spip.makeAjaxUrl = function(href,ajax_env){
+jQuery.spip.makeAjaxUrl = function(href,ajax_env,origin){
        var url = href.split('#');
        url[0] = parametre_url(url[0],'var_ajax',1);
        url[0] = parametre_url(url[0],'var_ajax_env',ajax_env);
+
+       // les arguments de origin qui ne sont pas dans href doivent etre explicitement fournis vides dans url
+       if (origin){
+               var p=origin.indexOf('?');
+               if (p!==-1){
+                       // recuperer la base
+                       var args = origin.substring(p+1).split('&');
+                       var val;
+                       var arg;
+                       for(var n=0;n<args.length;n++){
+                               arg = args[n].split('=');
+                               arg = arg[0];
+                               p = arg.indexOf('[');
+                               if (p!==-1)
+                                       arg = arg.substring(0,p);
+                               val = parametre_url(href,arg);
+                               if (typeof val=="undefined")
+                                       url[0] = url[0] + '&' + arg + '=';
+                       }
+               }
+       }
+
        if (url[1])
                url[0] = parametre_url(url[0],'var_ajax_ancre',url[1]);
        return url[0];
@@ -519,13 +573,14 @@ jQuery.spip.makeAjaxUrl = function(href,ajax_env){
  * @param object blocfrag
  * @param object options
  *   callback : fonction appelee apres le rechargement
+ *   href : url to load instead of origin url
  *   args : arguments passes a l'url rechargee (permet une modif du contexte)
  *   history : bool to specify if navigation history is modified by reload or not (false if not provided)
  */
 jQuery.spip.ajaxReload = function(blocfrag, options){
        var ajax_env = blocfrag.attr('data-ajax-env');
        if (!ajax_env || ajax_env==undefined) return;
-       var href = blocfrag.attr('data-url') || blocfrag.attr('data-origin');
+       var href = options.href || blocfrag.attr('data-url') || blocfrag.attr('data-origin');
        if (href && typeof href != undefined){
                options == options || {};
                var callback=options.callback || null;
@@ -533,7 +588,7 @@ jQuery.spip.ajaxReload = function(blocfrag, options){
                var args = options.args || {};
                for (var key in args)
                        href = parametre_url(href,key,args[key]==undefined?'':args[key],'&',args[key]==undefined?false:true);
-               var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
+               var url = jQuery.spip.makeAjaxUrl(href,ajax_env,blocfrag.attr('data-origin'));
                // recharger sans historisation dans l'url
                jQuery.spip.loadAjax(blocfrag, url, href, {force:true, callback:callback, history:history});
                return true;
@@ -562,7 +617,7 @@ jQuery.spip.ajaxClick = function(blocfrag, href, options){
                if ((d.getTime()-ajax_confirm_date)<=2)
                        return false;
        }
-       var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
+       var url = jQuery.spip.makeAjaxUrl(href,ajax_env,blocfrag.attr('data-origin'));
        jQuery.spip.loadAjax(blocfrag, url, href, options);
        return false;
 }
@@ -594,14 +649,19 @@ jQuery.fn.ajaxbloc = function() {
                  if (jQuery.spip.ajaxReload(blocfrag,options))
                                // don't trig reload of parent blocks
                                event.stopPropagation();
-         }).addClass('bind-ajaxReload');
+         }).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');
 
                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)});})
                        .addClass('bind-ajax')
                        .filter('.preload').each(function(){
                                var href = this.href;
-                               var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
+                               var url = jQuery.spip.makeAjaxUrl(href,ajax_env,blocfrag.attr('data-origin'));
                                if (!jQuery.spip.preloaded_urls[url]) {
                                        jQuery.spip.preloaded_urls[url] = '<!--loading-->';
                                        jQuery.ajax({url:url,onAjaxLoad:false,success:function(r){jQuery.spip.preloaded_urls[url]=r;},error:function(){jQuery.spip.preloaded_urls[url]='';}});
@@ -657,6 +717,7 @@ jQuery.fn.followLink = function(){
  * @param string ajaxid
  * @param object options
  *   callback : callback after reloading
+ *   href : url to load instead of origin url
  *   args : {arg:value,...} to pass tu the url
  *   history : bool to specify if navigation history is modified by reload or not (false if not provided)
  */
@@ -719,11 +780,16 @@ jQuery.fn.endLoading = function(hard) {
  */
 jQuery.fn.animateRemove = function(callback){
        if (this.length){
+               var me=this;
                var color = $("<div class='remove'></div>").css('background-color');
-               $(this).addClass('remove').css({backgroundColor: color}).animate({opacity: "0.0"}, 'fast',function(){
-                       $(this).removeClass('remove').css({backgroundColor: ''});
+               var sel=$(this);
+               // 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.removeClass('remove').css({backgroundColor: ''});
                        if (callback)
-                               callback.apply(this);
+                               callback.apply(me);
                });
        }
        return this; // don't break the chain
@@ -748,7 +814,7 @@ jQuery.fn.animateAppend = function(callback){
                var sel=$(this);
                // 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.add('>td',sel);
+                       sel = sel.add('>td',sel);
                sel.css('opacity','0.0').addClass('append').css({backgroundColor: color}).animate({opacity: "1.0"}, 1000,function(){
                        sel.animate({backgroundColor: origin}, 3000,function(){
                                sel.removeClass('append').css({backgroundColor: ''});
@@ -804,12 +870,13 @@ function parametre_url(url,c,v,sep,force_vide){
                if (p>0) a=url.substring(0,p);
                args = url.substring(p+1).split('&');
        }
-        else
-            a=url;
-       var regexp = new RegExp('^(' + c.replace('[]','\[\]') + '\[?\]?)(=.*)?$');
+       else
+               a=url;
+       var regexp = new RegExp('^(' + c.replace('[]','\\[\\]') + '\\[?\\]?)(=.*)?$');
        var ajouts = [];
        var u = (typeof(v)!=='object')?encodeURIComponent(v):v;
        var na = [];
+       var v_read = null;
        // lire les variables et agir
        for(var n=0;n<args.length;n++){
                var val = args[n];
@@ -819,24 +886,34 @@ function parametre_url(url,c,v,sep,force_vide){
                var r=val.match(regexp);
                if (r && r.length){
                        if (v==null){
-                               return (r.length>2)?r[2].substring(1):'';
+                               // c'est un tableau, on memorise les valeurs
+                               if (r[1].substr(-2) == '[]') {
+                                       if (!v_read) v_read = [];
+                                       v_read.push((r.length>2 && typeof r[2]!=='undefined')?r[2].substring(1):'');
+                               }
+                               // c'est un scalaire, on retourne direct
+                               else {
+                                       return (r.length>2 && typeof r[2]!=='undefined')?r[2].substring(1):'';
+                               }
                        }
                        // suppression
                        else if (!v.length) {
                        }
                        // Ajout. Pour une variable, remplacer au meme endroit,
                        // pour un tableau ce sera fait dans la prochaine boucle
-                       else if (r[1].substring(-2) != '[]') {
+                       else if (r[1].substr(-2) != '[]') {
                                na.push(r[1]+'='+u);
                                ajouts.push(r[1]);
                        }
-                       else na.push(args[n]);
+                       /* Pour les tableaux ont laisse tomber les valeurs de départ, on
+                       remplira à l'étape suivante */
+                       // else na.push(args[n]);
                }
                else
                        na.push(args[n]);
        }
 
-       if (v==null) return v; // rien de trouve
+       if (v==null) return v_read; // rien de trouve ou un tableau
        // traiter les parametres pas encore trouves
        if (v || v.length || force_vide) {
                ajouts = "="+ajouts.join("=")+"=";