\r
/*jslint laxbreak:true */\r
\r
-// les statuts possibes du client\r
-euphorik.Client.statutType = {\r
- // mode enregistré, peut poster des messages et modifier son profile\r
- auth_registered : 0,\r
- // mode identifié, peut poster des messages mais n'a pas accès au profile\r
- auth_not_registered : 1,\r
- // mode déconnecté, ne peut pas poster de message\r
- deconnected : 2\r
-};\r
-\r
/**\r
- * Représente l'utilisateur du site.\r
+ * Object that represents the user.\r
+ * It carries all the user data like\r
+ * - ID\r
+ * - Nick\r
+ * - E-Mail\r
+ * - etc..\r
+ * It can access directly to the DOM if needed. Some examples :\r
+ * - Changes the menu if the user is logged or not\r
+ * - Changes the logo if the user is an ek master\r
+ * - etc..\r
+ * @util See util.js.\r
+ * @communication See communication.js.\r
*/\r
-euphorik.Client = function(util) {\r
+euphorik.Client = function(util, communication) {\r
this.util = util;\r
- \r
+ this.communication = communication;\r
+\r
this.cookie = null;\r
- this.regexCookie = /^cookie=([^;]*)/;\r
- \r
- // données personnels\r
- this.resetDonneesPersonnelles();\r
- \r
- this.setStatut(euphorik.Client.statutType.deconnected);\r
- \r
- // si true alors chaque modification du client est mémorisé sur le serveur\r
+ this.regexCookie = /cookie=([^;]*)/;\r
+\r
+ this.resetPersonalData();\r
+\r
+ this.setStatus(euphorik.Client.statusType.disconnected);\r
+\r
+ // If true then each data change is flushed to the server.\r
+ // Active only for opera which doesn't support the unload event.\r
this.autoflush = $.browser.opera;\r
};\r
\r
-euphorik.Client.prototype.resetDonneesPersonnelles = function() {\r
+// The three status of a client.\r
+euphorik.Client.statusType = {\r
+ // Authentified and registered : The user can post messages and can modify his profile.\r
+ auth_registered : 0,\r
+ // Authentified but not registered : The user can only post messages.\r
+ auth_not_registered : 1,\r
+ // Disconnected (the initial state) : The user cannot post message.\r
+ disconnected : 2\r
+};\r
+\r
+/**\r
+ * Reset all the local personal data. Does not erase the remote data.\r
+ * This function is used during disconnecting.\r
+ */\r
+euphorik.Client.prototype.resetPersonalData = function() {\r
this.id = 0;\r
- this.pseudo = euphorik.conf.pseudoDefaut;\r
+ this.nick = euphorik.conf.defaultNick;\r
this.login = "";\r
this.password = "";\r
this.email = "";\r
- this.css = $("link#cssPrincipale").attr("href");\r
+ this.css = $("link#mainCss").attr("href");\r
this.chatOrder = "reverse";\r
this.nickFormat = "nick";\r
this.viewTimes = true;\r
this.viewTooltips = true;\r
this.cookie = undefined;\r
- \r
- this.pagePrincipale = 1;\r
+\r
+ this.mainConversationPage = 1;\r
this.ekMaster = false;\r
this.ostentatiousMaster = "light";\r
- \r
- // les conversations, une conversation est un objet possédant les propriétés suivantes :\r
- // - root (entier)\r
- // - page (entier)\r
- // - reduit (bool)\r
+\r
+ // The user opened conversations.\r
+ // Each conversation object owns theses properties :\r
+ // - root (integer)\r
+ // - page (integer)\r
+ // - isCollapsed (bool)\r
this.conversations = [];\r
};\r
\r
+/**\r
+ * Set the CSS. Dynamically change the href to the CSS in the DOM.\r
+ * @css The relative path to the CSS file, for example :\r
+ * "styles/1/euphorik.css".\r
+ */\r
euphorik.Client.prototype.setCss = function(css) {\r
if (this.css === css || !css) {\r
return;\r
}\r
\r
this.css = css;\r
- $("link#cssPrincipale").attr("href", this.css);\r
+ $("link#mainCss").attr("href", this.css);\r
+\r
if (this.autoflush) {\r
this.flush(true);\r
}\r
};\r
\r
-euphorik.Client.prototype.pageSuivante = function(numConv) {\r
- if (numConv < 0 && this.pagePrincipale > 1) {\r
- this.pagePrincipale -= 1;\r
+/**\r
+ * Change the current page to the next one for the given conversation.\r
+ * @numConv The number of the conversation.\r
+ */\r
+euphorik.Client.prototype.nextPage = function(numConv) {\r
+ if (numConv < 0 && this.mainConversationPage > 1) {\r
+ this.mainConversationPage -= 1;\r
} else if (this.conversations[numConv].page > 1) {\r
this.conversations[numConv].page -= 1;\r
}\r
};\r
\r
-euphorik.Client.prototype.pagePrecedente = function(numConv) {\r
+/**\r
+ * Change the current page to the previous one for the given conversation.\r
+ * @numConv The number of the conversation.\r
+ */\r
+euphorik.Client.prototype.previousPage = function(numConv) {\r
if (numConv < 0) {\r
- this.pagePrincipale += 1;\r
+ this.mainConversationPage += 1;\r
} else {\r
this.conversations[numConv].page += 1;\r
}\r
* Définit la première page pour la conversation donnée.\r
* @return true si la page a changé sinon false\r
*/\r
-euphorik.Client.prototype.goPremierePage = function(numConv)\r
-{\r
+euphorik.Client.prototype.goFirstPage = function(numConv) {\r
if (numConv < 0) {\r
- if (this.pagePrincipale == 1) {\r
+ if (this.mainConversationPage === 1) {\r
return false;\r
}\r
- this.pagePrincipale = 1;\r
+ this.mainConversationPage = 1;\r
} else {\r
- if (this.conversations[numConv].page == 1) {\r
+ if (this.conversations[numConv].page === 1) {\r
return false;\r
}\r
this.conversations[numConv].page = 1;\r
* @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)\r
*/\r
euphorik.Client.prototype.ajouterConversation = function(racine) {\r
- // vérification s'il elle n'existe pas déjà \r
+ // vérification s'il elle n'existe pas déjà\r
+ var existe = false;\r
this.conversations.each(function(i, conv) {\r
if (conv.root === racine) {\r
- return false;\r
+ existe = true;\r
}\r
});\r
- \r
- this.conversations.push({root : racine, page : 1, reduit : false});\r
+ if (existe) {\r
+ return false;\r
+ }\r
+\r
+ this.conversations.push({root : racine, page : 1, isCollapsed : false});\r
if (this.autoflush) {\r
this.flush(true);\r
}\r
- \r
+\r
return true;\r
};\r
\r
if (num < 0 || num >= this.conversations.length) {\r
return;\r
}\r
- \r
- // décalage TODO : supprimer le dernier élément \r
+\r
+ // décalage TODO : supprimer le dernier élément\r
for (var i = num; i < this.conversations.length - 1; i++) {\r
this.conversations[i] = this.conversations[i+1];\r
}\r
this.conversations.pop();\r
- \r
+\r
if (this.autoflush) {\r
this.flush(true);\r
}\r
};\r
\r
-euphorik.Client.prototype.getJSONLogin = function(login, password) {\r
- return {\r
- "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole },\r
- "login" : login,\r
- "password" : password\r
- };\r
-};\r
-\r
-euphorik.Client.prototype.getJSONLoginCookie = function() {\r
- return {\r
- "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole },\r
- "cookie" : this.cookie\r
- };\r
-}; \r
-\r
-/**\r
- * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible\r
- * de s'autentifier avec (login, password).\r
- */\r
-euphorik.Client.prototype.getJSONEnregistrement = function(login, password) {\r
- var mess = { "header" : { "action" : "register", "version" : euphorik.conf.versionProtocole } };\r
- \r
- if (login && password) {\r
- mess.login = login;\r
- mess.password = password;\r
- }\r
- \r
- return mess;\r
-};\r
-\r
euphorik.Client.prototype.getJSONConversations = function() {\r
var conversations = [];\r
this.conversations.each(function(i, conv) {\r
- conversations.push({ "root" : conv.root, "minimized" : conv.reduit });\r
+ conversations.push({ "root" : conv.root, "minimized" : conv.isCollapsed });\r
});\r
return conversations;\r
};\r
\r
euphorik.Client.prototype.getJSONProfile = function() {\r
return {\r
- "header" : { "action" : "set_profile", "version" : euphorik.conf.versionProtocole },\r
"cookie" : this.cookie,\r
"login" : this.login,\r
"password" : this.password,\r
- "nick" : this.pseudo,\r
+ "profile" : this.getJSONProfileInfos()\r
+ };\r
+};\r
+\r
+euphorik.Client.prototype.getJSONProfileInfos = function() {\r
+ return {\r
+ "nick" : this.nick,\r
"email" : this.email,\r
"css" : this.css,\r
"chat_order" : this.chatOrder,\r
if (!this.cookie) {\r
return;\r
}\r
- \r
- // ne fonctionne pas sous IE....\r
+\r
+ // doesn't work under IE....\r
/*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */\r
- \r
- document.cookie = \r
+\r
+ document.cookie =\r
"cookie="+this.cookie+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();\r
};\r
\r
euphorik.Client.prototype.authentifie = function() {\r
- return this.statut === euphorik.Client.statutType.auth_registered || this.statut === euphorik.Client.statutType.auth_not_registered;\r
+ return this.statut === euphorik.Client.statusType.auth_registered || this.statut === euphorik.Client.statusType.auth_not_registered;\r
};\r
\r
-euphorik.Client.prototype.setStatut = function(statut)\r
-{ \r
+euphorik.Client.prototype.setStatus = function(statut)\r
+{\r
// conversation en "enum" si en "string"\r
if (typeof(statut) === "string") {\r
statut =\r
statut === "auth_registered" ?\r
- euphorik.Client.statutType.auth_registered :\r
- (statut === "auth_not_registered" ? euphorik.Client.statutType.auth_not_registered : euphorik.Client.statutType.deconnected);\r
+ euphorik.Client.statusType.auth_registered :\r
+ (statut === "auth_not_registered" ? euphorik.Client.statusType.auth_not_registered : euphorik.Client.statusType.disconnected);\r
}\r
- \r
+\r
if (statut === this.statut) {\r
return;\r
}\r
- \r
+\r
this.statut = statut;\r
+\r
this.majMenu();\r
this.majLogo();\r
};\r
\r
/**\r
- * Effectue la connexion vers le serveur.\r
- * Cette fonction est bloquante tant que la connexion n'a pas été établie.\r
- * S'il existe un cookie en local on s'authentifie directement avec lui.\r
- * Si il n'est pas possible de s'authentifier alors on affiche un captcha anti-bot.\r
+ * Try to authentify the client with the cookie information.\r
+ * Do nothing if there is no cookie.\r
*/\r
-euphorik.Client.prototype.connexionCookie = function() {\r
+euphorik.Client.prototype.connectionCookie = function() {\r
this.getCookie();\r
if (!this.cookie) {\r
return false;\r
}\r
- return this.connexion(this.getJSONLoginCookie());\r
+ return this.connexion("authentification", { "cookie" : this.cookie });\r
};\r
\r
euphorik.Client.prototype.connexionLogin = function(login, password) {\r
- return this.connexion(this.getJSONLogin(login, password));\r
+ return this.connexion("authentification", {"login" : login, "password" : password });\r
};\r
\r
-euphorik.Client.prototype.enregistrement = function(login, password) { \r
+euphorik.Client.prototype.enregistrement = function(login, password) {\r
if (this.authentifie()) {\r
this.login = login;\r
this.password = password;\r
if(this.flush()) {\r
- this.setStatut(euphorik.Client.statutType.auth_registered);\r
+ this.setStatus(euphorik.Client.statusType.auth_registered);\r
return true;\r
}\r
return false;\r
} else {\r
- return this.connexion(this.getJSONEnregistrement(login, password));\r
+ return this.connexion("register", this.getJSONEnregistrement(login, password));\r
}\r
};\r
\r
+/**\r
+ * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible\r
+ * de s'autentifier avec (login, password).\r
+ */\r
+euphorik.Client.prototype.getJSONEnregistrement = function(login, password) {\r
+ var mess = {};\r
+\r
+ if (login && password) {\r
+ mess.login = login;\r
+ mess.password = password;\r
+ }\r
+\r
+ mess.profile = this.getJSONProfileInfos();\r
+\r
+ return mess;\r
+};\r
+\r
/**\r
* Connexion. Réalisé de manière synchrone.\r
*/\r
-euphorik.Client.prototype.connexion = function(messageJson) {\r
+euphorik.Client.prototype.connexion = function(action, messageJson) {\r
var thisClient = this;\r
- jQuery.ajax({\r
- async: false,\r
- type: "POST",\r
- url: "request",\r
- dataType: "json",\r
- data: this.util.jsonVersAction(messageJson),\r
- success:\r
- function(data){\r
- if (data.reply === "error") {\r
- thisClient.util.messageDialogue(data.error_message);\r
- // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification\r
- thisClient.delCookie();\r
- } else {\r
- thisClient.chargerDonnees(data);\r
- }\r
- }\r
- });\r
+\r
+ this.communication.requete(\r
+ action,\r
+ messageJson,\r
+ function(data) {\r
+ thisClient.chargerDonnees(data);\r
+ },\r
+ function(data) {\r
+ thisClient.util.messageDialog(data.error_message);\r
+ thisClient.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification\r
+ },\r
+ false\r
+ );\r
return this.authentifie();\r
};\r
\r
-euphorik.Client.prototype.deconnexion = function() {\r
+euphorik.Client.prototype.disconnect = function() {\r
this.flush(true);\r
this.delCookie();\r
- this.resetDonneesPersonnelles();\r
- this.setStatut(euphorik.Client.statutType.deconnected); // deconnexion\r
+ this.resetPersonalData();\r
+ this.setStatus(euphorik.Client.statusType.disconnected);\r
};\r
\r
euphorik.Client.prototype.chargerDonnees = function(data) {\r
// la modification du statut qui suit met à jour le menu, le menu dépend (page admin)\r
// de l'état ekMaster\r
this.ekMaster = data.ek_master ? data.ek_master : false;\r
- \r
- this.setStatut(data.status);\r
- \r
+\r
+ this.setStatus(data.status);\r
+\r
if (this.authentifie()) {\r
this.cookie = data.cookie;\r
this.setCookie();\r
- \r
+\r
this.id = data.id;\r
this.login = data.login;\r
- this.pseudo = data.nick;\r
- this.email = data.email;\r
- this.setCss(data.css);\r
- this.chatOrder = data.chat_order;\r
- this.nickFormat = data.nick_format;\r
- this.viewTimes = data.view_times;\r
- this.viewTooltips = data.view_tooltips;\r
- this.ostentatiousMaster = data.ostentatious_master;\r
- \r
+ this.nick = data.profile.nick;\r
+ this.email = data.profile.email;\r
+ this.setCss(data.profile.css);\r
+ this.chatOrder = data.profile.chat_order;\r
+ this.nickFormat = data.profile.nick_format;\r
+ this.viewTimes = data.profile.view_times;\r
+ this.viewTooltips = data.profile.view_tooltips;\r
+ this.ostentatiousMaster = data.profile.ostentatious_master;\r
+\r
// la page de la conversation principale\r
- this.pagePrincipale = 1;\r
- \r
+ this.mainConversationPage = 1;\r
+\r
// les conversations\r
- this.conversations = data.conversations;\r
+ this.conversations = data.profile.conversations;\r
this.conversations.map(function(conv) {\r
- return { root : conv.root, page : 1, reduit : conv.minimized };\r
+ return { root : conv.root, page : 1, isCollapsed : conv.minimized };\r
});\r
- \r
+\r
this.majBulle();\r
this.majCssSelectionee();\r
}\r
*/\r
euphorik.Client.prototype.flush = function(async) {\r
async = async || false;\r
- \r
+\r
if (!this.authentifie()) {\r
return false;\r
}\r
\r
var thisClient = this;\r
var ok = true;\r
- jQuery.ajax({\r
- async: async,\r
- type: "POST",\r
- url: "request",\r
- dataType: "json",\r
- data: this.util.jsonVersAction(this.getJSONProfile()),\r
- success:\r
- function(data) {\r
- if (data.reply === "error") {\r
- thisClient.util.messageDialogue(data.error_message);\r
- ok = false;\r
- } else {\r
- thisClient.majBulle();\r
- }\r
- }\r
- });\r
- \r
+\r
+ this.communication.requete(\r
+ "set_profile",\r
+ this.getJSONProfile(),\r
+ function(data) {\r
+ thisClient.majBulle();\r
+ },\r
+ function(data) {\r
+ thisClient.util.messageDialog(data.error_message);\r
+ ok = false;\r
+ },\r
+ async\r
+ );\r
+\r
return ok;\r
};\r
\r
\r
$("#menu .admin").css("display", this.ekMaster ? displayType : "none");\r
\r
- // met à jour le menu \r
- if (this.statut == euphorik.Client.statutType.auth_registered) {\r
+ // met à jour le menu\r
+ if (this.statut === euphorik.Client.statusType.auth_registered) {\r
$("#menu .profile").css("display", displayType).text("profile");\r
$("#menu .logout").css("display", displayType);\r
$("#menu .register").css("display", "none");\r
- } else if (this.statut == euphorik.Client.statutType.auth_not_registered) {\r
+ } else if (this.statut === euphorik.Client.statusType.auth_not_registered) {\r
$("#menu .profile").css("display", "none");\r
$("#menu .logout").css("display", displayType);\r
$("#menu .register").css("display", displayType);\r
\r
euphorik.Client.prototype.slap = function(userId, raison) {\r
var thisClient = this;\r
- \r
- jQuery.ajax({\r
- type: "POST",\r
- url: "request",\r
- dataType: "json",\r
- data: this.util.jsonVersAction({\r
- "header" : { "action" : "slap", "version" : euphorik.conf.versionProtocole },\r
- "cookie" : thisClient.cookie,\r
- "user_id" : userId,\r
- "reason" : raison\r
- }),\r
- success: \r
- function(data) {\r
- if (data.reply === "error") {\r
- thisClient.util.messageDialogue(data.error_message);\r
- }\r
- }\r
- });\r
+ this.communication.requete("slap", { "cookie" : thisClient.cookie, "user_id" : userId, "reason" : raison });\r
};\r
\r
-euphorik.Client.prototype.ban = function(userId, raison, minutes)\r
-{\r
+euphorik.Client.prototype.ban = function(userId, raison, minutes) {\r
var thisClient = this;\r
\r
// par défaut un ban correspond à 3 jours\r
minutes = minutes || euphorik.conf.tempsBan;\r
- \r
- jQuery.ajax({\r
- type: "POST",\r
- url: "request",\r
- dataType: "json",\r
- data: this.util.jsonVersAction({\r
- "header" : { "action" : "ban", "version" : euphorik.conf.versionProtocole },\r
- "cookie" : thisClient.cookie,\r
- "duration" : minutes,\r
- "user_id" : userId,\r
- "reason" : raison\r
- }),\r
- success: \r
- function(data) {\r
- if (data.reply === "error") {\r
- thisClient.util.messageDialogue(data.error_message);\r
- }\r
- }\r
- });\r
+ this.communication.requete("ban", { "cookie" : thisClient.cookie, "duration" : minutes, "user_id" : userId, "reason" : raison });\r
};\r
\r
euphorik.Client.prototype.kick = function(userId, raison) {\r