MOD avancement sur les conversations (on s'approche du but..)
[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 : 80, // (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 /**
437 * Ajoute une conversation à la vue de l'utilisateur.
438 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
439 * @param racines la racine de la conversation
440 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
441 */
442 Client.prototype.ajouterConversation = function(racine)
443 {
444 // vérification s'il elle n'existe pas déjà
445 for (var i = 0; i < this.conversations.length; i++)
446 if (this.conversations[i].racine == racine)
447 return false
448
449 this.conversations.push({racine : racine, page : 1})
450 this.flush(false)
451 return true
452 }
453
454 Client.prototype.supprimerConversation = function(num)
455 {
456 if (num < 0 || num >= this.conversations.length) return
457
458 // décalage TODO : supprimer le dernier élément
459 for (var i = num; i < this.conversations.length - 1; i++)
460 this.conversations[i] = this.conversations[i+1]
461 this.conversations.pop()
462
463 this.flush(false)
464 }
465
466 Client.prototype.getXMLlogin = function(login, password)
467 {
468 var XMLDocument = this.util.creerDocumentXMLAction()
469 XMLDocument.documentElement.setAttribute("name", "login")
470
471 var nodeLogin = XMLDocument.createElement("login")
472 nodeLogin.appendChild(XMLDocument.createTextNode(login))
473 XMLDocument.documentElement.appendChild(nodeLogin)
474
475 var nodePassword = XMLDocument.createElement("password")
476 nodePassword.appendChild(XMLDocument.createTextNode(password))
477 XMLDocument.documentElement.appendChild(nodePassword)
478
479 return XMLDocument
480 }
481
482 Client.prototype.getXMLloginCookie = function()
483 {
484 var XMLDocument = this.util.creerDocumentXMLAction()
485 XMLDocument.documentElement.setAttribute("name", "login")
486
487 var nodeCookie = XMLDocument.createElement("cookie")
488 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
489 XMLDocument.documentElement.appendChild(nodeCookie)
490
491 return XMLDocument
492 }
493
494 /* Obsolète
495 Client.prototype.getXMLloginCaptcha = function(captchaCrypt, captchaInput)
496 {
497 var XMLDocument = this.util.creerDocumentXMLAction()
498 XMLDocument.documentElement.setAttribute("name", "loginCaptcha")
499
500 var nodecaptchaCrypt = XMLDocument.createElement("captchaCrypt")
501 nodecaptchaCrypt.appendChild(XMLDocument.createTextNode(captchaCrypt))
502 XMLDocument.documentElement.appendChild(nodecaptchaCrypt)
503
504 var nodecaptchaInput = XMLDocument.createElement("captchaInput")
505 nodecaptchaInput.appendChild(XMLDocument.createTextNode(captchaInput))
506 XMLDocument.documentElement.appendChild(nodecaptchaInput)
507
508 return XMLDocument
509 }*/
510
511 /* Obsolète
512 Client.prototype.getXMLgenerationCaptcha = function()
513 {
514 var XMLDocument = this.util.creerDocumentXMLAction()
515 XMLDocument.documentElement.setAttribute("name", "generationCaptcha")
516
517 return XMLDocument
518 }*/
519
520 Client.prototype.getXMLEnregistrement = function(login, password)
521 {
522 var XMLDocument = this.util.creerDocumentXMLAction()
523 XMLDocument.documentElement.setAttribute("name", "register")
524
525 var nodeLogin = XMLDocument.createElement("login")
526 nodeLogin.appendChild(XMLDocument.createTextNode(login))
527 XMLDocument.documentElement.appendChild(nodeLogin)
528
529 var nodePassword = XMLDocument.createElement("password")
530 nodePassword.appendChild(XMLDocument.createTextNode(password))
531 XMLDocument.documentElement.appendChild(nodePassword)
532
533 return XMLDocument
534 }
535
536 Client.prototype.getXMLProfile = function()
537 {
538 var XMLDocument = this.util.creerDocumentXMLAction()
539 XMLDocument.documentElement.setAttribute("name", "profile")
540
541 var nodeCookie = XMLDocument.createElement("cookie")
542 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
543 XMLDocument.documentElement.appendChild(nodeCookie)
544
545 var nodeLogin = XMLDocument.createElement("login")
546 nodeLogin.appendChild(XMLDocument.createTextNode(this.login))
547 XMLDocument.documentElement.appendChild(nodeLogin)
548
549 var nodePassword = XMLDocument.createElement("password")
550 nodePassword.appendChild(XMLDocument.createTextNode(this.password))
551 XMLDocument.documentElement.appendChild(nodePassword)
552
553 var nodePseudo = XMLDocument.createElement("pseudo")
554 nodePseudo.appendChild(XMLDocument.createTextNode(this.pseudo))
555 XMLDocument.documentElement.appendChild(nodePseudo)
556
557 var nodeEmail = XMLDocument.createElement("email")
558 nodeEmail.appendChild(XMLDocument.createTextNode(this.email))
559 XMLDocument.documentElement.appendChild(nodeEmail)
560
561 var nodeCSS = XMLDocument.createElement("css")
562 nodeCSS.appendChild(XMLDocument.createTextNode(this.css))
563 XMLDocument.documentElement.appendChild(nodeCSS)
564
565 var nodePagePrincipale = XMLDocument.createElement("pagePrincipale")
566 nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale))
567 XMLDocument.documentElement.appendChild(nodePagePrincipale)
568
569 // mémorise les conversations affichées
570 for (var i = 0; i < this.conversations.length; i++)
571 {
572 var nodeConv = XMLDocument.createElement("conversation")
573 XMLDocument.documentElement.appendChild(nodeConv)
574
575 var nodeRacine = XMLDocument.createElement("racine")
576 nodeRacine.appendChild(XMLDocument.createTextNode(this.conversations[i].racine))
577 nodeConv.appendChild(nodeRacine)
578
579 var nodePage = XMLDocument.createElement("page")
580 nodePage.appendChild(XMLDocument.createTextNode(this.conversations[i].page))
581 nodeConv.appendChild(nodePage)
582 }
583
584 return XMLDocument
585 }
586
587 /**
588 * Renvoie null si pas définit.
589 */
590 Client.prototype.getCookie = function()
591 {
592 var cookie = this.regexCookie.exec(document.cookie)
593 if (cookie == null) this.cookie = null
594 else this.cookie = cookie[1]
595 }
596
597 Client.prototype.delCookie = function()
598 {
599 document.cookie = "cookie=; max-age=0"
600 }
601
602 Client.prototype.setCookie = function(cookie)
603 {
604 if (this.cookie == null)
605 return
606
607 document.cookie =
608 "cookie="+this.cookie+
609 "; max-age=" + (60 * 60 * 24 * 365)
610 }
611
612 Client.prototype.identifie = function()
613 {
614 return this.statut == statutType.enregistre || this.statut == statutType.identifie
615 }
616
617 Client.prototype.setStatut = function(statut)
618 {
619 if(typeof(statut) == "string")
620 {
621 statut =
622 statut == "enregistre" ?
623 statutType.enregistre : (statut == "identifie" ? statutType.identifie : statutType.non_identifie)
624 }
625
626 if (statut == this.statut) return
627
628 this.statut = statut
629 this.majMenu()
630 }
631
632 /**
633 * Demande la génération d'un captcha au serveur et l'affiche.
634 */
635 /* Obsolète
636 Client.prototype.afficherCaptcha = function(query)
637 {
638 var thisClient = this
639
640 $.post("request", this.util.xmlVersAction(this.getXMLgenerationCaptcha()),
641 function(data, textStatus)
642 {
643 var chemin = jQuery("chemin", data.documentElement).text()
644 thisClient.captchaCrypt = jQuery("captchaCrypt", data.documentElement).text()
645 jQuery(query).prepend(
646 "<p id=\"captcha\" >Es-tu un bot ? <img class=\"captchaImg\" src=\"" + chemin + "\" />" +
647 "<input name=\"captchaInput\" type=\"text\" size=\"5\" max_length=\"5\" ></p>"
648 )
649 }
650 )
651 }
652
653 Client.prototype.cacherCaptcha = function()
654 {
655 jQuery("#captcha").remove()
656 }*/
657
658 /**
659 * Effectue la connexion vers le serveur.
660 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
661 * S'il existe un cookie en local on s'authentifie directement avec lui.
662 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
663 */
664 Client.prototype.connexionCookie = function()
665 {
666 this.getCookie()
667 if (this.cookie == null) return false;
668 return this.connexion(this.util.xmlVersAction(this.getXMLloginCookie()))
669 }
670
671 Client.prototype.connexionLogin = function(login, password)
672 {
673 return this.connexion(this.util.xmlVersAction(this.getXMLlogin(login, password)))
674 }
675
676 /* Obsolète
677 Client.prototype.connexionCaptcha = function()
678 {
679 return this.connexion(this.util.xmlVersAction(this.getXMLloginCaptcha(this.captchaCrypt, jQuery("#captcha input").val())))
680 }*/
681
682 Client.prototype.enregistrement = function(login, password)
683 {
684 if (this.identifie())
685 {
686 this.login = login
687 this.password = password
688 if(this.flush())
689 this.setStatut(statutType.enregistre)
690 return true
691 }
692 else
693 {
694 if (login == undefined) login = ""
695 if (password == undefined) password = ""
696 return this.connexion(this.util.xmlVersAction(this.getXMLEnregistrement(login, password)))
697 }
698 }
699
700 Client.prototype.connexion = function(action)
701 {
702 //action.action.dump()
703 thisClient = this
704 jQuery.ajax(
705 {
706 async: false,
707 type: "POST",
708 url: "request",
709 dataType: "xml",
710 data: action,
711 success:
712 function(data)
713 {
714 //thisClient.util.serializer.serializeToString(data).dump()
715 thisClient.chargerDonnees(data)
716 }
717 }
718 )
719 return this.identifie()
720 }
721
722 Client.prototype.deconnexion = function()
723 {
724 this.setStatut(statutType.non_identifie) // deconnexion
725 this.resetDonneesPersonnelles()
726 this.delCookie ()
727 }
728
729 Client.prototype.chargerDonnees = function(data)
730 {
731 var thisClient = this
732
733 this.setStatut(jQuery("statut", data.documentElement).text())
734
735 if (this.identifie())
736 {
737 this.cookie = jQuery("cookie", data.documentElement).text()
738 this.setCookie()
739
740 this.login = jQuery("login", data.documentElement).text()
741 this.pseudo = jQuery("pseudo", data.documentElement).text()
742 this.email = jQuery("email", data.documentElement).text()
743 this.css = jQuery("css", data.documentElement).text()
744
745 // la page de la conversation principale
746 var tmp = jQuery("pagePrincipale", data.documentElement)
747 this.pagePrincipale = tmp.length < 1 ? 1 : tmp.text()
748
749 // met à jour la css
750 if (this.css != "")
751 {
752 jQuery("link#cssPrincipale").attr("href", this.css)
753 this.majMenu()
754 }
755 // les conversations
756 this.conversations = new Array()
757 jQuery("conversation", data.documentElement).each(
758 function(i)
759 {
760 thisClient.conversations.push( { racine : jQuery("racine", this).text(), page : jQuery("page", this).text() } )
761 }
762 )
763 }
764 this.dernierMessageErreur = jQuery("information", data.documentElement).text()
765 }
766
767 /**
768 * Met à jour les données personne sur serveur.
769 * @param async de manière asynchrone ? défaut = true
770 */
771 Client.prototype.flush = function(async)
772 {
773 if (async == undefined)
774 async = true
775
776 thisClient = this
777 //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action)
778 jQuery.ajax(
779 {
780 async: async,
781 type: "POST",
782 url: "request",
783 dataType: "xml",
784 data: this.util.xmlVersAction(this.getXMLProfile()),
785 success:
786 function(data)
787 {
788 //thisClient.util.log(thisClient.util.serializer.serializeToString(data))
789 }
790 }
791 )
792 // TODO : retourner false si un problème est survenu lors de l'update du profile
793 return true
794 }
795
796 Client.prototype.majMenu = function()
797 {
798 var displayType = this.css == "css/3/euphorik.css" ? "block" : "inline" //this.client
799
800 // met à jour le menu
801 if (this.statut == statutType.enregistre)
802 {
803 jQuery("#menu .profile").css("display", displayType).text("profile")
804 jQuery("#menu .logout").css("display", displayType)
805 jQuery("#menu .register").css("display", "none")
806 }
807 else if (this.statut == statutType.identifie)
808 {
809 jQuery("#menu .profile").css("display", "none")
810 jQuery("#menu .logout").css("display", displayType)
811 jQuery("#menu .register").css("display", displayType)
812 }
813 else
814 {
815 jQuery("#menu .profile").css("display", displayType).text("login")
816 jQuery("#menu .logout").css("display", "none")
817 jQuery("#menu .register").css("display", displayType)
818 }
819 }
820
821 ///////////////////////////////////////////////////////////////////////////////////////////////////
822
823 jQuery.noConflict()
824
825
826 // le main
827 jQuery(document).ready(
828 function()
829 {
830 /* FIXME : ce code pose problème sur konqueror, voir : http://www.kde-forum.org/thread.php?threadid=17993
831 var p = new DOMParser();
832 var doc = p.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
833 var s = new XMLSerializer()
834 alert(s.serializeToString(doc)) */
835
836 var util = new Util()
837 var client = new Client(util)
838 var pages = new Pages()
839 var formateur = new Formateur()
840
841 // connexion vers le serveur (utilise un cookie qui traine)
842 client.connexionCookie()
843
844 // les styles css
845 for (var i = 1; i <= 3; i++)
846 {
847 jQuery("#css"+i).click(function(){
848 client.setCss("css/" + jQuery(this).attr("id").charAt(3) + "/euphorik.css")
849 })
850 }
851
852 jQuery("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
853 jQuery("#menu .profile").click(function(){ pages.afficherPage("profile") })
854 jQuery("#menu .logout").click(function(){
855 util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType.question,
856 {"Oui" : function()
857 {
858 client.deconnexion();
859 pages.afficherPage("minichat", true)
860 },
861 "Non" : function(){}
862 }
863 )
864 })
865 jQuery("#menu .register").click(function(){ pages.afficherPage("register") })
866
867 pages.ajouterPage(new PageMinichat(client, formateur, util))
868 pages.ajouterPage(new PageProfile(client, formateur, util))
869 pages.ajouterPage(new PageRegister(client, formateur, util))
870 pages.afficherPage("minichat")
871 }
872 )
873