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.after(element); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 32 | |
| 33 | var getSuggestions = function(current, callback){ |
| 34 | callback(suggestions.reduce(function(prev, suggestion){ |
| 35 | if (current.toLowerCase().indexOf(suggestion.substr(0, current.length).toLowerCase()) === 0){ |
| 36 | prev.push(suggestion); |
| 37 | } |
| 38 | return prev; |
| 39 | }, [])); |
| 40 | } |
| 41 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 42 | var lastList = []; |
| 43 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 44 | var setAutoComplete = function(list){ |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 45 | lastList = list; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 46 | element.empty(); |
| 47 | |
| 48 | element.html(list.reduce(function(prev, current){ |
| 49 | return [prev, '<a href="#" class="list-group-item">', current, '</a>'].join(""); |
| 50 | }, "")); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | if (suggestions instanceof Function){ |
| 55 | getSuggestions = suggestions; |
| 56 | } |
| 57 | |
| 58 | var input = this; |
| 59 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 60 | var matcher = /^\/([-_\w]+\/)*/; //Returns only the absolute path. |
| 61 | |
| 62 | var getValue = function(){ |
| 63 | var res = matcher.exec(input.val()); |
| 64 | if (res){ |
| 65 | return res[0]; //Return the absolute match |
| 66 | } else { |
| 67 | throw new Error("Empty or incorrectly formatted path."); |
| 68 | } |
| 69 | } |
| 70 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 71 | element.bind('click', 'a', function(){ |
| 72 | input.val($(this).text()); |
| 73 | }); |
| 74 | |
| 75 | this.keydown(function(e){ |
| 76 | switch(e.which){ |
| 77 | case 38: //up |
| 78 | var active = element.find('.active'); |
| 79 | if (active.length === 0){ |
| 80 | element.find(':first-child').addClass('active'); |
| 81 | } else { |
| 82 | if (!active.is(':first-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 83 | var top = active.removeClass('active').prev().addClass('active').offset().top; |
| 84 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | e.preventDefault(); |
| 88 | break; |
| 89 | |
| 90 | case 40: //down |
| 91 | var active = element.find('.active'); |
| 92 | if (active.length === 0){ |
| 93 | element.find(':first-child').addClass('active'); |
| 94 | } else { |
| 95 | if (!active.is(':last-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 96 | var top = active.removeClass('active').next().addClass('active').offset().top; |
| 97 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | e.preventDefault(); |
| 101 | break; |
| 102 | |
| 103 | case 13: //Enter |
| 104 | var active = element.find('.active'); |
| 105 | if (active.length === 1){ |
| 106 | $(this).val(active.text()); |
| 107 | e.preventDefault(); |
| 108 | } |
| 109 | break; |
| 110 | |
| 111 | case 9: //Tab |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 112 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 113 | e.preventDefault(); //Don't print tab or select a different element. |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 114 | break; |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 115 | |
| 116 | default: |
| 117 | var val = input.val(); //Needs to be unfiltered, for filtering existing results. |
| 118 | setAutoComplete(lastList.reduce(function(prev, current){ |
| 119 | if (current.indexOf(val) === 0){ |
| 120 | prev.push(current); |
| 121 | } |
| 122 | return prev; |
| 123 | }, [])); |
| 124 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 127 | }) |
| 128 | .keyup(function(e){ |
| 129 | if (e.which === 191){ |
| 130 | getSuggestions(getValue(), setAutoComplete); |
| 131 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 132 | }); |
| 133 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 134 | this.on('autoComplete', function(){ |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 135 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 136 | }); |
| 137 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 138 | return this; |
| 139 | |
| 140 | } |
| 141 | }); |
| 142 | })(); |
| 143 | |
| 144 | /** |
| 145 | * @callback getSuggestions |
| 146 | * @param {string} current - The current value of the input field. |
| 147 | * @param {function} |
| 148 | */ |