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