3 Jappix - An open social platform
4 These are the seach tools JS script for Jappix
6 -------------------------------------------------
10 Last revision: 19/03/11
14 // Searches in the user's buddy list
15 function processBuddySearch(query) {
16 // No query submitted?
20 // Wildcard (*) submitted
24 // Replace forbidden characters in regex
25 query = escapeRegex(query);
27 // Create an empty array
28 var results = new Array();
31 var regex = new RegExp('((^)|( ))' + query, 'gi');
33 // Search in the roster
34 var buddies = getAllBuddies();
38 var nick = getBuddyName(xid);
40 // Buddy match our search, and not yet in the array
41 if(nick.match(regex) && !existArrayValue(results, xid))
45 // Return the results array
49 // Resets the buddy search tool
50 function resetBuddySearch(destination) {
51 $(destination + ' ul').remove();
52 $(destination + ' input').removeClass('suggested');
55 // Add the clicked XID to the input
56 function addBuddySearch(destination, xid) {
57 // Remove the search tool
58 resetBuddySearch(destination);
61 var input = $(destination + ' input');
62 var value = input.val();
64 // Get the old value (if there's another value)
67 if(value.match(/(^(.+)(,)(\s)?)(\w+)$/))
70 // Add the XID to the "to" input and focus on it
71 $(document).oneTime(10, function() {
72 input.val(old + xid).focus();
78 // Creates the appropriate markup for the search results
79 function createBuddySearch(destination) {
80 // Reset the search engine
81 resetBuddySearch(destination);
83 // Get the entered value
84 var value = $(destination + ' input').val();
86 // Separation with a comma?
87 if(value.match(/^(.+)((,)(\s)?)(\w+)$/))
90 // Get the result array
91 var entered = processBuddySearch(value);
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');
98 // Append each found buddy in the container
99 var regex = new RegExp('((^)|( ))' + value, 'gi');
101 // Initialize the code generation
105 // Get some values from the XID
106 var current = getBuddyName(entered[b]).htmlEnc();
107 current = current.replace(regex, '<b>$&</b>');
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>';
113 // Finish the code generation
116 // Creates the code in the DOM
117 $(destination).append(code);
119 // Put the hover on the first element
120 $(destination + ' ul li:first').addClass('hovered');
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');
127 // Add a marker for the blur event
128 $(destination + ' ul').attr('mouse-hover', 'true');
130 $(this).removeClass('hovered');
132 // Remove the mouse over marker
133 $(destination + ' ul').removeAttr('mouse-hover');
138 // Handles the keyboard arrows press when searching
139 function arrowsBuddySearch(e, destination) {
144 var code = e.keyCode;
146 // Not the key we want here
147 if((code != 40) && (code != 38))
150 // Remove the eventual mouse hover marker
151 $(destination + ' ul').removeAttr('mouse-hover');
153 // Create the path & get its size
154 var path = destination + ' ul li';
155 var pSize = $(path).size();
157 // Define the i value
160 // Switching yet launched
161 if(exists(path + '.hovered')) {
162 var index = $(path).attr('data-hovered');
176 // We must not override the maximum i limit
180 // We must not have negative i
185 $(path + '.hovered').removeClass('hovered');
186 $(path).eq(i).addClass('hovered');
189 $(path).attr('data-hovered', i);
194 // Filters the buddies in the roster
195 var SEARCH_FILTERED = false;
197 function goFilterBuddySearch(vFilter) {
199 SEARCH_FILTERED = true;
201 // Show the buddies that match the search string
202 var rFilter = processBuddySearch(vFilter);
204 // Hide all the buddies
205 $('#buddy-list .buddy').hide();
207 // Only show the buddies which match the search
209 // Choose the correct selector for this search
211 $('#buddy-list .buddy[data-xid=' + escape(rFilter[i]) + ']:not(.hidden-buddy)').show();
213 $('#buddy-list .buddy[data-xid=' + escape(rFilter[i]) + ']').show();
217 // Resets the buddy filtering in the roster
218 function resetFilterBuddySearch() {
220 SEARCH_FILTERED = false;
222 // Show all the buddies
223 $('#buddy-list .buddy').show();
225 // Only show available buddies
227 $('#buddy-list .buddy.hidden-buddy').hide();
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();
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('-');
244 // Nothing is entered, or escape pressed
245 if(!value || (keycode == 27)) {
249 resetFilterBuddySearch();
253 // Process the filtering
256 goFilterBuddySearch(value);
263 // Searches for the nearest element (with a lower stamp than the current one)
264 function sortElementByStamp(stamp, element) {
265 var array = new Array();
269 // Add the stamp values to the array
270 $(element).each(function() {
271 var current_stamp = parseInt($(this).attr('data-stamp'));
274 array.push(current_stamp);
280 // Get the nearest stamp value
281 while(stamp > array[i]) {