REPORT de la branche 1.1 : 477->494
[euphorik.git] / js / pageAdmin.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 // La page d'administation, ne peut être accédée que par les ekMaster (admins)
20
21 /*jslint laxbreak:true */
22
23
24 euphorik.PageAdmin = function(client, formateur, util, communication) {
25 this.nom = "admin";
26
27 this.client = client;
28 this.formateur = formateur;
29 this.util = util;
30 this.communication = communication;
31
32 this.comet = new Comet("admin", euphorik.conf.versionProtocole);
33
34 // le timer qui rappelle periodiquement le rafraichissement des IP bannies
35 this.timeoutIDmajIPs = null;
36 };
37
38 /**
39 * Interface des pages.
40 */
41 euphorik.PageAdmin.prototype.contenu = function() {
42 return '<h1>Trolls</h1>' +
43 '<p>Un troll est un sujet à débat, en général une question, affiché sur la page principale.</p>' +
44 '<p>Chaque semaine un troll est choisi au hasard parmis les trolls proposés et devient le troll de la semaine.</p>' +
45 '<form action="" id="nouveauTroll">' +
46 ' <p>' +
47 ' <input class="troll" name="troll" type="text" maxlength="500" value=""></input>' +
48 ' <button class="return" value="return">poster</button>' +
49 ' </p>' +
50 '</form>' +
51 '<div id="trolls"></div>' +
52 '<h1>IPs bannies</h1>' +
53 '<div id="ips"></div>';
54 };
55
56 /**
57 * Interface des pages.
58 */
59 euphorik.PageAdmin.prototype.charger = function() {
60 $("#page form#nouveauTroll").submit(function(){ return false; });
61
62 var thisPage = this;
63
64 // la liste des trolls proposés par les ekMasters
65 this.trolls = new euphorik.Trolls(this.client, this.util, this.formateur, this.communication);
66
67 this.waitEvent();
68
69 this.majIPs();
70
71 $("#page form#nouveauTroll input.troll").focus();
72
73 $("#page form#nouveauTroll button.return").click(
74 function() {
75 thisPage.posterTroll();
76 }
77 );
78 };
79
80 /**
81 * Interface des pages.
82 */
83 euphorik.PageAdmin.prototype.decharger = function() {
84 this.comet.stopAttenteCourante();
85
86 // supprime le rafraichissement période des ips
87 if (this.timeoutIDmajIPs) {
88 clearTimeout(this.timeoutIDmajIPs);
89 }
90 };
91
92 /**
93 * Post un troll, le contenu est lu à partir de "input.troll".
94 */
95 euphorik.PageAdmin.prototype.posterTroll = function() {
96 var thisPageAdmin = this;
97
98 var content = $("#page form#nouveauTroll input.troll").val();
99
100 content = content.trim();
101 if (content === "") {
102 this.util.messageDialogue("Le troll est vide");
103 return;
104 }
105
106 this.communication.requete(
107 "put_troll",
108 {"cookie" : this.client.cookie, "content" : content},
109 function(data) {
110 $("#page form#nouveauTroll input.troll").val("");
111 }
112 );
113 };
114
115 /**
116 * Met à jour la liste des IP bannies.
117 */
118 euphorik.PageAdmin.prototype.majIPs = function() {
119 if (this.timeoutIDmajIPs) {
120 clearTimeout(this.timeoutIDmajIPs);
121 }
122
123 var thisPageAdmin = this;
124
125 this.communication.requete(
126 "list_banned_ips",
127 {"cookie" : this.client.cookie},
128 function(data) {
129 var XHTML = "";
130 data.list.each(function(i, ip) {
131 XHTML += '<div class="ban"><span class="ip">' + ip.ip + '</span>|' +
132 '<span class="temps">' +
133 ip.remaining_time +
134 '</span>|';
135 ip.users.each(function(j, user) {
136 XHTML += (j > 0 ? ", " : "") +
137 '<span class="pseudo">' + thisPageAdmin.formateur.traitementComplet(user.nick) + '</span>' +
138 (user.login === "" ? "" : '<span class="login">(' + thisPageAdmin.formateur.traitementComplet(user.login) + ')</span>');
139 });
140 XHTML += '<span class="deban">débannir</span></div>';
141 });
142
143 if (data.list.length === 0) {
144 XHTML += '<p>Aucune IP bannie</p>';
145 }
146
147 $("#ips").html(XHTML);
148
149 $(".ban").each(function() {
150 var ip = $(".ip", this).html();
151 $(".deban", this).click(
152 function() {
153 thisPageAdmin.util.messageDialogue("Êtes-vous sur de vouloir débannir l'IP ''" + ip + "'' ?", euphorik.Util.messageType.question,
154 {"Oui" : function() {
155 thisPageAdmin.deban(ip);
156 },
157 "Non" : function(){}
158 }
159 );
160 }
161 );
162 });
163
164 // rafraichissement toutes les minutes (je sais c'est mal)
165 // le problème est le rafraichissement des temps restant de bannissement qui doit être fait du coté client
166 thisPageAdmin.timeoutIDmajIPs = setTimeout(function(){ thisPageAdmin.majIPs(); }, 60 * 1000);
167 }
168 );
169 };
170
171 /**
172 * Débannie une ip donnée.
173 */
174 euphorik.PageAdmin.prototype.deban = function(ip) {
175 var thisPageAdmin = this;
176
177 this.communication.requete(
178 "unban",
179 {"cookie" : this.client.cookie, "ip" : ip}
180 );
181 };
182
183 /**
184 * Attente d'événement de la part du serveur.
185 */
186 euphorik.PageAdmin.prototype.waitEvent = function() {
187 var thisPageAdmin = this;
188
189 this.comet.waitEvent(
190 function() { return { "last_troll" : thisPageAdmin.trolls.dernierTroll }; },
191 {
192 "troll_added" : function(data){ thisPageAdmin.trolls.ajouterTrollEvent(data); },
193 "troll_modified" : function(data){ thisPageAdmin.trolls.modifierTrollEvent(data); },
194 "troll_deleted" : function(data){ thisPageAdmin.trolls.supprimerTrollEvent(data); },
195 "banned_ips_refresh" : function(data){ thisPageAdmin.majIPs(); },
196 "error" :
197 function(data) {
198 thisTrolls.util.messageDialogue(data.error_message);
199 }
200 }
201 );
202 };
203
204 ///////////////////////////////////////////////////////////////////////////////////////////////////
205
206 /**
207 * Représente un troll, pas grand chose finalement.
208 */
209 euphorik.Troll = function(content, author) {
210 this.content = content;
211 this.author = author;
212 };
213
214 ///////////////////////////////////////////////////////////////////////////////////////////////////
215
216 euphorik.Trolls = function(client, util, formateur, communication) {
217 this.client = client;
218 this.util = util;
219 this.formateur = formateur;
220 this.communication = communication;
221 this.dernierTroll = 0;
222
223 this.trolls = {};
224 };
225
226 euphorik.Trolls.prototype.ajouterTrollEvent = function(data) {
227 var thisTrolls = this;
228
229 var XHTML = "";
230 data.trolls.each(function(i, trollData) {
231 var troll = new euphorik.Troll(trollData.content, trollData.author);
232 var trollId = trollData.troll_id;
233 thisTrolls.trolls[trollId] = troll;
234
235 XHTML +=
236 '<div id="troll' + trollId + '" class="troll">' +
237 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
238 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
239 (trollData.author_id === thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
240 '</div>';
241 });
242 $("#trolls").append(XHTML);
243 $("#trolls .troll").filter(function() { return parseInt($(this).attr("id").substr(5), 10) > thisTrolls.dernierTroll; }).each(
244 function() {
245 var troll = this;
246 var id = parseInt($(this).attr("id").substr(5), 10);
247
248 $("a[@rel*=lightbox]", this).lightBox();
249
250 $(this).keypress(
251 function(e) {
252 if (e.which === 13) { // return
253 $(".modifier", this).click();
254 }
255 }
256 );
257
258 $(".delTroll", this).click(
259 function() {
260 thisTrolls.util.messageDialogue(
261 "Êtes-vous sur de vouloir supprimer le troll \"" + thisTrolls.trolls[id].content + "\" ?",
262 euphorik.Util.messageType.question,
263 {
264 "oui" : function() {
265 thisTrolls.supprimer(id);
266 },
267 "non" : function(){}
268 }
269 );
270 }
271 );
272
273 $(".editTroll", this).click(
274 function() {
275 $("span", troll).css("display", "none");
276 $(troll).append(
277 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
278 thisTrolls.trolls[id].content +
279 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
280 );
281 $("form input.content").focus();
282
283 var virerLeFormulaire = function() {
284 $('form', troll).remove();
285 $('span', troll).css("display", "inline");
286 };
287 $("span.modifier", troll).click(
288 function() {
289 var content = $("form input.content", troll).val();
290 virerLeFormulaire();
291 thisTrolls.modifier(id, content);
292 }
293 );
294 $("span.annuler", troll).click( virerLeFormulaire );
295 $("form", troll).submit(function(){ return false; });
296 }
297 );
298 }
299 );
300
301 if (data.trolls.length > 0) {
302 thisTrolls.dernierTroll = data.trolls[data.trolls.length - 1].troll_id;
303 }
304 };
305
306 euphorik.Trolls.prototype.modifierTrollEvent = function(data) {
307 var thisTrolls = this;
308 $("#trolls #troll" + data.troll_id + " .content").html(thisTrolls.formateur.traitementComplet(data.content, thisTrolls.trolls[data.troll_id].author));
309 $("#trolls #troll" + data.troll_id + " a[@rel*=lightbox]").lightBox();
310 thisTrolls.trolls[data.troll_id].content = data.content;
311 };
312
313 euphorik.Trolls.prototype.supprimerTrollEvent = function(data) {
314 $("#trolls #troll" + data.troll_id).remove();
315 };
316
317 euphorik.Trolls.prototype.modifier = function(id, content) {
318 this.communication.requete(
319 "mod_troll",
320 {"cookie" : this.client.cookie, "troll_id" : id, "content" : content}
321 );
322 };
323
324 /**
325 * Supprime un troll en fonction de son id.
326 */
327 euphorik.Trolls.prototype.supprimer = function(id) {
328 this.communication.requete(
329 "del_troll",
330 {"cookie" : this.client.cookie, "troll_id" : id}
331 );
332 };