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