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 | /** |
Tyler Scott | e564a5a | 2015-09-16 17:43:33 -0600 | [diff] [blame] | 20 | * This setups up the default autocomplete functionality on an object. |
| 21 | * Using either a list of options or supplying a function that returns them, |
| 22 | * auto complete will create a menu for users to navigate and select a |
| 23 | * valid option. |
| 24 | * @param {Array<String>|function(String, function(Array<String>))} suggestions |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 25 | */ |
| 26 | autoComplete: function(suggestions) { |
| 27 | |
| 28 | var element = $('<div></div>'); |
| 29 | element.addClass('list-group') |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 30 | .addClass('autoComplete') |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 31 | .css({ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 32 | 'top': this.parent().height() |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 33 | }); |
| 34 | |
Tyler Scott | 0385485 | 2015-08-07 15:45:10 -0600 | [diff] [blame] | 35 | this.attr('autocomplete', 'off') |
| 36 | .after(element); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 37 | |
| 38 | var getSuggestions = function(current, callback){ |
| 39 | callback(suggestions.reduce(function(prev, suggestion){ |
| 40 | if (current.toLowerCase().indexOf(suggestion.substr(0, current.length).toLowerCase()) === 0){ |
| 41 | prev.push(suggestion); |
| 42 | } |
| 43 | return prev; |
| 44 | }, [])); |
| 45 | } |
| 46 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 47 | var lastList = []; |
| 48 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 49 | var setAutoComplete = function(list){ |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 50 | lastList = list; |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 51 | element.empty(); |
| 52 | |
| 53 | element.html(list.reduce(function(prev, current){ |
| 54 | return [prev, '<a href="#" class="list-group-item">', current, '</a>'].join(""); |
| 55 | }, "")); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | if (suggestions instanceof Function){ |
| 60 | getSuggestions = suggestions; |
| 61 | } |
| 62 | |
| 63 | var input = this; |
| 64 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 65 | var matcher = /^\/([-_\w]+\/)*/; //Returns only the absolute path. |
| 66 | |
| 67 | var getValue = function(){ |
| 68 | var res = matcher.exec(input.val()); |
| 69 | if (res){ |
| 70 | return res[0]; //Return the absolute match |
| 71 | } else { |
| 72 | throw new Error("Empty or incorrectly formatted path."); |
| 73 | } |
| 74 | } |
| 75 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 76 | element.bind('click', 'a', function(){ |
Tyler Scott | 39f7225 | 2015-08-07 16:21:45 -0600 | [diff] [blame] | 77 | input.val($(event.target).text()); |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 78 | getSuggestions(getValue(), setAutoComplete); |
| 79 | input.focus(); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 80 | }); |
| 81 | |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 82 | var updateFromList = function(){ |
| 83 | var val = input.val(); //Needs to be unfiltered, for filtering existing results. |
Tyler Scott | 149955a | 2015-08-13 15:25:41 -0600 | [diff] [blame] | 84 | var temp = lastList; |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 85 | setAutoComplete(lastList.reduce(function(prev, current){ |
| 86 | if (current.indexOf(val) === 0){ |
| 87 | prev.push(current); |
| 88 | } |
| 89 | return prev; |
| 90 | }, [])); |
Tyler Scott | 149955a | 2015-08-13 15:25:41 -0600 | [diff] [blame] | 91 | lastList = temp; |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 92 | } |
| 93 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 94 | this.keydown(function(e){ |
| 95 | switch(e.which){ |
| 96 | case 38: //up |
| 97 | var active = element.find('.active'); |
| 98 | if (active.length === 0){ |
| 99 | element.find(':first-child').addClass('active'); |
| 100 | } else { |
| 101 | if (!active.is(':first-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 102 | var top = active.removeClass('active').prev().addClass('active').offset().top; |
| 103 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | e.preventDefault(); |
| 107 | break; |
| 108 | |
| 109 | case 40: //down |
| 110 | var active = element.find('.active'); |
| 111 | if (active.length === 0){ |
| 112 | element.find(':first-child').addClass('active'); |
| 113 | } else { |
| 114 | if (!active.is(':last-child')){ |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 115 | var top = active.removeClass('active').next().addClass('active').offset().top; |
| 116 | active.parent().stop().animate({scrollTop: top}, 500); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | e.preventDefault(); |
| 120 | break; |
| 121 | |
| 122 | case 13: //Enter |
| 123 | var active = element.find('.active'); |
| 124 | if (active.length === 1){ |
| 125 | $(this).val(active.text()); |
| 126 | e.preventDefault(); |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 127 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 128 | } |
| 129 | break; |
| 130 | |
| 131 | case 9: //Tab |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 132 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | fe8e493 | 2015-07-28 17:45:45 -0600 | [diff] [blame] | 133 | e.preventDefault(); //Don't print tab or select a different element. |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 134 | break; |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 135 | |
Tyler Scott | 149955a | 2015-08-13 15:25:41 -0600 | [diff] [blame] | 136 | case 8: |
| 137 | console.log("Detected backspace"); |
| 138 | if (input.val().slice(-1) == "/"){ |
| 139 | e.preventDefault(); |
| 140 | input.val(input.val().slice(0,-1)); //Manually backspace early. (Have to do it manually) |
| 141 | getSuggestions(getValue(), setAutoComplete); |
| 142 | } else { |
| 143 | updateFromList(setAutoComplete); |
| 144 | } |
| 145 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 146 | default: |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 147 | updateFromList(); |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 148 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 149 | } |
| 150 | |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 151 | }) |
| 152 | .keyup(function(e){ |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 153 | switch (e.which){ |
| 154 | |
| 155 | case 191: |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 156 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | fc99039 | 2015-08-10 13:48:03 -0600 | [diff] [blame] | 157 | |
| 158 | break; |
| 159 | case 38: |
| 160 | case 40: |
| 161 | case 13: |
| 162 | case 9: |
| 163 | // Do nothing |
| 164 | |
| 165 | break; |
| 166 | default: |
| 167 | updateFromList(); |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 168 | } |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 169 | }); |
| 170 | |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 171 | this.on('autoComplete', function(){ |
Tyler Scott | 9eb6abd | 2015-08-04 14:48:23 -0600 | [diff] [blame] | 172 | getSuggestions(getValue(), setAutoComplete); |
Tyler Scott | c55879f | 2015-07-28 14:56:37 -0600 | [diff] [blame] | 173 | }); |
| 174 | |
Tyler Scott | 93cae87 | 2015-07-21 14:58:23 -0600 | [diff] [blame] | 175 | return this; |
| 176 | |
| 177 | } |
| 178 | }); |
| 179 | })(); |
| 180 | |