2 // Copyright 2008 Grégory Burri
4 // This file is part of Euphorik.
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.
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.
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/>.
19 /*jslint laxbreak:true */
21 euphorik
.PageMinichat = function(client
, formateur
, util
) {
22 this.nom
= "minichat";
25 this.formateur
= formateur
;
27 this.commandes
= new euphorik
.Commandes(this.client
);
29 // permet d'éviter d'envoyer plusieurs messages simultanément en pressant
30 // rapidement sur "enter" par exemple
31 this.envoieMessageEnCours
= false;
33 this.regexMessageTagMatch
= /\{.*?\}>/g;
34 this.regexMessageTagReplace
= /^(.*?\{.*?\}>)*/;
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">' +
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>' +
51 var trollXHTML
= '<div id="trollCourant">Troll de la semaine : <span class="troll"></span></div>';
52 var conversationXHTML
= '<table id="conversations"><tr></tr></table>';
54 if (this.client
.chatOrder
=== "reverse") {
55 return trollXHTML
+ formulaireXHTML
+ conversationXHTML
;
57 return trollXHTML
+ conversationXHTML
+ formulaireXHTML
;
61 euphorik
.PageMinichat
.prototype.classes = function() {
62 return this.client
.chatOrder
=== "reverse" ? "orderReverse" : "orderChrono";
65 euphorik
.PageMinichat
.prototype.charger = function() {
68 $("#posterMessage input.pseudo").val(this.client
.pseudo
);
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
);
73 this.chargerConversationsFragment();
75 this.conversations
.rafraichirMessages(true);
77 this.util
.setCaretToEnd($("form#posterMessage input.message")[0]);
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" />' +
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
));
97 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(
102 thisPage
.util
.afficherBoite(
103 $(".messages", this),
105 euphorik
.Util
.positionTypeX
.centre
,
106 thisPage
.client
.chatOrder
=== "reverse" ? euphorik
.Util
.positionTypeY
.bas : euphorik
.Util
.positionTypeY
.haut
109 function() { $(".messages", this).hide(); }
112 if ($(e
.target
).is(".nb")) {
113 thisPage
.conversations
.enleverMessagesRepond();
117 euphorik
.Util
.positionBulleType
.droite
121 $("body").append('<div id="smiles"></div>');
122 // affichage des smiles
123 $("#smiles").append(this.formateur
.getSmilesHTML()).children().each(
125 var opacityBase
= $(this).css("opacity");
128 thisPage
.util
.replaceSelection($("form#posterMessage input.message")[0], thisPage
.formateur
.smiles
[$(this).attr("class")][0].source
.replace(/\\/g
, ""));
131 function() { $(this).animate({opacity: 1}, 200); },
132 function() { $(this).animate({opacity: opacityBase
}, 200); }
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
); },
153 if ($("form#posterMessage input.captcha").val() !== "") {
157 var message
= $("form#posterMessage input.message").val();
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
);
165 case euphorik
.Commandes
.statut
.erreur_commande :
166 thisPage
.util
.messageDialogue(retCommandes
[1], euphorik
.Util
.messageType
.erreur
);
168 case euphorik
.Commandes
.statut
.ok :
169 $("form#posterMessage input.message").val("");
173 $("form#posterMessage input.message").focus();
176 $("form#posterMessage").keypress(
178 if (e
.which
=== 13) { // return
184 $("form#posterMessage button.return").click(nouveauMessage
);
186 // interdiction de submiter le formulaire
187 $("form#posterMessage").submit(function(){ return false; });
189 $("input.pseudo").click(
191 var input
= $("input.pseudo")[0];
192 if (input
.value
=== euphorik
.conf
.pseudoDefaut
) {
199 euphorik
.PageMinichat
.prototype.chargerConversationsFragment = function() {
200 var thisPageMinichat
= this;
202 // attention : "conv" doit être un tableau d'entier
204 var conv
= this.fragment
.getVal("conv");
206 conv
.each(function(i
, racine
) {
207 thisPageMinichat
.client
.ajouterConversation(racine
)
215 euphorik
.PageMinichat
.prototype.decharger = function() {
216 this.conversations
.comet
.stopAttenteCourante();
218 $("body #smiles").remove();
220 this.fragment
.delVal("conv")
224 euphorik
.PageMinichat
.prototype.getJSONMessage = function(pseudo
, message
) {
226 objectEach(this.conversations
.messagesRepond
, function(id
) {
227 repondA
.push(parseInt(id
, 10));
231 "header" : { "action" : "put_message", "version" : euphorik
.conf
.versionProtocole
},
232 "cookie" : this.client
.cookie
,
235 "answer_to" : repondA
239 euphorik
.PageMinichat
.prototype.envoyerMessage = function(pseudo
, message
) {
240 var thisPageMinichat
= this;
242 // (un pseudo vide est autorisé)
243 pseudo
= this.formateur
.filtrerInputPseudo(pseudo
);
245 if (pseudo
=== euphorik
.conf
.nickDefaut
) {
246 this.util
.messageDialogue("Le pseudo ne peut pas être " + euphorik
.conf
.nickDefaut
);
250 message
= message
.trim();
252 this.util
.messageDialogue("Le message est vide");
256 if (!this.client
.authentifie()) {
257 if (!this.client
.enregistrement()) {
258 this.util
.messageDialogue("login impossible");
263 this.client
.pseudo
= pseudo
;
265 // évite le double post
266 if (this.envoieMessageEnCours
) {
267 this.util
.messageDialogue("Message en cours d'envoie...");
270 this.envoieMessageEnCours
= true;
275 data : this.util
.jsonVersAction(this.getJSONMessage(pseudo
, message
)),
277 beforeSend : function(xmlHttpRequest
) {
278 // TODO : ça marche ça ??
279 xmlHttpRequest
.setRequestHeader("X-Requested-With", "");
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
];
289 mess
.clientARepondu
= true;
290 $("#conversations #" + mess
.getId(conv
.getId())).addClass("repondu")
295 $("form#posterMessage input.message").val("");
296 thisPageMinichat
.conversations
.enleverMessagesRepond();
297 } else if (data
.reply
=== "error") {
298 thisPageMinichat
.util
.messageDialogue(data
.error_message
);
300 thisPageMinichat
.envoieMessageEnCours
= false;
303 thisPageMinichat
.envoieMessageEnCours
= false;