ADD fin de 'communication.js'
[euphorik.git] / js / pageMinichat / pageMinichat.js
index d289a99..e952281 100755 (executable)
  
 /*jslint laxbreak:true */
 
-euphorik.PageMinichat = function(client, formateur, util) {
+euphorik.PageMinichat = function(client, formateur, util, communication) {
    this.nom = "minichat";
    
    this.client = client;
    this.formateur = formateur;
    this.util = util;
+   this.communication = communication;
    this.commandes = new euphorik.Commandes(this.client);
    
    // permet d'éviter d'envoyer plusieurs messages simultanément en pressant
@@ -226,22 +227,6 @@ euphorik.PageMinichat.prototype.decharger = function() {
     this.fragment.delVal("conv")
 };
 
-
-euphorik.PageMinichat.prototype.getJSONMessage = function(pseudo, message) {
-   var repondA = [];
-   objectEach(this.conversations.messagesRepond, function(id) {
-      repondA.push(parseInt(id, 10));
-   });
-      
-   return {
-      "header" : { "action" : "put_message", "version" : euphorik.conf.versionProtocole },
-      "cookie" : this.client.cookie,
-      "nick" : pseudo,
-      "content" : message,
-      "answer_to" : repondA
-   };
-};
-
 euphorik.PageMinichat.prototype.envoyerMessage = function(pseudo, message) {   
    var thisPageMinichat = this;
 
@@ -275,38 +260,52 @@ euphorik.PageMinichat.prototype.envoyerMessage = function(pseudo, message) {
    }
    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") {
-            // TODO : revoir cette partie
-            // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ?
-            objectEach(thisPageMinichat.conversations.messagesRepond, function(messId) {
-               thisPageMinichat.conversations.conversations.each(function(i, conv) {
-                  var mess = conv.messagesParId[messId];
-                  if (mess) {
-                     mess.clientARepondu = true;
-                     $("#conversations #" + mess.getId(conv.getId())).addClass("repondu")
-                  }
-               });
+   this.communication.requete(
+      "put_message",
+      this.getJSONMessage(pseudo, message),
+      function() {
+         // TODO : revoir cette partie
+         // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ?
+         objectEach(thisPageMinichat.conversations.messagesRepond, function(messId) {
+            thisPageMinichat.conversations.conversations.each(function(i, conv) {
+               var mess = conv.messagesParId[messId];
+               if (mess) {
+                  mess.clientARepondu = true;
+                  $("#conversations #" + mess.getId(conv.getId())).addClass("repondu")
+               }
             });
-            
-            $("form#posterMessage input.message").val("");
-            thisPageMinichat.conversations.enleverMessagesRepond();
-         } else if (data.reply === "error") {
-            thisPageMinichat.util.messageDialogue(data.error_message);
-         }
+         });
+         $("form#posterMessage input.message").val("");
+         thisPageMinichat.conversations.enleverMessagesRepond(); 
          thisPageMinichat.envoieMessageEnCours = false;
       },
-      error : function() {
+      function(data) {
+         thisPageMinichat.util.messageDialogue(data.error_message);
          thisPageMinichat.envoieMessageEnCours = false;
+      },
+      true,
+      {
+         beforeSend : function(xmlHttpRequest) {
+            // TODO : ça marche ça ??
+            xmlHttpRequest.setRequestHeader("X-Requested-With", "");
+         },
+         error : function() {
+            thisPageMinichat.envoieMessageEnCours = false;
+         }
       }
+   );
+};
+
+euphorik.PageMinichat.prototype.getJSONMessage = function(pseudo, message) {
+   var repondA = [];
+   objectEach(this.conversations.messagesRepond, function(id) {
+      repondA.push(parseInt(id, 10));
    });
+      
+   return {
+      "cookie" : this.client.cookie,
+      "nick" : pseudo,
+      "content" : message,
+      "answer_to" : repondA
+   };
 };