MOD fin du système de page et cleanage du code
[euphorik.git] / js / euphorik.js
1 // coding: utf-8
2
3 /**
4 * Contient la base javascript pour le site euphorik.ch.
5 * Chaque page possède son propre fichier js nommé "page<nom de la page>.js".
6 * Auteur : GBurri
7 * Date : 6.11.2007
8 */
9
10 /**
11 * La configuration.
12 * Normalement 'const' à la place de 'var' mais non supporté par IE7.
13 */
14 var conf = {
15 nbMessageAffiche : 10, // (par page)
16 pseudoDefaut : "<nick>",
17 tempsAffichageMessageDialogue : 4000, // en ms
18 smiles : {
19 "smile" : [/:\)/g, /:-\)/g],
20 "bigsmile" : [/:D/g, /:-D/g],
21 "clin" : [/;\)/g, /;-\)/g],
22 "cool" : [/8\)/g, /8-\)/g],
23 "eheheh" : [/:P/g, /:-P/g],
24 "oh" : [/:o/g, /:O/g],
25 "pascontent" : [/>\(/g, /&gt;\(/g],
26 "sniff" : [/:\(/g, /:-\(/g],
27 "argn" : [/\[:argn\]/g],
28 "bunny" : [/\[:lapin\]/g],
29 "chat" : [/\[:chat\]/g],
30 "renne" : [/\[:renne\]/g],
31 "lol" : [/\[:lol\]/g],
32 "spliff" : [/\[:spliff\]/g],
33 "star" : [/\[:star\]/g],
34 "triste" : [/\[:triste\]/g],
35 "kirby" : [/\[:kirby\]/g]
36 }
37 }
38
39 ///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 String.prototype.trim = function()
42 {
43 return this.replace(/^\s+|\s+$/g, "");
44 }
45
46 String.prototype.ltrim = function()
47 {
48 return this.replace(/^\s+/, "");
49 }
50
51 String.prototype.rtrim = function()
52 {
53 return this.replace(/\s+$/, "");
54 }
55
56 String.prototype.dump = function(titre)
57 {
58 titre = titre == undefined ? "" : titre
59 if (typeof dump != "undefined")
60 {
61 dump("\n--- EUPHORIK.CH : " + titre + " ---\n")
62 dump(this)
63 dump("\n------\n")
64 }
65 }
66
67 ///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 /**
70 * Cette classe regroupe des fonctions utilitaires (helpers).
71 */
72 function Util()
73 {
74 if(typeof XMLSerializer != "undefined")
75 this.serializer = new XMLSerializer()
76
77 jQuery("#info .fermer").click(function(){
78 jQuery("#info").slideUp(50)
79 })
80 }
81
82 /**
83 * Affiche une boite de dialogue avec un message à l'intérieur.
84 * @param message le message (string)
85 * @param type voir 'messageType'. par défaut messageType.informatif
86 * @param les boutons sous la forme d'un objet ou les clefs sont les labels des boutons
87 * et les valeurs les fonctions executées lorsqu'un bouton est activé.
88 */
89 Util.prototype.messageDialogue = function(message, type, boutons)
90 {
91 if (type == undefined)
92 type = messageType.informatif
93
94 if (this.timeoutMessageDialogue != undefined)
95 clearTimeout(this.timeoutMessageDialogue)
96
97 var fermer = function(){jQuery("#info").slideUp(100)}
98 fermer()
99
100 jQuery("#info .message").html(message)
101 switch(type)
102 {
103 case messageType.informatif : jQuery("#info #icone").attr("class", "information"); break
104 case messageType.question : jQuery("#info #icone").attr("class", "interrogation"); break
105 case messageType.erreur : jQuery("#info #icone").attr("class", "exclamation"); break
106 }
107 jQuery("#info .boutons").html("")
108 for (var b in boutons)
109 jQuery("#info .boutons").append("<div>" + b + "</div>").find("div:last").click(boutons[b]).click(fermer)
110
111 jQuery("#info").slideDown(200)
112 this.timeoutMessageDialogue = setTimeout(fermer, conf.tempsAffichageMessageDialogue)
113 }
114 var messageType = {informatif: 0, question: 1, erreur: 2}
115
116 /**
117 * Transforme un document XML en string.
118 */
119 Util.prototype.serializeXML = function(documentXML)
120 {
121 if (this.serializer)
122 return this.serializer.serializeToString(documentXML)
123 else
124 return documentXML.xml
125 }
126
127 var documentXMLBase = undefined // singleton
128 Util.prototype.creerDocumentXMLAction = function()
129 {
130 // FIXME : essayer de garder le doc de base en cache (singleton) et d'en retourner une copie
131 if (true)//documentXMLBase == undefined)
132 {
133 if (document.implementation && document.implementation.createDocument)
134 {
135 // var doc = document.implementation.createDocument("", "action", null)
136 var parser = new DOMParser();
137 documentXMLBase = parser.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
138 //alert(this.serializeXML(doc))
139 }
140 else if (window.ActiveXObject)
141 {
142 documentXMLBase = new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM")
143 documentXMLBase.appendChild(doc.createElement("action"));
144 //doc.loadXML("<action></action>")
145 //alert(doc.documentElement)
146 //doc.createElement("action")
147 }
148 }
149 return documentXMLBase
150 }
151
152 Util.prototype.xmlVersAction = function(xml)
153 {
154 //return {action: this.to_utf8(this.serializeXML(xml /*, "UTF-8"*/))}
155 return {action: this.serializeXML(xml)}
156 }
157
158 Util.prototype.md5 = function(chaine)
159 {
160 return hex_md5(chaine)
161 }
162
163 // pompé de http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130
164 Util.prototype.setSelectionRange = function(input, selectionStart, selectionEnd)
165 {
166 if (input.setSelectionRange)
167 {
168 input.focus()
169 input.setSelectionRange(selectionStart, selectionEnd)
170 }
171 else if (input.createTextRange)
172 {
173 var range = input.createTextRange()
174 range.collapse(true)
175 range.moveEnd('character', selectionEnd)
176 range.moveStart('character', selectionStart)
177 range.select()
178 }
179 }
180
181 Util.prototype.setCaretToEnd = function(input)
182 {
183 this.setSelectionRange(input, input.value.length, input.value.length)
184 }
185 Util.prototype.setCaretToBegin = function(input)
186 {
187 this.setSelectionRange(input, 0, 0)
188 }
189 Util.prototype.setCaretToPos = function(input, pos)
190 {
191 this.setSelectionRange(input, pos, pos)
192 }
193 Util.prototype.selectString = function(input, string)
194 {
195 var match = new RegExp(string, "i").exec(input.value)
196 if (match)
197 {
198 this.setSelectionRange (input, match.index, match.index + match[0].length)
199 }
200 }
201 Util.prototype.replaceSelection = function(input, replaceString) {
202 if (input.setSelectionRange)
203 {
204 var selectionStart = input.selectionStart
205 var selectionEnd = input.selectionEnd
206 input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd)
207
208 if (selectionStart != selectionEnd) // has there been a selection
209 this.setSelectionRange(input, selectionStart, selectionStart + replaceString.length)
210 else // set caret
211 this.setCaretToPos(input, selectionStart + replaceString.length)
212 }
213 else if (document.selection)
214 {
215 var range = document.selection.createRange();
216 if (range.parentElement() == input)
217 {
218 var isCollapsed = range.text == ''
219 range.text = replaceString
220 if (!isCollapsed)
221 {
222 // there has been a selection
223 // it appears range.select() should select the newly
224 // inserted text but that fails with IE
225 range.moveStart('character', -replaceString.length);
226 range.select();
227 }
228 }
229 }
230 }
231
232 ///////////////////////////////////////////////////////////////////////////////////////////////////
233
234 function Pages()
235 {
236 this.pageCourante = null
237 this.pages = {}
238 }
239
240 Pages.prototype.ajouterPage = function(page)
241 {
242 page.pages = this // la magie des langages dynamiques : le foutoire
243 this.pages[page.nom] = page
244 }
245
246 Pages.prototype.afficherPage = function(nomPage, forcerChargement)
247 {
248 if (forcerChargement == undefined) forcerChargement = false
249
250 var page = this.pages[nomPage]
251 if (page == undefined || (!forcerChargement && page == this.pageCourante)) return
252
253 if (this.pageCourante != null && this.pageCourante.decharger)
254 this.pageCourante.decharger()
255
256 jQuery("#menu div").removeClass("courante")
257 jQuery("#menu div." + nomPage).addClass("courante")
258
259 this.pageCourante = page
260 jQuery("#page").html(this.pageCourante.contenu()).removeClass().addClass(this.pageCourante.nom)
261
262 if (this.pageCourante.charger)
263 this.pageCourante.charger()
264 }
265
266 ///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 function Formateur()
269 {
270 this.smiles = conf.smiles
271 this.protocoles = "http|https|ed2k"
272
273 this.regexUrl = new RegExp("(?:(?:" + this.protocoles + ")://|www\\.)[^ ]*", "gi")
274 this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i")
275 this.regexDomaine = new RegExp("^(?:(?:" + this.protocoles + ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i")
276 this.regexTestProtocoleExiste = new RegExp("^(?:" + this.protocoles + ")://.*$", "i")
277 this.regexNomProtocole = new RegExp("^(.*?)://")
278 }
279
280 /**
281 * Formate un pseudo saise par l'utilisateur.
282 * @param pseudo le pseudo brut
283 * @return le pseudo filtré
284 */
285 Formateur.prototype.filtrerInputPseudo = function(pseudo)
286 {
287 return pseudo.replace(/{|}/g, "").trim()
288 }
289
290 Formateur.prototype.getSmilesHTML = function()
291 {
292 var XHTML = ""
293 for (var sNom in this.smiles)
294 {
295 XHTML += "<img class=\"" + sNom + "\" src=\"img/smileys/" + sNom + ".gif\" />"
296 }
297 return XHTML
298 }
299
300 Formateur.prototype.traitementComplet = function(M, pseudo)
301 {
302 return this.traiterSmiles(this.traiterURL(this.remplacerBalisesHTML(M), pseudo))
303 }
304
305 /**
306 * FIXME : Cette méthode est attrocement lourde ! A optimiser.
307 */
308 Formateur.prototype.traiterSmiles = function(M)
309 {
310 for (var sNom in this.smiles)
311 {
312 ss = this.smiles[sNom]
313 for (var i = 0; i < ss.length; i++)
314 M = M.replace(ss[i], "<img src=\"img/smileys/" + sNom + ".gif\" />")
315 }
316 return M
317 }
318
319 Formateur.prototype.remplacerBalisesHTML = function(M)
320 {
321 return M.replace(/</g, "&lt;").replace(/>/g, "&gt;")
322 }
323
324 Formateur.prototype.traiterURL = function(M, pseudo)
325 {
326 thisFormateur = this
327
328 if (pseudo == undefined)
329 pseudo = ""
330
331 var traitementUrl = function(url)
332 {
333 // si ya pas de protocole on rajoute "http://"
334 if (!thisFormateur.regexTestProtocoleExiste.test(url))
335 url = "http://" + url
336 var extension = thisFormateur.getShort(url)
337 return "<a " + (extension[1] ? "title=\"" + thisFormateur.traiterPourFenetreLightBox(pseudo, url) + ": " + thisFormateur.traiterPourFenetreLightBox(M, url) + "\"" + " rel=\"lightbox[groupe]\"" : "") + " href=\"" + url + "\" >[" + extension[0] + "]</a>"
338 }
339 return M.replace(this.regexUrl, traitementUrl)
340 }
341
342 /**
343 * Renvoie une version courte de l'url.
344 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
345 */
346 Formateur.prototype.getShort = function(url)
347 {
348 var estUneImage = false
349 var versionShort = null
350 var rechercheImg = this.regexImg.exec(url)
351 //alert(url)
352 if (rechercheImg != null)
353 {
354 versionShort = rechercheImg[1].toLowerCase()
355 if (versionShort == "jpeg") versionShort = "jpg" // jpeg -> jpg
356 estUneImage = true
357 }
358 else
359 {
360 var rechercheDomaine = this.regexDomaine.exec(url)
361 if (rechercheDomaine != null && rechercheDomaine.length >= 2)
362 versionShort = rechercheDomaine[1]
363 else
364 {
365 var nomProtocole = this.regexNomProtocole.exec(url)
366 if (nomProtocole != null && nomProtocole.length >= 2)
367 versionShort = nomProtocole[1]
368 }
369 }
370
371 return [versionShort == null ? "url" : versionShort, estUneImage]
372 }
373
374 /**
375 * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
376 */
377 Formateur.prototype.traiterPourFenetreLightBox = function(M, urlCourante)
378 {
379 thisFormateur = this
380 var traitementUrl = function(url)
381 {
382 return "[" + thisFormateur.getShort(url)[0] + (urlCourante == url ? ": image courante" : "") + "]"
383 }
384
385 return this.remplacerBalisesHTML(M).replace(this.regexUrl, traitementUrl)
386 }
387
388
389 ///////////////////////////////////////////////////////////////////////////////////////////////////
390
391 var statutType = {enregistre: 0, identifie: 1, non_identifie: 2}
392
393 function Client(util)
394 {
395 this.util = util
396
397 this.cookie = null
398 this.regexCookie = new RegExp("^cookie=([^;]*)")
399
400 // Obsolète
401 //this.captchaCrypt = null
402
403 // données personnels
404 this.resetDonneesPersonnelles()
405
406 this.setStatut(statutType.non_identifie)
407
408 // le dernier message d'erreur recut du serveur (par exemple une connexion foireuse : "login impossible")
409 this.dernierMessageErreur = ""
410 }
411
412 Client.prototype.resetDonneesPersonnelles = function()
413 {
414 this.pseudo = conf.pseudoDefaut
415 this.login = ""
416 this.password = ""
417 this.email = ""
418 this.css = jQuery("link#cssPrincipale").attr("href")
419
420 this.pagePrincipale = 1
421
422 // les conversations, une conversation est un objet possédant les attributs suivants :
423 // - racine (entier)
424 // - page (entier)
425 this.conversations = new Array()
426 }
427
428 Client.prototype.setCss = function(css)
429 {
430 if (this.css == css)
431 return
432
433 this.css = css
434 jQuery("link#cssPrincipale").attr("href", this.css)
435 this.majMenu()
436
437 if (this.identifie())
438 this.flush()
439 }
440
441 Client.prototype.pageSuivante = function(numConv)
442 {
443 if (numConv < 0 && this.pagePrincipale > 1)
444 this.pagePrincipale -= 1
445 else if (this.conversations[numConv].page > 1)
446 this.conversations[numConv].page -= 1
447 this.flush(false)
448 }
449
450 Client.prototype.pagePrecedente = function(numConv)
451 {
452 if (numConv < 0)
453 this.pagePrincipale += 1
454 else
455 this.conversations[numConv].page += 1
456 this.flush(false)
457 }
458
459 /**
460 * Définit la première page pour la conversation donnée.
461 * @return true si la page a changé sinon false
462 */
463 Client.prototype.goPremierePage = function(numConv)
464 {
465 if (numConv < 0)
466 {
467 if (this.pagePrincipale == 1)
468 return false
469 this.pagePrincipale = 1
470 }
471 else
472 {
473 if (this.conversations[numConv].page == 1)
474 return false
475 this.conversations[numConv].page = 1
476 }
477 this.flush(false)
478 return true
479 }
480
481 /**
482 * Ajoute une conversation à la vue de l'utilisateur.
483 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
484 * @param racines la racine de la conversation
485 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
486 */
487 Client.prototype.ajouterConversation = function(racine)
488 {
489 // vérification s'il elle n'existe pas déjà
490 for (var i = 0; i < this.conversations.length; i++)
491 if (this.conversations[i].racine == racine)
492 return false
493
494 this.conversations.push({racine : racine, page : 1})
495 this.flush(false)
496 return true
497 }
498
499 Client.prototype.supprimerConversation = function(num)
500 {
501 if (num < 0 || num >= this.conversations.length) return
502
503 // décalage TODO : supprimer le dernier élément
504 for (var i = num; i < this.conversations.length - 1; i++)
505 this.conversations[i] = this.conversations[i+1]
506 this.conversations.pop()
507
508 this.flush(false)
509 }
510
511 Client.prototype.getXMLlogin = function(login, password)
512 {
513 var XMLDocument = this.util.creerDocumentXMLAction()
514 XMLDocument.documentElement.setAttribute("name", "login")
515
516 var nodeLogin = XMLDocument.createElement("login")
517 nodeLogin.appendChild(XMLDocument.createTextNode(login))
518 XMLDocument.documentElement.appendChild(nodeLogin)
519
520 var nodePassword = XMLDocument.createElement("password")
521 nodePassword.appendChild(XMLDocument.createTextNode(password))
522 XMLDocument.documentElement.appendChild(nodePassword)
523
524 return XMLDocument
525 }
526
527 Client.prototype.getXMLloginCookie = function()
528 {
529 var XMLDocument = this.util.creerDocumentXMLAction()
530 XMLDocument.documentElement.setAttribute("name", "login")
531
532 var nodeCookie = XMLDocument.createElement("cookie")
533 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
534 XMLDocument.documentElement.appendChild(nodeCookie)
535
536 return XMLDocument
537 }
538
539 /* Obsolète
540 Client.prototype.getXMLloginCaptcha = function(captchaCrypt, captchaInput)
541 {
542 var XMLDocument = this.util.creerDocumentXMLAction()
543 XMLDocument.documentElement.setAttribute("name", "loginCaptcha")
544
545 var nodecaptchaCrypt = XMLDocument.createElement("captchaCrypt")
546 nodecaptchaCrypt.appendChild(XMLDocument.createTextNode(captchaCrypt))
547 XMLDocument.documentElement.appendChild(nodecaptchaCrypt)
548
549 var nodecaptchaInput = XMLDocument.createElement("captchaInput")
550 nodecaptchaInput.appendChild(XMLDocument.createTextNode(captchaInput))
551 XMLDocument.documentElement.appendChild(nodecaptchaInput)
552
553 return XMLDocument
554 }*/
555
556 /* Obsolète
557 Client.prototype.getXMLgenerationCaptcha = function()
558 {
559 var XMLDocument = this.util.creerDocumentXMLAction()
560 XMLDocument.documentElement.setAttribute("name", "generationCaptcha")
561
562 return XMLDocument
563 }*/
564
565 Client.prototype.getXMLEnregistrement = function(login, password)
566 {
567 var XMLDocument = this.util.creerDocumentXMLAction()
568 XMLDocument.documentElement.setAttribute("name", "register")
569
570 var nodeLogin = XMLDocument.createElement("login")
571 nodeLogin.appendChild(XMLDocument.createTextNode(login))
572 XMLDocument.documentElement.appendChild(nodeLogin)
573
574 var nodePassword = XMLDocument.createElement("password")
575 nodePassword.appendChild(XMLDocument.createTextNode(password))
576 XMLDocument.documentElement.appendChild(nodePassword)
577
578 return XMLDocument
579 }
580
581 /**
582 * Sérialize le profile en XML.
583 * TODO : méthode assez lourde, 3.25ms de moyenne
584 */
585 Client.prototype.getXMLProfile = function()
586 {
587 var XMLDocument = this.util.creerDocumentXMLAction()
588 XMLDocument.documentElement.setAttribute("name", "profile")
589
590 var nodeCookie = XMLDocument.createElement("cookie")
591 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
592 XMLDocument.documentElement.appendChild(nodeCookie)
593
594 var nodeLogin = XMLDocument.createElement("login")
595 nodeLogin.appendChild(XMLDocument.createTextNode(this.login))
596 XMLDocument.documentElement.appendChild(nodeLogin)
597
598 var nodePassword = XMLDocument.createElement("password")
599 nodePassword.appendChild(XMLDocument.createTextNode(this.password))
600 XMLDocument.documentElement.appendChild(nodePassword)
601
602 var nodePseudo = XMLDocument.createElement("pseudo")
603 nodePseudo.appendChild(XMLDocument.createTextNode(this.pseudo))
604 XMLDocument.documentElement.appendChild(nodePseudo)
605
606 var nodeEmail = XMLDocument.createElement("email")
607 nodeEmail.appendChild(XMLDocument.createTextNode(this.email))
608 XMLDocument.documentElement.appendChild(nodeEmail)
609
610 var nodeCSS = XMLDocument.createElement("css")
611 nodeCSS.appendChild(XMLDocument.createTextNode(this.css))
612 XMLDocument.documentElement.appendChild(nodeCSS)
613
614 var nodePagePrincipale = XMLDocument.createElement("pagePrincipale")
615 nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale < 1 ? 1 : this.pagePrincipale))
616 XMLDocument.documentElement.appendChild(nodePagePrincipale)
617
618 // mémorise les conversations affichées
619 for (var i = 0; i < this.conversations.length; i++)
620 {
621 var nodeConv = XMLDocument.createElement("conversation")
622 XMLDocument.documentElement.appendChild(nodeConv)
623
624 var nodeRacine = XMLDocument.createElement("racine")
625 nodeRacine.appendChild(XMLDocument.createTextNode(this.conversations[i].racine))
626 nodeConv.appendChild(nodeRacine)
627
628 var nodePage = XMLDocument.createElement("page")
629 nodePage.appendChild(XMLDocument.createTextNode(this.conversations[i].page))
630 nodeConv.appendChild(nodePage)
631 }
632
633 return XMLDocument
634 }
635
636 /**
637 * Renvoie null si pas définit.
638 */
639 Client.prototype.getCookie = function()
640 {
641 var cookie = this.regexCookie.exec(document.cookie)
642 if (cookie == null) this.cookie = null
643 else this.cookie = cookie[1]
644 }
645
646 Client.prototype.delCookie = function()
647 {
648 document.cookie = "cookie=; max-age=0"
649 }
650
651 Client.prototype.setCookie = function(cookie)
652 {
653 if (this.cookie == null)
654 return
655
656 document.cookie =
657 "cookie="+this.cookie+
658 "; max-age=" + (60 * 60 * 24 * 365)
659 }
660
661 Client.prototype.identifie = function()
662 {
663 return this.statut == statutType.enregistre || this.statut == statutType.identifie
664 }
665
666 Client.prototype.setStatut = function(statut)
667 {
668 if(typeof(statut) == "string")
669 {
670 statut =
671 statut == "enregistre" ?
672 statutType.enregistre : (statut == "identifie" ? statutType.identifie : statutType.non_identifie)
673 }
674
675 if (statut == this.statut) return
676
677 this.statut = statut
678 this.majMenu()
679 }
680
681 /**
682 * Demande la génération d'un captcha au serveur et l'affiche.
683 */
684 /* Obsolète
685 Client.prototype.afficherCaptcha = function(query)
686 {
687 var thisClient = this
688
689 $.post("request", this.util.xmlVersAction(this.getXMLgenerationCaptcha()),
690 function(data, textStatus)
691 {
692 var chemin = jQuery("chemin", data.documentElement).text()
693 thisClient.captchaCrypt = jQuery("captchaCrypt", data.documentElement).text()
694 jQuery(query).prepend(
695 "<p id=\"captcha\" >Es-tu un bot ? <img class=\"captchaImg\" src=\"" + chemin + "\" />" +
696 "<input name=\"captchaInput\" type=\"text\" size=\"5\" max_length=\"5\" ></p>"
697 )
698 }
699 )
700 }
701
702 Client.prototype.cacherCaptcha = function()
703 {
704 jQuery("#captcha").remove()
705 }*/
706
707 /**
708 * Effectue la connexion vers le serveur.
709 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
710 * S'il existe un cookie en local on s'authentifie directement avec lui.
711 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
712 */
713 Client.prototype.connexionCookie = function()
714 {
715 this.getCookie()
716 if (this.cookie == null) return false;
717 return this.connexion(this.util.xmlVersAction(this.getXMLloginCookie()))
718 }
719
720 Client.prototype.connexionLogin = function(login, password)
721 {
722 return this.connexion(this.util.xmlVersAction(this.getXMLlogin(login, password)))
723 }
724
725 /* Obsolète
726 Client.prototype.connexionCaptcha = function()
727 {
728 return this.connexion(this.util.xmlVersAction(this.getXMLloginCaptcha(this.captchaCrypt, jQuery("#captcha input").val())))
729 }*/
730
731 Client.prototype.enregistrement = function(login, password)
732 {
733 if (this.identifie())
734 {
735 this.login = login
736 this.password = password
737 if(this.flush())
738 this.setStatut(statutType.enregistre)
739 return true
740 }
741 else
742 {
743 if (login == undefined) login = ""
744 if (password == undefined) password = ""
745 return this.connexion(this.util.xmlVersAction(this.getXMLEnregistrement(login, password)))
746 }
747 }
748
749 Client.prototype.connexion = function(action)
750 {
751 //action.action.dump()
752 thisClient = this
753 jQuery.ajax(
754 {
755 async: false,
756 type: "POST",
757 url: "request",
758 dataType: "xml",
759 data: action,
760 success:
761 function(data)
762 {
763 //thisClient.util.serializer.serializeToString(data).dump("Charger client")
764 thisClient.chargerDonnees(data)
765 }
766 }
767 )
768 return this.identifie()
769 }
770
771 Client.prototype.deconnexion = function()
772 {
773 this.setStatut(statutType.non_identifie) // deconnexion
774 this.resetDonneesPersonnelles()
775 this.delCookie ()
776 }
777
778 Client.prototype.chargerDonnees = function(data)
779 {
780 var thisClient = this
781
782 this.setStatut(jQuery("statut", data.documentElement).text())
783
784 if (this.identifie())
785 {
786 this.cookie = jQuery("cookie", data.documentElement).text()
787 this.setCookie()
788
789 this.login = jQuery("login", data.documentElement).text()
790 this.pseudo = jQuery("pseudo", data.documentElement).text()
791 this.email = jQuery("email", data.documentElement).text()
792 this.css = jQuery("css", data.documentElement).text()
793
794 // la page de la conversation principale
795 var tmp = jQuery("pagePrincipale", data.documentElement)
796 this.pagePrincipale = tmp.length < 1 ? 1 : parseInt(tmp.text())
797
798 // met à jour la css
799 if (this.css != "")
800 {
801 jQuery("link#cssPrincipale").attr("href", this.css)
802 this.majMenu()
803 }
804 // les conversations
805 this.conversations = new Array()
806 jQuery("conversation", data.documentElement).each(
807 function(i)
808 {
809 thisClient.conversations.push( { racine : jQuery("racine", this).text(), page : jQuery("page", this).text() } )
810 }
811 )
812 }
813 this.dernierMessageErreur = jQuery("information", data.documentElement).text()
814 }
815
816 /**
817 * Met à jour les données personne sur serveur.
818 * @param async de manière asynchrone ? défaut = true
819 */
820 Client.prototype.flush = function(async)
821 {
822 if (async == undefined)
823 async = true
824
825 thisClient = this
826 //this.util.xmlVersAction(this.getXMLProfile()).action.dump("Flush client")
827 jQuery.ajax(
828 {
829 async: async,
830 type: "POST",
831 url: "request",
832 dataType: "xml",
833 data: this.util.xmlVersAction(this.getXMLProfile()),
834 success:
835 function(data)
836 {
837 //thisClient.util.log(thisClient.util.serializer.serializeToString(data))
838 }
839 }
840 )
841 // TODO : retourner false si un problème est survenu lors de l'update du profile
842 return true
843 }
844
845 Client.prototype.majMenu = function()
846 {
847 var displayType = this.css == "css/3/euphorik.css" ? "block" : "inline" //this.client
848
849 // met à jour le menu
850 if (this.statut == statutType.enregistre)
851 {
852 jQuery("#menu .profile").css("display", displayType).text("profile")
853 jQuery("#menu .logout").css("display", displayType)
854 jQuery("#menu .register").css("display", "none")
855 }
856 else if (this.statut == statutType.identifie)
857 {
858 jQuery("#menu .profile").css("display", "none")
859 jQuery("#menu .logout").css("display", displayType)
860 jQuery("#menu .register").css("display", displayType)
861 }
862 else
863 {
864 jQuery("#menu .profile").css("display", displayType).text("login")
865 jQuery("#menu .logout").css("display", "none")
866 jQuery("#menu .register").css("display", displayType)
867 }
868 }
869
870 ///////////////////////////////////////////////////////////////////////////////////////////////////
871
872 jQuery.noConflict()
873
874
875 // le main
876 jQuery(document).ready(
877 function()
878 {
879 /* FIXME : ce code pose problème sur konqueror, voir : http://www.kde-forum.org/thread.php?threadid=17993
880 var p = new DOMParser();
881 var doc = p.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
882 var s = new XMLSerializer()
883 alert(s.serializeToString(doc)) */
884
885 var util = new Util()
886 var client = new Client(util)
887 var pages = new Pages()
888 var formateur = new Formateur()
889
890 // connexion vers le serveur (utilise un cookie qui traine)
891 client.connexionCookie()
892
893 // les styles css
894 for (var i = 1; i <= 3; i++)
895 {
896 jQuery("#css"+i).click(function(){
897 client.setCss("css/" + jQuery(this).attr("id").charAt(3) + "/euphorik.css")
898 })
899 }
900
901 jQuery("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
902 jQuery("#menu .profile").click(function(){ pages.afficherPage("profile") })
903 jQuery("#menu .logout").click(function(){
904 util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType.question,
905 {"Oui" : function()
906 {
907 client.deconnexion();
908 pages.afficherPage("minichat", true)
909 },
910 "Non" : function(){}
911 }
912 )
913 })
914 jQuery("#menu .register").click(function(){ pages.afficherPage("register") })
915
916 pages.ajouterPage(new PageMinichat(client, formateur, util))
917 pages.ajouterPage(new PageProfile(client, formateur, util))
918 pages.ajouterPage(new PageRegister(client, formateur, util))
919 pages.afficherPage("minichat")
920 }
921 )
922