blob: d6987e2319eccf4d600af00532dc1162bae508e6 [file] [log] [blame]
Tyler Scott93cae872015-07-21 14:58:23 -06001/*
2 * The following code is a jquery extention written to add autocomplete functionality to bootstrap input groups.
Tyler Scottfc990392015-08-10 13:48:03 -06003 *
Tyler Scott93cae872015-07-21 14:58:23 -06004 * Usage:
Tyler Scottfc990392015-08-10 13:48:03 -06005 *
Tyler Scott93cae872015-07-21 14:58:23 -06006 * Then simply call $('.someClass').autoComplete(getSuggestions) on it to enable auto completion.
Tyler Scottfc990392015-08-10 13:48:03 -06007 *
Tyler Scott93cae872015-07-21 14:58:23 -06008 * getSuggestions returns by calling its callback parameter with an array of valid strings.
Tyler Scottfc990392015-08-10 13:48:03 -06009 *
Tyler Scottc55879f2015-07-28 14:56:37 -060010 * Autocomplete can be manually triggered by triggering the autoComplete event.
Tyler Scottfc990392015-08-10 13:48:03 -060011 *
Tyler Scott93cae872015-07-21 14:58:23 -060012 */
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 Scottfe8e4932015-07-28 17:45:45 -060026 .addClass('autoComplete')
Tyler Scott93cae872015-07-21 14:58:23 -060027 .css({
Tyler Scottfe8e4932015-07-28 17:45:45 -060028 'top': this.parent().height()
Tyler Scott93cae872015-07-21 14:58:23 -060029 });
30
Tyler Scott03854852015-08-07 15:45:10 -060031 this.attr('autocomplete', 'off')
32 .after(element);
Tyler Scott93cae872015-07-21 14:58:23 -060033
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 Scott9eb6abd2015-08-04 14:48:23 -060043 var lastList = [];
44
Tyler Scott93cae872015-07-21 14:58:23 -060045 var setAutoComplete = function(list){
Tyler Scott9eb6abd2015-08-04 14:48:23 -060046 lastList = list;
Tyler Scott93cae872015-07-21 14:58:23 -060047 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 Scott9eb6abd2015-08-04 14:48:23 -060061 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 Scott93cae872015-07-21 14:58:23 -060072 element.bind('click', 'a', function(){
Tyler Scott39f72252015-08-07 16:21:45 -060073 input.val($(event.target).text());
Tyler Scottfc990392015-08-10 13:48:03 -060074 getSuggestions(getValue(), setAutoComplete);
75 input.focus();
Tyler Scott93cae872015-07-21 14:58:23 -060076 });
77
Tyler Scottfc990392015-08-10 13:48:03 -060078 var updateFromList = function(){
79 var val = input.val(); //Needs to be unfiltered, for filtering existing results.
Tyler Scott149955a2015-08-13 15:25:41 -060080 var temp = lastList;
Tyler Scottfc990392015-08-10 13:48:03 -060081 setAutoComplete(lastList.reduce(function(prev, current){
82 if (current.indexOf(val) === 0){
83 prev.push(current);
84 }
85 return prev;
86 }, []));
Tyler Scott149955a2015-08-13 15:25:41 -060087 lastList = temp;
Tyler Scottfc990392015-08-10 13:48:03 -060088 }
89
Tyler Scott93cae872015-07-21 14:58:23 -060090 this.keydown(function(e){
91 switch(e.which){
92 case 38: //up
93 var active = element.find('.active');
94 if (active.length === 0){
95 element.find(':first-child').addClass('active');
96 } else {
97 if (!active.is(':first-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060098 var top = active.removeClass('active').prev().addClass('active').offset().top;
99 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -0600100 }
101 }
102 e.preventDefault();
103 break;
104
105 case 40: //down
106 var active = element.find('.active');
107 if (active.length === 0){
108 element.find(':first-child').addClass('active');
109 } else {
110 if (!active.is(':last-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -0600111 var top = active.removeClass('active').next().addClass('active').offset().top;
112 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -0600113 }
114 }
115 e.preventDefault();
116 break;
117
118 case 13: //Enter
119 var active = element.find('.active');
120 if (active.length === 1){
121 $(this).val(active.text());
122 e.preventDefault();
Tyler Scottfc990392015-08-10 13:48:03 -0600123 getSuggestions(getValue(), setAutoComplete);
Tyler Scott93cae872015-07-21 14:58:23 -0600124 }
125 break;
126
127 case 9: //Tab
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600128 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfe8e4932015-07-28 17:45:45 -0600129 e.preventDefault(); //Don't print tab or select a different element.
Tyler Scott93cae872015-07-21 14:58:23 -0600130 break;
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600131
Tyler Scott149955a2015-08-13 15:25:41 -0600132 case 8:
133 console.log("Detected backspace");
134 if (input.val().slice(-1) == "/"){
135 e.preventDefault();
136 input.val(input.val().slice(0,-1)); //Manually backspace early. (Have to do it manually)
137 getSuggestions(getValue(), setAutoComplete);
138 } else {
139 updateFromList(setAutoComplete);
140 }
141
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600142 default:
Tyler Scottfc990392015-08-10 13:48:03 -0600143 updateFromList();
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600144
Tyler Scott93cae872015-07-21 14:58:23 -0600145 }
146
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600147 })
148 .keyup(function(e){
Tyler Scottfc990392015-08-10 13:48:03 -0600149 switch (e.which){
150
151 case 191:
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600152 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfc990392015-08-10 13:48:03 -0600153
154 break;
155 case 38:
156 case 40:
157 case 13:
158 case 9:
159 // Do nothing
160
161 break;
162 default:
163 updateFromList();
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600164 }
Tyler Scott93cae872015-07-21 14:58:23 -0600165 });
166
Tyler Scottc55879f2015-07-28 14:56:37 -0600167 this.on('autoComplete', function(){
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600168 getSuggestions(getValue(), setAutoComplete);
Tyler Scottc55879f2015-07-28 14:56:37 -0600169 });
170
Tyler Scott93cae872015-07-21 14:58:23 -0600171 return this;
172
173 }
174 });
175})();
176
177/**
178 * @callback getSuggestions
179 * @param {string} current - The current value of the input field.
180 * @param {function}
Tyler Scott03854852015-08-07 15:45:10 -0600181 */