MOD french -> english (2)
[euphorik.git] / js / client.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19 /*jslint laxbreak:true */
20
21 /**
22 * Représente l'utilisateur du site.
23 */
24 euphorik.Client = function(util, communication) {
25 this.util = util;
26 this.communication = communication;
27
28 this.cookie = null;
29 this.regexCookie = /cookie=([^;]*)/;
30
31 // données personnels
32 this.resetDonneesPersonnelles();
33
34 this.setStatut(euphorik.Client.statutType.disconnected);
35
36 // si true alors chaque modification du client est mémorisé sur le serveur
37 this.autoflush = $.browser.opera;
38 };
39
40 // les statuts possibes du client
41 euphorik.Client.statutType = {
42 // mode enregistré, peut poster des messages et modifier son profile
43 auth_registered : 0,
44 // mode identifié, peut poster des messages mais n'a pas accès au profile
45 auth_not_registered : 1,
46 // mode déconnecté, ne peut pas poster de message
47 disconnected : 2
48 };
49
50 euphorik.Client.prototype.resetDonneesPersonnelles = function() {
51 this.id = 0;
52 this.pseudo = euphorik.conf.pseudoDefaut;
53 this.login = "";
54 this.password = "";
55 this.email = "";
56 this.css = $("link#mainCss").attr("href");
57 this.chatOrder = "reverse";
58 this.nickFormat = "nick";
59 this.viewTimes = true;
60 this.viewTooltips = true;
61 this.cookie = undefined;
62
63 this.pagePrincipale = 1;
64 this.ekMaster = false;
65 this.ostentatiousMaster = "light";
66
67 // les conversations, une conversation est un objet possédant les propriétés suivantes :
68 // - root (entier)
69 // - page (entier)
70 // - reduit (bool)
71 this.conversations = [];
72 };
73
74 euphorik.Client.prototype.setCss = function(css) {
75 if (this.css === css || !css) {
76 return;
77 }
78
79 this.css = css;
80 $("link#mainCss").attr("href", this.css);
81 if (this.autoflush) {
82 this.flush(true);
83 }
84 };
85
86 euphorik.Client.prototype.pageSuivante = function(numConv) {
87 if (numConv < 0 && this.pagePrincipale > 1) {
88 this.pagePrincipale -= 1;
89 } else if (this.conversations[numConv].page > 1) {
90 this.conversations[numConv].page -= 1;
91 }
92 };
93
94 euphorik.Client.prototype.pagePrecedente = function(numConv) {
95 if (numConv < 0) {
96 this.pagePrincipale += 1;
97 } else {
98 this.conversations[numConv].page += 1;
99 }
100 };
101
102 /**
103 * Définit la première page pour la conversation donnée.
104 * @return true si la page a changé sinon false
105 */
106 euphorik.Client.prototype.goPremierePage = function(numConv)
107 {
108 if (numConv < 0) {
109 if (this.pagePrincipale === 1) {
110 return false;
111 }
112 this.pagePrincipale = 1;
113 } else {
114 if (this.conversations[numConv].page === 1) {
115 return false;
116 }
117 this.conversations[numConv].page = 1;
118 }
119 return true;
120 };
121
122 /**
123 * Ajoute une conversation à la vue de l'utilisateur.
124 * Le profile de l'utilisateur est directement sauvegardé sur le serveur.
125 * @param racines la racine de la conversation (integer)
126 * @return true si la conversation a été créée sinon false (par exemple si la conv existe déjà)
127 */
128 euphorik.Client.prototype.ajouterConversation = function(racine) {
129 // vérification s'il elle n'existe pas déjà
130 var existe = false;
131 this.conversations.each(function(i, conv) {
132 if (conv.root === racine) {
133 existe = true;
134 }
135 });
136 if (existe) {
137 return false;
138 }
139
140 this.conversations.push({root : racine, page : 1, reduit : false});
141 if (this.autoflush) {
142 this.flush(true);
143 }
144
145 return true;
146 };
147
148 euphorik.Client.prototype.supprimerConversation = function(num) {
149 if (num < 0 || num >= this.conversations.length) {
150 return;
151 }
152
153 // décalage TODO : supprimer le dernier élément
154 for (var i = num; i < this.conversations.length - 1; i++) {
155 this.conversations[i] = this.conversations[i+1];
156 }
157 this.conversations.pop();
158
159 if (this.autoflush) {
160 this.flush(true);
161 }
162 };
163
164 euphorik.Client.prototype.getJSONConversations = function() {
165 var conversations = [];
166 this.conversations.each(function(i, conv) {
167 conversations.push({ "root" : conv.root, "minimized" : conv.reduit });
168 });
169 return conversations;
170 };
171
172 euphorik.Client.prototype.getJSONProfile = function() {
173 return {
174 "cookie" : this.cookie,
175 "login" : this.login,
176 "password" : this.password,
177 "profile" : this.getJSONProfileInfos()
178 };
179 };
180
181 euphorik.Client.prototype.getJSONProfileInfos = function() {
182 return {
183 "nick" : this.pseudo,
184 "email" : this.email,
185 "css" : this.css,
186 "chat_order" : this.chatOrder,
187 "nick_format" : this.nickFormat,
188 "view_times" : this.viewTimes,
189 "view_tooltips" : this.viewTooltips,
190 "conversations" : this.getJSONConversations(),
191 "ostentatious_master" : this.ostentatiousMaster
192 };
193 };
194
195 /**
196 * .
197 */
198 euphorik.Client.prototype.getCookie = function() {
199 var cookie = this.regexCookie.exec(document.cookie);
200 if (cookie) {
201 this.cookie = cookie[1];
202 } else {
203 this.cookie = undefined;
204 }
205 };
206
207 euphorik.Client.prototype.delCookie = function() {
208 document.cookie = "cookie=; max-age=0";
209 this.cookie = undefined;
210 };
211
212 euphorik.Client.prototype.setCookie = function() {
213 if (!this.cookie) {
214 return;
215 }
216
217 // ne fonctionne pas sous IE....
218 /*document.cookie = "cookie=" + this.cookie + "; max-age=" + (60 * 60 * 24 * 365) */
219
220 document.cookie =
221 "cookie="+this.cookie+"; expires=" + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365).toUTCString();
222 };
223
224 euphorik.Client.prototype.authentifie = function() {
225 return this.statut === euphorik.Client.statutType.auth_registered || this.statut === euphorik.Client.statutType.auth_not_registered;
226 };
227
228 euphorik.Client.prototype.setStatut = function(statut)
229 {
230 // conversation en "enum" si en "string"
231 if (typeof(statut) === "string") {
232 statut =
233 statut === "auth_registered" ?
234 euphorik.Client.statutType.auth_registered :
235 (statut === "auth_not_registered" ? euphorik.Client.statutType.auth_not_registered : euphorik.Client.statutType.disconnected);
236 }
237
238 if (statut === this.statut) {
239 return;
240 }
241
242 this.statut = statut;
243
244 this.majMenu();
245 this.majLogo();
246 };
247
248 /**
249 * Try to authentify the client with the cookie information.
250 * Do nothing if there is no cookie.
251 */
252 euphorik.Client.prototype.connectionCookie = function() {
253 this.getCookie();
254 if (!this.cookie) {
255 return false;
256 }
257 return this.connexion("authentification", { "cookie" : this.cookie });
258 };
259
260 euphorik.Client.prototype.connexionLogin = function(login, password) {
261 return this.connexion("authentification", {"login" : login, "password" : password });
262 };
263
264 euphorik.Client.prototype.enregistrement = function(login, password) {
265 if (this.authentifie()) {
266 this.login = login;
267 this.password = password;
268 if(this.flush()) {
269 this.setStatut(euphorik.Client.statutType.auth_registered);
270 return true;
271 }
272 return false;
273 } else {
274 return this.connexion("register", this.getJSONEnregistrement(login, password));
275 }
276 };
277
278 /**
279 * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible
280 * de s'autentifier avec (login, password).
281 */
282 euphorik.Client.prototype.getJSONEnregistrement = function(login, password) {
283 var mess = {};
284
285 if (login && password) {
286 mess.login = login;
287 mess.password = password;
288 }
289
290 mess.profile = this.getJSONProfileInfos();
291
292 return mess;
293 };
294
295 /**
296 * Connexion. Réalisé de manière synchrone.
297 */
298 euphorik.Client.prototype.connexion = function(action, messageJson) {
299 var thisClient = this;
300
301 this.communication.requete(
302 action,
303 messageJson,
304 function(data) {
305 thisClient.chargerDonnees(data);
306 },
307 function(data) {
308 thisClient.util.messageDialog(data.error_message);
309 thisClient.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification
310 },
311 false
312 );
313 return this.authentifie();
314 };
315
316 euphorik.Client.prototype.disconnect = function() {
317 this.flush(true);
318 this.delCookie();
319 this.resetDonneesPersonnelles();
320 this.setStatut(euphorik.Client.statutType.disconnected);
321 };
322
323 euphorik.Client.prototype.chargerDonnees = function(data) {
324 // la modification du statut qui suit met à jour le menu, le menu dépend (page admin)
325 // de l'état ekMaster
326 this.ekMaster = data.ek_master ? data.ek_master : false;
327
328 this.setStatut(data.status);
329
330 if (this.authentifie()) {
331 this.cookie = data.cookie;
332 this.setCookie();
333
334 this.id = data.id;
335 this.login = data.login;
336 this.pseudo = data.profile.nick;
337 this.email = data.profile.email;
338 this.setCss(data.profile.css);
339 this.chatOrder = data.profile.chat_order;
340 this.nickFormat = data.profile.nick_format;
341 this.viewTimes = data.profile.view_times;
342 this.viewTooltips = data.profile.view_tooltips;
343 this.ostentatiousMaster = data.profile.ostentatious_master;
344
345 // la page de la conversation principale
346 this.pagePrincipale = 1;
347
348 // les conversations
349 this.conversations = data.profile.conversations;
350 this.conversations.map(function(conv) {
351 return { root : conv.root, page : 1, reduit : conv.minimized };
352 });
353
354 this.majBulle();
355 this.majCssSelectionee();
356 }
357 };
358
359 /**
360 * Met à jour les données personne sur serveur.
361 * @param async de manière asynchrone ? défaut = true
362 * @return false si le flush n'a pas pû se faire sinon true
363 */
364 euphorik.Client.prototype.flush = function(async) {
365 async = async || false;
366
367 if (!this.authentifie()) {
368 return false;
369 }
370
371 var thisClient = this;
372 var ok = true;
373
374 this.communication.requete(
375 "set_profile",
376 this.getJSONProfile(),
377 function(data) {
378 thisClient.majBulle();
379 },
380 function(data) {
381 thisClient.util.messageDialog(data.error_message);
382 ok = false;
383 },
384 async
385 );
386
387 return ok;
388 };
389
390 euphorik.Client.prototype.majMenu = function() {
391 var displayType = "block";
392
393 $("#menu .admin").css("display", this.ekMaster ? displayType : "none");
394
395 // met à jour le menu
396 if (this.statut === euphorik.Client.statutType.auth_registered) {
397 $("#menu .profile").css("display", displayType).text("profile");
398 $("#menu .logout").css("display", displayType);
399 $("#menu .register").css("display", "none");
400 } else if (this.statut === euphorik.Client.statutType.auth_not_registered) {
401 $("#menu .profile").css("display", "none");
402 $("#menu .logout").css("display", displayType);
403 $("#menu .register").css("display", displayType);
404 } else {
405 $("#menu .profile").css("display", displayType).text("login");
406 $("#menu .logout").css("display", "none");
407 $("#menu .register").css("display", displayType);
408 }
409 };
410
411 /**
412 * Met à jour l'affichage ou non des infos bulles en fonction du profile.
413 */
414 euphorik.Client.prototype.majBulle = function() {
415 this.util.bulleActive = this.viewTooltips;
416 };
417
418 /**
419 * Met à jour la css sélectionnée, lors du chargement des données.
420 */
421 euphorik.Client.prototype.majCssSelectionee = function() {
422 // extraction du numéro de la css courante
423 var numCssCourante = this.css.match(/^.*?\/(\d)\/.*$/);
424 if (numCssCourante && numCssCourante[1]) {
425 $("#menuCss option").removeAttr("selected");
426 $("#menuCss option[value=" + numCssCourante[1]+ "]").attr("selected", "selected");
427 }
428 };
429
430 /**
431 * Change la "class" du logo en fonction du statut de ekMaster.
432 */
433 euphorik.Client.prototype.majLogo = function() {
434 if (this.ekMaster) {
435 $("#logo").addClass("ekMaster");
436 } else {
437 $("#logo").removeClass("ekMaster");
438 }
439 };
440
441 euphorik.Client.prototype.slap = function(userId, raison) {
442 var thisClient = this;
443 this.communication.requete("slap", { "cookie" : thisClient.cookie, "user_id" : userId, "reason" : raison });
444 };
445
446 euphorik.Client.prototype.ban = function(userId, raison, minutes) {
447 var thisClient = this;
448
449 // par défaut un ban correspond à 3 jours
450 minutes = minutes || euphorik.conf.tempsBan;
451 this.communication.requete("ban", { "cookie" : thisClient.cookie, "duration" : minutes, "user_id" : userId, "reason" : raison });
452 };
453
454 euphorik.Client.prototype.kick = function(userId, raison) {
455 this.ban(userId, raison, euphorik.conf.tempsKick);
456 };