2 // Copyright 2008 Grégory Burri
4 // This file is part of Euphorik.
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
20 * Contient la base javascript pour le site euphorik.ch.
21 * Chaque page possède son propre fichier js nommé "page<nom de la page>.js".
29 * Normalement 'const' à la place de 'var' mais non supporté par IE7.
32 versionProtocole : 3, // version du protcole
33 nickDefaut : "<nick>",
34 nbMessageAffiche : 10, // (par page)
35 pseudoDefaut : "<nick>",
36 tempsAffichageMessageDialogue : 4000, // en ms
37 tempsKick : 15, // en minute
38 tempsBan : 60 * 24 * 3, // en minutes (3jours)
40 "smile" : [/:\)/g, /:-\)/g],
41 "bigsmile" : [/:D/g, /:-D/g],
42 "clin" : [/;\)/g, /;-\)/g],
43 "cool" : [/8\)/g, /8-\)/g],
44 "eheheh" : [/:P/g, /:-P/g],
45 "lol" : [/\[-lol\]/g],
47 "spliff" : [/\[-spliff\]/g],
48 "oh" : [/:o/g, /:O/g],
49 "heink" : [/\[-heink\]/g],
50 "hum" : [/\[-hum\]/g],
51 "boh" : [/\[-boh\]/g],
52 "sniff" : [/:\(/g, /:-\(/g],
53 "triste" : [/\[-triste\]/g],
54 "pascontent" : [/>\(/g, />\(/g],
55 "argn" : [/\[-argn\]/g],
56 "redface" : [/\[-redface\]/g],
57 "bunny" : [/\[-lapin\]/g],
58 "chat" : [/\[-chat\]/g],
59 "renne" : [/\[-renne\]/g],
60 "star" : [/\[-star\]/g],
61 "kirby" : [/\[-kirby\]/g],
62 "slurp" : [/\[-slurp\]/g],
63 "agreed" : [/\[-agreed\]/g],
64 "dodo" : [/\[-dodo\]/g],
69 ///////////////////////////////////////////////////////////////////////////////////////////////////
71 String
.prototype.trim = function()
73 return jQuery
.trim(this) // anciennement : this.replace(/^\s+|\s+$/g, "");
76 String
.prototype.ltrim = function()
78 return this.replace(/^\s+/, "");
81 String
.prototype.rtrim = function()
83 return this.replace(/\s+$/, "");
86 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 * Cette classe regroupe des fonctions utilitaires (helpers).
90 * @formateur est permet de formater les messages affichés à l'aide de messageDialogue (facultatif)
92 function Util(formateur
)
94 $("#info .fermer").click(function(){
95 $("#info").slideUp(50)
98 $("body").append('<div id="flecheBulle"></div>').append('<div id="messageBulle"><p></p></div>')
100 this.formateur
= formateur
101 this.bulleActive
= true
104 var messageType
= {informatif: 0, question: 1, erreur: 2}
107 * Affiche une boite de dialogue avec un message à l'intérieur.
108 * @param message le message (string)
109 * @param type voir 'messageType'. par défaut messageType.informatif
110 * @param les boutons sous la forme d'un objet ou les clefs sont les labels des boutons
111 * et les valeurs les fonctions executées lorsqu'un bouton est activé.
112 * @param formate faut-il formaté le message ? true par défaut
114 Util
.prototype.messageDialogue = function(message
, type
, boutons
, formate
)
118 if (type
== undefined)
119 type
= messageType
.informatif
121 if (formate
== undefined)
124 if (this.timeoutMessageDialogue
!= undefined)
125 clearTimeout(this.timeoutMessageDialogue
)
127 var fermer = function(){$("#info").slideUp(100)}
130 $("#info .message").html(thisUtil
.formateur
== undefined || !formate
? message : thisUtil
.formateur
.traitementComplet(message
))
133 case messageType
.informatif : $("#info #icone").attr("class", "information"); break
134 case messageType
.question : $("#info #icone").attr("class", "interrogation"); break
135 case messageType
.erreur : $("#info #icone").attr("class", "exclamation"); break
137 $("#info .boutons").html("")
138 for (var b
in boutons
)
139 $("#info .boutons").append("<div>" + b
+ "</div>").find("div:last").click(boutons
[b
]).click(fermer
)
141 $("#info").slideDown(200)
142 this.timeoutMessageDialogue
= setTimeout(fermer
, conf
.tempsAffichageMessageDialogue
)
145 var positionTypeX
= {gauche: 0, gaucheRecouvrement: 1, centre: 2, droiteRecouvrement: 3, droite: 4}
146 var positionTypeY
= {haut: 0, hautRecouvrement: 1, centre: 2, basRecouvrement: 3, bas: 4}
149 * Afficher une boite flottante (élément) par rapport à une cible.
150 * La boite est affichée de manière à ne pas dépasser de la fenêtre.
151 * @boite l'élément à afficher
152 * @cible l'élément sur lequel est affiché la boite
153 * @positionX de type positionTypeX
154 * @positionY de type positionTypeY
156 Util
.prototype.afficherBoite = function(boite
, cible
, positionX
, positionY
)
158 var positionCible
= cible
.offset()
161 left : positionX
== positionTypeX
.gauche
? positionCible
.left
- boite
.width() :
162 (positionX
== positionTypeX
.gaucheRecouvrement
? positionCible
.left
- boite
.width() + cible
.width() :
163 (positionX
== positionTypeX
.droitelsRecouvrement
? positionCible
.left :
164 (positionX
== positionTypeX
.droite
? positionCible
.left
+ cible
.width() :
165 positionCible
.left
+ cible
.width() / 2 - boite
.width() / 2 ))), // centre
166 top : positionY
== positionTypeY
.haut
? positionCible
.top
- boite
.height() :
167 (positionY
== positionTypeY
.hautRecouvrement
? positionCible
.top
- boite
.height() + cible
.height() :
168 (positionY
== positionTypeY
.basRecouvrement
? positionCible
.top :
169 (positionY
== positionTypeY
.bas
? positionCible
.top
+ cible
.height() :
170 positionCible
.top
+ cible
.height() / 2 - boite
.height() / 2 ))) // centre
173 // calcul les décalages en x et en y pour éviter que la boite ne sorte de la fenêtre, tient compte de la position des barres de défilement
175 positionBoite
.left
= positionBoite
.left
< marge
+ window
.pageXOffset
? marge
+ window
.pageXOffset :
176 (boite
.width() - $(window
).width() + (positionBoite
.left
- window
.pageXOffset
) + marge
> 0 ? $(window
).width() - boite
.width() - marge
+ window
.pageXOffset : positionBoite
.left
)
177 positionBoite
.top
= positionBoite
.top
< marge
+ window
.pageYOffset
? marge
+ window
.pageYOffset :
178 (boite
.height() - $(window
).height() + (positionBoite
.top
- window
.pageYOffset
) + marge
> 0 ? $(window
).height() - boite
.height() - marge
+ window
.pageYOffset : positionBoite
.top
)
180 boite
.css("top", positionBoite
.top
).css("left", positionBoite
.left
).show()
183 var positionBulleType
= {haut : 0, droite : 1, bas : 2, gauche : 3}
186 * Affiche un info bulle lorsque le curseur survole l'élément donné.
187 * @param message le message à afficher
188 * @param element l'élément DOM sur lesquel afficher la bulle
189 * @param la position de la bulle, voir positionBulleType. Par défaut à 0 (haut)
191 Util
.prototype.infoBulle = function(message
, element
, position
)
194 if (position
== undefined)
195 position
= positionBulleType
.haut
197 var cacherBulle = function()
199 $("#flecheBulle").hide()
200 $("#messageBulle").hide()
206 if (!thisUtil
.bulleActive
)
209 var m
= $("#messageBulle")
210 var f
= $("#flecheBulle")
211 f
.removeClass().addClass(position
== positionBulleType
.haut
? "flecheBulleHaut" :
212 (position
== positionBulleType
.droite
? "flecheBulleDroite" :
213 (position
== positionBulleType
.bas
? "flecheBulleBas" : "flecheBulleGauche" )))
215 // remplie le paragraphe de la bulle avec le message
216 $("p", m
).html(message
)
218 // réinitialise la position, évite le cas ou la boite est collé à droite et remplie avec un texte la faisant dépassé
219 // dans ce cas la hauteur n'est pas calculé correctement
220 m
.css("top", 0).css("left", 0)
222 var positionFleche
= {
223 left : position
== positionBulleType
.haut
|| position
== positionBulleType
.bas
?
224 element
.offset().left
+ element
.outerWidth() / 2 - f
.width() / 2 :
225 (position
== positionBulleType
.gauche
? element
.offset().left
- f
.width() : element
.offset().left
+ element
.outerWidth()),
226 top : position
== positionBulleType
.gauche
|| position
== positionBulleType
.droite
?
227 element
.offset().top
+ element
.outerHeight() / 2 - f
.height() / 2 :
228 (position
== positionBulleType
.haut
? element
.offset().top
- f
.height() : element
.offset().top
+ element
.outerHeight())
230 var positionMessage
= {
231 left : position
== positionBulleType
.haut
|| position
== positionBulleType
.bas
?
232 element
.offset().left
+ element
.width() / 2 - m
.width() / 2 :
233 (position
== positionBulleType
.gauche
? element
.offset().left
- f
.width() - m
.width() : element
.offset().left
+ f
.width() + element
.outerWidth()),
234 top : position
== positionBulleType
.gauche
|| position
== positionBulleType
.droite
?
235 element
.offset().top
+ element
.outerHeight() / 2 - m
.height() / 2 :
236 (position
== positionBulleType
.haut
? element
.offset().top
- f
.height() - m
.height() : element
.offset().top
+ f
.height() + element
.outerHeight())
238 var depassementDroit
= (positionMessage
.left
+ m
.width()) - $("body").width()
239 if (depassementDroit
> 0)
240 positionMessage
.left
-= depassementDroit
243 if (positionMessage
.left
< 0)
244 positionMessage
.left
= 0
247 m
.css("top", positionMessage
.top
).css("left", positionMessage
.left
).show()
248 f
.css("top", positionFleche
.top
).css("left", positionFleche
.left
).show()
255 * Utilisé pour l'envoie de donnée avec la méthode ajax de jQuery.
257 Util
.prototype.jsonVersAction = function(json
)
259 return {action : JSON
.stringify(json
) }
262 Util
.prototype.md5 = function(chaine
)
264 return hex_md5(chaine
)
267 // pompé de http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130
268 Util
.prototype.setSelectionRange = function(input
, selectionStart
, selectionEnd
)
270 if (input
.setSelectionRange
)
273 input
.setSelectionRange(selectionStart
, selectionEnd
)
275 else if (input
.createTextRange
)
277 var range
= input
.createTextRange()
279 range
.moveEnd('character', selectionEnd
)
280 range
.moveStart('character', selectionStart
)
285 Util
.prototype.setCaretToEnd = function(input
)
287 this.setSelectionRange(input
, input
.value
.length
, input
.value
.length
)
289 Util
.prototype.setCaretToBegin = function(input
)
291 this.setSelectionRange(input
, 0, 0)
293 Util
.prototype.setCaretToPos = function(input
, pos
)
295 this.setSelectionRange(input
, pos
, pos
)
297 Util
.prototype.selectString = function(input
, string
)
299 var match
= new RegExp(string
, "i").exec(input
.value
)
302 this.setSelectionRange (input
, match
.index
, match
.index
+ match
[0].length
)
305 Util
.prototype.replaceSelection = function(input
, replaceString
) {
306 if (input
.setSelectionRange
)
308 var selectionStart
= input
.selectionStart
309 var selectionEnd
= input
.selectionEnd
310 input
.value
= input
.value
.substring(0, selectionStart
) + replaceString
+ input
.value
.substring(selectionEnd
)
312 if (selectionStart
!= selectionEnd
) // has there been a selection
313 this.setSelectionRange(input
, selectionStart
, selectionStart
+ replaceString
.length
)
315 this.setCaretToPos(input
, selectionStart
+ replaceString
.length
)
317 else if (document
.selection
)
320 var range
= document
.selection
.createRange()
321 if (range
.parentElement() == input
)
323 var isCollapsed
= range
.text
== ''
324 range
.text
= replaceString
327 range
.moveStart('character', -replaceString
.length
);
333 Util
.prototype.rot13 = function(chaine
)
335 var ACode
= 'A'.charCodeAt(0)
336 var aCode
= 'a'.charCodeAt(0)
337 var MCode
= 'M'.charCodeAt(0)
338 var mCode
= 'm'.charCodeAt(0)
339 var ZCode
= 'Z'.charCodeAt(0)
340 var zCode
= 'z'.charCodeAt(0)
342 var f = function(ch
, pos
) {
343 if (pos
== ch
.length
)
346 var c
= ch
.charCodeAt(pos
);
347 return String
.fromCharCode(
349 (c
>= ACode
&& c
<= MCode
|| c
>= aCode
&& c
<= mCode
? 13 :
350 (c
> MCode
&& c
<= ZCode
|| c
> mCode
&& c
<= zCode
? -13 : 0))
356 ///////////////////////////////////////////////////////////////////////////////////////////////////
360 this.pageCourante
= null
365 * Accepte soit un objet soit un string.
366 * un string correspond au nom de la page, par exemple : "page" -> "page.html"
368 Pages
.prototype.ajouterPage = function(page
)
370 if (typeof page
== "string")
372 this.pages
[page
] = page
376 page
.pages
= this // la magie des langages dynamiques : le foutoire
377 this.pages
[page
.nom
] = page
381 Pages
.prototype.afficherPage = function(nomPage
, forcerChargement
)
383 if (forcerChargement
== undefined) forcerChargement
= false
385 var page
= this.pages
[nomPage
]
386 if (page
== undefined || (!forcerChargement
&& page
== this.pageCourante
)) return
388 if (this.pageCourante
!= null && this.pageCourante
.decharger
)
389 this.pageCourante
.decharger()
391 $("#menu li").removeClass("courante")
392 $("#menu li." + nomPage
).addClass("courante")
394 this.pageCourante
= page
396 if (typeof page
== "string")
397 $.ajax({async: false, url: "pages/" + page
+ ".html", success : function(page
) { contenu
+= page
}})
400 contenu
+= this.pageCourante
.contenu()
402 $("#page").html(contenu
).removeClass().addClass(this.pageCourante
.nom
+
403 (this.pageCourante
.classes
!= undefined ? " " + this.pageCourante
.classes() : "") // l'objet peut fournire des classes css supplémentaires sous la forme d'un string
406 if (this.pageCourante
.charger
)
407 this.pageCourante
.charger()
410 ///////////////////////////////////////////////////////////////////////////////////////////////////
413 * Classe permettant de formater du texte par exemple pour la substitution des liens dans les
414 * message par "[url]".
415 * TODO : améliorer l'efficacité des méthods notamment lié au smiles.
419 this.smiles
= conf
.smiles
420 this.protocoles
= "http|https|ed2k"
422 this.regexUrl
= new RegExp("(?:(?:" + this.protocoles
+ ")://|www\\.)[^ ]*", "gi")
423 this.regexImg
= new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i")
424 this.regexDomaine
= new RegExp("^(?:(?:" + this.protocoles
+ ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i")
425 this.regexTestProtocoleExiste
= new RegExp("^(?:" + this.protocoles
+ ")://.*$", "i")
426 this.regexNomProtocole
= new RegExp("^(.*?)://")
430 * Formate un pseudo saise par l'utilisateur.
431 * @param pseudo le pseudo brut
432 * @return le pseudo filtré
434 Formateur
.prototype.filtrerInputPseudo = function(pseudo
)
436 return pseudo
.replace(/{|}/g, "").trim()
439 Formateur
.prototype.getSmilesHTML = function()
442 for (var sNom
in this.smiles
)
444 XHTML
+= "<img class=\"" + sNom
+ "\" src=\"img/smileys/" + sNom
+ ".gif\" alt =\"" + sNom
+ "\" />"
450 * Formatage complet d'un texte.
452 * @pseudo facultatif, permet de contruire le label des images sous la forme : "<Pseudo> : <Message>"
454 Formateur
.prototype.traitementComplet = function(M
, pseudo
)
456 return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(M
)), pseudo
)))
460 * Transforme les liens en entités clickables.
461 * Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message :
462 * "{5F}" ou 5F est la racine de la conversation.
463 * Ce lien sera transformer en <span class="lienConv">{5F}</span> pouvant être clické pour créer la conv 5F.
465 Formateur
.prototype.traiterLiensConv = function(M
)
471 return "<span class=\"lienConv\">" + lien
+ "</span>"
477 * FIXME : Cette méthode est attrocement lourde ! A optimiser.
478 * moyenne sur échantillon : 234ms
480 Formateur
.prototype.traiterSmiles = function(M
)
482 for (var sNom
in this.smiles
)
484 var ss
= this.smiles
[sNom
]
485 for (var i
= 0; i
< ss
.length
; i
++)
486 M
= M
.replace(ss
[i
], "<img src=\"img/smileys/" + sNom
+ ".gif\" alt =\"" + sNom
+ "\" />")
491 Formateur
.prototype.remplacerBalisesHTML = function(M
)
493 return M
.replace(/</g
, "<").replace(/>/g
, ">").replace(/"/g, ""
;")
496 Formateur.prototype.traiterURL = function(M, pseudo)
498 var thisFormateur = this
500 var traitementUrl = function(url)
502 // si ya pas de protocole on rajoute "http://"
503 if (!thisFormateur
.regexTestProtocoleExiste
.test(url
))
504 url
= "http://" + url
505 var extension
= thisFormateur
.getShort(url
)
506 return "<a " + (extension
[1] ? "title=\"" + (pseudo
== undefined ? "" : thisFormateur
.traiterPourFenetreLightBox(pseudo
, url
) + ": ") + thisFormateur
.traiterPourFenetreLightBox(M
, url
) + "\"" + " rel=\"lightbox\"" : "") + " href=\"" + url
+ "\" >[" + extension
[0] + "]</a>"
508 return M
.replace(this.regexUrl
, traitementUrl
)
512 * Formatage en utilisant un sous-ensemble des règles de mediwiki.
513 * par exemple ''italic'' devient <i>italic</i>
515 Formateur
.prototype.traiterWikiSyntaxe = function(M
)
519 function(texte
, capture
)
521 return "<b>" + capture
+ "</b>"
525 function(texte
, capture
)
527 return "<i>" + capture
+ "</i>"
533 * Renvoie une version courte de l'url.
534 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
536 Formateur
.prototype.getShort = function(url
)
538 var estUneImage
= false
539 var versionShort
= null
540 var rechercheImg
= this.regexImg
.exec(url
)
542 if (rechercheImg
!= null)
544 versionShort
= rechercheImg
[1].toLowerCase()
545 if (versionShort
== "jpeg") versionShort
= "jpg" // jpeg -> jpg
550 var rechercheDomaine
= this.regexDomaine
.exec(url
)
551 if (rechercheDomaine
!= null && rechercheDomaine
.length
>= 2)
552 versionShort
= rechercheDomaine
[1]
555 var nomProtocole
= this.regexNomProtocole
.exec(url
)
556 if (nomProtocole
!= null && nomProtocole
.length
>= 2)
557 versionShort
= nomProtocole
[1]
561 return [versionShort
== null ? "url" : versionShort
, estUneImage
]
565 * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
567 Formateur
.prototype.traiterPourFenetreLightBox = function(M
, urlCourante
)
569 var thisFormateur
= this
570 var traitementUrl = function(url
)
572 return "[" + thisFormateur
.getShort(url
)[0] + (urlCourante
== url
? "*" : "") + "]"
575 return this.remplacerBalisesHTML(M
).replace(this.regexUrl
, traitementUrl
)
579 ///////////////////////////////////////////////////////////////////////////////////////////////////
581 // les statuts possibes du client
583 // mode enregistré, peut poster des messages et modifier son profile
585 // mode identifié, peut poster des messages mais n'a pas accès au profile
586 auth_not_registered : 1,
587 // mode déconnecté, ne peut pas poster de message
591 function Client(util
)
596 this.regexCookie
= new RegExp("^cookie=([^;]*)")
598 // données personnels
599 this.resetDonneesPersonnelles()
601 this.setStatut(statutType
.deconnected
)
603 // si true alors chaque modification du client est mémorisé sur le serveur
604 this.autoflush
= $.browser
["opera"]
607 Client
.prototype.resetDonneesPersonnelles = function()
610 this.pseudo
= conf
.pseudoDefaut
614 this.css
= $("link#cssPrincipale").attr("href")
615 this.chatOrder
= "reverse"
616 this.nickFormat
= "nick"
617 this.viewTimes
= true
618 this.viewTooltips
= true
619 this.cookie
= undefined
621 this.pagePrincipale
= 1
622 this.ekMaster
= false
623 this.ostentatiousMaster
= "light"
625 // les conversations, une conversation est un objet possédant les attributs suivants :
629 this.conversations
= []
632 Client
.prototype.setCss = function(css
)
634 if (this.css
== css
|| css
== "")
638 $("link#cssPrincipale").attr("href", this.css
)
639 if (this.autoflush
) this.flush(true)
642 Client
.prototype.pageSuivante = function(numConv
)
644 if (numConv
< 0 && this.pagePrincipale
> 1)
645 this.pagePrincipale
-= 1
646 else if (this.conversations
[numConv
].page
> 1)
647 this.conversations
[numConv
].page
-= 1
650 Client
.prototype.pagePrecedente = function(numConv
)
653 this.pagePrincipale
+= 1
655 this.conversations
[numConv
].page
+= 1
659 * Définit la première page pour la conversation donnée.
660 * @return true si la page a changé sinon false
662 Client
.prototype.goPremierePage = function(numConv
)
666 if (this.pagePrincipale
== 1)
668 this.pagePrincipale
= 1
672 if (this.conversations
[numConv
].page
== 1)
674 this.conversations
[numConv
].page
= 1
680 * Ajoute une conversation à la vue de l'utilisateur.
681 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
682 * @param racines la racine de la conversation (integer)
683 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
685 Client
.prototype.ajouterConversation = function(racine
)
687 // vérification s'il elle n'existe pas déjà
688 for (var i
= 0; i
< this.conversations
.length
; i
++)
689 if (this.conversations
[i
].root
== racine
)
692 this.conversations
.push({root : racine
, page : 1, reduit : false})
693 if (this.autoflush
) this.flush(true)
698 Client
.prototype.supprimerConversation = function(num
)
700 if (num
< 0 || num
>= this.conversations
.length
) return
702 // décalage TODO : supprimer le dernier élément
703 for (var i
= num
; i
< this.conversations
.length
- 1; i
++)
704 this.conversations
[i
] = this.conversations
[i
+1]
705 this.conversations
.pop()
707 if (this.autoflush
) this.flush(true)
710 Client
.prototype.getJSONLogin = function(login
, password
)
713 "header" : { "action" : "authentification", "version" : conf
.versionProtocole
},
715 "password" : password
719 Client
.prototype.getJSONLoginCookie = function()
722 "header" : { "action" : "authentification", "version" : conf
.versionProtocole
},
723 "cookie" : this.cookie
728 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
729 * de s'autentifier avec (login, password).
731 Client
.prototype.getJSONEnregistrement = function(login
, password
)
733 var mess
= { "header" : { "action" : "register", "version" : conf
.versionProtocole
}}
735 if (login
!= undefined && password
!= undefined)
737 mess
["login"] = login
738 mess
["password"] = password
744 Client
.prototype.getJSONConversations = function()
746 var conversations
= new Array()
747 for (var i
= 0; i
< this.conversations
.length
; i
++)
748 conversations
.push({root : this.conversations
[i
].root
, minimized : this.conversations
[i
].reduit
})
752 Client
.prototype.getJSONProfile = function()
755 "header" : { "action" : "set_profile", "version" : conf
.versionProtocole
},
756 "cookie" : this.cookie
,
757 "login" : this.login
,
758 "password" : this.password
,
759 "nick" : this.pseudo
,
760 "email" : this.email
,
762 "chat_order" : this.chatOrder
,
763 "nick_format" : this.nickFormat
,
764 "view_times" : this.viewTimes
,
765 "view_tooltips" : this.viewTooltips
,
766 "conversations" : this.getJSONConversations(),
767 "ostentatious_master" : this.ostentatiousMaster
772 * Renvoie null si pas définit.
774 Client
.prototype.getCookie = function()
776 var cookie
= this.regexCookie
.exec(document
.cookie
)
777 if (cookie
== null) this.cookie
= null
778 else this.cookie
= cookie
[1]
781 Client
.prototype.delCookie = function()
783 document
.cookie
= "cookie=; max-age=0"
786 Client
.prototype.setCookie = function()
788 if (this.cookie
== null || this.cookie
== undefined)
791 // ne fonctionne pas sous IE....
792 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
795 "cookie="+this.cookie
+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString()
798 Client
.prototype.authentifie = function()
800 return this.statut
== statutType
.auth_registered
|| this.statut
== statutType
.auth_not_registered
803 Client
.prototype.setStatut = function(statut
)
805 // conversation en "enum" si en "string"
806 if (typeof(statut
) == "string")
809 statut
== "auth_registered" ?
810 statutType
.auth_registered :
811 (statut
== "auth_not_registered" ? statutType
.auth_not_registered : statutType
.deconnected
)
814 if (statut
== this.statut
) return
822 * Effectue la connexion vers le serveur.
823 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
824 * S'il existe un cookie en local on s'authentifie directement avec lui.
825 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
827 Client
.prototype.connexionCookie = function()
830 if (this.cookie
== null) return false;
831 return this.connexion(this.getJSONLoginCookie())
834 Client
.prototype.connexionLogin = function(login
, password
)
836 return this.connexion(this.getJSONLogin(login
, password
))
839 Client
.prototype.enregistrement = function(login
, password
)
841 if (this.authentifie())
844 this.password
= password
847 this.setStatut(statutType
.auth_registered
)
854 return this.connexion(this.getJSONEnregistrement(login
, password
))
859 * Connexion. Réalisé de manière synchrone.
861 Client
.prototype.connexion = function(messageJson
)
863 var thisClient
= this
870 data: this.util
.jsonVersAction(messageJson
),
874 if (data
["reply"] == "error")
876 thisClient
.util
.messageDialogue(data
["error_message"])
877 // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
878 thisClient
.delCookie()
881 thisClient
.chargerDonnees(data
)
885 return this.authentifie()
888 Client
.prototype.deconnexion = function()
892 this.resetDonneesPersonnelles()
893 this.setStatut(statutType
.deconnected
) // deconnexion
896 Client
.prototype.chargerDonnees = function(data
)
898 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
899 // de l'état ekMaster
900 this.ekMaster
= data
["ek_master"] != undefined ? data
["ek_master"] : false
902 this.setStatut(data
["status"])
904 if (this.authentifie())
906 this.cookie
= data
["cookie"]
910 this.login
= data
["login"]
911 this.pseudo
= data
["nick"]
912 this.email
= data
["email"]
913 this.setCss(data
["css"])
914 this.chatOrder
= data
["chat_order"]
915 this.nickFormat
= data
["nick_format"]
916 this.viewTimes
= data
["view_times"]
917 this.viewTooltips
= data
["view_tooltips"]
918 this.ostentatiousMaster
= data
["ostentatious_master"]
920 // la page de la conversation principale
921 this.pagePrincipale
= 1
924 this.conversations
= data
["conversations"]
925 for (var i
= 0; i
< this.conversations
.length
; i
++)
926 this.conversations
[i
] = {root : this.conversations
[i
].root
, page : 1, reduit : this.conversations
[i
].minimized
}
929 this.majCssSelectionee()
935 * Met à jour les données personne sur serveur.
936 * @param async de manière asynchrone ? défaut = true
937 * @return false si le flush n'a pas pû se faire sinon true
939 Client
.prototype.flush = function(async
)
941 if (async
== undefined)
944 if (!this.authentifie())
947 var thisClient
= this
955 data: this.util
.jsonVersAction(this.getJSONProfile()),
959 if (data
["reply"] == "error")
961 thisClient
.util
.messageDialogue(data
["error_message"])
966 thisClient
.majBulle()
975 Client
.prototype.majMenu = function()
977 var displayType
= "block"
979 $("#menu .admin").css("display", this.ekMaster
? displayType : "none")
981 // met à jour le menu
982 if (this.statut
== statutType
.auth_registered
)
984 $("#menu .profile").css("display", displayType
).text("profile")
985 $("#menu .logout").css("display", displayType
)
986 $("#menu .register").css("display", "none")
988 else if (this.statut
== statutType
.auth_not_registered
)
990 $("#menu .profile").css("display", "none")
991 $("#menu .logout").css("display", displayType
)
992 $("#menu .register").css("display", displayType
)
996 $("#menu .profile").css("display", displayType
).text("login")
997 $("#menu .logout").css("display", "none")
998 $("#menu .register").css("display", displayType
)
1003 * Met à jour l'affichage des infos bulles en fonction du profile.
1005 Client
.prototype.majBulle = function()
1007 this.util
.bulleActive
= this.viewTooltips
1011 * Met à jour la css sélectionnée, lors du chargement des données.
1013 Client
.prototype.majCssSelectionee = function()
1015 // extraction du numéro de la css courante
1016 var numCssCourante
= this.css
.match(/^.*?\/(\d)\/.*$/)
1017 if (numCssCourante
!= null && numCssCourante
[1] != undefined)
1019 $("#menuCss option").removeAttr("selected")
1020 $("#menuCss option[value=" + numCssCourante
[1]+ "]").attr("selected", "selected")
1025 * Change la "class" du logo en fonction du statut de ekMaster.
1027 Client
.prototype.majLogo = function()
1030 $("#logo").addClass("ekMaster")
1032 $("#logo").removeClass("ekMaster")
1036 Client
.prototype.slap = function(userId
, raison
)
1038 var thisClient
= this
1044 data: this.util
.jsonVersAction(
1046 "header" : { "action" : "slap", "version" : conf
.versionProtocole
},
1047 "cookie" : thisClient
.cookie
,
1054 if (data
["reply"] == "error")
1055 thisClient
.util
.messageDialogue(data
["error_message"])
1060 Client
.prototype.ban = function(userId
, raison
, minutes
)
1062 var thisClient
= this
1064 // par défaut un ban correspond à 3 jours
1065 if (typeof(minutes
) == "undefined")
1066 minutes
= conf
.tempsBan
;
1072 data: this.util
.jsonVersAction(
1074 "header" : { "action" : "ban", "version" : conf
.versionProtocole
},
1075 "cookie" : thisClient
.cookie
,
1076 "duration" : minutes
,
1083 if (data
["reply"] == "error")
1084 thisClient
.util
.messageDialogue(data
["error_message"])
1089 Client
.prototype.kick = function(userId
, raison
)
1091 this.ban(userId
, raison
, conf
.tempsKick
)
1094 ///////////////////////////////////////////////////////////////////////////////////////////////////
1097 * classe permettant de gérer les événements (push serveur).
1098 * l'information envoyé est sous la forme :
1100 * "header" : {"action" : "wait_event", "version" : <v> },
1104 * l'information reçu est sous la forme :
1110 function PageEvent(page
, util
)
1115 // l'objet JSONHttpRequest représentant la connexion d'attente
1116 this.attenteCourante
= null
1118 // le multhreading du pauvre, merci javascript de m'offrire autant de primitives pour la gestion de la concurrence...
1123 * Arrête l'attente courante s'il y en a une.
1125 PageEvent
.prototype.stopAttenteCourante = function()
1129 if (this.attenteCourante
!= null)
1131 this.attenteCourante
.abort()
1136 * Attend un événement lié à la page.
1137 * @funSend une fonction renvoyant les données json à envoyer
1138 * @funsReceive est un objet comprenant les fonctions à appeler en fonction du "reply"
1139 * les fonctions acceptent un paramètre correspondant au données reçues.
1140 * exemple : {"new_message" : function(data){ ... }}
1142 PageEvent
.prototype.waitEvent = function(funSend
, funsReceive
)
1144 this.stopAttenteCourante()
1148 var thisPageEvent
= this
1150 // on doit conserver l'ordre des valeurs de l'objet JSON (le serveur les veut dans l'ordre définit dans le protocole)
1151 // TODO : ya pas mieux ?
1154 "header" : { "action" : "wait_event", "version" : conf
.versionProtocole
},
1157 var poulpe
= funSend()
1158 for (var v
in poulpe
)
1159 dataToSend
[v
] = poulpe
[v
]
1161 this.attenteCourante
= jQuery
.ajax({
1165 // TODO : doit disparaitre
1166 timeout: 180000, // timeout de 3min. Gros HACK pas beau. FIXME problème décrit ici : http://groups.google.com/group/jquery-en/browse_thread/thread/8724e64af3333a76
1167 data: this.util
.jsonVersAction(dataToSend
),
1171 funsReceive
[data
["reply"]](data
)
1173 // rappel de la fonction dans 100 ms
1174 setTimeout(function(){ thisPageEvent
.waitEvent2(funSend
, funsReceive
) }, 100)
1177 function(XMLHttpRequest
, textStatus
, errorThrown
)
1179 ;; console
.log("Connexion perdue dans waitEvent")
1180 setTimeout(function(){ thisPageEvent
.waitEvent2(funSend
, funsReceive
) }, 1000)
1186 * Si un stopAttenteCourante survient un peu n'importe quand il faut imédiatement arreter de boucler.
1188 PageEvent
.prototype.waitEvent2 = function(funSend
, funsReceive
)
1192 this.waitEvent(funSend
, funsReceive
)
1195 ///////////////////////////////////////////////////////////////////////////////////////////////////
1197 // charge dynamiquement le script de debug (obsolète)
1198 ;; jQuery
.ajax({async : false, url : "js/debug.js", dataType : "script"})
1204 var formateur
= new Formateur()
1205 var util
= new Util(formateur
)
1206 var client
= new Client(util
)
1207 var pages
= new Pages()
1209 // connexion vers le serveur (utilise un cookie qui traine)
1210 client
.connexionCookie()
1212 $("#menuCss").change(function(){ client
.setCss("css/" + $("option:selected", this).attr("value") + "/euphorik.css")})
1214 // FIXME : ne fonctionne pas sous opera
1215 // voir : http://dev.jquery.com/ticket/2892#preview
1216 $(window
).unload(function(){client
.flush()})
1218 $("#menu .minichat").click(function(){ pages
.afficherPage("minichat") })
1219 $("#menu .admin").click(function(){ pages
.afficherPage("admin") })
1220 $("#menu .profile").click(function(){ pages
.afficherPage("profile") })
1221 $("#menu .logout").click(function(){
1222 util
.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType
.question
,
1225 client
.deconnexion();
1226 pages
.afficherPage("minichat", true)
1228 "Non" : function(){}
1232 $("#menu .register").click(function(){ pages
.afficherPage("register") })
1233 $("#menu .about").click(function(){ pages
.afficherPage("about") })
1235 // TODO : simplifier et pouvoir créer des liens par exemple : <span class="lien" href="conditions">Conditions d'utilisation</span>
1236 $("#footer .conditions").click(function(){ pages
.afficherPage("conditions_utilisation") })
1238 pages
.ajouterPage(new PageMinichat(client
, formateur
, util
))
1239 pages
.ajouterPage(new PageAdmin(client
, formateur
, util
))
1240 pages
.ajouterPage(new PageProfile(client
, formateur
, util
))
1241 pages
.ajouterPage(new PageRegister(client
, formateur
, util
))
1242 pages
.ajouterPage(new PageAbout(client
, formateur
, util
))
1243 pages
.ajouterPage("conditions_utilisation")
1245 pages
.afficherPage("minichat")