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