MOD avancement dans la Grande Restructuration : découpe en sous-fichiers
[euphorik.git] / js / pageMinichat / pageMinichat.js
diff --git a/js/pageMinichat/pageMinichat.js b/js/pageMinichat/pageMinichat.js
new file mode 100755 (executable)
index 0000000..1213cab
--- /dev/null
@@ -0,0 +1,284 @@
+// 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)
+{
+   this.nom = "minichat"
+   
+   this.client = client
+   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 =  /^(.*?\{.*?\}>)*/
+}
+
+PageMinichat.prototype.contenu = function()
+{
+   // le fait que tout soit collé est fait exprès, permet d'éviter d'avoir des espaces supplémentaires entre les spans'
+   var formulaireXHTML = '<form method="post" action="" id ="posterMessage">\
+<p>\
+<input class="captcha" name="captcha" type="text" size="8" maxlength="8"></input>\
+<input class="pseudo" name="pseudo" type="text" maxlength="50" value="' + encodeURI(euphorik.conf.nickDefaut) + '"></input>\
+<span id="repondA"><span class="nb">0</span><span class="messages"></span></span>\
+<input class="message" name="message" type="text" maxlength="500" value=""></input>\
+<button class="smiles"></button>\
+<button class="return"></button>\
+</p>\
+</form>'
+   var trollXHTML = '<div id="trollCourant">Troll de la semaine : <span class="troll"></span></div>'
+   //var titreXHTML = '<tr id="titres"></tr>'
+   //var messagesXHTML = '<tr id="messages"></tr>'
+   var conversationXHTML = '<table id="conversations"><tr></tr></table>'
+    
+   if (this.client.chatOrder == "reverse")
+      return trollXHTML + formulaireXHTML + conversationXHTML
+   else
+      return trollXHTML + conversationXHTML + formulaireXHTML
+}
+
+PageMinichat.prototype.classes = function()
+{
+   return this.client.chatOrder == "reverse" ? "orderReverse" : "orderChrono"
+}
+
+PageMinichat.prototype.charger = function()
+{   
+   thisPage = this
+
+   $("form input.pseudo").val(this.client.pseudo)
+   
+   // cet appel ne doit pas être fait avant l'appel à 'charger'
+   this.conversations = new Conversations(this.client, this.formateur, this.util)
+   
+   this.conversations.rafraichirMessages(true)
+   
+   this.util.setCaretToEnd($("form input.message")[0])
+
+   // les outils de bannissement (uniquement pour les ekMaster)
+   if (this.client.ekMaster)
+   {    
+      this.util.outilsBan = $(
+         '<span id="outilsBan">' +
+         '<span class="spacer"></span>' +
+         '<form action=""><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" />' +
+         '</span>'
+      )
+      
+      this.util.infoBulle("Slap", $("#slap", this.util.outilsBan))
+      this.util.infoBulle("Kick (" + euphorik.conf.tempsKick + "min)", $("#kick", this.util.outilsBan))
+      this.util.infoBulle("Ban (" + euphorik.conf.tempsBan / 24 / 60 + " jours)", $("#ban", this.util.outilsBan))
+      this.util.infoBulle("La raison", $("input", this.util.outilsBan))
+   }
+   
+   this.util.infoBulle("Ouvrir la conversation liée au troll de la semaine", $("#trollCourant .troll")) 
+   
+   this.util.infoBulle("Cliquer sur les messages pour les enlevers de la liste",
+      $("form#posterMessage #repondA").hover(
+         function() { thisPage.util.afficherBoite($(".messages", this), $(this), positionTypeX.centre, positionTypeY.bas) },
+         function() { $(".messages", this).hide() }
+      ).click(
+         function(e)
+         {
+            if ($(e.target).is(".nb"))
+               thisPage.conversations.enleverMessagesRepond()             
+         }     
+      ),
+      euphorik.Util.positionBulleType.droite
+   )
+
+   // <smiles>
+   $("body").append('<div id="smiles"></div>')
+   // affichage des smiles
+   $("#smiles").append(this.formateur.getSmilesHTML()).children().each(
+      function(i)
+      {
+         var opacityBase = $(this).css("opacity")
+         $(this).click(
+            function()
+            {
+               thisPage.util.replaceSelection($("form#posterMessage input.message")[0], thisPage.formateur.smiles[$(this).attr("class")][0].source.replace(/\\/g, ""))
+            }
+         ).hover(
+            function() { $(this).animate({opacity: 1}, 200) },
+            function() { $(this).animate({opacity: opacityBase}, 200) }
+         )
+      }
+   )
+   $("form button.smiles").hover(
+      // affichage de la boite présentant les smiles
+      function(e){ thisPage.util.afficherBoite($("#smiles"), $(e.target), positionTypeX.centre, positionTypeY.basRecouvrement) },
+      function(){}
+   )
+   $("#smiles").hover(
+      function(){},
+      function()
+      {
+         $("#smiles").hide()
+      }
+   )
+   // </smiles>
+   
+   // événements
+   var nouveauMessage = 
+      function()
+      {  
+         // captcha anti bot
+         if ($("form input.captcha").val() != "") return
+      
+         thisPage.envoyerMessage(
+            $("form input.pseudo").val(), 
+            $("form input.message").val()
+         )
+            
+         $("form input.message").focus()
+      }
+      
+   $("form").keypress(
+      function(e)
+      {
+         if (e.which == 13) // return
+            nouveauMessage()
+      }
+   )
+   
+   $("form button.return").click(nouveauMessage)
+   
+   // interdiction de submiter le formulaire
+   $("form").submit(function(){ return false})
+   
+   $("input.pseudo").click(
+      function()
+      {
+         var input = $("input.pseudo")[0]
+         if (input.value == euphorik.conf.pseudoDefaut)
+            input.value = ""
+      }
+   )
+}
+
+PageMinichat.prototype.decharger = function()
+{
+   this.conversations.pageEvent.stopAttenteCourante()
+   
+   $("body #smiles").remove()
+}
+
+PageMinichat.prototype.getJSONMessage = function(pseudo, message)
+{
+   var repondA = []
+   for (var id in this.conversations.messagesRepond)
+      repondA.push(parseInt(id)) // FIXME : une propriété ne peut pas être de type int ?
+      
+   return {
+      "header" : { "action" : "put_message", "version" : euphorik.conf.versionProtocole },
+      "cookie" : this.client.cookie,
+      "nick" : pseudo,
+      "content" : message,
+      "answer_to" : repondA
+   }
+}
+
+PageMinichat.prototype.envoyerMessage = function(pseudo, message)
+{   
+   var thisPageMinichat = this
+
+   // (un pseudo vide est autorisé)
+   pseudo = this.formateur.filtrerInputPseudo(pseudo)
+   
+   if (pseudo == euphorik.conf.nickDefaut)
+   {
+      this.util.messageDialogue("Le pseudo ne peut pas être " + euphorik.conf.nickDefaut)
+      return
+   }
+   
+   message = message.trim()
+   if (message == "")
+   {
+      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
+   
+   // évite le double post
+   if (this.envoieMessageEnCours)
+   {
+      this.util.messageDialogue("Message en cours d'envoie...")
+      return
+   }
+   this.envoieMessageEnCours = true
+   
+   jQuery.ajax(
+      {
+         url : "request", 
+         type: "POST",
+         data : this.util.jsonVersAction(this.getJSONMessage(pseudo, message)),
+         dataType : "json",
+         beforeSend : function(xmlHttpRequest)
+         {
+            // TODO : ça marche ça ??
+            xmlHttpRequest.setRequestHeader("X-Requested-With", "")
+         },
+         success : function(data, textStatus)
+         {         
+            if(data["reply"] == "ok")
+            {           
+               // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ?
+               for (var messId in thisPageMinichat.conversations.messagesRepond)
+               {
+                  for (var j = 0; j < thisPageMinichat.conversations.conversations.length; j++)
+                  {
+                     var mess = thisPageMinichat.conversations.conversations[j].messagesParId[messId]
+                     if (mess != undefined)
+                        mess.clientARepondu = true
+                  }
+                  // TODO : ca sert à qque chose ?
+                  //$("#conversations div#" + thisPageMinichat.conversations.messagesRepond[messId].getId()).addClass("repondu")
+               }
+               
+               $("form input.message").val("")
+               thisPageMinichat.conversations.enleverMessagesRepond()
+            }
+            else if (data["reply"] == "error")
+            {
+               thisPageMinichat.util.messageDialogue(data["error_message"])
+            }
+            thisPageMinichat.envoieMessageEnCours = false
+         },
+         error : function()
+         {
+            thisPageMinichat.envoieMessageEnCours = false         
+         }
+      }
+   )
+}\r