Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 1 | /* |
| 2 | * The following code is a jquery extention written to add autocomplete functionality to bootstrap input groups. |
| 3 | * |
| 4 | * Usage: |
| 5 | * |
| 6 | * Then simply call $('.someClass').autoComplete(getSuggestions) on it to enable auto completion. |
| 7 | * |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 8 | * getSuggestions returns by calling its callback parameter with an array of valid strings. |
| 9 | * |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 10 | * Autocomplete can be manually triggered by triggering the autoComplete event. |
| 11 | * |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 12 | */ |
| 13 | (function(){ |
| 14 | "use strict"; |
| 15 | if (!jQuery){ |
| 16 | throw new Error("jQuery is required and must be loaded before this script.") |
| 17 | } |
| 18 | jQuery.fn.extend({ |
| 19 | /** |
| 20 | * @param {Array<String>|getSuggestions} |
| 21 | */ |
| 22 | autoComplete: function(suggestions) { |
| 23 | |
| 24 | var element = $('<div></div>'); |
| 25 | element.addClass('list-group') |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 26 | .addClass('autoComplete') |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 27 | .css({ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 28 | 'top': this.parent().height() |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 29 | }); |
| 30 | |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 31 | // this.focus(function(){ |
| 32 | // element.slideDown(); |
| 33 | // }).blur(function(){ |
| 34 | // element.slideUp(); |
| 35 | // }).before(element); |
| 36 | |
| 37 | this.after(element); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 38 | |
| 39 | var getSuggestions = function(current, callback){ |
| 40 | callback(suggestions.reduce(function(prev, suggestion){ |
| 41 | if (current.toLowerCase().indexOf(suggestion.substr(0, current.length).toLowerCase()) === 0){ |
| 42 | prev.push(suggestion); |
| 43 | } |
| 44 | return prev; |
| 45 | }, [])); |
| 46 | } |
| 47 | |
| 48 | var setAutoComplete = function(list){ |
| 49 | element.empty(); |
| 50 | |
| 51 | element.html(list.reduce(function(prev, current){ |
| 52 | return [prev, '<a href="#" class="list-group-item">', current, '</a>'].join(""); |
| 53 | }, "")); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | if (suggestions instanceof Function){ |
| 58 | getSuggestions = suggestions; |
| 59 | } |
| 60 | |
| 61 | var input = this; |
| 62 | |
| 63 | element.bind('click', 'a', function(){ |
| 64 | input.val($(this).text()); |
| 65 | }); |
| 66 | |
| 67 | this.keydown(function(e){ |
| 68 | switch(e.which){ |
| 69 | case 38: //up |
| 70 | var active = element.find('.active'); |
| 71 | if (active.length === 0){ |
| 72 | element.find(':first-child').addClass('active'); |
| 73 | } else { |
| 74 | if (!active.is(':first-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 75 | var top = active.removeClass('active').prev().addClass('active').offset().top; |
| 76 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | e.preventDefault(); |
| 80 | break; |
| 81 | |
| 82 | case 40: //down |
| 83 | var active = element.find('.active'); |
| 84 | if (active.length === 0){ |
| 85 | element.find(':first-child').addClass('active'); |
| 86 | } else { |
| 87 | if (!active.is(':last-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 88 | var top = active.removeClass('active').next().addClass('active').offset().top; |
| 89 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | e.preventDefault(); |
| 93 | break; |
| 94 | |
| 95 | case 13: //Enter |
| 96 | var active = element.find('.active'); |
| 97 | if (active.length === 1){ |
| 98 | $(this).val(active.text()); |
| 99 | e.preventDefault(); |
| 100 | } |
| 101 | break; |
| 102 | |
| 103 | case 9: //Tab |
| 104 | getSuggestions(input.val(), setAutoComplete); |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame^] | 105 | e.preventDefault(); //Don't print tab or select a different element. |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | |
| 109 | }); |
| 110 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 111 | this.on('autoComplete', function(){ |
| 112 | getSuggestions(input.val(), setAutoComplete); |
| 113 | }); |
| 114 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 115 | return this; |
| 116 | |
| 117 | } |
| 118 | }); |
| 119 | })(); |
| 120 | |
| 121 | /** |
| 122 | * @callback getSuggestions |
| 123 | * @param {string} current - The current value of the input field. |
| 124 | * @param {function} |
| 125 | */ |