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