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