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