ADD affichage des ip bannies sur la page d'admin (pas fini, manque bouton "debannir")
[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
12 PageAdmin.prototype.contenu = function()
13 {
14 return '<h1>Trolls</h1>\
15 <p>Chaque semaine un troll est choisit au hasard parmis les trolls proposés et devient le troll de la semaine.</p>\
16 <form action="" id="nouveauTroll">\
17 <p>\
18 <input class="troll" name="troll" type="text" maxlength="500" value=""></input>\
19 <button class="return" value="return">poster</button>\
20 </p>\
21 </form>\
22 <div id="trolls"></div>\
23 <h1>IPs bannies</h1>\
24 <div id="ips"></div>'
25 }
26
27 PageAdmin.prototype.charger = function()
28 {
29 $("#page form#nouveauTroll").submit(function(){return false})
30
31 var thisPage = this
32
33 this.trolls = new Trolls(this.client, this.util, this.formateur)
34 this.trolls.rafraichirTrolls()
35
36 this.majIPs()
37
38 $("#page form#nouveauTroll input.troll").focus()
39
40 $("#page form#nouveauTroll button.return").click(
41 function()
42 {
43 thisPage.posterTroll()
44 }
45 )
46 }
47
48 PageAdmin.prototype.decharger = function()
49 {
50 this.trolls.pageEvent.stopAttenteCourante()
51 }
52
53 PageAdmin.prototype.posterTroll = function()
54 {
55 var thisPageAdmin = this
56
57 var content = $("#page form#nouveauTroll input.troll").val()
58
59 content = content.trim()
60 if (content == "")
61 {
62 this.util.messageDialogue("Le troll est vide")
63 return
64 }
65
66 var dataToSend =
67 {
68 "action" : "put_troll",
69 "cookie" : this.client.cookie,
70 "content" : content
71 }
72
73 ;;; dumpObj(dataToSend)
74 jQuery.ajax(
75 {
76 type: "POST",
77 url: "request",
78 dataType: "json",
79 data: this.util.jsonVersAction(dataToSend),
80 success:
81 function(data)
82 {
83 ;;; dumpObj(data)
84
85 if (data["reply"] == "ok")
86 {
87 $("#page form#nouveauTroll input.troll").val("")
88 }
89 else if (data["reply"] == "error")
90 {
91 thisPageAdmin.util.messageDialogue(data["error_message"])
92 }
93 }
94 }
95 )
96 }
97
98
99 /**
100 * Met à jour la liste des IP bannies.
101 */
102 PageAdmin.prototype.majIPs = function()
103 {
104 var thisPageAdmin = this
105
106 var dataToSend =
107 {
108 "action" : "list_banned_ips",
109 "cookie" : this.client.cookie
110 }
111
112 ;;; dumpObj(dataToSend)
113 jQuery.ajax(
114 {
115 type: "POST",
116 url: "request",
117 dataType: "json",
118 data: this.util.jsonVersAction(dataToSend),
119 success:
120 function(data)
121 {
122 ;;; dumpObj(data)
123
124 if (data["reply"] == "list_banned_ips")
125 {
126 var XHTML = ""
127 for(var i = 0; i < data["list"].length; i++)
128 {
129 var ip = data["list"][i]
130 XHTML += '<span class="ip">' + ip["ip"] + '</span>' +
131 '<span class="temps">' +
132 ip["remaining_time"] +
133 '</span>['
134 for(var j = 0; j < ip["users"].length; j++)
135 {
136 var user = ip["users"][j]
137 XHTML += (j > 0 ? ", " : "") +
138 '<span class="pseudo">' + thisPageAdmin.formateur.traitementComplet(user["nick"]) + '</span>' +
139 (user["login"] == "" ? "" : '<span class="login">(' + thisPageAdmin.formateur.traitementComplet(user["login"]) + ')</span>')
140 }
141 XHTML += ']'
142 }
143 $("#ips").html(XHTML)
144 }
145 else if (data["reply"] == "error")
146 {
147 thisPageAdmin.util.messageDialogue(data["error_message"])
148 }
149 }
150 }
151 )
152 }
153
154 ///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
157 function Troll(content, author)
158 {
159 this.content = content
160 this.author = author
161 }
162
163
164 ///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
167 function Trolls(client, util, formateur)
168 {
169 this.client = client
170 this.util = util
171 this.formateur = formateur
172 this.dernierTroll = 0
173 this.pageEvent = new PageEvent("admin", this.util)
174
175 this.trolls = {}
176 }
177
178
179 Trolls.prototype.modifier = function(id, content)
180 {
181 var thisTrolls = this
182
183 var dataToSend =
184 {
185 "action" : "mod_troll",
186 "cookie" : this.client.cookie,
187 "troll_id" : id,
188 "content" : content
189 }
190
191 ;;; dumpObj(dataToSend)
192 jQuery.ajax(
193 {
194 type: "POST",
195 url: "request",
196 dataType: "json",
197 data: this.util.jsonVersAction(dataToSend),
198 success:
199 function(data)
200 {
201 ;;; dumpObj(data)
202 if (data["reply"] == "error")
203 {
204 thisTrolls.util.messageDialogue(data["error_message"])
205 }
206 }
207 }
208 )
209 }
210
211 /**
212 * Supprime un troll en fonction de son id.
213 */
214 Trolls.prototype.supprimer = function(id)
215 {
216 var thisTrolls = this
217
218 var dataToSend =
219 {
220 "action" : "del_troll",
221 "cookie" : this.client.cookie,
222 "troll_id" : id
223 }
224
225 ;;; dumpObj(dataToSend)
226 jQuery.ajax(
227 {
228 type: "POST",
229 url: "request",
230 dataType: "json",
231 data: this.util.jsonVersAction(dataToSend),
232 success:
233 function(data)
234 {
235 ;;; dumpObj(data)
236 if (data["reply"] == "error")
237 {
238 thisTrolls.util.messageDialogue(data["error_message"])
239 }
240 }
241 }
242 )
243 }
244
245 Trolls.prototype.rafraichirTrolls = function()
246 {
247 var thisTrolls = this
248
249 this.pageEvent.waitEvent(
250 function() { return { "last_troll" : thisTrolls.dernierTroll }},
251 function(data)
252 {
253 switch (data["reply"])
254 {
255 case "troll_added" :
256 var XHTML = ""
257 for (var i = 0; i < data["trolls"].length; i++)
258 {
259 var troll = new Troll(data["trolls"][i]["content"], data["trolls"][i]["author"])
260 var trollId = data["trolls"][i]["troll_id"]
261 thisTrolls.trolls[trollId] = troll
262
263 XHTML +=
264 '<div id="troll' + trollId + '" class="troll">' +
265 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
266 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
267 (data["trolls"][i]["author_id"] == thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
268 '</div>'
269 }
270 $("#trolls").append(XHTML)
271 $("#trolls .troll").filter(function(){return parseInt($(this).attr("id").substr(5)) > thisTrolls.dernierTroll}).each(
272 function()
273 {
274 var troll = this
275 var id = parseInt($(this).attr("id").substr(5))
276
277 $("a[@rel*=lightbox]", this).lightBox()
278
279 $(this).keypress(
280 function(e)
281 {
282 if (e.which == 13) // return
283 $(".modifier", this).click()
284 }
285 )
286 $(".delTroll", this).click(
287 function()
288 {
289 thisTrolls.util.messageDialogue(
290 "Êtes-vous sur de vouloir supprimer le troll \"" + $("#trolls .troll .content").html() + "\" ?",
291 messageType.question,
292 {
293 "oui" : function()
294 {
295 thisTrolls.supprimer(id)
296 },
297 "non" : function(){}
298 }
299 )
300 }
301 )
302 $(".editTroll", this).click(
303 function()
304 {
305 $("span", troll).css("display", "none")
306 $(troll).append(
307 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
308 thisTrolls.trolls[id].content +
309 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
310 )
311 $("form input.content").focus()
312
313 var virerLeFormulaire = function()
314 {
315 $("form", troll).remove()
316 $('span', troll).css("display", "inline")
317 }
318 $("span.modifier", troll).click(
319 function()
320 {
321 var content = $("form input.content", troll).val()
322 virerLeFormulaire()
323 thisTrolls.modifier(id, content)
324 }
325 )
326 $("span.annuler", troll).click( virerLeFormulaire )
327 $("form", troll).submit(function(){ return false})
328 }
329 )
330 }
331 )
332
333 if (data["trolls"].length > 0)
334 thisTrolls.dernierTroll = data["trolls"][data["trolls"].length - 1]["troll_id"]
335 break
336 case "troll_modified" :
337 $("#trolls #troll" + data["troll_id"] + " .content").html(thisTrolls.formateur.traitementComplet(data["content"], thisTrolls.trolls[data["troll_id"]].author))
338 $("#trolls #troll" + data["troll_id"] + " a[@rel*=lightbox]").lightBox()
339 thisTrolls.trolls[data["troll_id"]].content = data["content"]
340 break
341 case "troll_deleted" :
342 $("#trolls #troll"+data["troll_id"]).remove()
343 break
344 case "error" :
345 thisTrolls.util.messageDialogue(data["error_message"])
346 break
347 }
348 }
349 )
350 }