diff options
| author | Alon Levy <alon@pobox.com> | 2014-12-14 14:25:53 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-12-14 14:25:53 +0200 |
| commit | 67e37f60078e9a09dddbbef7d8f3629163ca1677 (patch) | |
| tree | 52e8babbde06521777fd2671410a0eddeebdd5e4 /src/view | |
| parent | e39f2ef7715751ee53e3b3620281af7addf4a5ea (diff) | |
completer: small refactor of clicks
Diffstat (limited to 'src/view')
| -rw-r--r-- | src/view/completer.js | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/view/completer.js b/src/view/completer.js index 113c11a3..a973f571 100644 --- a/src/view/completer.js +++ b/src/view/completer.js @@ -139,13 +139,13 @@ var completer = (function (input_element, dropdown, base_config) { if (string.length < minimum_length) { return; } - // [hash] [cursor] [space/end] completions(string).forEach(function(name) { - dropdown.append($('<div class="suggestion-item">' + name + '</div>')); - }); - dropdown.children().each(function (index, elem) { - elem.onclick = function() { _applySuggestion(index); }; - input_element.focus(); + var suggestion = $('<div class="suggestion-item">' + name + '</div>'); + suggestion.on('click', function(e) { + _applySuggestion(name); + input_element.focus(); + }); + dropdown.append(suggestion); }); show(); } @@ -176,6 +176,11 @@ var completer = (function (input_element, dropdown, base_config) { _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 s = dropdown.children()[index].innerText; if (s.indexOf(' ') != -1) { return '"' + s + '"'; @@ -194,9 +199,9 @@ var completer = (function (input_element, dropdown, base_config) { } selected_index = new_index; } - function _applySuggestion(index) { + function _applySuggestion(str) { var cur = input_element.val(), - start = cur.slice(0, completion_start) + _get_option(index) + ' '; + start = cur.slice(0, completion_start) + str + ' '; input_element.val(start + cur.slice(completion_end)); setCaret(input_element, start.length); oninput('', 0); @@ -205,7 +210,7 @@ var completer = (function (input_element, dropdown, base_config) { if (selected_index == -1) { return false; } - _applySuggestion(selected_index); + _applySuggestion(_get_option(selected_index)); return true; } |
