Welcome to mirror list, hosted at ThFree Co, Russian Federation.

autocompletion.js « javascripts « app - github.com/jappix/jappix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78f48b50457d8a91f13662aac14506020a9e63fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*

Jappix - An open social platform
These are the autocompletion tools JS script for Jappix

-------------------------------------------------

License: AGPL
Author: Valérian Saliou

*/

// Bundle
var Autocompletion = (function () {

    /**
     * Alias of this
     * @private
     */
    var self = {};


    /**
     * Sort an autocompletion result array with insensitivity to the case,
     * using the 1st elements (a[0] and b[0]) to process comparison
     * @public
     * @param {array} a
     * @param {array} b
     * @return {undefined}
     */
    self.caseInsensitiveSort = function(a, b) {

        try {
            // Put the two strings into lower case
            var sort_a = a[0].toLowerCase();
            var sort_b = b[0].toLowerCase();

            // Process the sort
            if(sort_a > sort_b) {
                return 1;
            }

            if(sort_a < sort_b) {
                return -1;
            }
        } catch(e) {
            Console.error('Autocompletion.caseInsensitiveSort', e);
        }

    };


    /**
     * Split a query into its subqueries ready to be used in autocompletion
     * @public
     * @param {string} query
     * @return {object}
     */
    self.getSubQueries = function(query) {

        var result = [];

        try {
            var subqueries = [];
            var remnants = [];

            var query_last_char_pos = query.length - 1;
            var space_counter = 0;
            var cur_char;

            for(var i = query_last_char_pos; i >= 0; i--) {
                // Search from the end of the query
                cur_char = query.charAt(i);

                if(space_counter === 0 && cur_char.search(/\s/) === 0) {
                    // The first "local" space was found
                    // Add the subquery and its remnant to results
                    subqueries.push(query.slice(i+1));
                    remnants.push(query.slice(0, i+1));

                    space_counter++;
                } else {
                    space_counter = 0;
                }
            }

            if(space_counter === 0) {
                // If the first char of the query is not a space, add the full query to results
                subqueries.push(query);
                remnants.push('');
            }

            result = [subqueries, remnants];
        } catch(e) {
            Console.error('Autocompletion.getSubQueries', e);
        } finally {
            return result;
        }

    };


    /**
     * Creates an array with the autocompletion results. An autocompletion result
     * is an array containing the result himself and the rank of the query which
     * matched this answer
     * @public
     * @param {Array} query
     * @param {string} id
     * @return {Array}
     */
    self.process = function(query, id) {

        var results = [];

        try {
            // Replace forbidden characters in regex
            query = Common.escapeRegex(query);

            // Build an array of regex to use
            var query_reg_exp = [];

            for(i = 0; i < query.length; i++) {
                if(query[i] !== null) {
                    query_reg_exp.push(
                        new RegExp('(^)' + query[i], 'gi')
                    );
                }
            }

            // Search in the roster
            var nick, regex;

            $('#' + id + ' .user').each(function() {
                nick = $(this).find('.name').text();

                for(i = 0; i < query_reg_exp.length; i++) {
                    regex = query_reg_exp[i];

                    if(nick.match(regex)) {
                        results.push([nick, i]);
                    }
                }
            });

            // Sort the array
            results = results.sort(
                self.caseInsensitiveSort
            );
        } catch(e) {
            Console.error('Autocompletion.process', e);
        } finally {
            return results;
        }

    };


    /**
     * Resets the autocompletion tools
     * @public
     * @param {string} hash
     * @return {undefined}
     */
    self.reset = function(hash) {

        try {
            $('#' + hash + ' .message-area').removeAttr('data-autocompletion-pointer')
                                            .removeAttr('data-autocompletion-query');
        } catch(e) {
            Console.error('Autocompletion.reset', e);
        }

    };


    /**
     * Autocompletes the chat input nick
     * @public
     * @param {string} hash
     * @return {undefined}
     */
    self.create = function(hash) {

       try {
            // Initialize
            var message_area_sel = $('#' + hash + ' .message-area');
            var value = message_area_sel.val();

            if(!value) {
                self.reset(hash);
            }

            var query = message_area_sel.attr('data-autocompletion-query');

            if(query === undefined) {
                // The autocompletion has not been yet launched
                query = self.getSubQueries(value);
                message_area_sel.attr('data-autocompletion-query', JSON.stringify(query));
            } else {
                // The autocompletion has already stored a query
                query = JSON.parse(query);
            }

            // Get the pointer
            var pointer = message_area_sel.attr('data-autocompletion-pointer');
            var i = pointer ? parseInt(pointer, 10) : 0;

            // We get the nickname
            var nick_result = self.process(query[0], hash)[i];
            var nick;

            if(nick_result !== undefined) {
                nick = nick_result[0];
            }

            // Shit, this is my nick!
            if((nick !== undefined) && (nick.toLowerCase() == Name.getMUCNick(hash).toLowerCase())) {
                // Increment
                i++;

                // Get the next nick
                nick_result = self.process(query[0], hash)[i];

                if (nick_result !== undefined) {
                    nick = nick_result[0];
                }
            }

            // We quote the nick
            if((nick_result !== undefined) && (nick !== undefined)) {
                // Increment
                i++;

                Utils.quoteMyNick(
                    hash,
                    nick,
                    query[1][nick_result[1]]
                );

                // Put a pointer
                message_area_sel.attr('data-autocompletion-pointer', i);
            }
        } catch(e) {
            Console.error('Autocompletion.create', e);
        }

    };


    /**
     * Return class scope
     */
    return self;

})();