]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/rosterx.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / rosterx.js
1 /*
2
3 Jappix - An open social platform
4 These are the Roster Item Exchange JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 23/06/11
11
12 */
13
14 // Opens the welcome tools
15 function openRosterX(data) {
16         // Popup HTML content
17         var html = 
18         '<div class="top">' + _e("Suggested friends") + '</div>' + 
19         
20         '<div class="content">' + 
21                 '<div class="rosterx-head">' + 
22                         '<a href="#" class="uncheck">' + _e("Uncheck all") + '</a>' + 
23                         '<a href="#" class="check">' + _e("Check all") + '</a>' + 
24                 '</div>' + 
25                 
26                 '<div class="results"></div>' + 
27         '</div>' + 
28         
29         '<div class="bottom">' + 
30                 '<a href="#" class="finish save">' + _e("Save") + '</a>' + 
31                 '<a href="#" class="finish cancel">' + _e("Cancel") + '</a>' + 
32         '</div>';
33         
34         // Create the popup
35         createPopup('rosterx', html);
36         
37         // Associate the events
38         launchRosterX();
39         
40         // Parse the data
41         parseRosterX(data);
42         
43         logThis('Roster Item Exchange popup opened.');
44 }
45
46 // Closes the welcome tools
47 function closeRosterX() {
48         // Destroy the popup
49         destroyPopup('rosterx');
50         
51         return false;
52 }
53
54 // Parses a rosterx query
55 function parseRosterX(data) {
56         // Main selector
57         var x = $(data).find('x[xmlns=' + NS_ROSTERX + ']:first');
58         
59         // Parse data
60         x.find('item').each(function() {
61                 // Generate group XML
62                 var group = '';
63                 
64                 $(this).find('group').each(function() {
65                         group += '<group>' + $(this).text().htmlEnc() + '</group>';
66                 });
67                 
68                 if(group)
69                         group = '<groups>' + group + '</groups>';
70                 
71                 // Display it!
72                 displayRosterX($(this).attr('jid'), $(this).attr('name'), group, $(this).attr('action'));
73         });
74         
75         // Click to check/uncheck
76         $('#rosterx .oneresult').click(function(evt) {
77                 // No need to apply when click on input
78                 if($(evt.target).is('input[type=checkbox]'))
79                         return;
80                 
81                 // Input selector
82                 var checkbox = $(this).find('input[type=checkbox]');
83                 
84                 // Check or uncheck?
85                 if(checkbox.filter(':checked').size())
86                         checkbox.removeAttr('checked');
87                 else
88                         checkbox.attr('checked', true);
89         });
90 }
91
92 // Displays a rosterx item
93 function displayRosterX(xid, nick, group, action) {
94         // End if no XID
95         if(!xid)
96                 return false;
97         
98         // Set up a default action if no one
99         if(!action || (action != 'modify') || (action != 'delete'))
100                 action = 'add';
101         
102         // Override "undefined" for nickname
103         if(!nick)
104                 nick = '';
105         
106         // Display it
107         $('#rosterx .results').append(
108                 '<div class="oneresult">' + 
109                         '<input type="checkbox" checked="" data-name="' + encodeQuotes(nick) + '" data-xid="' + encodeQuotes(xid) + '" data-action="' + encodeQuotes(action) + '" data-group="' + encodeQuotes(group) + '" />' + 
110                         '<span class="name">' + nick.htmlEnc() + '</span>' + 
111                         '<span class="xid">' + xid.htmlEnc() + '</span>' + 
112                         '<span class="action ' + action + ' talk-images"></span>' + 
113                 '</div>'
114         );
115 }
116
117 // Saves the rosterx settings
118 function saveRosterX() {
119         // Send the requests
120         $('#rosterx .results input[type=checkbox]').filter(':checked').each(function() {
121                 // Read the attributes
122                 var nick = $(this).attr('data-name');
123                 var xid = $(this).attr('data-xid');
124                 var action = $(this).attr('data-action');
125                 var group = $(this).attr('data-group');
126                 
127                 // Parse groups XML
128                 if(group) {
129                         var group_arr = []
130                         
131                         $(group).find('group').each(function() {
132                                 group_arr.push($(this).text().revertHtmlEnc());
133                         });
134                 }
135                 
136                 // Process the asked action
137                 var roster_item = $('#buddy-list .' + hex_md5(xid));
138                 
139                 switch(action) {
140                         // Buddy add
141                         case 'add':
142                                 if(!exists(roster_item)) {
143                                         sendSubscribe(xid, 'subscribe');
144                                         sendRoster(xid, '', nick, group_arr);
145                                 }
146                                 
147                                 break;
148                         
149                         // Buddy edit
150                         case 'modify':
151                                 if(exists(roster_item))
152                                         sendRoster(xid, '', nick, group_arr);
153                                 
154                                 break;
155                         
156                         // Buddy delete
157                         case 'delete':
158                                 if(exists(roster_item))
159                                         sendRoster(xid, 'remove');
160                                 
161                                 break;
162                 }
163         });
164         
165         // Close the popup
166         closeRosterX();
167 }
168
169 // Addon launcher
170 function launchRosterX() {
171         // Click events
172         $('#rosterx .bottom .finish').click(function() {
173                 if($(this).is('.save'))
174                         return saveRosterX();
175                 if($(this).is('.cancel'))
176                         return closeRosterX();
177         });
178         
179         $('#rosterx .rosterx-head a').click(function() {
180                 if($(this).is('.check'))
181                         $('#rosterx .results input[type=checkbox]').attr('checked', true);
182                 else if($(this).is('.uncheck'))
183                         $('#rosterx .results input[type=checkbox]').removeAttr('checked');
184                 
185                 return false;
186         });
187 }