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