MOD légère amélioration de code HTML pour plus de respect des standards
[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"><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 thisTrolls = this
120
121 var dataToSend =
122 {
123 "action" : "mod_troll",
124 "cookie" : this.client.cookie,
125 "troll_id" : id,
126 "content" : content
127 }
128
129 ;;; dumpObj(dataToSend)
130 jQuery.ajax(
131 {
132 type: "POST",
133 url: "request",
134 dataType: "json",
135 data: this.util.jsonVersAction(dataToSend),
136 success:
137 function(data)
138 {
139 ;;; dumpObj(data)
140 if (data["reply"] == "error")
141 {
142 thisTrolls.util.messageDialogue(data["error_message"])
143 }
144 }
145 }
146 )
147 }
148
149 /**
150 * Supprime un troll en fonction de son id.
151 */
152 Trolls.prototype.supprimer = function(id)
153 {
154 var thisTrolls = this
155
156 var dataToSend =
157 {
158 "action" : "del_troll",
159 "cookie" : this.client.cookie,
160 "troll_id" : id
161 }
162
163 ;;; dumpObj(dataToSend)
164 jQuery.ajax(
165 {
166 type: "POST",
167 url: "request",
168 dataType: "json",
169 data: this.util.jsonVersAction(dataToSend),
170 success:
171 function(data)
172 {
173 ;;; dumpObj(data)
174 if (data["reply"] == "error")
175 {
176 thisTrolls.util.messageDialogue(data["error_message"])
177 }
178 }
179 }
180 )
181 }
182
183 Trolls.prototype.rafraichirTrolls = function()
184 {
185 var thisTrolls = this
186
187 this.pageEvent.waitEvent(
188 function() { return { "last_troll" : thisTrolls.dernierTroll }},
189 function(data)
190 {
191 switch (data["reply"])
192 {
193 case "troll_added" :
194 var XHTML = ""
195 for (var i = 0; i < data["trolls"].length; i++)
196 {
197 var troll = new Troll(data["trolls"][i]["content"], data["trolls"][i]["author"])
198 var trollId = data["trolls"][i]["troll_id"]
199 thisTrolls.trolls[trollId] = troll
200
201 XHTML +=
202 '<div id="troll' + trollId + '" class="troll">' +
203 '<span class="content">' + thisTrolls.formateur.traitementComplet(troll.content, troll.author) + '</span>' +
204 '<span class="author"> - ' + thisTrolls.formateur.traitementComplet(troll.author) + '</span>' +
205 (data["trolls"][i]["author_id"] == thisTrolls.client.id ? '<span class="editTroll">éditer</span><span class="delTroll">Supprimer</span>' : '') +
206 '</div>'
207 }
208 $("#trolls").append(XHTML)
209 $("#trolls .troll").filter(function(){return parseInt($(this).attr("id").substr(5)) > thisTrolls.dernierTroll}).each(
210 function()
211 {
212 var troll = this
213 var id = parseInt($(this).attr("id").substr(5))
214
215 $("a[@rel*=lightbox]", this).lightBox()
216
217 $(this).keypress(
218 function(e)
219 {
220 if (e.which == 13) // return
221 $(".modifier", this).click()
222 }
223 )
224 $(".delTroll", this).click(
225 function()
226 {
227 thisTrolls.util.messageDialogue(
228 "Êtes-vous sur de vouloir supprimer le troll \"" + $("#trolls .troll .content").html() + "\" ?",
229 messageType.question,
230 {
231 "oui" : function()
232 {
233 thisTrolls.supprimer(id)
234 },
235 "non" : function(){}
236 }
237 )
238 }
239 )
240 $(".editTroll", this).click(
241 function()
242 {
243 $("span", troll).css("display", "none")
244 $(troll).append(
245 '<form><p><input class="content" type="text" size="50" maxlength="500" value="' +
246 thisTrolls.trolls[id].content +
247 '"></input><span class="modifier">modifier</span><span class="annuler">annuler</span></p></form>'
248 )
249 $("form input.content").focus()
250
251 var virerLeFormulaire = function()
252 {
253 $("form", troll).remove()
254 $('span', troll).css("display", "inline")
255 }
256 $("span.modifier", troll).click(
257 function()
258 {
259 var content = $("form input.content", troll).val()
260 virerLeFormulaire()
261 thisTrolls.modifier(id, content)
262 }
263 )
264 $("span.annuler", troll).click( virerLeFormulaire )
265 $("form", troll).submit(function(){ return false})
266 }
267 )
268 }
269 )
270
271 if (data["trolls"].length > 0)
272 thisTrolls.dernierTroll = data["trolls"][data["trolls"].length - 1]["troll_id"]
273 break
274 case "troll_modified" :
275 $("#trolls #troll" + data["troll_id"] + " .content").html(thisTrolls.formateur.traitementComplet(data["content"], thisTrolls.trolls[data["troll_id"]].author))
276 $("#trolls #troll" + data["troll_id"] + " a[@rel*=lightbox]").lightBox()
277 thisTrolls.trolls[data["troll_id"]].content = data["content"]
278 break
279 case "troll_deleted" :
280 $("#trolls #troll"+data["troll_id"]).remove()
281 break
282 case "error" :
283 thisTrolls.util.messageDialogue(data["error_message"])
284 break
285 }
286 }
287 )
288 }