ADD support des conversations (pas fini)
[euphorik.git] / js / euphorik.js
index 433c0bc..fafa854 100755 (executable)
@@ -1,6 +1,8 @@
 // coding: utf-8\r
 \r
-/**
+/**\r
+  * Contient la base javascript pour le site euphorik.ch.\r
+  * Chaque page possède son propre fichier js nommé "page<nom de la page>.js".
   * Auteur : GBurri
   * Date : 6.11.2007
   */
@@ -38,15 +40,17 @@ var conf = {
 \r
 String.prototype.trim = function()\r
 {\r
-       return this.replace(/^\s+|\s+$/g,"");\r
+       return this.replace(/^\s+|\s+$/g, "");\r
 }\r
+\r
 String.prototype.ltrim = function()\r
 {\r
-       return this.replace(/^\s+/,"");\r
+       return this.replace(/^\s+/, "");\r
 }\r
+\r
 String.prototype.rtrim = function()\r
 {\r
-       return this.replace(/\s+$/,"");\r
+       return this.replace(/\s+$/, "");\r
 }\r
 \r
 String.prototype.dump = function()\r
@@ -60,12 +64,16 @@ String.prototype.dump = function()
 }\r
 \r
 ///////////////////////////////////////////////////////////////////////////////////////////////////\r
-
-function Util(serializer)
+\r
+/**\r
+  * Cette classe regroupe des fonctions utilitaires (helpers).\r
+  */
+function Util()
 {\r
    if(typeof XMLSerializer != "undefined")
       this.serializer = new XMLSerializer()
-      
+      \r
+   // fermeture des dialogues d
    jQuery("#info .fermer").click(function(){
       jQuery("#info").slideUp(50) 
    })
@@ -73,7 +81,10 @@ function Util(serializer)
 
 /**
   * Affiche une boite de dialogue avec un message à l'intérieur.
-  * 
+  * @param message le message (string)\r
+  * @param type voir 'messageType'. par défaut messageType.informatif\r
+  * @param les boutons sous la forme d'un objet ou les clefs sont les labels des boutons\r
+  *        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}
 \r
-/* obsolète\r
-Util.prototype.log = function(str)\r
-{\r
-}*/\r
-\r
+/**\r
+  * Transforme un document XML en string.\r
+  */\r
 Util.prototype.serializeXML = function(documentXML)\r
 {\r
    if (this.serializer)\r
@@ -119,7 +128,11 @@ Util.prototype.creerDocumentXMLAction = function()
 {\r
    if (document.implementation && document.implementation.createDocument)\r
    {\r
-      return document.implementation.createDocument("", "action", null)\r
+      // var doc = document.implementation.createDocument("", "action", null)
+      var parser = new DOMParser();
+      var doc =  parser.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
+      //alert(this.serializeXML(doc))
+      return doc\r
    }\r
    else if (window.ActiveXObject)\r
    {\r
@@ -134,32 +147,9 @@ Util.prototype.creerDocumentXMLAction = function()
 
 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)
-{\r
-   if (!s) return ""\r
-
-   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;
-}\r
 \r
 Util.prototype.md5 = function(chaine)\r
 {\r
@@ -421,7 +411,12 @@ Client.prototype.resetDonneesPersonnelles = function()
    this.login = ""\r
    this.password = ""\r
    this.email = ""\r
-   this.css = jQuery("link#cssPrincipale").attr("href")\r
+   this.css = jQuery("link#cssPrincipale").attr("href")
+   
+   // les conversations, une conversation est un objet possédant les attributs suivants :
+   // - racine (entier)
+   // - page (entier)
+   this.conversations = new Array()\r
 }
 
 Client.prototype.setCss = function(css)
@@ -432,14 +427,20 @@ 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()   
+}
+
+/**
+  * 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
+  */
+Client.prototype.ajouterConversation = function(racine)
+{
+   this.conversations.push({racine : racine, page : 1})
+   this.flush()
 }\r
 
 Client.prototype.getXMLlogin = function(login, password)
@@ -541,6 +542,26 @@ Client.prototype.getXMLProfile = function()
    nodeCSS.appendChild(XMLDocument.createTextNode(this.css))
    XMLDocument.documentElement.appendChild(nodeCSS)
    
+   // mémorise les conversations affichées
+   if (this.conversations.length > 0)
+   {
+      var nodeConversations = XMLDocument.createElement("conversations")
+      XMLDocument.documentElement.appendChild(nodeConversations)
+      for (var i = 0; i < this.conversations.length; i++)
+      {
+         var nodeConv = XMLDocument.createElement("conversation")
+         nodeConversations.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    
 }
 
@@ -713,7 +734,7 @@ Client.prototype.chargerDonnees = function(data)
 Client.prototype.flush = function()
 {
    thisClient = this
-   //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action)         
+   //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action)      
    jQuery.ajax(
       {
          async: true,
@@ -765,8 +786,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("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
+      var s = new XMLSerializer()
+      alert(s.serializeToString(doc)) */
+   
       var util = new Util()
       var client = new Client(util)
       var pages = new Pages()