REPORT de la branche 1.0 (tag 1.0.1)
[euphorik.git] / js / pageMinichat.js
index 03024bc..da53627 100755 (executable)
@@ -1,19 +1,19 @@
-// coding: utf-8\r
-// Copyright 2008 Grégory Burri\r
-//\r
-// This file is part of Euphorik.\r
-//\r
-// Euphorik is free software: you can redistribute it and/or modify\r
-// it under the terms of the GNU General Public License as published by\r
-// the Free Software Foundation, either version 3 of the License, or\r
-// (at your option) any later version.\r
-//\r
-// Euphorik is distributed in the hope that it will be useful,\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// GNU General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU General Public License\r
+// coding: utf-8
+// Copyright 2008 Grégory Burri
+//
+// This file is part of Euphorik.
+//
+// Euphorik is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Euphorik is distributed in the hope that it will be useful,
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
 // along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.
  
 function PageMinichat(client, formateur, util)
@@ -24,6 +24,10 @@ function PageMinichat(client, formateur, util)
    this.formateur = formateur
    this.util = util
    
+   // permet d'éviter d'envoyer plusieurs messages simultanément en pressant
+   // rapidement sur "enter" par exemple
+   this.envoieMessageEnCours = false
+   
    this.regexMessageTagMatch = /\{.*?\}>/g
    this.regexMessageTagReplace =  /^(.*?\{.*?\}>)*/
 }
@@ -54,7 +58,7 @@ PageMinichat.prototype.charger = function()
    this.messages = new Messages(this.client, this.formateur, this.util)
    
    this.messages.rafraichirMessages(true)
-   \r
+   
    this.util.setCaretToEnd($("form input.message")[0])
 
    // les outils de bannissement (uniquement pour les ekMaster)
@@ -173,9 +177,9 @@ PageMinichat.prototype.getJSONMessage = function(pseudo, message, repondA)
 }
 
 PageMinichat.prototype.envoyerMessage = function(pseudo, message)
-{
+{   
    var thisPageMinichat = this
-\r
+
    // (un pseudo vide est autorisé)
    pseudo = this.formateur.filtrerInputPseudo(pseudo)
 
@@ -187,53 +191,75 @@ PageMinichat.prototype.envoyerMessage = function(pseudo, message)
       for(var i = 0; i < tags.length; i++)
          repondA.push(parseInt(/\{(.*?)\}>/.exec(tags[i])[1], 36))
       message = message.replace(this.regexMessageTagReplace, "")
-   }\r
-   \r
-   message = message.trim()\r
-   if (message == "")\r
+   }
+   
+   message = message.trim()
+   if (message == "")
    {
-      this.util.messageDialogue("Le message est vide")\r
-      return\r
-   }\r
-\r
-   if (!this.client.authentifie())\r
-      if (!this.client.enregistrement())\r
-      {\r
-         this.util.messageDialogue("login impossible")\r
-         return\r
+      this.util.messageDialogue("Le message est vide")
+      return
+   }
+
+   if (!this.client.authentifie())
+      if (!this.client.enregistrement())
+      {
+         this.util.messageDialogue("login impossible")
+         return
       }
       
-   this.client.pseudo = pseudo\r
-   
-   ;; dumpObj(this.getJSONMessage(pseudo, message, repondA))\r
-   jQuery.ajax(\r
-      {\r
-         url : "request", \r
-         type: "POST",\r
-         data : this.util.jsonVersAction(this.getJSONMessage(pseudo, message, repondA)),\r
+   this.client.pseudo = pseudo
+   
+   // évite le double post
+   if (this.envoieMessageEnCours)
+   {
+      this.util.messageDialogue("Message en cours d'envoie...")
+      return
+   }
+   this.envoieMessageEnCours = true
+   
+   ;; dumpObj(this.getJSONMessage(pseudo, message, repondA))
+   jQuery.ajax(
+      {
+         url : "request", 
+         type: "POST",
+         data : this.util.jsonVersAction(this.getJSONMessage(pseudo, message, repondA)),
          dataType : "json",
          beforeSend : function(xmlHttpRequest)
          {
+            // TODO : ça marche ça ??
             xmlHttpRequest.setRequestHeader("X-Requested-With", "")
-         },\r
-         success : function(data, textStatus)\r
+         },
+         success : function(data, textStatus)
          {
             ;; dumpObj(data)
          
             if(data["reply"] == "ok")
-            {  \r
+            {  
                $("form input.message").val("")
                         
                // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ?
                for (var i = 0; i < repondA.length; i++)
+               {
+                  for (var j = 0; j < thisPageMinichat.messages.conversations.length; j++)
+                  {
+                     var mess = thisPageMinichat.messages.conversations[j].messagesParId[repondA[i]]
+                     if (mess != undefined)
+                        mess.clientARepondu = true
+                  }
                   $("#conversations div#mess" + repondA[i].toString(36)).addClass("repondu")
+               }
             }
             else if (data["reply"] == "error")
             {
                thisPageMinichat.util.messageDialogue(data["error_message"])
-            }  \r
-         }\r
-      }\r
+            }
+            thisPageMinichat.envoieMessageEnCours = false
+         },
+         error : function()
+         {
+            thisPageMinichat.envoieMessageEnCours = false         
+         }
+      }
    )
 }
 
@@ -561,7 +587,7 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
    var DOM = $(XHTML)
    DOM.each(
       function()
-      {
+      {      
          $(".lienConv", this).click(
             function(event)
             {          
@@ -570,6 +596,49 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
                funClickOuvrirConv(parseInt(racine.substring(1, racine.length - 1), 36))
                return false
             }
+         )         
+         
+         thisConversation.util.infoBulle("Extraction de la conversation", $(".extraire", this))
+         
+         // l'id du message
+         var idMess36 = $(this).attr("id").substr(4)
+         var idMess = parseInt(idMess36, 36)
+         
+         $(this).click(
+            function(event)
+            {
+               if ($(event.target).is("a") || $(event.target).parents("#outilsBan").length > 0) return
+                              
+               // extraction d'une conversation
+               if ($(event.target).is(".extraire"))
+               {
+                  funClickOuvrirConv(idMess)
+                  return
+               }
+            
+               var valCourant = $("input.message").val()
+               if (valCourant == undefined) valCourant = ""
+               var tag = $(".pseudo span.ident", this).text()  + "{" + idMess36 + "}" + ">"
+               if (valCourant.indexOf(tag, 0) == -1)
+                  $("input.message").val(tag + " " + valCourant)
+               thisConversation.util.setCaretToEnd($("form input.message")[0])
+            }
+         )
+         
+         // mise en évidence de la conversation
+         $(".entete", this).hover(
+            function()
+            {
+               thisConversation.decolorerEntetes()
+               thisConversation.afficherConversation(idMess)
+            },
+            // quand on sort de l'entête du message la mise en évidence est enlevée
+            function()
+            {
+               thisConversation.enleverMiseEnEvidence()
+               thisConversation.decolorerEntetes()
+               thisConversation.colorerEntetes()
+            }
          )
          
          if (thisConversation.client.viewTimes)
@@ -619,50 +688,7 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
                {
                   $("#outilsBan", this).hide()
                }
-            )
-         
-         thisConversation.util.infoBulle("Extraction de la conversation", $(".extraire", this))
-         
-         // l'id du message
-         var idMess36 = $(this).attr("id").substr(4)
-         var idMess = parseInt(idMess36, 36)
-         
-         $(this).click(
-            function(event)
-            {
-               if ($(event.target).is("a") || $(event.target).parents("#outilsBan").length > 0) return
-                              
-               // extraction d'une conversation
-               if ($(event.target).is(".extraire"))
-               {
-                  funClickOuvrirConv(idMess)
-                  return
-               }
-            
-               var valCourant = $("input.message").val()
-               if (valCourant == undefined) valCourant = ""
-               var tag = $(".pseudo span.ident", this).text()  + "{" + idMess36 + "}" + ">"
-               if (valCourant.indexOf(tag, 0) == -1)
-                  $("input.message").val(tag + " " + valCourant)
-               thisConversation.util.setCaretToEnd($("form input.message")[0])
-            }
-         )\r
-         \r
-         // mise en évidence de la conversation
-         $(".entete", this).hover(\r
-            function()\r
-            {
-               thisConversation.decolorerEntetes()\r
-               thisConversation.afficherConversation(idMess)\r
-            },
-            // quand on sort de l'entête du message la mise en évidence est enlevée
-            function()
-            {
-               thisConversation.enleverMiseEnEvidence()
-               thisConversation.decolorerEntetes()
-               thisConversation.colorerEntetes()
-            }
-         )
+            )\r
       }
    )
    DOM.prependTo("#conversations #" + this.getId())
@@ -817,7 +843,7 @@ Messages.prototype.ajouterMessage = function(element, numConversation)
       element["nick"],
       element["login"],
       element["content"]
-   )   \r
+   )   
    
    message.appartientAuClient = element["owner"]
    message.clientARepondu = element["answered"]
@@ -904,10 +930,10 @@ Messages.prototype.supprimerConversation = function(num)
   * Ajuste la largeur des conversations en fonction de leur nombre. modifie l'attribut CSS 'width'.
   */
 Messages.prototype.ajusterLargeurConversations = function()
-{\r
-   var largeurPourcent = (100 / this.conversations.length)   \r
-   // le "- 0.01" evite que IE se chie dessus lamentablement et affiche les conversations les unes au dessus des autres\r
-   if($.browser["msie"])\r
+{
+   var largeurPourcent = (100 / this.conversations.length)   
+   // le "- 0.01" evite que IE se chie dessus lamentablement et affiche les conversations les unes au dessus des autres
+   if($.browser["msie"])
       largeurPourcent -= 0.05
    $("#conversations .conversation").css("width", largeurPourcent + "%")
 }