X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Feuphorik.js;h=bd0cd3ad403a73b316a39a35fdd77600bf4ccbd0;hp=433c0bca25454745bab0b483d4bc4eaef60c038d;hb=e04de9d41e7955b0092fc33b8619b4627af6b3f3;hpb=6feaef2a51f7226c41d69f9e86f605718b78aa02 diff --git a/js/euphorik.js b/js/euphorik.js index 433c0bc..bd0cd3a 100755 --- a/js/euphorik.js +++ b/js/euphorik.js @@ -1,6 +1,8 @@ // coding: utf-8 -/** +/** + * Contient la base javascript pour le site euphorik.ch. + * Chaque page possède son propre fichier js nommé "page.js". * Auteur : GBurri * Date : 6.11.2007 */ @@ -10,7 +12,7 @@ * Normalement 'const' à la place de 'var' mais non supporté par IE7. */ var conf = { - nbMessageAffiche : 80, // (par page) + nbMessageAffiche : 10, // (par page) pseudoDefaut : "", tempsAffichageMessageDialogue : 4000, // en ms smiles : { @@ -38,30 +40,36 @@ var conf = { String.prototype.trim = function() { - return this.replace(/^\s+|\s+$/g,""); + return this.replace(/^\s+|\s+$/g, ""); } + String.prototype.ltrim = function() { - return this.replace(/^\s+/,""); + return this.replace(/^\s+/, ""); } + String.prototype.rtrim = function() { - return this.replace(/\s+$/,""); + return this.replace(/\s+$/, ""); } -String.prototype.dump = function() -{ +String.prototype.dump = function(titre) +{ + titre = titre == undefined ? "" : titre if (typeof dump != "undefined") { - dump("\n--- EUPHORIK.CH ---\n") + dump("\n--- EUPHORIK.CH : " + titre + " ---\n") dump(this) dump("\n------\n") } } /////////////////////////////////////////////////////////////////////////////////////////////////// - -function Util(serializer) + +/** + * Cette classe regroupe des fonctions utilitaires (helpers). + */ +function Util() { if(typeof XMLSerializer != "undefined") this.serializer = new XMLSerializer() @@ -73,7 +81,10 @@ function Util(serializer) /** * Affiche une boite de dialogue avec un message à l'intérieur. - * + * @param message le message (string) + * @param type voir 'messageType'. par défaut messageType.informatif + * @param les boutons sous la forme d'un objet ou les clefs sont les labels des boutons + * et les valeurs les fonctions executées lorsqu'un bouton est activé. */ Util.prototype.messageDialogue = function(message, type, boutons) { @@ -102,11 +113,9 @@ Util.prototype.messageDialogue = function(message, type, boutons) } var messageType = {informatif: 0, question: 1, erreur: 2} -/* obsolète -Util.prototype.log = function(str) -{ -}*/ - +/** + * Transforme un document XML en string. + */ Util.prototype.serializeXML = function(documentXML) { if (this.serializer) @@ -115,51 +124,45 @@ Util.prototype.serializeXML = function(documentXML) return documentXML.xml } +var documentXMLBase = undefined // singleton Util.prototype.creerDocumentXMLAction = function() -{ - if (document.implementation && document.implementation.createDocument) - { - return document.implementation.createDocument("", "action", null) - } - else if (window.ActiveXObject) +{ + // FIXME : essayer de garder le doc de base en cache (singleton) et d'en retourner une copie + if (true)//documentXMLBase == undefined) { - var doc = new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM") - doc.appendChild(doc.createElement("action")); - //doc.loadXML("") - //alert(doc.documentElement) - //doc.createElement("action") - return doc - } + if (document.implementation && document.implementation.createDocument) + { + // var doc = document.implementation.createDocument("", "action", null) + var parser = new DOMParser(); + documentXMLBase = parser.parseFromString("\n", "text/xml") + //alert(this.serializeXML(doc)) + } + else if (window.ActiveXObject) + { + documentXMLBase = new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM") + documentXMLBase.appendChild(doc.createElement("action")); + //doc.loadXML("") + //alert(doc.documentElement) + //doc.createElement("action") + } + } + return documentXMLBase } Util.prototype.xmlVersAction = function(xml) { - return {action: this.to_utf8(this.serializeXML(xml /*, "UTF-8"*/))} + //return {action: this.to_utf8(this.serializeXML(xml /*, "UTF-8"*/))} + return {action: this.serializeXML(xml)} } -// voir : http://homepage3.nifty.com/aokura/jscript/utf8.html -// et : http://www1.tip.nl/~t876506/utf8tbl.html -Util.prototype.to_utf8 = function(s) -{ - if (!s) return "" - - var c, d = "" - for (var i = 0; i < s.length; i++) - { - c = s.charCodeAt(i); - if (c <= 0x7f) { - d += s.charAt(i); - } else if (c >= 0x80 && c <= 0x7ff) { - d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0); - d += String.fromCharCode((c & 0x3f) | 0x80); - } else { - d += String.fromCharCode((c >> 12) | 0xe0); - d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80); - d += String.fromCharCode((c & 0x3f) | 0x80); - } - } - return d; -} +/** + * Utilisé pour l'envoie de donnée avec la méthode ajax de jQuery. + */ +Util.prototype.jsonVersAction = function(json) +{ + // FIXME : ne plus encapsuler json dans de l'xml (problème avec yaws) + return {action: "" + JSON.stringify(json) + "" } +} Util.prototype.md5 = function(chaine) { @@ -305,11 +308,28 @@ Formateur.prototype.getSmilesHTML = function() Formateur.prototype.traitementComplet = function(M, pseudo) { - return this.traiterSmiles(this.traiterURL(this.remplacerBalisesHTML(M), pseudo)) + return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.remplacerBalisesHTML(M), pseudo))) +} + +/** + * Transforme les liens en entités clickables. + * Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message : + * "{5F}" ou 5F est la racine de la conversation. + * Ce lien sera transformer en {5F} pouvant être clické pour créer la conv 5F. + */ +Formateur.prototype.traiterLiensConv = function(M) +{ + return M.replace( + /\{\w+\}/g, + function(lien) + { + return "" + lien + "" + } + ) } /** - * FIXME : Cette méthode est attrocement lourde !! + * FIXME : Cette méthode est attrocement lourde ! A optimiser. */ Formateur.prototype.traiterSmiles = function(M) { @@ -402,9 +422,6 @@ function Client(util) this.cookie = null this.regexCookie = new RegExp("^cookie=([^;]*)") - - // Obsolète - //this.captchaCrypt = null // données personnels this.resetDonneesPersonnelles() @@ -421,7 +438,14 @@ Client.prototype.resetDonneesPersonnelles = function() this.login = "" this.password = "" this.email = "" - this.css = jQuery("link#cssPrincipale").attr("href") + this.css = jQuery("link#cssPrincipale").attr("href") + + this.pagePrincipale = 1 + + // les conversations, une conversation est un objet possédant les attributs suivants : + // - racine (entier) + // - page (entier) + this.conversations = new Array() } Client.prototype.setCss = function(css) @@ -432,14 +456,79 @@ Client.prototype.setCss = function(css) this.css = css jQuery("link#cssPrincipale").attr("href", this.css) this.majMenu() - - /* enregistement automatique.. - if (!this.identifie()) - if (!this.enregistrement()) - return - */ + if (this.identifie()) this.flush() +} + +Client.prototype.pageSuivante = function(numConv) +{ + if (numConv < 0 && this.pagePrincipale > 1) + this.pagePrincipale -= 1 + else if (this.conversations[numConv].page > 1) + this.conversations[numConv].page -= 1 + this.flush(false) +} + +Client.prototype.pagePrecedente = function(numConv) +{ + if (numConv < 0) + this.pagePrincipale += 1 + else + this.conversations[numConv].page += 1 + this.flush(false) +} + +/** + * Définit la première page pour la conversation donnée. + * @return true si la page a changé sinon false + */ +Client.prototype.goPremierePage = function(numConv) +{ + if (numConv < 0) + { + if (this.pagePrincipale == 1) + return false + this.pagePrincipale = 1 + } + else + { + if (this.conversations[numConv].page == 1) + return false + this.conversations[numConv].page = 1 + } + this.flush(false) + return true +} + +/** + * Ajoute une conversation à la vue de l'utilisateur. + * Le profile de l'utilisateur est directement sauvegardé sur le serveur. + * @param racines la racine de la conversation + * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà) + */ +Client.prototype.ajouterConversation = function(racine) +{ + // vérification s'il elle n'existe pas déjà + for (var i = 0; i < this.conversations.length; i++) + if (this.conversations[i].racine == racine) + return false + + this.conversations.push({racine : racine, page : 1}) + this.flush(false) + return true +} + +Client.prototype.supprimerConversation = function(num) +{ + if (num < 0 || num >= this.conversations.length) return + + // décalage TODO : supprimer le dernier élément + for (var i = num; i < this.conversations.length - 1; i++) + this.conversations[i] = this.conversations[i+1] + this.conversations.pop() + + this.flush(false) } Client.prototype.getXMLlogin = function(login, password) @@ -458,6 +547,15 @@ Client.prototype.getXMLlogin = function(login, password) return XMLDocument } +Client.prototype.getJSONLogin = function(login, password) +{ + return { + "action" : "authentification", + "login" : login, + "password" : password + } +} + Client.prototype.getXMLloginCookie = function() { var XMLDocument = this.util.creerDocumentXMLAction() @@ -469,32 +567,6 @@ Client.prototype.getXMLloginCookie = function() return XMLDocument } - -/* Obsolète -Client.prototype.getXMLloginCaptcha = function(captchaCrypt, captchaInput) -{ - var XMLDocument = this.util.creerDocumentXMLAction() - XMLDocument.documentElement.setAttribute("name", "loginCaptcha") - - var nodecaptchaCrypt = XMLDocument.createElement("captchaCrypt") - nodecaptchaCrypt.appendChild(XMLDocument.createTextNode(captchaCrypt)) - XMLDocument.documentElement.appendChild(nodecaptchaCrypt) - - var nodecaptchaInput = XMLDocument.createElement("captchaInput") - nodecaptchaInput.appendChild(XMLDocument.createTextNode(captchaInput)) - XMLDocument.documentElement.appendChild(nodecaptchaInput) - - return XMLDocument -}*/ - -/* Obsolète -Client.prototype.getXMLgenerationCaptcha = function() -{ - var XMLDocument = this.util.creerDocumentXMLAction() - XMLDocument.documentElement.setAttribute("name", "generationCaptcha") - - return XMLDocument -}*/ Client.prototype.getXMLEnregistrement = function(login, password) { @@ -512,6 +584,10 @@ Client.prototype.getXMLEnregistrement = function(login, password) return XMLDocument } +/** + * Sérialize le profile en XML. + * TODO : méthode assez lourde, 3.25ms de moyenne + */ Client.prototype.getXMLProfile = function() { var XMLDocument = this.util.creerDocumentXMLAction() @@ -541,6 +617,25 @@ Client.prototype.getXMLProfile = function() nodeCSS.appendChild(XMLDocument.createTextNode(this.css)) XMLDocument.documentElement.appendChild(nodeCSS) + var nodePagePrincipale = XMLDocument.createElement("pagePrincipale") + nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale < 1 ? 1 : this.pagePrincipale)) + XMLDocument.documentElement.appendChild(nodePagePrincipale) + + // mémorise les conversations affichées + for (var i = 0; i < this.conversations.length; i++) + { + var nodeConv = XMLDocument.createElement("conversation") + XMLDocument.documentElement.appendChild(nodeConv) + + var nodeRacine = XMLDocument.createElement("racine") + nodeRacine.appendChild(XMLDocument.createTextNode(this.conversations[i].racine)) + nodeConv.appendChild(nodeRacine) + + var nodePage = XMLDocument.createElement("page") + nodePage.appendChild(XMLDocument.createTextNode(this.conversations[i].page)) + nodeConv.appendChild(nodePage) + } + return XMLDocument } @@ -587,33 +682,7 @@ Client.prototype.setStatut = function(statut) this.statut = statut this.majMenu() -} - -/** - * Demande la génération d'un captcha au serveur et l'affiche. - */ - /* Obsolète -Client.prototype.afficherCaptcha = function(query) -{ - var thisClient = this - - $.post("request", this.util.xmlVersAction(this.getXMLgenerationCaptcha()), - function(data, textStatus) - { - var chemin = jQuery("chemin", data.documentElement).text() - thisClient.captchaCrypt = jQuery("captchaCrypt", data.documentElement).text() - jQuery(query).prepend( - "

Es-tu un bot ? " + - "

" - ) - } - ) } - -Client.prototype.cacherCaptcha = function() -{ - jQuery("#captcha").remove() -}*/ /** * Effectue la connexion vers le serveur. @@ -630,14 +699,9 @@ Client.prototype.connexionCookie = function() Client.prototype.connexionLogin = function(login, password) { - return this.connexion(this.util.xmlVersAction(this.getXMLlogin(login, password))) -} - -/* Obsolète -Client.prototype.connexionCaptcha = function() -{ - return this.connexion(this.util.xmlVersAction(this.getXMLloginCaptcha(this.captchaCrypt, jQuery("#captcha input").val()))) -}*/ + // return this.connexion(this.util.xmlVersAction(this.getXMLlogin(login, password))) + return this.connexion(this.util.jsonVersAction(this.getJSONLogin(login, password))) +} Client.prototype.enregistrement = function(login, password) { @@ -659,17 +723,19 @@ Client.prototype.enregistrement = function(login, password) Client.prototype.connexion = function(action) { + action.action.dump("Connexion client") thisClient = this jQuery.ajax( { async: false, type: "POST", url: "request", - dataType: "xml", + dataType: "json", data: action, success: function(data) { + thisClient.util.serializer.serializeToString(data).dump("Charger client") thisClient.chargerDonnees(data) } } @@ -686,6 +752,8 @@ Client.prototype.deconnexion = function() Client.prototype.chargerDonnees = function(data) { + var thisClient = this + this.setStatut(jQuery("statut", data.documentElement).text()) if (this.identifie()) @@ -697,26 +765,43 @@ Client.prototype.chargerDonnees = function(data) this.pseudo = jQuery("pseudo", data.documentElement).text() this.email = jQuery("email", data.documentElement).text() this.css = jQuery("css", data.documentElement).text() + + // la page de la conversation principale + var tmp = jQuery("pagePrincipale", data.documentElement) + this.pagePrincipale = tmp.length < 1 ? 1 : parseInt(tmp.text()) + // met à jour la css if (this.css != "") { jQuery("link#cssPrincipale").attr("href", this.css) this.majMenu() } + // les conversations + this.conversations = new Array() + jQuery("conversation", data.documentElement).each( + function(i) + { + thisClient.conversations.push( { racine : jQuery("racine", this).text(), page : jQuery("page", this).text() } ) + } + ) } this.dernierMessageErreur = jQuery("information", data.documentElement).text() } /** * Met à jour les données personne sur serveur. + * @param async de manière asynchrone ? défaut = true */ -Client.prototype.flush = function() +Client.prototype.flush = function(async) { + if (async == undefined) + async = true + thisClient = this - //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action) + this.util.xmlVersAction(this.getXMLProfile()).action.dump("Flush client") jQuery.ajax( { - async: true, + async: async, type: "POST", url: "request", dataType: "xml", @@ -765,8 +850,13 @@ jQuery.noConflict() // le main jQuery(document).ready( function() - { - + { + /* FIXME : ce code pose problème sur konqueror, voir : http://www.kde-forum.org/thread.php?threadid=17993 + var p = new DOMParser(); + var doc = p.parseFromString("\n", "text/xml") + var s = new XMLSerializer() + alert(s.serializeToString(doc)) */ + var util = new Util() var client = new Client(util) var pages = new Pages()