MOD fin du système de page et cleanage du code
[euphorik.git] / js / euphorik.js
index 8487f77..4b76b93 100755 (executable)
@@ -53,11 +53,12 @@ String.prototype.rtrim = function()
        return this.replace(/\s+$/, "");\r
 }\r
 \r
-String.prototype.dump = function()\r
-{\r
+String.prototype.dump = function(titre)\r
+{
+   titre = titre == undefined ? "" : titre\r
    if (typeof dump != "undefined")\r
    {\r
-      dump("\n--- EUPHORIK.CH ---\n")\r
+      dump("\n--- EUPHORIK.CH : " + titre + " ---\n")\r
       dump(this)\r
       dump("\n------\n")\r
    }\r
@@ -123,25 +124,29 @@ Util.prototype.serializeXML = function(documentXML)
       return documentXML.xml\r
 }\r
 
+var documentXMLBase = undefined // singleton 
 Util.prototype.creerDocumentXMLAction = function()
-{\r
-   if (document.implementation && document.implementation.createDocument)\r
-   {\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
+{
+   // FIXME : essayer de garder le doc de base en cache (singleton) et d'en retourner une copie
+   if (true)//documentXMLBase == undefined)
    {\r
-      var doc = new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM")\r
-      doc.appendChild(doc.createElement("action"));\r
-      //doc.loadXML("<action></action>")\r
-      //alert(doc.documentElement)\r
-      //doc.createElement("action")\r
-      return doc\r
-   }   
+      if (document.implementation && document.implementation.createDocument)\r
+      {\r
+         // var doc = document.implementation.createDocument("", "action", null)
+         var parser = new DOMParser();
+         documentXMLBase =  parser.parseFromString("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<action/>", "text/xml")
+         //alert(this.serializeXML(doc))\r
+      }\r
+      else if (window.ActiveXObject)\r
+      {\r
+         documentXMLBase = new ActiveXObject("MSXML2.DOMDocument") //("Microsoft.XMLDOM")\r
+         documentXMLBase.appendChild(doc.createElement("action"));\r
+         //doc.loadXML("<action></action>")\r
+         //alert(doc.documentElement)\r
+         //doc.createElement("action")\r
+      }
+   }
+   return documentXMLBase
 }
 
 Util.prototype.xmlVersAction = function(xml)
@@ -298,7 +303,7 @@ Formateur.prototype.traitementComplet = function(M, pseudo)
 }
 \r
 /**\r
-  * FIXME : Cette méthode est attrocement lourde !!\r
+  * FIXME : Cette méthode est attrocement lourde ! A optimiser.\r
   */
 Formateur.prototype.traiterSmiles = function(M)
 {  
@@ -434,21 +439,43 @@ Client.prototype.setCss = function(css)
 }
 
 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
+   else 
       this.conversations[numConv].page += 1
    this.flush(false)
 }
 
-Client.prototype.pagePrecedente = function(numConv)
+/**
+  * 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)
-      this.pagePrincipale -= 1
-   else 
-      this.conversations[numConv].page -= 1
+   {
+      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
 }
 
 /**
@@ -551,6 +578,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()
@@ -581,7 +612,7 @@ Client.prototype.getXMLProfile = function()
    XMLDocument.documentElement.appendChild(nodeCSS)
    
    var nodePagePrincipale = XMLDocument.createElement("pagePrincipale")
-   nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale))
+   nodePagePrincipale.appendChild(XMLDocument.createTextNode(this.pagePrincipale < 1 ? 1 : this.pagePrincipale))
    XMLDocument.documentElement.appendChild(nodePagePrincipale)
    
    // mémorise les conversations affichées
@@ -729,7 +760,7 @@ Client.prototype.connexion = function(action)
          success:
             function(data)
             {
-               //thisClient.util.serializer.serializeToString(data).dump()
+               //thisClient.util.serializer.serializeToString(data).dump("Charger client")
                thisClient.chargerDonnees(data)
             }
       }
@@ -762,7 +793,7 @@ Client.prototype.chargerDonnees = function(data)
       
       // la page de la conversation principale
       var tmp = jQuery("pagePrincipale", data.documentElement)
-      this.pagePrincipale = tmp.length < 1 ? 1 : tmp.text()
+      this.pagePrincipale = tmp.length < 1 ? 1 : parseInt(tmp.text())
       
       // met à jour la css
       if (this.css != "")
@@ -792,7 +823,7 @@ Client.prototype.flush = function(async)
       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: async,