Valider 481c3c28 rédigé par cerdic's avatar cerdic
Parcourir les fichiers

optimisations des blocs ajas : stocker l'env dans un attribut data-ajax-env

et factoriser les fonctions en passant simplement les parametres necessaires. Eviter de faire un each sur chaque lien.
parent 1bdfae7a
Chargement en cours
Chargement en cours
Chargement en cours
Chargement en cours
+1 −1
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -2165,7 +2165,7 @@ function encoder_contexte_ajax($c,$form='', $emboite=NULL, $ajaxid='') {
	if ($ajaxid AND is_string($ajaxid)){
		$c .= ' ajax-id-'.$ajaxid;
	}
	return "<div class='ajaxbloc env-$c'$r>\n$emboite</div><!-- ajaxbloc -->\n";
	return "<div class='ajaxbloc' data-ajax-env='$c'$r>\n$emboite</div><!--ajaxbloc-->\n";
}

// la procedure inverse de encoder_contexte_ajax()
+43 −33
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -317,6 +317,39 @@ jQuery.spip.makeAjaxUrl = function(href,ajax_env){
	return url[0];
}

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');
	if (href && typeof href != undefined){
		options == options || {};
		var callback=options.callback || null;
		var args = options.args || {};
		for (var key in args)
			href = parametre_url(href,key,args[key]);
		var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
		jQuery.spip.loadAjax(blocfrag, url, href, true, callback);
		return true;
	}
}

jQuery.spip.ajaxClick = function(blocfrag, href, force){
	var ajax_env = blocfrag.attr('data-ajax-env');
	if (!ajax_env || ajax_env==undefined) return;
	if (!ajax_confirm) {
		// on rearme pour le prochain clic
		ajax_confirm=true;
		var d = new Date();
		// seule une annulation par confirm() dans les 2 secondes precedentes est prise en compte
		if ((d.getTime()-ajax_confirm_date)<=2)
			return false;
	}
	var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
	jQuery.spip.loadAjax(blocfrag, url, href, force);
	return false;
}


jQuery.fn.ajaxbloc = function() {
	if (this.length)
		jQuery.spip.initReaderBuffer();
@@ -327,51 +360,28 @@ jQuery.fn.ajaxbloc = function() {
	  jQuery('div.ajaxbloc',this).ajaxbloc(); // traiter les enfants d'abord
		var blocfrag = jQuery(this);

		var ajax_env = (""+blocfrag.attr('class')).match(/env-([^ ]+)/);
		var ajax_env = blocfrag.attr('data-ajax-env');
		if (!ajax_env || ajax_env==undefined) return;
		ajax_env = ajax_env[1];

	  jQuery(this).not('.bind-ajaxReload').bind('ajaxReload',function(event, options){
		  var href = $(this).attr('data-url') || $(this).attr('data-origin');
		  if (href && typeof href != undefined){
			  options == options || {};
			  var callback=options.callback || null;
			  var args = options.args || {};
			  for (var key in args)
	        href = parametre_url(href,key,args[key]);
			  var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
			  jQuery.spip.loadAjax(blocfrag, url, href, true, callback);
		  if (jQuery.spip.ajaxReload(blocfrag,options))
				// don't trig reload of parent blocks
				event.stopPropagation();
		  }
	  }).addClass('bind-ajaxReload');

		jQuery(ajaxbloc_selecteur,this).not('.noajax').not('.bind-ajax').each(function(){
			if (jQuery(this).is('.preload')){
		jQuery(ajaxbloc_selecteur,this).not('.noajax,.bind-ajax')
			.click(function(){return jQuery.spip.ajaxClick(blocfrag,this.href,jQuery(this).is('.nocache'));})
			.filter('.preload').each(function(){
				var href = this.href;
				var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
				if (!jQuery.spip.preloaded_urls[url]) {
					jQuery.ajax({"url":url,onAjaxLoad:false,"success":function(r){jQuery.spip.preloaded_urls[url]=r;}});
				}
			}
			jQuery(this).click(function(){
				if (!ajax_confirm) {
					// on rearme pour le prochain clic
					ajax_confirm=true;
					var d = new Date();
					// seule une annulation par confirm() dans les 2 secondes precedentes est prise en compte
					if ((d.getTime()-ajax_confirm_date)<=2)
						return false;
				}
				var href = this.href;
				var url = jQuery.spip.makeAjaxUrl(href,ajax_env);
				jQuery.spip.loadAjax(blocfrag, url, href, jQuery(this).is('.nocache'));
				return false;
			});
			}).addClass('bind-ajax'); // previent qu'on ajax pas deux fois le meme lien

		// ajaxer les boutons actions qui sont techniquement des form minimaux
		// mais se comportent comme des liens
		jQuery('form.bouton_action_post.ajax', this).not('.noajax').not('.bind-ajax').each(function(){
		jQuery('form.bouton_action_post.ajax', this).not('.noajax,.bind-ajax').each(function(){
			var leform = this;
			var url = jQuery(this).attr('action').split('#');
			jQuery(this)