db0a42c9d0c37911a5a70d84f9b06d2fbbae5b0e
[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 id="nouveauTroll"><p>\
17 <input class="troll" name="troll" type="text" maxlength="500" value=""></input>\
18 <button class="return" value="return">poster</button>\
19 </p></form><div id="trolls"></div>'
20 }
21
22 PageAdmin.prototype.charger = function()
23 {
24 $("#page form#nouveauTroll").submit(function(){return false})
25
26 var thisPage = this
27
28 this.trolls = new Trolls(this.client, this.util, this.formateur)
29 this.trolls.rafraichirTrolls()
30
31 $("#page form#nouveauTroll input.troll").focus()
32
33 $("#page form#nouveauTroll button.return").click(
34 function()
35 {
36 thisPage.posterTroll()
37 }
38 )
39 }
40
41 PageAdmin.prototype.decharger = function()
42 {
43 this.trolls.pageEvent.stopAttenteCourante()
44 }
45
46 PageAdmin.prototype.posterTroll = function()
47 {
48 var thisPageAdmin = this
49
50 var content = $("#page form#nouveauTroll input.troll").val()
51
52 content = content.trim()
53 if (content == "")
54 {
55 this.util.messageDialogue("Le troll est vide")
56 return
57 }
58
59 var dataToSend =
60 {
61 "action" : "put_troll",
62 "cookie" : this.client.cookie,
63 "content" : content
64 }
65
66 ;;; dumpObj(dataToSend)
67 jQuery.ajax(
68 {
69 type: "POST",
70 url: "request",
71 dataType: "json",
72 data: this.util.jsonVersAction(dataToSend),
73 success:
74 function(data)
75 {
76 ;;; dumpObj(data)
77
78 if (data["reply"] == "ok")
79 {
80 $("#page form#nouveauTroll input.troll").val("")
81 }
82 else if (data["reply"] == "error")
83 {
84 thisPageAdmin.util.messageDialogue(data["error_message"])
85 }
86 }
87 }
88 )
89 }
90
91
92 ///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
95 function Troll(content, author)
96 {
97 this.content = content
98 this.author = author
99 }
100
101
102 ///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
105 function Trolls(client, util, formateur)
106 {
107 this.client = client
108 this.util = util
109 this.formateur = formateur
110 this.dernierTroll = 0
111 this.pageEvent = new PageEvent("admin", this.util)
112
113 this.trolls = {}
114 }
115
116
117 Trolls.prototype.modifier = function(id, content)
118 {
119 var dataToSend =
120 {
121 "action" : "mod_troll",
122 "cookie" : this.client.cookie,
123 "troll_id" : id,
124 "content" : content
125 }
126
127 ;;; dumpObj(dataToSend)
128 jQuery.ajax(
129 {
130 type: "POST",
131 url: "request",
132 dataType: "json",
133 data: this.util.jsonVersAction(dataToSend),
134 success:
135 function(data)
136 {
137 ;;; dumpObj(data)
138 if (data["reply"] == "error")
139 {
140 thisPageAdmin.util.messageDialogue(data["error_message"])
141 }
142 }
143 }
144 )
145 }
146
147 /**
148 * Supprime un troll en fonction de son id.
149 */
150 Trolls.prototype.supprimer = function(id)
151 {
152 var dataToSend =
153 {
154 "action" : "del_troll",
155 "cookie" : this.client.cookie,
156 "troll_id" : id
157 }
158
159 ;;; dumpObj(dataToSend)
160 jQuery.ajax(
161 {
162 type: "POST",
163 url: "request",
164 dataType: "json",
165 data: this.util.jsonVersAction(dataToSend),
166 success:
167 function(data)
168 {
169 ;;; dumpObj(data)
170 if (data["reply"] == "error")
171 {
172 thisPageAdmin.util.messageDialogue(data["error_message"])
173 }
174 }
175 }
176 )
177 }
178
179 Trolls.prototype.rafraichirTrolls = function()
180 {
181 var thisTrolls = this
182
183 this.pageEvent.waitEvent(
184 function() { return { "last_troll" : thisTrolls.dernierTroll }},
185 function(data)
186 {
187 switch (data["reply"])
188 {
189 case "troll_added" :
190 var XHTML = ""
191 for (var i = 0; i < data["trolls"].length; i++)
192 {
193 var troll = new Troll(data["trolls"][i]["content"], data["trolls"][i]["author"])
194 var trollId = data["trolls"][i]["troll_id"]
195 thisTrolls.trolls[trollId] = troll
196
197 XHTML +=
198 '<div id="troll' + trollId + '" class="troll">' +
199 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
200 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
201 (data["trolls"][i]["author_id"] == thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
202 '</div>'
203 }
204 $("#trolls").append(XHTML)
205 $("#trolls .troll").filter(function(){return parseInt($(this).attr("id").substr(5)) > thisTrolls.dernierTroll}).each(
206 function()
207 {
208 var troll = this
209 var id = parseInt($(this).attr("id").substr(5))
210
211 $("a[@rel*=lightbox]", this).lightBox()
212
213 $(this).keypress(
214 function(e)
215 {
216 if (e.which == 13) // return
217 $(".modifier", this).click()
218 }
219 )
220 $(".delTroll", this).click(
221 function()
222 {
223 thisTrolls.util.messageDialogue(
224 "Êtes-vous sur de vouloir supprimer le troll \"" + $("#trolls .troll .content").html() + "\" ?",
225 messageType.question,
226 {
227 "oui" : function()
228 {
229 thisTrolls.supprimer(id)
230 },
231 "non" : function(){}
232 }
233 )
234 }
235 )
236 $(".editTroll", this).click(
237 function()
238 {
239 $("span", troll).css("display", "none")
240 $(troll).append(
241 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
242 thisTrolls.trolls[id].content +
243 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
244 )
245 $("form input.content").focus()
246
247 var virerLeFormulaire = function()
248 {
249 $("form", troll).remove()
250 $('span', troll).css("display", "inline")
251 }
252 $("span.modifier", troll).click(
253 function()
254 {
255 var content = $("form input.content", troll).val()
256 virerLeFormulaire()
257 thisTrolls.modifier(id, content)
258 }
259 )
260 $("span.annuler", troll).click( virerLeFormulaire )
261 $("form", troll).submit(function(){ return false})
262 }
263 )
264 }
265 )
266
267 if (data["trolls"].length > 0)
268 thisTrolls.dernierTroll = data["trolls"][data["trolls"].length - 1]["troll_id"]
269 break
270 case "troll_modified" :
271 $("#trolls #troll" + data["troll_id"] + " .content").html(thisTrolls.formateur.traitementComplet(data["content"], thisTrolls.trolls[data["troll_id"]].author))
272 $("#trolls #troll" + data["troll_id"] + " a[@rel*=lightbox]").lightBox()
273 thisTrolls.trolls[data["troll_id"]].content = data["content"]
274 break
275 case "troll_deleted" :
276 $("#trolls #troll"+data["troll_id"]).remove()
277 break
278 }
279 }
280 )
281 }