FIX il manquait le dernier message à l'affichage (problème dans euphorik_minichat_con...
[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.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.remplacerBalisesHTML(M), pseudo)))
303 }
304
305 /**
306 * Transforme les liens en entités clickables.
307 * Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message :
308 * "{5F}" ou 5F est la racine de la conversation.
309 * Ce lien sera transformer en <span class="lienConv">{5F}</span> pouvant être clické pour créer la conv 5F.
310 */
311 Formateur.prototype.traiterLiensConv = function(M)
312 {
313 return M.replace(
314 /\{\w+\}/g,
315 function(lien)
316 {
317 return "<span class=\"lienConv\">" + lien + "</span>"
318 }
319 )
320 }
321
322 /**
323 * FIXME : Cette méthode est attrocement lourde ! A optimiser.
324 */
325 Formateur.prototype.traiterSmiles = function(M)
326 {
327 for (var sNom in this.smiles)
328 {
329 ss = this.smiles[sNom]
330 for (var i = 0; i < ss.length; i++)
331 M = M.replace(ss[i], "<img src=\"img/smileys/" + sNom + ".gif\" />")
332 }
333 return M
334 }
335
336 Formateur.prototype.remplacerBalisesHTML = function(M)
337 {
338 return M.replace(/</g, "&lt;").replace(/>/g, "&gt;")
339 }
340
341 Formateur.prototype.traiterURL = function(M, pseudo)
342 {
343 thisFormateur = this
344
345 if (pseudo == undefined)
346 pseudo = ""
347
348 var traitementUrl = function(url)
349 {
350 // si ya pas de protocole on rajoute "http://"
351 if (!thisFormateur.regexTestProtocoleExiste.test(url))
352 url = "http://" + url
353 var extension = thisFormateur.getShort(url)
354 return "<a " + (extension[1] ? "title=\"" + thisFormateur.traiterPourFenetreLightBox(pseudo, url) + ": " + thisFormateur.traiterPourFenetreLightBox(M, url) + "\"" + " rel=\"lightbox[groupe]\"" : "") + " href=\"" + url + "\" >[" + extension[0] + "]</a>"
355 }
356 return M.replace(this.regexUrl, traitementUrl)
357 }
358
359 /**
360 * Renvoie une version courte de l'url.
361 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
362 */
363 Formateur.prototype.getShort = function(url)
364 {
365 var estUneImage = false
366 var versionShort = null
367 var rechercheImg = this.regexImg.exec(url)
368 //alert(url)
369 if (rechercheImg != null)
370 {
371 versionShort = rechercheImg[1].toLowerCase()
372 if (versionShort == "jpeg") versionShort = "jpg" // jpeg -> jpg
373 estUneImage = true
374 }
375 else
376 {
377 var rechercheDomaine = this.regexDomaine.exec(url)
378 if (rechercheDomaine != null && rechercheDomaine.length >= 2)
379 versionShort = rechercheDomaine[1]
380 else
381 {
382 var nomProtocole = this.regexNomProtocole.exec(url)
383 if (nomProtocole != null && nomProtocole.length >= 2)
384 versionShort = nomProtocole[1]
385 }
386 }
387
388 return [versionShort == null ? "url" : versionShort, estUneImage]
389 }
390
391 /**
392 * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
393 */
394 Formateur.prototype.traiterPourFenetreLightBox = function(M, urlCourante)
395 {
396 thisFormateur = this
397 var traitementUrl = function(url)
398 {
399 return "[" + thisFormateur.getShort(url)[0] + (urlCourante == url ? ": image courante" : "") + "]"
400 }
401
402 return this.remplacerBalisesHTML(M).replace(this.regexUrl, traitementUrl)
403 }
404
405
406 ///////////////////////////////////////////////////////////////////////////////////////////////////
407
408 var statutType = {enregistre: 0, identifie: 1, non_identifie: 2}
409
410 function Client(util)
411 {
412 this.util = util
413
414 this.cookie = null
415 this.regexCookie = new RegExp("^cookie=([^;]*)")
416
417 // données personnels
418 this.resetDonneesPersonnelles()
419
420 this.setStatut(statutType.non_identifie)
421
422 // le dernier message d'erreur recut du serveur (par exemple une connexion foireuse : "login impossible")
423 this.dernierMessageErreur = ""
424 }
425
426 Client.prototype.resetDonneesPersonnelles = function()
427 {
428 this.pseudo = conf.pseudoDefaut
429 this.login = ""
430 this.password = ""
431 this.email = ""
432 this.css = jQuery("link#cssPrincipale").attr("href")
433
434 this.pagePrincipale = 1
435
436 // les conversations, une conversation est un objet possédant les attributs suivants :
437 // - racine (entier)
438 // - page (entier)
439 this.conversations = new Array()
440 }
441
442 Client.prototype.setCss = function(css)
443 {
444 if (this.css == css)
445 return
446
447 this.css = css
448 jQuery("link#cssPrincipale").attr("href", this.css)
449 this.majMenu()
450
451 if (this.identifie())
452 this.flush()
453 }
454
455 Client.prototype.pageSuivante = function(numConv)
456 {
457 if (numConv < 0 && this.pagePrincipale > 1)
458 this.pagePrincipale -= 1
459 else if (this.conversations[numConv].page > 1)
460 this.conversations[numConv].page -= 1
461 this.flush(false)
462 }
463
464 Client.prototype.pagePrecedente = function(numConv)
465 {
466 if (numConv < 0)
467 this.pagePrincipale += 1
468 else
469 this.conversations[numConv].page += 1
470 this.flush(false)
471 }
472
473 /**
474 * Définit la première page pour la conversation donnée.
475 * @return true si la page a changé sinon false
476 */
477 Client.prototype.goPremierePage = function(numConv)
478 {
479 if (numConv < 0)
480 {
481 if (this.pagePrincipale == 1)
482 return false
483 this.pagePrincipale = 1
484 }
485 else
486 {
487 if (this.conversations[numConv].page == 1)
488 return false
489 this.conversations[numConv].page = 1
490 }
491 this.flush(false)
492 return true
493 }
494
495 /**
496 * Ajoute une conversation à la vue de l'utilisateur.
497 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
498 * @param racines la racine de la conversation
499 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
500 */
501 Client.prototype.ajouterConversation = function(racine)
502 {
503 // vérification s'il elle n'existe pas déjà
504 for (var i = 0; i < this.conversations.length; i++)
505 if (this.conversations[i].racine == racine)
506 return false
507
508 this.conversations.push({racine : racine, page : 1})
509 this.flush(false)
510 return true
511 }
512
513 Client.prototype.supprimerConversation = function(num)
514 {
515 if (num < 0 || num >= this.conversations.length) return
516
517 // décalage TODO : supprimer le dernier élément
518 for (var i = num; i < this.conversations.length - 1; i++)
519 this.conversations[i] = this.conversations[i+1]
520 this.conversations.pop()
521
522 this.flush(false)
523 }
524
525 Client.prototype.getXMLlogin = function(login, password)
526 {
527 var XMLDocument = this.util.creerDocumentXMLAction()
528 XMLDocument.documentElement.setAttribute("name", "login")
529
530 var nodeLogin = XMLDocument.createElement("login")
531 nodeLogin.appendChild(XMLDocument.createTextNode(login))
532 XMLDocument.documentElement.appendChild(nodeLogin)
533
534 var nodePassword = XMLDocument.createElement("password")
535 nodePassword.appendChild(XMLDocument.createTextNode(password))
536 XMLDocument.documentElement.appendChild(nodePassword)
537
538 return XMLDocument
539 }
540
541 Client.prototype.getXMLloginCookie = function()
542 {
543 var XMLDocument = this.util.creerDocumentXMLAction()
544 XMLDocument.documentElement.setAttribute("name", "login")
545
546 var nodeCookie = XMLDocument.createElement("cookie")
547 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
548 XMLDocument.documentElement.appendChild(nodeCookie)
549
550 return XMLDocument
551 }
552
553 Client.prototype.getXMLEnregistrement = function(login, password)
554 {
555 var XMLDocument = this.util.creerDocumentXMLAction()
556 XMLDocument.documentElement.setAttribute("name", "register")
557
558 var nodeLogin = XMLDocument.createElement("login")
559 nodeLogin.appendChild(XMLDocument.createTextNode(login))
560 XMLDocument.documentElement.appendChild(nodeLogin)
561
562 var nodePassword = XMLDocument.createElement("password")
563 nodePassword.appendChild(XMLDocument.createTextNode(password))
564 XMLDocument.documentElement.appendChild(nodePassword)
565
566 return XMLDocument
567 }
568
569 /**
570 * Sérialize le profile en XML.
571 * TODO : méthode assez lourde, 3.25ms de moyenne
572 */
573 Client.prototype.getXMLProfile = function()
574 {
575 var XMLDocument = this.util.creerDocumentXMLAction()
576 XMLDocument.documentElement.setAttribute("name", "profile")
577
578 var nodeCookie = XMLDocument.createElement("cookie")
579 nodeCookie.appendChild(XMLDocument.createTextNode(this.cookie))
580 XMLDocument.documentElement.appendChild(nodeCookie)
581
582 var nodeLogin = XMLDocument.createElement("login")
583 nodeLogin.appendChild(XMLDocument.createTextNode(this.login))
584 XMLDocument.documentElement.appendChild(nodeLogin)
585
586 var nodePassword = XMLDocument.createElement("password")
587 nodePassword.appendChild(XMLDocument.createTextNode(this.password))
588 XMLDocument.documentElement.appendChild(nodePassword)
589
590 var nodePseudo = XMLDocument.createElement("pseudo")
591 nodePseudo.appendChild(XMLDocument.createTextNode(this.pseudo))
592 XMLDocument.documentElement.appendChild(nodePseudo)
593
594 var nodeEmail = XMLDocument.createElement("email")
595 nodeEmail.appendChild(XMLDocument.createTextNode(this.email))
596 XMLDocument.documentElement.appendChild(nodeEmail)
597
598 var nodeCSS = XMLDocument.createElement("css")
599 nodeCSS.appendChild(XMLDocument.createTextNode(this.css))
600 XMLDocument.documentElement.appendChild(nodeCSS)
601
602 var nodePagePrincipale = XMLDocument.createElement("pagePrincipale")
603 nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale < 1 ? 1 : this.pagePrincipale))
604 XMLDocument.documentElement.appendChild(nodePagePrincipale)
605
606 // mémorise les conversations affichées
607 for (var i = 0; i < this.conversations.length; i++)
608 {
609 var nodeConv = XMLDocument.createElement("conversation")
610 XMLDocument.documentElement.appendChild(nodeConv)
611
612 var nodeRacine = XMLDocument.createElement("racine")
613 nodeRacine.appendChild(XMLDocument.createTextNode(this.conversations[i].racine))
614 nodeConv.appendChild(nodeRacine)
615
616 var nodePage = XMLDocument.createElement("page")
617 nodePage.appendChild(XMLDocument.createTextNode(this.conversations[i].page))
618 nodeConv.appendChild(nodePage)
619 }
620
621 return XMLDocument
622 }
623
624 /**
625 * Renvoie null si pas définit.
626 */
627 Client.prototype.getCookie = function()
628 {
629 var cookie = this.regexCookie.exec(document.cookie)
630 if (cookie == null) this.cookie = null
631 else this.cookie = cookie[1]
632 }
633
634 Client.prototype.delCookie = function()
635 {
636 document.cookie = "cookie=; max-age=0"
637 }
638
639 Client.prototype.setCookie = function(cookie)
640 {
641 if (this.cookie == null)
642 return
643
644 document.cookie =
645 "cookie="+this.cookie+
646 "; max-age=" + (60 * 60 * 24 * 365)
647 }
648
649 Client.prototype.identifie = function()
650 {
651 return this.statut == statutType.enregistre || this.statut == statutType.identifie
652 }
653
654 Client.prototype.setStatut = function(statut)
655 {
656 if(typeof(statut) == "string")
657 {
658 statut =
659 statut == "enregistre" ?
660 statutType.enregistre : (statut == "identifie" ? statutType.identifie : statutType.non_identifie)
661 }
662
663 if (statut == this.statut) return
664
665 this.statut = statut
666 this.majMenu()
667 }
668
669 /**
670 * Effectue la connexion vers le serveur.
671 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
672 * S'il existe un cookie en local on s'authentifie directement avec lui.
673 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
674 */
675 Client.prototype.connexionCookie = function()
676 {
677 this.getCookie()
678 if (this.cookie == null) return false;
679 return this.connexion(this.util.xmlVersAction(this.getXMLloginCookie()))
680 }
681
682 Client.prototype.connexionLogin = function(login, password)
683 {
684 return this.connexion(this.util.xmlVersAction(this.getXMLlogin(login, password)))
685 }
686
687 Client.prototype.enregistrement = function(login, password)
688 {
689 if (this.identifie())
690 {
691 this.login = login
692 this.password = password
693 if(this.flush())
694 this.setStatut(statutType.enregistre)
695 return true
696 }
697 else
698 {
699 if (login == undefined) login = ""
700 if (password == undefined) password = ""
701 return this.connexion(this.util.xmlVersAction(this.getXMLEnregistrement(login, password)))
702 }
703 }
704
705 Client.prototype.connexion = function(action)
706 {
707 action.action.dump("Connexion client")
708 thisClient = this
709 jQuery.ajax(
710 {
711 async: false,
712 type: "POST",
713 url: "request",
714 dataType: "xml",
715 data: action,
716 success:
717 function(data)
718 {
719 thisClient.util.serializer.serializeToString(data).dump("Charger client")
720 thisClient.chargerDonnees(data)
721 }
722 }
723 )
724 return this.identifie()
725 }
726
727 Client.prototype.deconnexion = function()
728 {
729 this.setStatut(statutType.non_identifie) // deconnexion
730 this.resetDonneesPersonnelles()
731 this.delCookie ()
732 }
733
734 Client.prototype.chargerDonnees = function(data)
735 {
736 var thisClient = this
737
738 this.setStatut(jQuery("statut", data.documentElement).text())
739
740 if (this.identifie())
741 {
742 this.cookie = jQuery("cookie", data.documentElement).text()
743 this.setCookie()
744
745 this.login = jQuery("login", data.documentElement).text()
746 this.pseudo = jQuery("pseudo", data.documentElement).text()
747 this.email = jQuery("email", data.documentElement).text()
748 this.css = jQuery("css", data.documentElement).text()
749
750 // la page de la conversation principale
751 var tmp = jQuery("pagePrincipale", data.documentElement)
752 this.pagePrincipale = tmp.length < 1 ? 1 : parseInt(tmp.text())
753
754 // met à jour la css
755 if (this.css != "")
756 {
757 jQuery("link#cssPrincipale").attr("href", this.css)
758 this.majMenu()
759 }
760 // les conversations
761 this.conversations = new Array()
762 jQuery("conversation", data.documentElement).each(
763 function(i)
764 {
765 thisClient.conversations.push( { racine : jQuery("racine", this).text(), page : jQuery("page", this).text() } )
766 }
767 )
768 }
769 this.dernierMessageErreur = jQuery("information", data.documentElement).text()
770 }
771
772 /**
773 * Met à jour les données personne sur serveur.
774 * @param async de manière asynchrone ? défaut = true
775 */
776 Client.prototype.flush = function(async)
777 {
778 if (async == undefined)
779 async = true
780
781 thisClient = this
782 this.util.xmlVersAction(this.getXMLProfile()).action.dump("Flush client")
783 jQuery.ajax(
784 {
785 async: async,
786 type: "POST",
787 url: "request",
788 dataType: "xml",
789 data: this.util.xmlVersAction(this.getXMLProfile()),
790 success:
791 function(data)
792 {
793 //thisClient.util.log(thisClient.util.serializer.serializeToString(data))
794 }
795 }
796 )
797 // TODO : retourner false si un problème est survenu lors de l'update du profile
798 return true
799 }
800
801 Client.prototype.majMenu = function()
802 {
803 var displayType = this.css == "css/3/euphorik.css" ? "block" : "inline" //this.client
804
805 // met à jour le menu
806 if (this.statut == statutType.enregistre)
807 {
808 jQuery("#menu .profile").css("display", displayType).text("profile")
809 jQuery("#menu .logout").css("display", displayType)
810 jQuery("#menu .register").css("display", "none")
811 }
812 else if (this.statut == statutType.identifie)
813 {
814 jQuery("#menu .profile").css("display", "none")
815 jQuery("#menu .logout").css("display", displayType)
816 jQuery("#menu .register").css("display", displayType)
817 }
818 else
819 {
820 jQuery("#menu .profile").css("display", displayType).text("login")
821 jQuery("#menu .logout").css("display", "none")
822 jQuery("#menu .register").css("display", displayType)
823 }
824 }
825
826 ///////////////////////////////////////////////////////////////////////////////////////////////////
827
828 jQuery.noConflict()
829
830
831 // le main
832 jQuery(document).ready(
833 function()
834 {
835 /* FIXME : ce code pose problème sur konqueror, voir : http://www.kde-forum.org/thread.php?threadid=17993
836 var p = new DOMParser();
837 var doc = p.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
838 var s = new XMLSerializer()
839 alert(s.serializeToString(doc)) */
840
841 var util = new Util()
842 var client = new Client(util)
843 var pages = new Pages()
844 var formateur = new Formateur()
845
846 // connexion vers le serveur (utilise un cookie qui traine)
847 client.connexionCookie()
848
849 // les styles css
850 for (var i = 1; i <= 3; i++)
851 {
852 jQuery("#css"+i).click(function(){
853 client.setCss("css/" + jQuery(this).attr("id").charAt(3) + "/euphorik.css")
854 })
855 }
856
857 jQuery("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
858 jQuery("#menu .profile").click(function(){ pages.afficherPage("profile") })
859 jQuery("#menu .logout").click(function(){
860 util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType.question,
861 {"Oui" : function()
862 {
863 client.deconnexion();
864 pages.afficherPage("minichat", true)
865 },
866 "Non" : function(){}
867 }
868 )
869 })
870 jQuery("#menu .register").click(function(){ pages.afficherPage("register") })
871
872 pages.ajouterPage(new PageMinichat(client, formateur, util))
873 pages.ajouterPage(new PageProfile(client, formateur, util))
874 pages.ajouterPage(new PageRegister(client, formateur, util))
875 pages.afficherPage("minichat")
876 }
877 )
878