]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/search.js
Twitter: Fetch the contact relation
[friendica-addons.git] / jappixmini / jappix / js / search.js
1 /*
2
3 Jappix - An open social platform
4 These are the seach tools JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 19/03/11
11
12 */
13
14 // Searches in the user's buddy list
15 function processBuddySearch(query) {
16         // No query submitted?
17         if(!query)
18                 return;
19         
20         // Wildcard (*) submitted
21         if(query == '*')
22                 query = '';
23         
24         // Replace forbidden characters in regex
25         query = escapeRegex(query);
26         
27         // Create an empty array
28         var results = new Array();
29         
30         // Search regex
31         var regex = new RegExp('((^)|( ))' + query, 'gi');
32         
33         // Search in the roster
34         var buddies = getAllBuddies();
35         
36         for(i in buddies) {
37                 var xid = buddies[i];
38                 var nick = getBuddyName(xid);
39                 
40                 // Buddy match our search, and not yet in the array
41                 if(nick.match(regex) && !existArrayValue(results, xid))
42                         results.push(xid);
43         }
44         
45         // Return the results array
46         return results;
47 }
48
49 // Resets the buddy search tool
50 function resetBuddySearch(destination) {
51         $(destination + ' ul').remove();
52         $(destination + ' input').removeClass('suggested');
53 }
54
55 // Add the clicked XID to the input
56 function addBuddySearch(destination, xid) {
57         // Remove the search tool
58         resetBuddySearch(destination);
59         
60         // Define a selector
61         var input = $(destination + ' input');
62         var value = input.val();
63         
64         // Get the old value (if there's another value)
65         var old = '';
66         
67         if(value.match(/(^(.+)(,)(\s)?)(\w+)$/))
68                 old = RegExp.$1;
69         
70         // Add the XID to the "to" input and focus on it
71         $(document).oneTime(10, function() {
72                 input.val(old + xid).focus();
73         });
74         
75         return false;
76 }
77
78 // Creates the appropriate markup for the search results
79 function createBuddySearch(destination) {
80         // Reset the search engine
81         resetBuddySearch(destination);
82         
83         // Get the entered value
84         var value = $(destination + ' input').val();
85         
86         // Separation with a comma?
87         if(value.match(/^(.+)((,)(\s)?)(\w+)$/))
88                 value = RegExp.$5;
89         
90         // Get the result array
91         var entered = processBuddySearch(value);
92         
93         // Display each result (if any)
94         if(entered && entered.length) {
95                 // Set a special class to the search input
96                 $(destination + ' input').addClass('suggested');
97                 
98                 // Append each found buddy in the container 
99                 var regex = new RegExp('((^)|( ))' + value, 'gi');
100                 
101                 // Initialize the code generation
102                 var code = '<ul>';
103                 
104                 for(b in entered) {
105                         // Get some values from the XID
106                         var current = getBuddyName(entered[b]).htmlEnc();
107                         current = current.replace(regex, '<b>$&</b>');
108                         
109                         // Add the current element to the global code
110                         code += '<li onclick="return addBuddySearch(\'' + encodeOnclick(destination) + '\', \'' + encodeOnclick(entered[b]) + '\');" data-xid="' + encodeQuotes(entered[b]) + '">' + current + '</li>';
111                 }
112                 
113                 // Finish the code generation
114                 code += '</ul>';
115                 
116                 // Creates the code in the DOM
117                 $(destination).append(code);
118                 
119                 // Put the hover on the first element
120                 $(destination + ' ul li:first').addClass('hovered');
121                 
122                 // Hover event, to not to remove this onblur and loose the click event
123                 $(destination + ' ul li').hover(function() {
124                         $(destination + ' ul li').removeClass('hovered');
125                         $(this).addClass('hovered');
126                         
127                         // Add a marker for the blur event
128                         $(destination + ' ul').attr('mouse-hover', 'true');
129                 }, function() {
130                         $(this).removeClass('hovered');
131                         
132                         // Remove the mouse over marker
133                         $(destination + ' ul').removeAttr('mouse-hover');
134                 });
135         }
136 }
137
138 // Handles the keyboard arrows press when searching
139 function arrowsBuddySearch(e, destination) {
140         // Down arrow: 40
141         // Up arrown: 38
142         
143         // Initialize
144         var code = e.keyCode;
145         
146         // Not the key we want here
147         if((code != 40) && (code != 38))
148                 return;
149         
150         // Remove the eventual mouse hover marker
151         $(destination + ' ul').removeAttr('mouse-hover');
152         
153         // Create the path & get its size
154         var path = destination + ' ul li';
155         var pSize = $(path).size();
156         
157         // Define the i value
158         var i = 0;
159         
160         // Switching yet launched
161         if(exists(path + '.hovered')) {
162                 var index = $(path).attr('data-hovered');
163                 
164                 if(index)
165                         i = parseInt(index);
166                 
167                 if(code == 40)
168                         i++;
169                 else
170                         i--;
171         }
172         
173         else if(code == 38)
174                 i = pSize - 1;
175         
176         // We must not override the maximum i limit
177         if(i >= pSize)
178                 i = 0;
179         
180         // We must not have negative i
181         else if(i < 0)
182                 i = pSize - 1;
183         
184         // Modify the list
185         $(path + '.hovered').removeClass('hovered');
186         $(path).eq(i).addClass('hovered');
187         
188         // Store the i index
189         $(path).attr('data-hovered', i);
190         
191         return false;
192 }
193
194 // Filters the buddies in the roster
195 var SEARCH_FILTERED = false;
196
197 function goFilterBuddySearch(vFilter) {
198         // Put a marker
199         SEARCH_FILTERED = true;
200         
201         // Show the buddies that match the search string
202         var rFilter = processBuddySearch(vFilter);
203         
204         // Hide all the buddies
205         $('#buddy-list .buddy').hide();
206         
207         // Only show the buddies which match the search
208         for(i in rFilter) {
209                 // Choose the correct selector for this search
210                 if(!BLIST_ALL)
211                         $('#buddy-list .buddy[data-xid=' + escape(rFilter[i]) + ']:not(.hidden-buddy)').show();
212                 else
213                         $('#buddy-list .buddy[data-xid=' + escape(rFilter[i]) + ']').show();
214         }
215 }
216
217 // Resets the buddy filtering in the roster
218 function resetFilterBuddySearch() {
219         // Remove the marker
220         SEARCH_FILTERED = false;
221         
222         // Show all the buddies
223         $('#buddy-list .buddy').show();
224         
225         // Only show available buddies
226         if(!BLIST_ALL)
227                 $('#buddy-list .buddy.hidden-buddy').hide();
228         
229         // Update the groups
230         updateGroups();
231 }
232
233 // Funnels the buddy filtering
234 function funnelFilterBuddySearch(keycode) {
235         // Get the input value
236         var input = $('#buddy-list .filter input');
237         var cancel = $('#buddy-list .filter a');
238         var value = input.val();
239         
240         // Security: reset all the groups, empty or not, deployed or not
241         $('#buddy-list .one-group, #buddy-list .group-buddies').show();
242         $('#buddy-list .group span').text('-');
243         
244         // Nothing is entered, or escape pressed
245         if(!value || (keycode == 27)) {
246                 if(keycode == 27)
247                         input.val('');
248                 
249                 resetFilterBuddySearch();
250                 cancel.hide();
251         }
252         
253         // Process the filtering
254         else {
255                 cancel.show();
256                 goFilterBuddySearch(value);
257         }
258         
259         // Update the groups
260         updateGroups();
261 }
262
263 // Searches for the nearest element (with a lower stamp than the current one)
264 function sortElementByStamp(stamp, element) {
265         var array = new Array();
266         var i = 0;
267         var nearest = 0;
268         
269         // Add the stamp values to the array
270         $(element).each(function() {
271                 var current_stamp = parseInt($(this).attr('data-stamp'));
272                 
273                 // Push it!
274                 array.push(current_stamp);
275         });
276         
277         // Sort the array
278         array.sort();
279         
280         // Get the nearest stamp value
281         while(stamp > array[i]) {
282                 nearest = array[i];
283                 
284                 i++;
285         }
286         
287         return nearest;
288 }