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".
12 * Normalement 'const' à la place de 'var' mais non supporté par IE7.
15 nbMessageAffiche : 80, // (par page)
16 pseudoDefaut : "<nick>",
17 tempsAffichageMessageDialogue : 4000, // en ms
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, />\(/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]
39 ///////////////////////////////////////////////////////////////////////////////////////////////////
41 String
.prototype.trim = function()
43 return this.replace(/^\s+|\s+$/g, "");
46 String
.prototype.ltrim = function()
48 return this.replace(/^\s+/, "");
51 String
.prototype.rtrim = function()
53 return this.replace(/\s+$/, "");
56 String
.prototype.dump = function()
58 if (typeof dump
!= "undefined")
60 dump("\n--- EUPHORIK.CH ---\n")
66 ///////////////////////////////////////////////////////////////////////////////////////////////////
69 * Cette classe regroupe des fonctions utilitaires (helpers).
73 if(typeof XMLSerializer
!= "undefined")
74 this.serializer
= new XMLSerializer()
76 // fermeture des dialogues d
77 jQuery("#info .fermer").click(function(){
78 jQuery("#info").slideUp(50)
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é.
89 Util
.prototype.messageDialogue = function(message
, type
, boutons
)
91 if (type
== undefined)
92 type
= messageType
.informatif
94 if (this.timeoutMessageDialogue
!= undefined)
95 clearTimeout(this.timeoutMessageDialogue
)
97 var fermer = function(){jQuery("#info").slideUp(100)}
100 jQuery("#info .message").html(message
)
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
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
)
111 jQuery("#info").slideDown(200)
112 this.timeoutMessageDialogue
= setTimeout(fermer
, conf
.tempsAffichageMessageDialogue
)
114 var messageType
= {informatif: 0, question: 1, erreur: 2}
117 * Transforme un document XML en string.
119 Util
.prototype.serializeXML = function(documentXML
)
122 return this.serializer
.serializeToString(documentXML
)
124 return documentXML
.xml
127 Util
.prototype.creerDocumentXMLAction = function()
129 if (document
.implementation
&& document
.implementation
.createDocument
)
131 // var doc = document.implementation.createDocument("", "action", null)
132 var parser
= new DOMParser();
133 var doc
= parser
.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
134 //alert(this.serializeXML(doc))
137 else if (window
.ActiveXObject
)
139 var doc
= new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM")
140 doc
.appendChild(doc
.createElement("action"));
141 //doc.loadXML("<action></action>")
142 //alert(doc.documentElement)
143 //doc.createElement("action")
148 Util
.prototype.xmlVersAction = function(xml
)
150 //return {action: this.to_utf8(this.serializeXML(xml /*, "UTF-8"*/))}
151 return {action: this.serializeXML(xml
)}
154 Util
.prototype.md5 = function(chaine
)
156 return hex_md5(chaine
)
159 // pompé de http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130
160 Util
.prototype.setSelectionRange = function(input
, selectionStart
, selectionEnd
)
162 if (input
.setSelectionRange
)
165 input
.setSelectionRange(selectionStart
, selectionEnd
)
167 else if (input
.createTextRange
)
169 var range
= input
.createTextRange()
171 range
.moveEnd('character', selectionEnd
)
172 range
.moveStart('character', selectionStart
)
177 Util
.prototype.setCaretToEnd = function(input
)
179 this.setSelectionRange(input
, input
.value
.length
, input
.value
.length
)
181 Util
.prototype.setCaretToBegin = function(input
)
183 this.setSelectionRange(input
, 0, 0)
185 Util
.prototype.setCaretToPos = function(input
, pos
)
187 this.setSelectionRange(input
, pos
, pos
)
189 Util
.prototype.selectString = function(input
, string
)
191 var match
= new RegExp(string
, "i").exec(input
.value
)
194 this.setSelectionRange (input
, match
.index
, match
.index
+ match
[0].length
)
197 Util
.prototype.replaceSelection = function(input
, replaceString
) {
198 if (input
.setSelectionRange
)
200 var selectionStart
= input
.selectionStart
201 var selectionEnd
= input
.selectionEnd
202 input
.value
= input
.value
.substring(0, selectionStart
) + replaceString
+ input
.value
.substring(selectionEnd
)
204 if (selectionStart
!= selectionEnd
) // has there been a selection
205 this.setSelectionRange(input
, selectionStart
, selectionStart
+ replaceString
.length
)
207 this.setCaretToPos(input
, selectionStart
+ replaceString
.length
)
209 else if (document
.selection
)
211 var range
= document
.selection
.createRange();
212 if (range
.parentElement() == input
)
214 var isCollapsed
= range
.text
== ''
215 range
.text
= replaceString
218 // there has been a selection
219 // it appears range.select() should select the newly
220 // inserted text but that fails with IE
221 range
.moveStart('character', -replaceString
.length
);
228 ///////////////////////////////////////////////////////////////////////////////////////////////////
232 this.pageCourante
= null
236 Pages
.prototype.ajouterPage = function(page
)
238 page
.pages
= this // la magie des langages dynamiques : le foutoire
239 this.pages
[page
.nom
] = page
242 Pages
.prototype.afficherPage = function(nomPage
, forcerChargement
)
244 if (forcerChargement
== undefined) forcerChargement
= false
246 var page
= this.pages
[nomPage
]
247 if (page
== undefined || (!forcerChargement
&& page
== this.pageCourante
)) return
249 if (this.pageCourante
!= null && this.pageCourante
.decharger
)
250 this.pageCourante
.decharger()
252 jQuery("#menu div").removeClass("courante")
253 jQuery("#menu div." + nomPage
).addClass("courante")
255 this.pageCourante
= page
256 jQuery("#page").html(this.pageCourante
.contenu()).removeClass().addClass(this.pageCourante
.nom
)
258 if (this.pageCourante
.charger
)
259 this.pageCourante
.charger()
262 ///////////////////////////////////////////////////////////////////////////////////////////////////
266 this.smiles
= conf
.smiles
267 this.protocoles
= "http|https|ed2k"
269 this.regexUrl
= new RegExp("(?:(?:" + this.protocoles
+ ")://|www\\.)[^ ]*", "gi")
270 this.regexImg
= new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i")
271 this.regexDomaine
= new RegExp("^(?:(?:" + this.protocoles
+ ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i")
272 this.regexTestProtocoleExiste
= new RegExp("^(?:" + this.protocoles
+ ")://.*$", "i")
273 this.regexNomProtocole
= new RegExp("^(.*?)://")
277 * Formate un pseudo saise par l'utilisateur.
278 * @param pseudo le pseudo brut
279 * @return le pseudo filtré
281 Formateur
.prototype.filtrerInputPseudo = function(pseudo
)
283 return pseudo
.replace(/{|}/g, "").trim()
286 Formateur
.prototype.getSmilesHTML = function()
289 for (var sNom
in this.smiles
)
291 XHTML
+= "<img class=\"" + sNom
+ "\" src=\"img/smileys/" + sNom
+ ".gif\" />"
296 Formateur
.prototype.traitementComplet = function(M
, pseudo
)
298 return this.traiterSmiles(this.traiterURL(this.remplacerBalisesHTML(M
), pseudo
))
302 * FIXME : Cette méthode est attrocement lourde !!
304 Formateur
.prototype.traiterSmiles = function(M
)
306 for (var sNom
in this.smiles
)
308 ss
= this.smiles
[sNom
]
309 for (var i
= 0; i
< ss
.length
; i
++)
310 M
= M
.replace(ss
[i
], "<img src=\"img/smileys/" + sNom
+ ".gif\" />")
315 Formateur
.prototype.remplacerBalisesHTML = function(M
)
317 return M
.replace(/</g
, "<").replace(/>/g
, ">")
320 Formateur
.prototype.traiterURL = function(M
, pseudo
)
324 if (pseudo
== undefined)
327 var traitementUrl = function(url
)
329 // si ya pas de protocole on rajoute "http://"
330 if (!thisFormateur
.regexTestProtocoleExiste
.test(url
))
331 url
= "http://" + url
332 var extension
= thisFormateur
.getShort(url
)
333 return "<a " + (extension
[1] ? "title=\"" + thisFormateur
.traiterPourFenetreLightBox(pseudo
, url
) + ": " + thisFormateur
.traiterPourFenetreLightBox(M
, url
) + "\"" + " rel=\"lightbox[groupe]\"" : "") + " href=\"" + url
+ "\" >[" + extension
[0] + "]</a>"
335 return M
.replace(this.regexUrl
, traitementUrl
)
339 * Renvoie une version courte de l'url.
340 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
342 Formateur
.prototype.getShort = function(url
)
344 var estUneImage
= false
345 var versionShort
= null
346 var rechercheImg
= this.regexImg
.exec(url
)
348 if (rechercheImg
!= null)
350 versionShort
= rechercheImg
[1].toLowerCase()
351 if (versionShort
== "jpeg") versionShort
= "jpg" // jpeg -> jpg
356 var rechercheDomaine
= this.regexDomaine
.exec(url
)
357 if (rechercheDomaine
!= null && rechercheDomaine
.length
>= 2)
358 versionShort
= rechercheDomaine
[1]
361 var nomProtocole
= this.regexNomProtocole
.exec(url
)
362 if (nomProtocole
!= null && nomProtocole
.length
>= 2)
363 versionShort
= nomProtocole
[1]
367 return [versionShort
== null ? "url" : versionShort
, estUneImage
]
371 * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
373 Formateur
.prototype.traiterPourFenetreLightBox = function(M
, urlCourante
)
376 var traitementUrl = function(url
)
378 return "[" + thisFormateur
.getShort(url
)[0] + (urlCourante
== url
? ": image courante" : "") + "]"
381 return this.remplacerBalisesHTML(M
).replace(this.regexUrl
, traitementUrl
)
385 ///////////////////////////////////////////////////////////////////////////////////////////////////
387 var statutType
= {enregistre: 0, identifie: 1, non_identifie: 2}
389 function Client(util
)
394 this.regexCookie
= new RegExp("^cookie=([^;]*)")
397 //this.captchaCrypt = null
399 // données personnels
400 this.resetDonneesPersonnelles()
402 this.setStatut(statutType
.non_identifie
)
404 // le dernier message d'erreur recut du serveur (par exemple une connexion foireuse : "login impossible")
405 this.dernierMessageErreur
= ""
408 Client
.prototype.resetDonneesPersonnelles = function()
410 this.pseudo
= conf
.pseudoDefaut
414 this.css
= jQuery("link#cssPrincipale").attr("href")
417 Client
.prototype.setCss = function(css
)
423 jQuery("link#cssPrincipale").attr("href", this.css
)
426 /* enregistement automatique..
427 if (!this.identifie())
428 if (!this.enregistrement())
431 if (this.identifie())
435 Client
.prototype.getXMLlogin = function(login
, password
)
437 var XMLDocument
= this.util
.creerDocumentXMLAction()
438 XMLDocument
.documentElement
.setAttribute("name", "login")
440 var nodeLogin
= XMLDocument
.createElement("login")
441 nodeLogin
.appendChild(XMLDocument
.createTextNode(login
))
442 XMLDocument
.documentElement
.appendChild(nodeLogin
)
444 var nodePassword
= XMLDocument
.createElement("password")
445 nodePassword
.appendChild(XMLDocument
.createTextNode(password
))
446 XMLDocument
.documentElement
.appendChild(nodePassword
)
451 Client
.prototype.getXMLloginCookie = function()
453 var XMLDocument
= this.util
.creerDocumentXMLAction()
454 XMLDocument
.documentElement
.setAttribute("name", "login")
456 var nodeCookie
= XMLDocument
.createElement("cookie")
457 nodeCookie
.appendChild(XMLDocument
.createTextNode(this.cookie
))
458 XMLDocument
.documentElement
.appendChild(nodeCookie
)
464 Client.prototype.getXMLloginCaptcha = function(captchaCrypt, captchaInput)
466 var XMLDocument = this.util.creerDocumentXMLAction()
467 XMLDocument.documentElement.setAttribute("name", "loginCaptcha")
469 var nodecaptchaCrypt = XMLDocument.createElement("captchaCrypt")
470 nodecaptchaCrypt.appendChild(XMLDocument.createTextNode(captchaCrypt))
471 XMLDocument.documentElement.appendChild(nodecaptchaCrypt)
473 var nodecaptchaInput = XMLDocument.createElement("captchaInput")
474 nodecaptchaInput.appendChild(XMLDocument.createTextNode(captchaInput))
475 XMLDocument.documentElement.appendChild(nodecaptchaInput)
481 Client.prototype.getXMLgenerationCaptcha = function()
483 var XMLDocument = this.util.creerDocumentXMLAction()
484 XMLDocument.documentElement.setAttribute("name", "generationCaptcha")
489 Client
.prototype.getXMLEnregistrement = function(login
, password
)
491 var XMLDocument
= this.util
.creerDocumentXMLAction()
492 XMLDocument
.documentElement
.setAttribute("name", "register")
494 var nodeLogin
= XMLDocument
.createElement("login")
495 nodeLogin
.appendChild(XMLDocument
.createTextNode(login
))
496 XMLDocument
.documentElement
.appendChild(nodeLogin
)
498 var nodePassword
= XMLDocument
.createElement("password")
499 nodePassword
.appendChild(XMLDocument
.createTextNode(password
))
500 XMLDocument
.documentElement
.appendChild(nodePassword
)
505 Client
.prototype.getXMLProfile = function()
507 var XMLDocument
= this.util
.creerDocumentXMLAction()
508 XMLDocument
.documentElement
.setAttribute("name", "profile")
510 var nodeCookie
= XMLDocument
.createElement("cookie")
511 nodeCookie
.appendChild(XMLDocument
.createTextNode(this.cookie
))
512 XMLDocument
.documentElement
.appendChild(nodeCookie
)
514 var nodeLogin
= XMLDocument
.createElement("login")
515 nodeLogin
.appendChild(XMLDocument
.createTextNode(this.login
))
516 XMLDocument
.documentElement
.appendChild(nodeLogin
)
518 var nodePassword
= XMLDocument
.createElement("password")
519 nodePassword
.appendChild(XMLDocument
.createTextNode(this.password
))
520 XMLDocument
.documentElement
.appendChild(nodePassword
)
522 var nodePseudo
= XMLDocument
.createElement("pseudo")
523 nodePseudo
.appendChild(XMLDocument
.createTextNode(this.pseudo
))
524 XMLDocument
.documentElement
.appendChild(nodePseudo
)
526 var nodeEmail
= XMLDocument
.createElement("email")
527 nodeEmail
.appendChild(XMLDocument
.createTextNode(this.email
))
528 XMLDocument
.documentElement
.appendChild(nodeEmail
)
530 var nodeCSS
= XMLDocument
.createElement("css")
531 nodeCSS
.appendChild(XMLDocument
.createTextNode(this.css
))
532 XMLDocument
.documentElement
.appendChild(nodeCSS
)
538 * Renvoie null si pas définit.
540 Client
.prototype.getCookie = function()
542 var cookie
= this.regexCookie
.exec(document
.cookie
)
543 if (cookie
== null) this.cookie
= null
544 else this.cookie
= cookie
[1]
547 Client
.prototype.delCookie = function()
549 document
.cookie
= "cookie=; max-age=0"
552 Client
.prototype.setCookie = function(cookie
)
554 if (this.cookie
== null)
558 "cookie="+this.cookie
+
559 "; max-age=" + (60 * 60 * 24 * 365)
562 Client
.prototype.identifie = function()
564 return this.statut
== statutType
.enregistre
|| this.statut
== statutType
.identifie
567 Client
.prototype.setStatut = function(statut
)
569 if(typeof(statut
) == "string")
572 statut
== "enregistre" ?
573 statutType
.enregistre : (statut
== "identifie" ? statutType
.identifie : statutType
.non_identifie
)
576 if (statut
== this.statut
) return
583 * Demande la génération d'un captcha au serveur et l'affiche.
586 Client.prototype.afficherCaptcha = function(query)
588 var thisClient = this
590 $.post("request", this.util.xmlVersAction(this.getXMLgenerationCaptcha()),
591 function(data, textStatus)
593 var chemin = jQuery("chemin", data.documentElement).text()
594 thisClient.captchaCrypt = jQuery("captchaCrypt", data.documentElement).text()
595 jQuery(query).prepend(
596 "<p id=\"captcha\" >Es-tu un bot ? <img class=\"captchaImg\" src=\"" + chemin + "\" />" +
597 "<input name=\"captchaInput\" type=\"text\" size=\"5\" max_length=\"5\" ></p>"
603 Client.prototype.cacherCaptcha = function()
605 jQuery("#captcha").remove()
609 * Effectue la connexion vers le serveur.
610 * Cette fonction est bloquante tant que la connexion n'a pas été établie.
611 * S'il existe un cookie en local on s'authentifie directement avec lui.
612 * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.
614 Client
.prototype.connexionCookie = function()
617 if (this.cookie
== null) return false;
618 return this.connexion(this.util
.xmlVersAction(this.getXMLloginCookie()))
621 Client
.prototype.connexionLogin = function(login
, password
)
623 return this.connexion(this.util
.xmlVersAction(this.getXMLlogin(login
, password
)))
627 Client.prototype.connexionCaptcha = function()
629 return this.connexion(this.util.xmlVersAction(this.getXMLloginCaptcha(this.captchaCrypt, jQuery("#captcha input").val())))
632 Client
.prototype.enregistrement = function(login
, password
)
634 if (this.identifie())
637 this.password
= password
639 this.setStatut(statutType
.enregistre
)
644 if (login
== undefined) login
= ""
645 if (password
== undefined) password
= ""
646 return this.connexion(this.util
.xmlVersAction(this.getXMLEnregistrement(login
, password
)))
650 Client
.prototype.connexion = function(action
)
663 thisClient
.chargerDonnees(data
)
667 return this.identifie()
670 Client
.prototype.deconnexion = function()
672 this.setStatut(statutType
.non_identifie
) // deconnexion
673 this.resetDonneesPersonnelles()
677 Client
.prototype.chargerDonnees = function(data
)
679 this.setStatut(jQuery("statut", data
.documentElement
).text())
681 if (this.identifie())
683 this.cookie
= jQuery("cookie", data
.documentElement
).text()
686 this.login
= jQuery("login", data
.documentElement
).text()
687 this.pseudo
= jQuery("pseudo", data
.documentElement
).text()
688 this.email
= jQuery("email", data
.documentElement
).text()
689 this.css
= jQuery("css", data
.documentElement
).text()
693 jQuery("link#cssPrincipale").attr("href", this.css
)
697 this.dernierMessageErreur
= jQuery("information", data
.documentElement
).text()
701 * Met à jour les données personne sur serveur.
703 Client
.prototype.flush = function()
706 //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action)
713 data: this.util
.xmlVersAction(this.getXMLProfile()),
717 //thisClient.util.log(thisClient.util.serializer.serializeToString(data))
721 // TODO : retourner false si un problème est survenu lors de l'update du profile
725 Client
.prototype.majMenu = function()
727 var displayType
= this.css
== "css/3/euphorik.css" ? "block" : "inline" //this.client
729 // met à jour le menu
730 if (this.statut
== statutType
.enregistre
)
732 jQuery("#menu .profile").css("display", displayType
).text("profile")
733 jQuery("#menu .logout").css("display", displayType
)
734 jQuery("#menu .register").css("display", "none")
736 else if (this.statut
== statutType
.identifie
)
738 jQuery("#menu .profile").css("display", "none")
739 jQuery("#menu .logout").css("display", displayType
)
740 jQuery("#menu .register").css("display", displayType
)
744 jQuery("#menu .profile").css("display", displayType
).text("login")
745 jQuery("#menu .logout").css("display", "none")
746 jQuery("#menu .register").css("display", displayType
)
750 ///////////////////////////////////////////////////////////////////////////////////////////////////
756 jQuery(document
).ready(
759 /* FIXME : ce code pose problème sur konqueror, voir : http://www.kde-forum.org/thread.php?threadid=17993
760 var p = new DOMParser();
761 var doc = p.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
762 var s = new XMLSerializer()
763 alert(s.serializeToString(doc)) */
765 var util
= new Util()
766 var client
= new Client(util
)
767 var pages
= new Pages()
768 var formateur
= new Formateur()
770 // connexion vers le serveur (utilise un cookie qui traine)
771 client
.connexionCookie()
774 for (var i
= 1; i
<= 3; i
++)
776 jQuery("#css"+i
).click(function(){
777 client
.setCss("css/" + jQuery(this).attr("id").charAt(3) + "/euphorik.css")
781 jQuery("#menu .minichat").click(function(){ pages
.afficherPage("minichat") })
782 jQuery("#menu .profile").click(function(){ pages
.afficherPage("profile") })
783 jQuery("#menu .logout").click(function(){
784 util
.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType
.question
,
787 client
.deconnexion();
788 pages
.afficherPage("minichat", true)
794 jQuery("#menu .register").click(function(){ pages
.afficherPage("register") })
796 pages
.ajouterPage(new PageMinichat(client
, formateur
, util
))
797 pages
.ajouterPage(new PageProfile(client
, formateur
, util
))
798 pages
.ajouterPage(new PageRegister(client
, formateur
, util
))
799 pages
.afficherPage("minichat")