]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/autocompletion.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / autocompletion.js
1 /*
2
3 Jappix - An open social platform
4 These are the autocompletion tools JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 12/11/10
11
12 */
13
14 // Sort an array with insensitivity to the case
15 function caseInsensitiveSort(a, b) { 
16         // Put the two strings into lower case
17         a = a.toLowerCase();
18         b = b.toLowerCase();
19         
20         // Process the sort
21         if(a > b)
22                 return 1;
23         if(a < b)
24                 return -1;
25 }
26
27 // Creates an array with the autocompletion results
28 function processAutocompletion(query, id) {
29         // Replace forbidden characters in regex
30         query = escapeRegex(query);
31         
32         // Create an empty array
33         var results = new Array();
34         
35         // Search in the roster
36         $('#' + id + ' .user').each(function() {
37                 var nick = $(this).find('.name').text();
38                 var regex = new RegExp('(^)' + query, 'gi');
39                 
40                 if(nick.match(regex))
41                         results.push(nick);
42         });
43         
44         // Sort the array
45         results = results.sort(caseInsensitiveSort);
46         
47         // Return the results array
48         return results;
49 }
50
51 // Resets the autocompletion tools
52 function resetAutocompletion(hash) {
53         $('#' + hash + ' .message-area').removeAttr('data-autocompletion-pointer').removeAttr('data-autocompletion-query');
54 }
55
56 // Autocompletes the chat input nick
57 function createAutocompletion(hash) {
58         // Initialize
59         var vSelector = $('#' + hash + ' .message-area');
60         var value = vSelector.val();
61         if(!value)
62                 resetAutocompletion(hash);
63         var query = vSelector.attr('data-autocompletion-query');
64         
65         // The autocompletion has not been yet launched
66         if(query == undefined) {
67                 query = value;
68                 vSelector.attr('data-autocompletion-query', query);
69         }
70         
71         // Get the pointer
72         var pointer = vSelector.attr('data-autocompletion-pointer');
73         var i = 0;
74         
75         if(pointer)
76                 i = parseInt(pointer);
77         
78         // We get the nickname
79         var nick = processAutocompletion(query, hash)[i];
80         
81         // Shit, this is my nick!
82         if((nick != undefined) && (nick.toLowerCase() == getMUCNick(hash).toLowerCase())) {
83                 // Increment
84                 i++;
85                 
86                 // Get the next nick
87                 nick = processAutocompletion(query, hash)[i];
88         }
89         
90         // We quote the nick
91         if(nick != undefined) {
92                 // Increment
93                 i++;
94                 quoteMyNick(hash, nick);
95                 
96                 // Put a pointer
97                 vSelector.attr('data-autocompletion-pointer', i);
98         }
99 }