summaryrefslogtreecommitdiff
path: root/src/client/view/completer.js
blob: c75ee93e4e05dfc0709519b391bf72ceae2fb61d (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
define(
['jquery', 'Bacon'],
function($, Bacon) {

function unquoted(name)
{
    var start = 0,
        end = name.length;

    if (name.length >= 1) {
        if (name.charAt(0) == '"') {
            start = 1;
            if (name.length > 1 && name.charAt(name.length - 1) == '"') {
                end = name.length - 1;
            }
        }
        return name.substring(start, end);
    }
    return name;
}

function setCaret(e, num)
{
    e.selectionStart = e.selectionEnd = num;
}

var completer = (function (input_element, dropdown, base_config) {
    var config = get_config(base_config),
        dropdown_raw = dropdown[0],
        options_bus = new Bacon.Bus(),
        options = [],
        selected_index = -1,
        input_element_raw = input_element[0],
        completion_start = 0,
        completion_end = 0,
        minimum_length = 1;

    // turn off the browser's autocomplete
    input_element.attr('autocomplete', 'off');

    //$('.ui-autocomplete').css('width', '10px');
    options_bus.onValue(function update_options(new_options) {
        options = new_options;
    });

    input_element.keyup(function(e) {
        var ret = undefined;
        switch (e.keyCode) {
        case 38: //UP
            prev_option();
            ret = false;
            break;
        case 40: //DOWN
            next_option();
            ret = false;
            break;
        case 27: // Escape
            hide();
            ret = false;
            break;
        default:
            // This catches cursor move due to keyboard events. no event for cursor movement itself
            // below we catch cursor moves due to mouse click
            oninput(input_element_raw.value, input_element_raw.selectionStart);
        }
        return ret;
    });
    input_element.keydown(function(e) {
        switch (e.keyCode) {
        case 38:
        case 40:
            return false;
        case 9: // Tab
            if (config.hideOnTab) {
                hide();
            }
            break;
        }
    });

    function get_config(base) {
        return {
            triggerStart: base && base.triggerStart || '#',
            triggerEnd: base && base.triggerEnd || ' ',
            hideOnTab: base && base.hasOwnProperty('hideOnTab') ? base.hideOnTab : true,
        };
    }

    function completions(text)
    {
        var ret = [];

        for (var name in options) {
            if (name.toLowerCase().indexOf(text.toLowerCase()) === 0) {
                ret.push(name);
            }
        }
        return ret;
    }

    function show() {
        if (dropdown.children().length > 0) {
            dropdown.show();
        }
    }
    function hide()
    {
        dropdown.hide();
    }

    /***
     * #this is a #
     *             ^
     *
     * #this is a #t
     *              ^
     *
     * #this and #that then #he
     *             ^
     */
    function oninput(text, cursor) {
        var hash = text.slice(0, cursor).lastIndexOf(config.triggerStart);
        // TODO check if current completion has been invalidated
        _invalidateSelection();
        hide();
        dropdown_raw.innerHTML = ""; // remove all elements
        if (hash == -1 && config.triggerStart != ' ') { // space matches start of string too
            return;
        }
        var space = text.slice(hash + 1).indexOf(config.triggerEnd);
        space = space == -1 ? text.length : space;
        if (space < cursor) {
            return;
        }
        completion_start = hash + 1;
        completion_end = space;
        var string = unquoted(text.slice(completion_start, completion_end));
        if (string.length < minimum_length) {
            return;
        }
        completions(string).forEach(function(name) {
            var suggestion = $('<div class="suggestion-item">' + name + '</div>');
            suggestion.on('click', function(e) {
                _applySuggestion(name);
                input_element.focus();
            });
            dropdown.append(suggestion);
        });
        show();
    }

    function _invalidateSelection() {
        update_highlighting(-1);
    }

    function _move_option(change, default_value) {
        var next,
            n = dropdown.children().length;

        if (n == 0) {
            return;
        }
        show();
        if (selected_index == -1) {
            next = default_value;
        } else {
            next = (selected_index + change) % n;
        }
        update_highlighting(next);
    }
    function next_option() {
        _move_option(1, 0);
    }
    function prev_option() {
        _move_option(dropdown.children().length - 1, dropdown.children().length - 1);
    }
    function _get_option(index) {
        if (dropdown.children().length <= index) {
            console.log('error: dropdown does not contain index ' + index +
                        ', it has ' + dropdown.children().length + ' elements');
            return '';
        }
        var e = dropdown.children()[index],
            s = e.innerText || e.textContent;
        if (s.indexOf(' ') != -1) {
            return '"' + s + '"';
        }
        return s;
    }
    function _choice(i) {
        return dropdown.children().eq(i);
    }
    function update_highlighting(new_index) {
        if (selected_index != -1) {
            _choice(selected_index).removeClass('selected');
        }
        if (new_index != -1) {
            _choice(new_index).addClass('selected');
        }
        selected_index = new_index;
    }
    function _applySuggestion(str) {
        var cur = input_element.val(),
            start = cur.slice(0, completion_start) + str + ' ';
        input_element.val(start + cur.slice(completion_end));
        setCaret(input_element, start.length);
        oninput('', 0);
    }
    function handleEnter() {
        if (selected_index == -1) {
            return false;
        }
        _applySuggestion(_get_option(selected_index));
        return true;
    }

    return {
        options: options_bus,
        oninput: oninput,
        next_option: next_option,
        prev_option: prev_option,
        handleEnter: handleEnter,
    };
});

return completer;

});