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