ADD type mime application/xhtml-xml pour tout ce qui n'est pas IE
[euphorik.git] / js / pageMinichat / 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, formateur, util) {
22 this.nom = "minichat";
23
24 this.client = client;
25 this.formateur = formateur;
26 this.util = util;
27 this.commandes = new euphorik.Commandes(this.client);
28
29 // permet d'éviter d'envoyer plusieurs messages simultanément en pressant
30 // rapidement sur "enter" par exemple
31 this.envoieMessageEnCours = false;
32
33 this.regexMessageTagMatch = /\{.*?\}>/g;
34 this.regexMessageTagReplace = /^(.*?\{.*?\}>)*/;
35 };
36
37 euphorik.PageMinichat.prototype.contenu = function() {
38 // le fait que tout soit collé est fait exprès, permet d'éviter d'avoir des espaces supplémentaires entre les spans'
39 var formulaireXHTML = '' +
40 '<form method="post" action="" id ="posterMessage">' +
41 ' <p>' +
42 ' <input class="captcha" name="captcha" type="text" size="8" maxlength="8"></input>' +
43 ' <input class="pseudo" name="pseudo" type="text" maxlength="50" value="' + encodeURI(euphorik.conf.nickDefaut) + '"></input>' +
44 ' <span id="repondA"><span class="nb">0</span><span class="messages"></span></span>' +
45 ' <input class="message" name="message" type="text" maxlength="500" value=""></input>' +
46 ' <button class="smiles"></button>' +
47 ' <button class="return"></button>' +
48 ' </p>' +
49 '</form>';
50
51 var trollXHTML = '<div id="trollCourant">Troll de la semaine : <span class="troll"></span></div>';
52 var conversationXHTML = '<table id="conversations"><tr></tr></table>';
53
54 if (this.client.chatOrder === "reverse") {
55 return trollXHTML + formulaireXHTML + conversationXHTML;
56 } else {
57 return trollXHTML + conversationXHTML + formulaireXHTML;
58 }
59 };
60
61 euphorik.PageMinichat.prototype.classes = function() {
62 return this.client.chatOrder === "reverse" ? "orderReverse" : "orderChrono";
63 };
64
65 euphorik.PageMinichat.prototype.charger = function() {
66 thisPage = this;
67
68 $("#posterMessage input.pseudo").val(this.client.pseudo);
69
70 // cet appel ne doit pas être fait avant l'appel à 'charger'
71 this.conversations = new euphorik.Conversations(this.client, this.formateur, this.util, this.fragment);
72
73 this.chargerConversationsFragment();
74
75 this.conversations.rafraichirMessages(true);
76
77 this.util.setCaretToEnd($("form#posterMessage input.message")[0]);
78
79 // les outils de bannissement (uniquement pour les ekMaster)
80 if (this.client.ekMaster) {
81 this.util.outilsBan = $(
82 '<span id="outilsBan">' +
83 ' <span class="spacer"></span>' +
84 ' <form action=""><p><input id="raison" name="raison" type="text" size="10" maxlength="200"></input></p></form>' +
85 ' <img id="ban" src="img/ban.gif" alt="Ban de 3 jours" />' +
86 ' <img id="kick" src="img/kick.gif" alt="Ban de 15min" />' +
87 ' <img id="slap" src="img/slap.gif" alt="Avertissement" />' +
88 '</span>'
89 );
90
91 this.util.infoBulle("Slap", $("#slap", this.util.outilsBan));
92 this.util.infoBulle("Kick (" + euphorik.conf.tempsKick + "min)", $("#kick", this.util.outilsBan));
93 this.util.infoBulle("Ban (" + euphorik.conf.tempsBan / 24 / 60 + " jours)", $("#ban", this.util.outilsBan));
94 this.util.infoBulle("La raison", $("input", this.util.outilsBan));
95 }
96
97 this.util.infoBulle("Ouvrir la conversation liée au troll de la semaine", $("#trollCourant .troll"));
98
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.formateur.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.formateur.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($("form#posterMessage input.pseudo").val(), message);
164 break;
165 case euphorik.Commandes.statut.erreur_commande :
166 thisPage.util.messageDialogue(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.pseudo").click(
190 function() {
191 var input = $("input.pseudo")[0];
192 if (input.value === euphorik.conf.pseudoDefaut) {
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 euphorik.PageMinichat.prototype.getJSONMessage = function(pseudo, message) {
225 var repondA = [];
226 objectEach(this.conversations.messagesRepond, function(id) {
227 repondA.push(parseInt(id, 10));
228 });
229
230 return {
231 "header" : { "action" : "put_message", "version" : euphorik.conf.versionProtocole },
232 "cookie" : this.client.cookie,
233 "nick" : pseudo,
234 "content" : message,
235 "answer_to" : repondA
236 };
237 };
238
239 euphorik.PageMinichat.prototype.envoyerMessage = function(pseudo, message) {
240 var thisPageMinichat = this;
241
242 // (un pseudo vide est autorisé)
243 pseudo = this.formateur.filtrerInputPseudo(pseudo);
244
245 if (pseudo === euphorik.conf.nickDefaut) {
246 this.util.messageDialogue("Le pseudo ne peut pas être " + euphorik.conf.nickDefaut);
247 return;
248 }
249
250 message = message.trim();
251 if (!message) {
252 this.util.messageDialogue("Le message est vide");
253 return;
254 }
255
256 if (!this.client.authentifie()) {
257 if (!this.client.enregistrement()) {
258 this.util.messageDialogue("login impossible");
259 return;
260 }
261 }
262
263 this.client.pseudo = pseudo;
264
265 // évite le double post
266 if (this.envoieMessageEnCours) {
267 this.util.messageDialogue("Message en cours d'envoie...");
268 return;
269 }
270 this.envoieMessageEnCours = true;
271
272 jQuery.ajax({
273 url : "request",
274 type: "POST",
275 data : this.util.jsonVersAction(this.getJSONMessage(pseudo, message)),
276 dataType : "json",
277 beforeSend : function(xmlHttpRequest) {
278 // TODO : ça marche ça ??
279 xmlHttpRequest.setRequestHeader("X-Requested-With", "");
280 },
281 success : function(data, textStatus) {
282 if(data.reply === "ok") {
283 // TODO : revoir cette partie
284 // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ?
285 objectEach(thisPageMinichat.conversations.messagesRepond, function(messId) {
286 thisPageMinichat.conversations.conversations.each(function(i, conv) {
287 var mess = conv.messagesParId[messId];
288 if (mess) {
289 mess.clientARepondu = true;
290 $("#conversations #" + mess.getId(conv.getId())).addClass("repondu")
291 }
292 });
293 });
294
295 $("form#posterMessage input.message").val("");
296 thisPageMinichat.conversations.enleverMessagesRepond();
297 } else if (data.reply === "error") {
298 thisPageMinichat.util.messageDialogue(data.error_message);
299 }
300 thisPageMinichat.envoieMessageEnCours = false;
301 },
302 error : function() {
303 thisPageMinichat.envoieMessageEnCours = false;
304 }
305 });
306 };