ADD un objet permettant l'attente des événements. Utilisé pour la page 'chat' et...
[euphorik.git] / js / euphorik.js
index 2b885ab..8aaf875 100755 (executable)
@@ -350,8 +350,8 @@ Formateur.prototype.getShort = function(url)
 {\r
       var estUneImage = false
       var versionShort = null
-      var rechercheImg = this.regexImg.exec(url)\r
-      //alert(url)
+      var rechercheImg = this.regexImg.exec(url)
+      
       if (rechercheImg != null)\r
       {
          versionShort = rechercheImg[1].toLowerCase()\r
@@ -437,7 +437,7 @@ Client.prototype.resetDonneesPersonnelles = function()
 
 Client.prototype.setCss = function(css)
 {
-   if (this.css == css)
+   if (this.css == css || css == "")
       return
 
    this.css = css
@@ -599,7 +599,6 @@ Client.prototype.authentifie = function()
 
 Client.prototype.setStatut = function(statut)
 {  
-   //alert(statut)
    // conversation en "enum" si en "string"\r
    if (typeof(statut) == "string")\r
    {
@@ -681,8 +680,10 @@ Client.prototype.deconnexion = function()
 
 Client.prototype.chargerDonnees = function(data)
 {
-   var thisClient = this
-
+   // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
+   // de l'état ekMaster
+   this.ekMaster = data["ek_master"] != undefined ? data["ek_master"] : false
+   
    this.setStatut(data["status"]) 
    
    if (this.authentifie())
@@ -693,22 +694,14 @@ Client.prototype.chargerDonnees = function(data)
       this.login = data["login"]
       this.pseudo = data["nick"]\r
       this.email = data["email"]\r
-      this.css = data["css"]
+      this.setCss(data["css"])
       this.nickFormat = data["nick_format"]
       
       // la page de la conversation principale
       this.pagePrincipale = data["main_page"] == undefined ? 1 : data["main_page"]
       
-      // met à jour la css
-      if (this.css != "")
-      {
-         $("link#cssPrincipale").attr("href", this.css)
-         this.majMenu()
-      }
       // les conversations
-      thisClient.conversations = data["conversations"]
-      
-      thisClient.ekMaster = data["ek_master"]
+      this.conversations = data["conversations"]
    }
    this.dernierMessageErreur = data["error_message"]
 }
@@ -748,8 +741,11 @@ Client.prototype.flush = function(async)
 
 Client.prototype.majMenu = function()
 {
+   // TODO : à virer : ne plus changer de style de display ... spa beau .. ou trouver une autre méthode
    var displayType = this.css == "css/3/euphorik.css" ? "block" : "inline" //this.client
 
+   $("#menu .admin").css("display", this.ekMaster ? "inline" : "none")
+
    // met à jour le menu   
    if (this.statut == statutType.auth_registered)
    {
@@ -771,7 +767,31 @@ Client.prototype.majMenu = function()
    }
 }
 
-Client.prototype.ban = function(userId, minutes)
+Client.prototype.slap = function(userId, raison)
+{
+   var thisClient = this
+   
+   jQuery.ajax({
+      type: "POST",
+      url: "request",
+      dataType: "json",
+      data: this.util.jsonVersAction(
+         {
+            "action" : "slap",
+            "cookie" : thisClient.cookie,
+            "user_id" : userId,
+            "reason" : raison
+         }),
+      success: 
+         function(data)
+         {
+            if (data["reply"] == "error")
+               thisClient.util.messageDialogue(data["error_message"])
+         }
+   })
+}
+
+Client.prototype.ban = function(userId, raison, minutes)
 {
    var thisClient = this
 
@@ -788,7 +808,8 @@ Client.prototype.ban = function(userId, minutes)
             "action" : "ban",
             "cookie" : thisClient.cookie,
             "duration" : minutes,
-            "user_id" : userId
+            "user_id" : userId,
+            "reason" : raison
          }),
       success: 
          function(data)
@@ -799,9 +820,80 @@ Client.prototype.ban = function(userId, minutes)
    })
 }
 
-Client.prototype.kick = function(userId)
+Client.prototype.kick = function(userId, raison)
 {
-   this.ban(userId, 15)
+   this.ban(userId, raison, 15)
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+   * classe permettant de gérer les événements (push serveur).
+   * @page la page
+   */
+function PageEvent(page, util)
+{
+   this.page = page
+   this.util = util
+   
+   // l'objet JSONHttpRequest représentant la connexion d'attente
+   this.attenteCourante = null
+}
+
+/**
+  * Arrête l'attente courante s'il y en a une.
+  */
+PageEvent.prototype.stopAttenteCourante = function()
+{
+   if (this.attenteCourante != null)
+      this.attenteCourante.abort()   
+}
+
+/**
+  * Attend un événement lié à la page. 
+  * @funSend une fonction renvoyant les données json à envoyer
+  * @funReceive une fonction qui accepte un paramètre correspondant au données reçues
+  */
+PageEvent.prototype.waitEvent = function(funSend, funReceive)
+{
+   var thisPageEvent = this
+      
+   this.stopAttenteCourante()
+      
+   // on doit conserver l'ordre des valeurs de l'objet JSON (le serveur les veux dans l'ordre définit dans le protocole)
+   // TODO : ya pas mieux ?
+   var dataToSend = 
+   {
+      "action" : "wait_event",
+      "page" : this.page
+   }
+   var poulpe = funSend()
+   for (v in poulpe)
+      dataToSend[v] = poulpe[v]
+   
+   ;;; dumpObj(dataToSend)
+   this.attenteCourante = jQuery.ajax({
+      type: "POST",
+      url: "request",
+      dataType: "json",
+      data: this.util.jsonVersAction(dataToSend),
+      success:
+         function(data)
+         {            
+            ;;; dumpObj(data)
+            
+            funReceive(data)
+            
+            // rappel de la fonction dans 100 ms
+            setTimeout(function(){ thisPageEvent.waitEvent(funSend, funReceive) }, 100);
+         },
+      error:
+         function(XMLHttpRequest, textStatus, errorThrown)
+         {
+            setTimeout(function(){ thisPageEvent.rafraichirMessages(funSend, funReceive) }, 1000);
+         }
+   })
+
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -837,6 +929,7 @@ $(document).ready(
       $(window).unload(function(){client.flush(false)})
       
       $("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
+      $("#menu .admin").click(function(){ pages.afficherPage("admin") })
       $("#menu .profile").click(function(){ pages.afficherPage("profile") })\r
       $("#menu .logout").click(function(){
          util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", messageType.question,
@@ -853,6 +946,7 @@ $(document).ready(
       $("#menu .about").click(function(){ pages.afficherPage("about") })
 
       pages.ajouterPage(new PageMinichat(client, formateur, util))
+      pages.ajouterPage(new PageAdmin(client, formateur, util))
       pages.ajouterPage(new PageProfile(client, formateur, util))
       pages.ajouterPage(new PageRegister(client, formateur, util))
       pages.ajouterPage(new PageAbout(client, formateur, util))