Skip to content
Extraits de code Groupes Projets
Valider 0fb317fe rédigé par Fil's avatar Fil
Parcourir les fichiers

gestion de l'ajax, une methode plus simple que [11580] : on ajoute la classe...

gestion de l'ajax, une methode plus simple que [11580] : on ajoute la classe ajax sur un element englobant du squelette, et le formulaire est ajaxe ; disparition de ajaxid, disparition du script inline. Au passage, deux corrections sur la pagination ajax : 1) possibilite de paginer vers l'item 0 quand on a calcule la page avec debut_x=1  2) eviter un plantage javascript avec triggerAjaxLoad(blocfrag)
parent 8825749c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -5,6 +5,7 @@ config/remove.txt -text
dist/favicon.ico -text
dist/feed.png -text
dist/fond/ajax.html -text
dist/fond/ajax_fonctions.php -text
dist/fond/ajax_stat.html -text
dist/formulaires/choix_mots.html -text
dist/formulaires/editer_article.html -text
......@@ -18,7 +19,6 @@ dist/formulaires/editer_rubrique.php -text
dist/formulaires/editer_site.html -text
dist/formulaires/editer_site.php -text
dist/formulaires/editer_site_fonctions.php -text
dist/formulaires/formulaire_.html -text
dist/formulaires/login_forum_abo.html -text
dist/formulaires/logo_auteur.html -text
dist/formulaires/oubli/charger.php -text
......
#SET{ajaxid,id#EVAL{'include_spip("inc/acces")?substr(md5(creer_uniqid()),0,8):""'}}
<div id='#GET{ajaxid}'><INCLURE{fond=#ENV*{fond_ajax}}{env}></div>
[(#REM) calculer l'env et la cle pour le contexte du traitement ajax]
#SET{env,#ENV**|base64_encode}
#SET{env,#ENV**|supprimer_debuts|base64_encode}
#SET{cle,#EVAL{#VAL{'include_spip("inc/securiser_action")?calculer_cle_action("'}|concat{#GET{env},'"):""'}}}
<script type='text/javascript'>/*<!#EVAL{chr(91)}CDATA#EVAL{chr(91)}*/
if (window.jQuery) {
var ajaxbloc_#GET{ajaxid} = function() {
jQuery('div#[(#GET{ajaxid})]')
.prepend('<div><input type=\'hidden\' name=\'var_ajax_env\' value=\'#GET{env}\' />'
+'<input type=\'hidden\' name=\'var_ajax_cle\' value=\'#GET{cle}\' /><\/div>')
.ajaxbloc();
};
jQuery('document').ready(ajaxbloc_#GET{ajaxid});
onAjaxLoad(ajaxbloc_#GET{ajaxid});
}
/*#EVAL{chr(93)}#EVAL{chr(93)}>*/</script>
[
<div class='ajaxbloc[ env-(#GET{env})][ cle-(#GET{cle})]'>
(#INCLURE{fond=#ENV*{fond_ajax}}{env})
</div><!-- ajaxbloc -->
]
<?php
/***************************************************************************\
* SPIP, Systeme de publication pour l'internet *
* *
* Copyright (c) 2001-2008 *
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
* *
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
\***************************************************************************/
if (!defined("_ECRIRE_INC_VERSION")) return;
/* filtre necessaire pour que la pagination ajax ne soit pas plantee
* si on charge la page &debut_x=1 : car alors en cliquant sur l'item 0,
* le debut_x=0 n'existe pas, et on resterait sur 1
*/
function supprimer_debuts($env) {
$env = unserialize($env);
foreach ($env as $k => $v)
if (strpos($k,'debut_') === 0)
unset($env[$k]);
return serialize($env);
}
?>
\ No newline at end of file
[#SET{ajaxid,ids#EVAL{'include_spip("inc/acces")?substr(md5(creer_uniqid()),0,8):""'}}
<div id='#GET{ajaxid}'>(#INCLURE{fond=#ENV*{fond_ajax}}{env})</div>
[(#REM) calculer l'env et la cle pour le contexte du traitement ajax]
#SET{env,#ENV**|base64_encode}
#SET{cle,#EVAL{#VAL{'include_spip("inc/securiser_action")?calculer_cle_action("'}|concat{#GET{env},'"):""'}}}
<script type='text/javascript'>/*<!#EVAL{chr(91)}CDATA#EVAL{chr(91)}*/
if (window.jQuery) {
var ajaxbloc_#GET{ajaxid} = function() {
jQuery('div#[(#GET{ajaxid})]')
.prepend('<div><input type=\'hidden\' name=\'var_ajax_env\' value=\'#GET{env}\' />'
+'<input type=\'hidden\' name=\'var_ajax_cle\' value=\'#GET{cle}\' /><\/div>')
.ajaxbloc();
};
jQuery('document').ready(ajaxbloc_#GET{ajaxid});
onAjaxLoad(ajaxbloc_#GET{ajaxid});
}
/*#EVAL{chr(93)}#EVAL{chr(93)}>*/</script>
]
\ No newline at end of file
[
<div class='ajaxbloc[ env-(#GET{env})][ cle-(#GET{cle})]'>
(#INCLURE{fond=#ENV*{fond_ajax}}{env})
</div><!-- ajaxbloc -->
]
<div id='#ENV{ajaxid}'>
<INCLURE{fond=formulaires/#ENV{form}}{env}>
</div>
<script type='text/javascript'>/*<!#EVAL{chr(91)}CDATA#EVAL{chr(91)}*/
if (window.jQuery) {
var ajaxform_#ENV{ajaxid} = function() {
jQuery('##ENV{ajaxid}',this).formulaire_dyn_ajax();
};
jQuery('document')
.ready(ajaxform_#ENV{ajaxid});
onAjaxLoad(ajaxform_#ENV{ajaxid});
}
/*#EVAL{chr(93)}#EVAL{chr(93)}>*/</script>
......@@ -64,20 +64,47 @@ jQuery.fn.animeajax = function(end) {
return this; // don't break the chain
}
// rechargement ajax d'un formulaire dynamique implemente par formulaires/formulaire_.html
// s'il n'est pas totalement visible, scroller pour positionner
// le bloc cible en haut de l'ecran
jQuery.fn.positionner = function() {
var offset = $(this).offset({'scroll':false});
var hauteur = parseInt($(this).css('height'));
var scrolltop = self['pageYOffset'] ||
$.boxModel && document.documentElement[ 'scrollTop' ] ||
document.body[ 'scrollTop' ];
var h = $(window).height();
var scroll=0;
if (offset['top'] - 5 <= scrolltop)
scroll = offset['top'] - 5;
else if (offset['top'] + hauteur - h + 5 > scrolltop)
scroll = 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();
}
// rechargement ajax d'un formulaire dynamique implemente par formulaires/xxx.html
jQuery.fn.formulaire_dyn_ajax = function(target) {
if (typeof target == 'undefined') target = this;
this
.find('form:not(.noajax)')
.prepend("<input type='hidden' name='var_ajax' value='form' />")
.ajaxForm({
target: target,
beforeSubmit: function(){
jQuery(target).addClass('loading').animeajax();
},
success: function(){
jQuery(target).removeClass('loading');
window.location.hash = jQuery(target).attr('id');
success: function(c){
var d = jQuery('.ajax',
jQuery('<div><\/div>').html(c));
if (d.length)
c = d.html();
jQuery(target)
.removeClass('loading')
.html(c)
.positionner();
},
iframe: jQuery.browser.msie
})
......@@ -92,9 +119,17 @@ var preloaded_urls = {};
var ajaxbloc_selecteur;
jQuery.fn.ajaxbloc = function() {
var blocfrag = this;
var ajax_env = jQuery('input[@name=var_ajax_env]',this).eq(0).attr('value');
var on_pagination = function(c) {
jQuery(blocfrag)
.html(c)
.removeClass('loading')
.positionner();
}
var ajax_env = (""+this.attr('class')).match(/env-([^ ]+)/)[1];
if (!ajax_env || ajax_env==undefined) return;
var ajax_cle = jQuery('input[@name=var_ajax_cle]',this).eq(0).attr('value');
var ajax_cle = (""+this.attr('class')).match(/cle-([^ ]+)/)[1];
if (!ajax_cle || ajax_cle==undefined) return;
if (ajaxbloc_selecteur==undefined)
ajaxbloc_selecteur = '.pagination a,a.ajax';
......@@ -105,20 +140,36 @@ jQuery.fn.ajaxbloc = function() {
jQuery.ajax({"url":url[0],"success":function(r){preloaded_urls[url[0]]=r;}});
}
jQuery(this).click(function(){
jQuery(blocfrag).animeajax().addClass('loading');
var on_pagination = function(contenu) {
preloaded_urls[url[0]] = contenu;
jQuery(blocfrag).html(preloaded_urls[url[0]]);
jQuery(blocfrag).removeClass('loading');
window.location.hash = url[1];
}
if(preloaded_urls[url[0]]) {
jQuery(blocfrag)
.animeajax()
.addClass('loading');
if (preloaded_urls[url[0]]) {
on_pagination(preloaded_urls[url[0]]);
triggerAjaxLoad(blocfrag);
triggerAjaxLoad(document);
} else {
jQuery.ajax({"url":url[0],"success":on_pagination});
jQuery.ajax({
url: url[0],
success: function(c){
on_pagination(c);
preloaded_urls[url[0]] = c;
}
});
}
return false;
});
}).addClass('noajax'); // previent qu'on ajax pas deux fois le meme lien
};
// Ajaxer les formulaires qui le demandent, au demarrage
jQuery(function() {
jQuery('.ajax').formulaire_dyn_ajax();
jQuery('.ajaxbloc').ajaxbloc();
});
// ... et a chaque fois que le DOM change
onAjaxLoad(function() {
jQuery('.ajax', this)
.formulaire_dyn_ajax();
jQuery('.ajaxbloc', this)
.ajaxbloc();
});
......@@ -87,13 +87,6 @@ function balise_FORMULAIRE__dyn($form)
$action = parametre_url($action,'formulaire_action_cle',''); // nettoyer l'url des champs qui vont etre saisis
$action = parametre_url($action,'formulaire_action_args',''); // nettoyer l'url des champs qui vont etre saisis
$ajaxid = "";
if ((!$ajax=_request('var_ajax'))
AND (isset($valeurs['_ajax']))
AND $valeurs['_ajax']){
include_spip('inc/acces');
$ajaxid = substr(md5(creer_uniqid()),0,8);
}
if (isset($valeurs['_action'])){
$securiser_action = charger_fonction('securiser_action','inc');
$secu = inc_securiser_action_dist(reset($valeurs['_action']),end($valeurs['_action']),'',-1);
......@@ -102,10 +95,11 @@ function balise_FORMULAIRE__dyn($form)
. "<input type='hidden' name='hash' value='".$secu['hash']."' />";
}
return array($ajaxid?"formulaires/formulaire_":"formulaires/$form", 0,
return array("formulaires/$form",
3600,
array_merge(
$valeurs,
array(
$valeurs,
array(
'form' => $form,
'action' => $action,
'formulaire_args' => base64_encode(serialize($args)),
......@@ -114,8 +108,8 @@ function balise_FORMULAIRE__dyn($form)
'message_ok' => $message_ok,
'message_erreur' => $message_erreur,
'editable' => $editable?' ':'',
),
$ajaxid?array('ajaxid' => "id$ajaxid"):array())
)
)
);
}
......
......@@ -147,10 +147,13 @@ function traiter_formulaires_dynamiques(){
|| (count($_POST["erreurs_$form"] = call_user_func_array($verifier,$args))==0))
&& ($traiter = charger_fonction("traiter","formulaires/$form/",true))
) {
$_POST["message_ok_$form"] = call_user_func_array($traiter,$args);
$rev = call_user_func_array($traiter,$args);
// traiter peut retourner soit un message, soit un array(editable,message)
if (is_array($_POST["message_ok_$form"]))
list($_POST["editable_$form"],$_POST["message_ok_$form"]) = $_POST["message_ok_$form"];
if (is_array($rev)) {
$_POST["editable_$form"] = $rev[0];
$_POST["message_ok_$form"] = $rev[1];
} else
$rev = $_POST["message_ok_$form"];
}
// si le formulaire a ete soumis en ajax, on le renvoie direct !
if (_request('var_ajax')){
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter