X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Fclient.js;h=c378075daa34e2439244d1d41cc1efcd3a51bcaa;hp=ea96902c2fa56d305ab1ac4c1aaef4dcfd1bf6e7;hb=8ee1535f5594573931ddaebee77bf6148a5358cb;hpb=ff1b8889e37bdecb354caa1f330a3ce2cfa82519 diff --git a/js/client.js b/js/client.js index ea96902..c378075 100644 --- a/js/client.js +++ b/js/client.js @@ -18,24 +18,15 @@ /*jslint laxbreak:true */ -// les statuts possibes du client -euphorik.Client.statutType = { - // mode enregistré, peut poster des messages et modifier son profile - auth_registered : 0, - // mode identifié, peut poster des messages mais n'a pas accès au profile - auth_not_registered : 1, - // mode déconnecté, ne peut pas poster de message - deconnected : 2 -}; - /** * Représente l'utilisateur du site. */ -euphorik.Client = function(util) { +euphorik.Client = function(util, communication) { this.util = util; + this.communication = communication; this.cookie = null; - this.regexCookie = /^cookie=([^;]*)/; + this.regexCookie = /cookie=([^;]*)/; // données personnels this.resetDonneesPersonnelles(); @@ -46,6 +37,16 @@ euphorik.Client = function(util) { this.autoflush = $.browser.opera; }; +// les statuts possibes du client +euphorik.Client.statutType = { + // mode enregistré, peut poster des messages et modifier son profile + auth_registered : 0, + // mode identifié, peut poster des messages mais n'a pas accès au profile + auth_not_registered : 1, + // mode déconnecté, ne peut pas poster de message + deconnected : 2 +}; + euphorik.Client.prototype.resetDonneesPersonnelles = function() { this.id = 0; this.pseudo = euphorik.conf.pseudoDefaut; @@ -105,12 +106,12 @@ euphorik.Client.prototype.pagePrecedente = function(numConv) { euphorik.Client.prototype.goPremierePage = function(numConv) { if (numConv < 0) { - if (this.pagePrincipale == 1) { + if (this.pagePrincipale === 1) { return false; } this.pagePrincipale = 1; } else { - if (this.conversations[numConv].page == 1) { + if (this.conversations[numConv].page === 1) { return false; } this.conversations[numConv].page = 1; @@ -126,11 +127,15 @@ euphorik.Client.prototype.goPremierePage = function(numConv) */ euphorik.Client.prototype.ajouterConversation = function(racine) { // vérification s'il elle n'existe pas déjà + var existe = false; this.conversations.each(function(i, conv) { if (conv.root === racine) { - return false; + existe = true; } }); + if (existe) { + return false; + } this.conversations.push({root : racine, page : 1, reduit : false}); if (this.autoflush) { @@ -156,36 +161,6 @@ euphorik.Client.prototype.supprimerConversation = function(num) { } }; -euphorik.Client.prototype.getJSONLogin = function(login, password) { - return { - "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole }, - "login" : login, - "password" : password - }; -}; - -euphorik.Client.prototype.getJSONLoginCookie = function() { - return { - "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole }, - "cookie" : this.cookie - }; -}; - -/** - * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible - * de s'autentifier avec (login, password). - */ -euphorik.Client.prototype.getJSONEnregistrement = function(login, password) { - var mess = { "header" : { "action" : "register", "version" : euphorik.conf.versionProtocole } }; - - if (login && password) { - mess.login = login; - mess.password = password; - } - - return mess; -}; - euphorik.Client.prototype.getJSONConversations = function() { var conversations = []; this.conversations.each(function(i, conv) { @@ -196,10 +171,15 @@ euphorik.Client.prototype.getJSONConversations = function() { euphorik.Client.prototype.getJSONProfile = function() { return { - "header" : { "action" : "set_profile", "version" : euphorik.conf.versionProtocole }, "cookie" : this.cookie, "login" : this.login, "password" : this.password, + "profile" : this.getJSONProfileInfos() + }; +}; + +euphorik.Client.prototype.getJSONProfileInfos = function() { + return { "nick" : this.pseudo, "email" : this.email, "css" : this.css, @@ -260,6 +240,7 @@ euphorik.Client.prototype.setStatut = function(statut) } this.statut = statut; + this.majMenu(); this.majLogo(); }; @@ -275,11 +256,11 @@ euphorik.Client.prototype.connexionCookie = function() { if (!this.cookie) { return false; } - return this.connexion(this.getJSONLoginCookie()); + return this.connexion("authentification", { "cookie" : this.cookie }); }; euphorik.Client.prototype.connexionLogin = function(login, password) { - return this.connexion(this.getJSONLogin(login, password)); + return this.connexion("authentification", {"login" : login, "password" : password }); }; euphorik.Client.prototype.enregistrement = function(login, password) { @@ -292,32 +273,45 @@ euphorik.Client.prototype.enregistrement = function(login, password) { } return false; } else { - return this.connexion(this.getJSONEnregistrement(login, password)); + return this.connexion("register", this.getJSONEnregistrement(login, password)); + } +}; + +/** + * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible + * de s'autentifier avec (login, password). + */ +euphorik.Client.prototype.getJSONEnregistrement = function(login, password) { + var mess = {}; + + if (login && password) { + mess.login = login; + mess.password = password; } + + mess.profile = this.getJSONProfileInfos(); + + return mess; }; /** * Connexion. Réalisé de manière synchrone. */ -euphorik.Client.prototype.connexion = function(messageJson) { +euphorik.Client.prototype.connexion = function(action, messageJson) { var thisClient = this; - jQuery.ajax({ - async: false, - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction(messageJson), - success: - function(data){ - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification - thisClient.delCookie(); - } else { - thisClient.chargerDonnees(data); - } - } - }); + + this.communication.requete( + action, + messageJson, + function(data) { + thisClient.chargerDonnees(data); + }, + function(data) { + thisClient.util.messageDialogue(data.error_message); + thisClient.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification + }, + false + ); return this.authentifie(); }; @@ -341,20 +335,20 @@ euphorik.Client.prototype.chargerDonnees = function(data) { this.id = data.id; this.login = data.login; - this.pseudo = data.nick; - this.email = data.email; - this.setCss(data.css); - this.chatOrder = data.chat_order; - this.nickFormat = data.nick_format; - this.viewTimes = data.view_times; - this.viewTooltips = data.view_tooltips; - this.ostentatiousMaster = data.ostentatious_master; + this.pseudo = data.profile.nick; + this.email = data.profile.email; + this.setCss(data.profile.css); + this.chatOrder = data.profile.chat_order; + this.nickFormat = data.profile.nick_format; + this.viewTimes = data.profile.view_times; + this.viewTooltips = data.profile.view_tooltips; + this.ostentatiousMaster = data.profile.ostentatious_master; // la page de la conversation principale this.pagePrincipale = 1; // les conversations - this.conversations = data.conversations; + this.conversations = data.profile.conversations; this.conversations.map(function(conv) { return { root : conv.root, page : 1, reduit : conv.minimized }; }); @@ -375,25 +369,22 @@ euphorik.Client.prototype.flush = function(async) { if (!this.authentifie()) { return false; } - + var thisClient = this; var ok = true; - jQuery.ajax({ - async: async, - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction(this.getJSONProfile()), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - ok = false; - } else { - thisClient.majBulle(); - } - } - }); + + this.communication.requete( + "set_profile", + this.getJSONProfile(), + function(data) { + thisClient.majBulle(); + }, + function(data) { + thisClient.util.messageDialogue(data.error_message); + ok = false; + }, + async + ); return ok; }; @@ -402,13 +393,13 @@ euphorik.Client.prototype.majMenu = function() { var displayType = "block"; $("#menu .admin").css("display", this.ekMaster ? displayType : "none"); - + // met à jour le menu - if (this.statut == euphorik.Client.statutType.auth_registered) { + if (this.statut === euphorik.Client.statutType.auth_registered) { $("#menu .profile").css("display", displayType).text("profile"); $("#menu .logout").css("display", displayType); $("#menu .register").css("display", "none"); - } else if (this.statut == euphorik.Client.statutType.auth_not_registered) { + } else if (this.statut === euphorik.Client.statutType.auth_not_registered) { $("#menu .profile").css("display", "none"); $("#menu .logout").css("display", displayType); $("#menu .register").css("display", displayType); @@ -451,51 +442,15 @@ euphorik.Client.prototype.majLogo = function() { euphorik.Client.prototype.slap = function(userId, raison) { var thisClient = this; - - jQuery.ajax({ - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction({ - "header" : { "action" : "slap", "version" : euphorik.conf.versionProtocole }, - "cookie" : thisClient.cookie, - "user_id" : userId, - "reason" : raison - }), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - } - } - }); + this.communication.requete("slap", { "cookie" : thisClient.cookie, "user_id" : userId, "reason" : raison }); }; -euphorik.Client.prototype.ban = function(userId, raison, minutes) -{ +euphorik.Client.prototype.ban = function(userId, raison, minutes) { var thisClient = this; // par défaut un ban correspond à 3 jours minutes = minutes || euphorik.conf.tempsBan; - - jQuery.ajax({ - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction({ - "header" : { "action" : "ban", "version" : euphorik.conf.versionProtocole }, - "cookie" : thisClient.cookie, - "duration" : minutes, - "user_id" : userId, - "reason" : raison - }), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - } - } - }); + this.communication.requete("ban", { "cookie" : thisClient.cookie, "duration" : minutes, "user_id" : userId, "reason" : raison }); }; euphorik.Client.prototype.kick = function(userId, raison) {