FIX légère optimisation du rafraichissement des messages
[euphorik.git] / js / pageMinichat.js
index 6b7b340..217069d 100755 (executable)
@@ -15,11 +15,12 @@ function PageMinichat(client, formateur, util)
 PageMinichat.prototype.contenu = function()
 {
    return '\
+<div id="trollCourant">Troll de la semaine : <span class="troll"></span></div>\
 <form method="post" action="">\
    <p>\
       <input class="captcha" name="captcha" type="text" size="8" maxlength="8"></input>\
-      <input class="pseudo" name="pseudo" type="text" size="12" maxlength="50" value="&lt;nick&gt;"></input>\
-      <input class="message" name="message" type="text" size="80" maxlength="500" value=""></input>\
+      <input class="pseudo" name="pseudo" type="text" maxlength="50" value="&lt;nick&gt;"></input>\
+      <input class="message" name="message" type="text" maxlength="500" value=""></input>\
       <button class="smiles"></button>\
       <button class="return"></button>\
    </p>\
@@ -44,7 +45,12 @@ PageMinichat.prototype.charger = function()
    if (this.client.ekMaster)
    {    
       $("body").append(
-         "<div id=\"outilsBan\"><img id=\"ban\" alt=\"Ban de 3 jours\" src=\"img/ban.gif\" /><img id=\"kick\" alt=\"Ban de 15min\" src=\"img/kick.gif\" /><img id=\"slap\" alt=\"Avertissement\" src=\"img/slap.gif\" /></div>"
+         '<div id="outilsBan">' +
+         '<form><p><input id="raison" name="raison" type="text" size="10" maxlength="200"></input></p></form>' +
+         '<img id="ban" src="img/ban.gif" alt="Ban de 3 jours" />' +
+         '<img id="kick" src="img/kick.gif" alt="Ban de 15min" />' +
+         '<img id="slap" src="img/slap.gif" alt="Avertissement" />' +
+         '</div>'
       )
       $("#outilsBan").hover(
          function(){},
@@ -91,8 +97,12 @@ PageMinichat.prototype.charger = function()
    $("form button.smiles").hover(
       function(e)
       {
-         var offset = $(e.target).offset()
-         $("#smiles").css("top", offset.top).css("left", offset.left).show()
+         var position = $(e.target).offset()
+         // le décalage pour ne pas dépasser à droite (10 correspond à la marge pour éviter de coller le bord)
+         var decalage = $("body").width() - $("#smiles").width() - position.left - 10
+         decalage = decalage > 0 ? 0 : decalage
+         
+         $("#smiles").css("top", position.top).css("left", position.left + decalage).show()
       },
       function(e){}
    )
@@ -117,7 +127,7 @@ PageMinichat.prototype.charger = function()
             $("form input.message").val()
          )
             
-         $("form input.message")[0].focus()
+         $("form input.message").focus()
       }
       
    $("form").keypress(
@@ -145,8 +155,7 @@ PageMinichat.prototype.charger = function()
 
 PageMinichat.prototype.decharger = function()
 {
-   //alert(this.attenteCourante)
-   this.messages.stopAttenteCourante()
+   this.messages.pageEvent.stopAttenteCourante()
    
    $("body #smiles").remove()
    $("body #outilsBan").remove()
@@ -483,10 +492,10 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
       if (this.messages[i].id > this.idDernierMessageAffiche)
       {      
          var message = this.messages[i]
-         
+        
          // construit l'identifiant de la personne
          var identifiant = 
-            this.client.nickFormat == "nick" ? this.formateur.traitementComplet(message.pseudo) : 
+            this.client.nickFormat == "nick" || message.login == "" ? this.formateur.traitementComplet(message.pseudo) : 
             (this.client.nickFormat == "login" ? this.formateur.traitementComplet(message.login) : 
             this.formateur.traitementComplet(message.pseudo) + "<span class=\"login\">(" + this.formateur.traitementComplet(message.login) +")</span>" )
          
@@ -535,7 +544,7 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
             }
          )
          
-         $("a[@rel*=lightbox]").lightBox()
+         $("a[@rel*=lightbox]", this).lightBox()
          
          // les outils de bannissement (uniquement pour les ekMaster)
          if (thisConversation.client.ekMaster)
@@ -543,27 +552,30 @@ Conversation.prototype.flush = function(funClickOuvrirConv)
                function(e)
                {     
                   var userId =  parseInt($(this).attr("id").substr(4))
-                  var element = $(e.target)
+                  var element = $(this)
                   var h = element.height()
                   var offset = element.offset()
-                  var outils = $("#outilsBan").css("top", offset.top - 2).css("left", offset.left - 2).height(h < 16 ? 16 : h).width(element.width() + 16 * 3 + 4).show()
+                  var outils = $("#outilsBan").css("top", offset.top - 2).css("left", offset.left - 2).height(h < 16 ? 16 : h).width(element.width() + 16 * 3 + 4 + 64).show()
                   $("img", outils).unbind()
                   $("#slap", outils).click(
                      function(e)
                      {
-                        thisConversation.client.slap(userId)
+                        thisConversation.client.slap(userId, $("#outilsBan input").val())
+                        $("#outilsBan input").val("")
                      }
                   )
                   $("#kick", outils).click(
                      function(e)
                      {
-                        thisConversation.client.kick(userId)
+                        thisConversation.client.kick(userId, $("#outilsBan input").val())
+                        $("#outilsBan input").val("")
                      }
                   )
                   $("#ban", outils).click(
                      function(e)
                      {
-                        thisConversation.client.ban(userId)
+                        thisConversation.client.ban(userId, $("#outilsBan input").val())
+                        $("#outilsBan input").val("")
                      }
                   )
                },
@@ -664,8 +676,9 @@ function Messages(client, formateur, util)
    this.conversations = new Array() // les conversations, la première représente la conversation principale
    this.nouvelleConversation(0)
    
-   // l'objet JSONHttpRequest représentant la connexion d'attente
-   this.attenteCourante = null
+   this.trollIdCourant = 0
+   
+   this.pageEvent = new PageEvent("chat", this.util)
 }
 
 /**
@@ -674,19 +687,15 @@ function Messages(client, formateur, util)
 Messages.prototype.getJSONrafraichirMessages = function()
 {
    var mess =  {
-      "action" : "wait_event",
-      "page" : "chat",
       "message_count" : conf.nbMessageAffiche,
       "main_page" : this.client.pagePrincipale,
-      "conversations" : this.getJSONConversations()
+      "conversations" : this.getJSONConversations(),
+      "troll_id" : this.trollIdCourant
    }
    
-   if (this.client.cookie != null) mess["cookie"] = this.client.cookie;
+   if (this.client.cookie != null) mess["cookie"] = this.client.cookie
    mess["last_message_id"] = this.conversations[0].idDernierMessageAffiche
    
-   // obsolète
-   //if (this.idDernierMessage != null) mess["last_message_id"] = this.idDernierMessage
-   
    return mess
 }
 
@@ -708,8 +717,8 @@ Messages.prototype.getJSONConversations = function()
   */
 Messages.prototype.ajouterMessages = function(elements, numConversation)
 {
-   if (elements["messages"].length == 0 && typeof(this.conversations[numConversation]) == "undefined")
-      return false
+   if (elements["messages"].length == 0)
+      return this.conversations[numConversation] != undefined
 
    for (var i = 0; i < elements["messages"].length; i++)
       this.ajouterMessage(elements["messages"][i], numConversation)
@@ -869,22 +878,10 @@ Messages.prototype.ouvrirConversation = function(racine)
 
 Messages.prototype.viderMessages = function()
 {
-   // Obsolète
-   //this.idDernierMessage = null
-   
    for (var i = 0; i < this.conversations.length; i++)
       this.conversations[i].viderMessages()
 }
 
-/**
-  * Arrete l'attente courante s'il y en a une.
-  */
-Messages.prototype.stopAttenteCourante = function()
-{
-   if (this.attenteCourante != null)
-      this.attenteCourante.abort()   
-}
-
 /**
   * Met à jour les messages de manière continue.
   * (AJAX-Comet-style proof)
@@ -892,51 +889,46 @@ Messages.prototype.stopAttenteCourante = function()
   */
 Messages.prototype.rafraichirMessages = function(vider)
 {
-   var thisMessages = this // caisupair javacrypte
+   var thisMessages = this
    
    if (vider == undefined)
       vider = false
-      
-   this.stopAttenteCourante()
    
    if (vider)
       for (var i = 0; i < this.conversations.length; i++)
-         this.conversations[i].idDernierMessageAffiche = 0\r
-      
-   dumpObj(this.getJSONrafraichirMessages())
-   this.attenteCourante = jQuery.ajax({
-      type: "POST",
-      url: "request",
-      dataType: "json",
-      data: this.util.jsonVersAction(this.getJSONrafraichirMessages()),
-      success:
-         function(data)
-         {            \r
-            ;;; dumpObj(data)\r
+         this.conversations[i].idDernierMessageAffiche = 0
          
-            if (vider)
-               thisMessages.viderMessages()
-            
-            // ajoute les messages reçus à leur conversation respective
-            for (var numConv = 0; numConv < data["conversations"].length; numConv++)
-            {
-               // ya pas de nouveaux message -> on passe à la prochaine conversation FIXME : marche pas
-               //if (data["conversations"][numConv]["messages"].length == 0) continue
+   this.pageEvent.waitEvent(
+      function() { return thisMessages.getJSONrafraichirMessages() },
+      function(data)
+      {
+         if (vider)
+         {
+            thisMessages.viderMessages()
+            vider = false
+         }
                
-               if (! thisMessages.ajouterMessages(data["conversations"][numConv], numConv))
+         // en fonction du message
+         switch(data["reply"])
+         {
+            case "new_troll" :
+               thisMessages.trollIdCourant = data["troll_id"]
+               $("#trollCourant .troll").html(thisMessages.formateur.traitementComplet(data["content"]))
+               $("#trollCourant .troll a[@rel*=lightbox]").lightBox()
+               break
+               
+            case "new_messages" :                        
+               // ajoute les messages reçus à leur conversation respective
+               for (var numConv = 0; numConv < data["conversations"].length; numConv++)
                {
-                  thisMessages.util.messageDialogue("La conversation {" + thisMessages.client.conversations[numConv -1].root.toString(36) + "} n'existe pas")
-                  thisMessages.client.supprimerConversation(numConv - 1) 
+                  if (! thisMessages.ajouterMessages(data["conversations"][numConv], numConv))
+                  {
+                     thisMessages.util.messageDialogue("La conversation {" + thisMessages.client.conversations[numConv -1].root.toString(36) + "} n'existe pas")
+                     thisMessages.client.supprimerConversation(numConv - 1) 
+                  }
                }
-            }
-            
-            // rappel de la fonction dans 100 ms
-            setTimeout(function(){ thisMessages.rafraichirMessages() }, 100);
-         },
-      error:
-         function(XMLHttpRequest, textStatus, errorThrown)
-         {
-            setTimeout(function(){ thisMessages.rafraichirMessages() }, 1000);
+               break
          }
-   })
+      }
+   )
 }