Replace some french comments by english ones.
[euphorik.git] / js / pageMinichat.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 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
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 euphorik.PageMinichat = function(client, formatter, util, communication) {
22 this.name = "minichat";
23
24 this.client = client;
25 this.formatter = formatter;
26 this.util = util;
27 this.communication = communication;
28 this.commandes = new euphorik.Commandes(this.client, this, this.util, this.formatter);
29
30 // permet d'éviter d'envoyer plusieurs messages simultanément en pressant
31 // rapidement sur "enter" par exemple
32 this.envoieMessageEnCours = false;
33 };
34
35 euphorik.PageMinichat.prototype.contenu = function() {
36 // le fait que tout soit collé est fait exprès, permet d'éviter d'avoir des espaces supplémentaires entre les spans
37 var formulaireXHTML = '' +
38 '<form method="post" action="" id ="posterMessage">' +
39 ' <p>' +
40 ' <input class="captcha" name="captcha" type="text" size="8" maxlength="8"></input>' +
41 ' <input class="nick" name="nick" type="text" maxlength="50" value="' + encodeURI(euphorik.conf.defaultNick) + '"></input>' +
42 ' <span id="repondA"><span class="nb">0</span><span class="messages"></span></span>' +
43 ' <input class="message" name="message" type="text" maxlength="500" value=""></input>' +
44 ' <button class="smiles"></button>' +
45 ' <button class="return"></button>' +
46 ' </p>' +
47 '</form>';
48
49 var conversationXHTML = '<table id="conversations"><tr></tr></table>';
50
51 if (this.client.chatOrder === "reverse") {
52 return formulaireXHTML + conversationXHTML;
53 } else {
54 return conversationXHTML + formulaireXHTML;
55 }
56 };
57
58 euphorik.PageMinichat.prototype.classes = function() {
59 return this.client.chatOrder === "reverse" ? "orderReverse" : "orderChrono";
60 };
61
62 euphorik.PageMinichat.prototype.charger = function() {
63 thisPage = this;
64
65 $("#posterMessage input.nick").val(this.client.nick);
66
67 // cet appel ne doit pas être fait avant l'appel à 'charger'
68 this.conversations = new euphorik.Conversations(this.client, this.formatter, this.util, this.communication, this.fragment);
69
70 this.chargerConversationsFragment();
71
72 this.conversations.rafraichirMessages(true);
73
74 this.util.setCaretToEnd($("form#posterMessage input.message")[0]);
75
76 // les outils de bannissement (uniquement pour les ekMaster)
77 if (this.client.ekMaster) {
78 // TODO : augmentation un peu space, à revoir
79 this.util.outilsBan = $(
80 '<span id="outilsBan">' +
81 ' <span class="spacer"></span>' +
82 ' <form action=""><p><input id="raison" name="raison" type="text" size="10" maxlength="200"></input></p></form>' +
83 ' <img id="ban" src="img/ban.gif" alt="Ban de 3 jours" />' +
84 ' <img id="kick" src="img/kick.gif" alt="Ban de 15 min" />' +
85 ' <img id="slap" src="img/slap.gif" alt="Avertissement" />' +
86 '</span>'
87 );
88
89 this.util.infoBulle("Slap", $("#slap", this.util.outilsBan));
90 this.util.infoBulle("Kick (" + euphorik.conf.tempsKick + "min)", $("#kick", this.util.outilsBan));
91 this.util.infoBulle("Ban (" + euphorik.conf.tempsBan / 24 / 60 + " jours)", $("#ban", this.util.outilsBan));
92 this.util.infoBulle("La raison", $("input", this.util.outilsBan));
93 }
94
95 // la barre d'outils liée à chaque message
96 this.util.outilsMessage = $('<div id="outilsMess"><div class="extraire"></div><div class="extraireCompletement"></div></div>').prependTo("#page.minichat");
97 this.util.infoBulle("Cliquer sur les messages pour les enlevers de la liste",
98 $("form#posterMessage #repondA").hover(
99 function() {
100 thisPage.util.afficherBoite(
101 $(".messages", this),
102 $(this),
103 euphorik.Util.positionTypeX.centre,
104 thisPage.client.chatOrder === "reverse" ? euphorik.Util.positionTypeY.bas : euphorik.Util.positionTypeY.haut
105 );
106 },
107 function() { $(".messages", this).hide(); }
108 ).click(
109 function(e) {
110 if ($(e.target).is(".nb")) {
111 thisPage.conversations.enleverMessagesRepond();
112 }
113 }
114 ),
115 euphorik.Util.positionBulleType.droite
116 );
117
118 // <smiles>
119 $("body").append('<div id="smiles"></div>');
120 // affichage des smiles
121 $("#smiles").append(this.formatter.getSmilesHTML()).children().each(
122 function(i) {
123 var opacityBase = $(this).css("opacity");
124 $(this).click(
125 function() {
126 thisPage.util.replaceSelection($("form#posterMessage input.message")[0], thisPage.formatter.smiles[$(this).attr("class")][0].source.replace(/\\/g, ""));
127 }
128 ).hover(
129 function() { $(this).animate({opacity: 1}, 200); },
130 function() { $(this).animate({opacity: opacityBase}, 200); }
131 );
132 }
133 );
134 $("form#posterMessage button.smiles").hover(
135 // affichage de la boite présentant les smiles
136 function(e){ thisPage.util.afficherBoite($("#smiles"), $(e.target), euphorik.Util.positionTypeX.centre, euphorik.Util.positionTypeY.basRecouvrement); },
137 function(){}
138 );
139 $("#smiles").hover(
140 function(){},
141 function() {
142 $("#smiles").hide();
143 }
144 );
145 // </smiles>
146
147 // événements
148 var nouveauMessage =
149 function() {
150 // captcha anti bot
151 if ($("form#posterMessage input.captcha").val() !== "") {
152 return;
153 }
154
155 var message = $("form#posterMessage input.message").val();
156
157 // traitement des commandes..
158 var retCommandes = thisPage.commandes.exec(message);
159 switch (retCommandes[0]) {
160 case euphorik.Commandes.statut.pas_une_commande :
161 thisPage.envoyerMessage(message);
162 break;
163 case euphorik.Commandes.statut.erreur_commande :
164 thisPage.util.messageDialog(retCommandes[1], euphorik.Util.messageType.erreur);
165 break;
166 case euphorik.Commandes.statut.ok :
167 $("form#posterMessage input.message").val("");
168 break;
169 }
170
171 $("form#posterMessage input.message").focus();
172 };
173
174 $("form#posterMessage").keypress(
175 function(e) {
176 if (e.which === 13) { // return
177 nouveauMessage();
178 }
179 }
180 );
181
182 $("form#posterMessage button.return").click(nouveauMessage);
183
184 // interdiction de submiter le formulaire
185 $("form#posterMessage").submit(function(){ return false; });
186
187 $("input.nick").click(
188 function() {
189 var input = $("input.nick")[0];
190 if (input.value === euphorik.conf.defaultNick) {
191 input.value = "";
192 }
193 }
194 );
195 };
196
197 euphorik.PageMinichat.prototype.chargerConversationsFragment = function() {
198 var thisPageMinichat = this;
199
200 // attention : "conv" doit être un tableau d'entier
201 try {
202 var conv = this.fragment.getVal("conv");
203 if (conv) {
204 conv.each(function(i, racine) {
205 thisPageMinichat.client.ajouterConversation(racine)
206 });
207 }
208 } catch(e) {
209 ;; console.log(e)
210 }
211 };
212
213 euphorik.PageMinichat.prototype.decharger = function() {
214 this.conversations.comet.stopAttenteCourante();
215
216 $("body #smiles").remove();
217
218 this.fragment.delVal("conv");
219 };
220
221 /**
222 * Envoie un nouve message donné, le nick utilisé est celui se trouvant
223 * dans la zone de saisie (form#posterMessage input.nick).
224 */
225 euphorik.PageMinichat.prototype.envoyerMessage = function(message) {
226 var thisPageMinichat = this;
227 var nick = $("form#posterMessage input.nick").val();
228
229 // (un nick vide est autorisé)
230 nick = this.formatter.formatNick(nick);
231
232 if (nick === euphorik.conf.defaultNick) {
233 this.util.messageDialog("Choose a nickname");
234 return;
235 }
236
237 message = message.trim();
238 if (!message) {
239 this.util.messageDialog("The message is empty");
240 return;
241 }
242
243 if (!this.client.authentifie()) {
244 if (!this.client.enregistrement()) {
245 this.util.messageDialog("unable to login");
246 return;
247 }
248 }
249
250 // évite le double post
251 if (this.envoieMessageEnCours) {
252 this.util.messageDialog("Message en cours d'envoie...");
253 return;
254 }
255 this.envoieMessageEnCours = true;
256
257 this.client.nick = nick;
258
259 this.communication.requete(
260 "put_message",
261 this.getJSONMessage(this.client.nick, message),
262 function() {
263 $("form#posterMessage input.message").val("");
264 thisPageMinichat.conversations.enleverMessagesRepond();
265 thisPageMinichat.envoieMessageEnCours = false;
266 },
267 function(data) {
268 thisPageMinichat.util.messageDialog(data.error_message);
269 thisPageMinichat.envoieMessageEnCours = false;
270 },
271 true,
272 {
273 error : function() {
274 thisPageMinichat.envoieMessageEnCours = false;
275 }
276 }
277 );
278 };
279
280 euphorik.PageMinichat.prototype.getJSONMessage = function(nick, message) {
281 var repondA = [];
282 objectEach(this.conversations.messagesRepond, function(id) {
283 repondA.push(parseInt(id, 10));
284 });
285
286 return {
287 "cookie" : this.client.cookie,
288 "nick" : nick,
289 "content" : message,
290 "answer_to" : repondA
291 };
292 };