[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / prive / javascript / layer.js
1 var url_chargee = new Array();
2 var xhr_actifs = {};
3
4 //
5 // Fonctions pour mini_nav
6 //
7
8 function slide_horizontal (couche, slide, align, depart, etape ) {
9
10 var obj = jQuery("#"+couche);
11
12 if (!obj.length) return;
13 obj = obj.get(0);
14 if (!etape) {
15 if (align == 'left') depart = obj.scrollLeft;
16 else depart = obj.firstChild.offsetWidth - obj.scrollLeft;
17 etape = 0;
18 }
19 etape = Math.round(etape) + 1;
20 pos = Math.round(depart) + Math.round(((slide - depart) / 10) * etape);
21
22 if (align == 'left') obj.scrollLeft = pos;
23 else obj.scrollLeft = obj.firstChild.offsetWidth - pos;
24 if (etape < 10) setTimeout("slide_horizontal('"+couche+"', '"+slide+"', '"+align+"', '"+depart+"', '"+etape+"')", 60);
25 //else obj.scrollLeft = slide;
26 }
27
28 function changerhighlight (couche) {
29 jQuery(couche)
30 .addClass('on')
31 .siblings()
32 .not(couche)
33 .removeClass('on');
34 jQuery('.petite-racine.on').removeClass('on');
35 }
36
37 function aff_selection (arg, idom, url, event) {
38 var noeud = jQuery("#"+idom);
39 if (noeud.length) {
40 noeud.hide();
41 charger_node_url(url+arg, noeud.get(0), '','',event);
42 }
43 return false;
44 }
45
46 // selecteur de rubrique et affichage de son titre dans le bandeau
47
48 function aff_selection_titre(titre, id, idom, nid)
49 {
50 var t = jQuery('#titreparent');
51 var p = t.closest('form');
52 t.attr('value',titre);
53 p.find('#'+nid).attr('value',id).trigger('change'); // declencher le onchange
54 p.find("#"+idom).hide('fast');
55 if (p.is('.submit_plongeur')) p.get(p.length-1).submit();
56 else p.find("#"+idom).prev('div').find('a').eq(0).focus();
57 }
58
59
60 /**
61 * Utilise dans inc/plonger
62 * @param id
63 * @param racine
64 * @param url
65 * @param col
66 * @param sens
67 * @param informer
68 * @param event
69 */
70 function aff_selection_provisoire(id, racine, url, col, sens,informer,event) {
71 if(url.href == 'javascript:void(0)'){
72 slide_horizontal(racine + '_principal', ((col-1)*150), sens);
73 aff_selection (id, racine + "_selection", informer);
74 }
75 else{
76 charger_id_url(url.href,
77 racine + '_col_' + (col+1),
78 function() {
79 slide_horizontal(racine + '_principal', ((col-1)*150), sens);
80 aff_selection (id, racine + "_selection", informer);
81 },
82 event);
83 }
84 // empecher le chargement non Ajax
85 return false;
86 }
87
88 /**
89 * Lanche une requete Ajax a chaque frappe au clavier dans une balise de saisie.
90 * Si l'entree redevient vide, rappeler l'URL initiale si dispo.
91 * Sinon, controler au retour si le resultat est unique,
92 * auquel cas forcer la selection.
93 * utlise dans inc/selectionner
94 * @param valeur
95 * @param rac
96 * @param url
97 * @param img
98 * @param nid
99 * @param init
100 */
101 function onkey_rechercher(valeur, rac, url, img, nid, init) {
102 var Field = jQuery("#"+rac).get(0);
103 if (!valeur.length) {
104 init = jQuery("#"+init).get(0);
105 if (init && init.href) { charger_node_url(init.href, Field);}
106 } else {
107 charger_node_url(url+valeur,
108 Field,
109 function () {
110 var n = Field.childNodes.length - 1;
111 // Safari = 0 & Firefox = 1 !
112 // et gare aux negatifs en cas d'abort
113 if ((n == 1)) {
114 noeud = Field.childNodes[n].firstChild;
115 if (noeud.title)
116 // cas de la rubrique, pas des auteurs
117 aff_selection_titre(noeud.firstChild.nodeValue, noeud.title, rac, nid);
118 }
119 },
120 img);
121 }
122 return false;
123 }
124
125
126 // Recupere tous les formulaires de la page
127 // (ou du fragment qu'on vient de recharger en ajax)
128 // et leur applique les comportements js souhaites
129 // ici :
130 // * utiliser ctrl-s, F8 etc comme touches de sauvegarde
131 var verifForm_clicked=false;
132 function verifForm(racine) {
133 verifForm_clicked = false; // rearmer quand on passe ici (il y a eu de l'ajax par exemple)
134 if (!jQuery) return; // appels ajax sur iframe
135 // Clavier pour sauver (cf. crayons)
136 // cf http://www.quirksmode.org/js/keys.html
137 if (!jQuery.browser.msie)
138 // keypress renvoie le charcode correspondant au caractere frappe (ici s)
139 jQuery('form:not(.bouton_action_post)', racine||document).not('.verifformok')
140 .keypress(function(e){
141 if (
142 ((e.ctrlKey && (
143 /* ctrl-s ou ctrl-maj-S, firefox */
144 (((e.charCode||e.keyCode) == 115) || ((e.charCode||e.keyCode) == 83))
145 /* ctrl-s, safari */
146 || (e.charCode==19 && e.keyCode==19)
147 )
148 ) /* ctrl-s, Opera Mac */
149 || (e.keyCode==19 && jQuery.browser.opera))
150 && !verifForm_clicked
151 ) {
152 verifForm_clicked = true;
153 jQuery(this).find('input[type=submit]')
154 .click();
155 return false;
156 }
157 }).addClass('verifformok');
158 else
159 // keydown renvoie le keycode correspondant a la touche pressee (ici F8)
160 jQuery('form:not(.bouton_action_post)', racine||document).not('.verifformok')
161 .keydown(function(e){
162 //jQuery('#ps').after("<div>ctrl:"+e.ctrlKey+"<br />charcode:"+e.charCode+"<br />keycode:"+e.keyCode+"<hr /></div>");
163 if (!e.charCode && e.keyCode == 119 /* F8, windows */ && !verifForm_clicked){
164 verifForm_clicked = true;
165 jQuery(this).find('input[type=submit]')
166 .click();
167 return false;
168 }
169 }).addClass('verifformok');
170 }
171
172 // La fonction qui fait vraiment le travail decrit ci-dessus.
173 // Son premier argument est deja le noeud du DOM
174 // et son resultat booleen est inverse ce qui lui permet de retourner
175 // le gestionnaire Ajax comme valeur non fausse
176 function AjaxSqueezeNode(trig, target, f, event) {
177 var i, callback;
178
179 // retour std si pas precise: affecter ce noeud avec ce retour
180 if (!f) {
181 callback = function() { verifForm(this);}
182 }
183 else {
184 callback = function(res,status) {
185 f.apply(this,[res,status]);
186 verifForm(this);
187 }
188 }
189
190 valid = false;
191 if (typeof(window['_OUTILS_DEVELOPPEURS']) != 'undefined'){
192 if (!(navigator.userAgent.toLowerCase().indexOf("firefox/1.0")))
193 valid = (typeof event == 'object') && (event.altKey || event.metaKey);
194 }
195
196 if (typeof(trig) == 'string') {
197 // laisser le choix de la touche enfoncee au moment du clic
198 // car beaucoup de systemes en prenne une a leur usage
199 if (valid) {
200 window.open(trig+'&transformer_xml=valider_xml');
201 } else {
202 jQuery(target).animeajax();
203 }
204 res = jQuery.ajax({
205 "url":trig,
206 "complete": function(r,s) {
207 AjaxRet(r,s,target, callback);
208 jQuery(target).endLoading();
209 }
210 });
211 return res;
212 }
213
214 if(valid) {
215 //open a blank window
216 var doc = window.open("","valider").document;
217 //create a document to enable receiving the result of the ajax post
218 doc.open();
219 doc.close();
220 //set the element receiving the ajax post
221 target = doc.body;
222 }
223 else {
224 jQuery(target).animeajax();
225 }
226
227 jQuery(trig).ajaxSubmit({
228 "target": target,
229 "success": function(res,status) {
230 if(status=='error') return this.html('Erreur HTTP');
231 callback.apply(this,[res,status]);
232 },
233 "beforeSubmit":function (vars) {
234 if (valid)
235 vars.push({"name":"transformer_xml","value":"valider_xml"});
236 return true;
237 }
238 });
239 return true;
240 }
241
242
243 function AjaxRet(res,status, target, callback) {
244 if (res.aborted) return;
245 if (status=='error') return jQuery(target).html('HTTP Error');
246
247 // Inject the HTML into all the matched elements
248 jQuery(target)
249 .html(res.responseText)
250 // Execute callback
251 .each(callback, [res.responseText, status]);
252 }
253
254
255 // Comme AjaxSqueeze,
256 // mais avec un cache sur le noeud et un cache sur la reponse
257 // et une memorisation des greffes en attente afin de les abandonner
258 // (utile surtout a la frappe interactive au clavier)
259 // De plus, la fonction optionnelle n'a pas besoin de greffer la reponse.
260
261 function charger_id_url(myUrl, myField, jjscript, event) {
262 var Field = jQuery("#"+myField);
263 if (!Field.length) return true;
264
265 if (!myUrl) {
266 Field.empty();
267 retour_id_url(Field.get(0), jjscript);
268 return true; // url vide, c'est un self complet
269 }
270 else
271 return charger_node_url(myUrl, Field.get(0), jjscript, jQuery('#'+'img_' + myField).get(0), event);
272 }
273
274 // La suite
275 function charger_node_url(myUrl, Field, jjscript, img, event) {
276 // disponible en cache ?
277 if (url_chargee[myUrl]) {
278 var el = jQuery(Field).html(url_chargee[myUrl])[0];
279 retour_id_url(el, jjscript);
280 jQuery.spip.triggerAjaxLoad(el);
281 return false;
282 }
283 else {
284 if (img) img.style.visibility = "visible";
285 if (xhr_actifs[Field]) { xhr_actifs[Field].aborted = true;xhr_actifs[Field].abort(); }
286 xhr_actifs[Field] = AjaxSqueezeNode(myUrl,
287 Field,
288 function (r) {
289 xhr_actifs[Field] = undefined;
290 if (img) img.style.visibility = "hidden";
291 url_chargee[myUrl] = r;
292 retour_id_url(Field, jjscript);
293 slide_horizontal($(Field).children().attr("id")+'_principal', $(Field).width() , $(Field).css("text-align"));
294 },
295 event);
296 return false;
297 }
298 }
299
300 function retour_id_url(Field, jjscript) {
301 jQuery(Field).css({'visibility':'visible','display':'block'});
302 if (jjscript) jjscript();
303 }
304
305 function charger_node_url_si_vide(url, noeud, gifanime, jjscript,event) {
306
307 if (noeud.style.display !='none') {
308 noeud.style.display='none';}
309 else {
310 if (noeud.innerHTML != "") {
311 noeud.style.visibility = "visible";
312 noeud.style.display = "block";
313 } else {
314 charger_node_url(url, noeud,'',gifanime,event);
315 }
316 }
317 return false;
318 }
319
320 // Lancer verifForm
321 jQuery(function(){
322 verifForm();
323 onAjaxLoad(verifForm);
324 });