]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/mucadmin.js
jappixmini: include jappix source
[friendica-addons.git] / jappixmini / jappix / js / mucadmin.js
1 /*
2
3 Jappix - An open social platform
4 These are the mucadmin JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Authors: Vanaryon, Maranda
10 Last revision: 03/03/11
11
12 */
13
14 // Opens the MUC admin popup
15 function openMucAdmin(xid, aff) {
16         // Popup HTML content
17         var html_full = 
18         '<div class="top">' + _e("MUC administration") + '</div>' + 
19         
20         '<div class="content">' + 
21                 '<div class="head mucadmin-head">' + 
22                         '<div class="head-text mucadmin-head-text">' + _e("You administrate this room") + '</div>' + 
23                         
24                         '<div class="mucadmin-head-jid">' + xid + '</div>' + 
25                 '</div>' + 
26                 
27                 '<div class="mucadmin-forms">' + 
28                         '<div class="mucadmin-topic">' + 
29                                 '<fieldset>' + 
30                                         '<legend>' + _e("Subject") + '</legend>' + 
31                                         
32                                         '<label for="topic-text">' + _e("Enter new subject") + '</label>' + 
33                                         '<textarea id="topic-text" name="room-topic" rows="8" cols="60" ></textarea>' + 
34                                 '</fieldset>' + 
35                         '</div>' + 
36                         
37                         '<div class="mucadmin-conf">' + 
38                                 '<fieldset>' + 
39                                         '<legend>' + _e("Configuration") + '</legend>' + 
40                                         
41                                         '<div class="results mucadmin-results"></div>' + 
42                                 '</fieldset>' + 
43                         '</div>' + 
44                         
45                         '<div class="mucadmin-aut">' + 
46                                 '<fieldset>' + 
47                                         '<legend>' + _e("Authorizations") + '</legend>' + 
48                                         
49                                         '<label>' + _e("Member list") + '</label>' + 
50                                         '<div class="aut-member aut-group">' + 
51                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'member\');">' + _e("Add an input") + '</a>' + 
52                                         '</div>' + 
53                                         
54                                         '<label>' + _e("Owner list") + '</label>' + 
55                                         '<div class="aut-owner aut-group">' + 
56                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'owner\');">' + _e("Add an input") + '</a>' + 
57                                         '</div>' + 
58                                         
59                                         '<label>' + _e("Administrator list") + '</label>' + 
60                                         '<div class="aut-admin aut-group">' + 
61                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'admin\');">' + _e("Add an input") + '</a>' + 
62                                         '</div>' + 
63                                         
64                                         '<label>' + _e("Outcast list") + '</label>' + 
65                                         '<div class="aut-outcast aut-group">' + 
66                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'outcast\');">' + _e("Add an input") + '</a>' + 
67                                         '</div>' + 
68                                 '</fieldset>' + 
69                         '</div>' + 
70                         
71                         '<div class="mucadmin-others">' + 
72                                 '<fieldset>' + 
73                                         '<legend>' + _e("Others") + '</legend>' + 
74                                         
75                                         '<label>' + _e("Destroy this MUC") + '</label>' + 
76                                         '<a href="#" onclick="return destroyMucAdmin();">' + _e("Yes, let's do it!") + '</a>' + 
77                                 '</fieldset>' + 
78                         '</div>' + 
79                 '</div>' + 
80         '</div>' + 
81         
82         '<div class="bottom">' + 
83                 '<div class="wait wait-medium"></div>' + 
84                 
85                 '<a href="#" class="finish save">' + _e("Save") + '</a>' + 
86                 '<a href="#" class="finish cancel">' + _e("Cancel") + '</a>' + 
87         '</div>';
88         
89         var html_partial = 
90         '<div class="top">' + _e("MUC administration") + '</div>' + 
91         
92         '<div class="content">' + 
93                 '<div class="head mucadmin-head">' + 
94                         '<div class="head-text mucadmin-head-text">' + _e("You administrate this room") + '</div>' + 
95                         
96                         '<div class="mucadmin-head-jid">' + xid + '</div>' + 
97                 '</div>' + 
98                 
99                 '<div class="mucadmin-forms">' + 
100                         '<div class="mucadmin-aut">' + 
101                                 '<fieldset>' + 
102                                         '<legend>' + _e("Authorizations") + '</legend>' + 
103                                         
104                                         '<label>' + _e("Member list") + '</label>' + 
105                                         '<div class="aut-member aut-group">' + 
106                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'member\');">' + _e("Add an input") + '</a>' + 
107                                         '</div>' + 
108                                         
109                                         '<label>' + _e("Outcast list") + '</label>' + 
110                                         '<div class="aut-outcast aut-group">' + 
111                                                 '<a href="#" class="aut-add" onclick="return addInputMucAdmin(\'\', \'outcast\');">' + _e("Add an input") + '</a>' + 
112                                         '</div>' + 
113                                 '</fieldset>' + 
114                         '</div>' + 
115                 '</div>' + 
116         '</div>' + 
117         
118         '<div class="bottom">' + 
119                 '<div class="wait wait-medium"></div>' + 
120                 
121                 '<a href="#" class="finish save">' + _e("Save") + '</a>' + 
122                 '<a href="#" class="finish cancel">' + _e("Cancel") + '</a>' + 
123         '</div>';       
124         
125         // Create the popup
126         if(aff == 'owner')
127                 createPopup('mucadmin', html_full);
128         if(aff == 'admin')
129                 createPopup('mucadmin', html_partial);
130         
131         // Associate the events
132         launchMucAdmin();
133                 
134         // We get the affiliated user's privileges
135         if(aff == 'owner') {
136                 queryMucAdmin(xid, 'member');
137                 queryMucAdmin(xid, 'owner');
138                 queryMucAdmin(xid, 'admin');
139                 queryMucAdmin(xid, 'outcast');
140                 // We query the room to edit
141                 dataForm(xid, 'muc', '', '', 'mucadmin');
142         } else if(aff == 'admin') {
143                 queryMucAdmin(xid, 'member');
144                 queryMucAdmin(xid, 'outcast');
145         }
146 }
147
148 // Closes the MUC admin popup
149 function closeMucAdmin() {
150         // Destroy the popup
151         destroyPopup('mucadmin');
152         
153         return false;
154 }
155
156 // Removes a MUC admin input
157 function removeInputMucAdmin(element) {
158         var path = $(element).parent();
159         
160         // We first hide the container of the input
161         path.hide();
162         
163         // Then, we add a special class to the input
164         path.find('input').addClass('aut-dustbin');
165         
166         return false;
167 }
168
169 // Adds a MUC admin input
170 function addInputMucAdmin(xid, affiliation) {
171         var hash = hex_md5(xid + affiliation);
172         
173         // Add the HTML code
174         $('#mucadmin .aut-' + affiliation + ' .aut-add').after(
175                 '<div class="one-aut ' + hash + '">' + 
176                         '<input id="aut-' + affiliation + '" name="' + affiliation + '" type="text" class="mucadmin-i" value="' + xid + '" />' + 
177                         '<a href="#" class="aut-remove">[-]</a>' + 
178                 '</div>'
179         );
180         
181         // Click event
182         $('#mucadmin .' + hash + ' .aut-remove').click(function() {
183                 return removeInputMucAdmin(this);
184         });
185         
186         // Focus on the input we added
187         if(!xid)
188                 $(document).oneTime(10, function() {
189                         $('#mucadmin .' + hash + ' input').focus();
190                 });
191         
192         return false;
193 }
194
195 // Handles the MUC admin form
196 function handleMucAdminAuth(iq) {
197         // We got the authorizations results
198         $(iq.getQuery()).find('item').each(function() {
199                 // We parse the received xml
200                 var xid = $(this).attr('jid');
201                 var affiliation = $(this).attr('affiliation');
202                 
203                 // We create one input for one XID
204                 addInputMucAdmin(xid, affiliation);
205         });
206         
207         // Hide the wait icon
208         $('#mucadmin .wait').hide();
209         
210         logThis('MUC admin items received: ' + fullXID(getStanzaFrom(iq)));
211 }
212
213 // Queries the MUC admin form
214 function queryMucAdmin(xid, type) {
215         // Show the wait icon
216         $('#mucadmin .wait').show();
217         
218         // New IQ
219         var iq = new JSJaCIQ();
220         
221         iq.setTo(xid);
222         iq.setType('get');
223         
224         var iqQuery = iq.setQuery(NS_MUC_ADMIN);
225         iqQuery.appendChild(iq.buildNode('item', {'affiliation': type, 'xmlns': NS_MUC_ADMIN}));
226         
227         con.send(iq, handleMucAdminAuth);
228 }
229
230 // Sends the new chat-room topic
231 function sendMucAdminTopic(xid) {
232         // We get the new topic
233         var topic = $('.mucadmin-topic textarea').val();
234         
235         // We send the new topic if not blank
236         if(topic) {
237                 var m = new JSJaCMessage();
238                 m.setTo(xid);
239                 m.setType('groupchat');
240                 m.setSubject(topic);
241                 con.send(m);
242                 
243                 logThis('MUC admin topic sent: ' + topic, 3);
244         }
245 }
246
247 // Sends the MUC admin auth form
248 function sendMucAdminAuth(xid) {
249         // We define the values array
250         var types = new Array('member', 'owner', 'admin', 'outcast');
251
252         for(i in types) {
253                 // We get the current type
254                 var tType = types[i];
255                 
256                 // We loop for all the elements
257                 $('.mucadmin-aut .aut-' + tType + ' input').each(function() {
258                         // We set the iq headers
259                         var iq = new JSJaCIQ();
260                         iq.setTo(xid);
261                         iq.setType('set');
262
263                         var iqQuery = iq.setQuery(NS_MUC_ADMIN);
264         
265                         // We get the needed values
266                         var value = $(this).val();
267                         
268                         // If there's a value
269                         if(value)
270                                 var item = iqQuery.appendChild(iq.buildNode('item', {'jid': value, 'xmlns': NS_MUC_ADMIN}));
271                         
272                         // It the user had removed the XID
273                         if($(this).hasClass('aut-dustbin') && value)
274                                 item.setAttribute('affiliation', 'none');
275                         
276                         // If the value is not blank and okay
277                         else if(value)
278                                 item.setAttribute('affiliation', tType);
279         
280                         // We send the iq !
281                         con.send(iq, handleErrorReply);
282                 });
283         }       
284         
285         logThis('MUC admin authorizations form sent: ' + xid, 3);
286 }
287
288 // Checks if the MUC room was destroyed
289 function handleDestroyMucAdminIQ(iq) {
290         if(!handleErrorReply(iq)) {
291                 // We close the groupchat
292                 var room = fullXID(getStanzaFrom(iq));
293                 var hash = hex_md5(room);
294                 quitThisChat(room, hash, 'groupchat');
295                 
296                 // We close the muc admin popup
297                 closeMucAdmin();
298                 
299                 // We tell the user that all is okay
300                 openThisInfo(5);
301                 
302                 // We remove the user's favorite
303                 if(existDB('favorites', room))
304                         removeThisFavorite(room, explodeThis('@', room, 0));
305                 
306                 logThis('MUC admin destroyed: ' + room, 3);
307         }
308         
309         // We hide the wait icon
310         $('#mucadmin .wait').hide();
311 }
312
313 // Destroys a MUC room
314 function destroyMucAdminIQ(xid) {
315         // We ask the server to delete the room
316         var iq = new JSJaCIQ();
317         
318         iq.setTo(xid);
319         iq.setType('set');
320         var iqQuery = iq.setQuery(NS_MUC_OWNER);
321         iqQuery.appendChild(iq.buildNode('destroy', {'xmlns': NS_MUC_OWNER}));
322         
323         con.send(iq, handleDestroyMucAdminIQ);
324         
325         logThis('MUC admin destroy sent: ' + xid, 3);
326         
327         return false;
328 }
329
330 // Performs the MUC room destroy functions
331 function destroyMucAdmin() {
332         // We get the XID of the current room
333         var xid = $('#mucadmin .mucadmin-head-jid').text();
334         
335         // We show the wait icon
336         $('#mucadmin .wait').show();
337         
338         // We send the iq
339         destroyMucAdminIQ(xid);
340 }
341
342 // Sends all the MUC admin stuffs
343 function sendMucAdmin() {
344         // We get the XID of the current room
345         var xid = $('#mucadmin .mucadmin-head-jid').text();
346         
347         // We change the room topic
348         sendMucAdminTopic(xid);
349         
350         // We send the needed queries
351         sendDataForm('x', 'submit', 'submit', $('#mucadmin .mucadmin-results').attr('data-session'), xid, '', '', 'mucadmin');
352         sendMucAdminAuth(xid);
353 }
354
355 // Saves the MUC admin elements
356 function saveMucAdmin() {
357         // We send the new options
358         sendMucAdmin();
359         
360         // And we quit the popup
361         return closeMucAdmin();
362 }
363
364 // Plugin launcher
365 function launchMucAdmin() {
366         // Click events
367         $('#mucadmin .bottom .finish').click(function() {
368                 if($(this).is('.cancel'))
369                         return closeMucAdmin();
370                 if($(this).is('.save'))
371                         return saveMucAdmin();
372         });
373 }