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