MOD gestion des pages (pas fini)
[euphorik.git] / js / euphorik.js
index 9dd3357..8487f77 100755 (executable)
@@ -12,7 +12,7 @@
   * Normalement 'const' à la place de 'var' mais non supporté par IE7.
   */\r
 var conf = {\r
-   nbMessageAffiche : 80, // (par page)
+   nbMessageAffiche : 10, // (par page)
    pseudoDefaut : "<nick>",\r
    tempsAffichageMessageDialogue : 4000, // en ms\r
    smiles : {   \r
@@ -72,8 +72,7 @@ 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) 
    })
@@ -128,7 +127,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
@@ -143,32 +146,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
@@ -430,7 +410,14 @@ 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")
+   
+   this.pagePrincipale = 1
+   
+   // 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)
@@ -441,14 +428,57 @@ 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
+   else
+      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)
+}
+
+/**
+  * 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)
 }\r
 
 Client.prototype.getXMLlogin = function(login, password)
@@ -550,6 +580,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))
+   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    
 }
 
@@ -668,6 +717,7 @@ Client.prototype.enregistrement = function(login, password)
 
 Client.prototype.connexion = function(action)
 {
+   //action.action.dump()
    thisClient = this
    jQuery.ajax(
       {
@@ -679,6 +729,7 @@ Client.prototype.connexion = function(action)
          success:
             function(data)
             {
+               //thisClient.util.serializer.serializeToString(data).dump()
                thisClient.chargerDonnees(data)
             }
       }
@@ -695,6 +746,8 @@ Client.prototype.deconnexion = function()
 
 Client.prototype.chargerDonnees = function(data)
 {
+   var thisClient = this
+
    this.setStatut(jQuery("statut", data.documentElement).text())       
    
    if (this.identifie())
@@ -706,26 +759,43 @@ Client.prototype.chargerDonnees = function(data)
       this.pseudo = jQuery("pseudo", data.documentElement).text()\r
       this.email = jQuery("email", data.documentElement).text()\r
       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 : 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)         
+   //thisClient.util.log(this.util.xmlVersAction(this.getXMLProfile()).action)      
    jQuery.ajax(
       {
-         async: true,
+         async: async,
          type: "POST",
          url: "request",
          dataType: "xml",
@@ -775,6 +845,12 @@ jQuery.noConflict()
 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()