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