blob: df54c17b08cb34fe50c1beca2acc32cbba98dfed [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.
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 Scott93cae872015-07-21 14:58:23 -060088 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 Scottfe8e4932015-07-28 17:45:45 -060096 var top = active.removeClass('active').prev().addClass('active').offset().top;
97 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060098 }
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 Scottfe8e4932015-07-28 17:45:45 -0600109 var top = active.removeClass('active').next().addClass('active').offset().top;
110 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -0600111 }
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 Scottfc990392015-08-10 13:48:03 -0600121 getSuggestions(getValue(), setAutoComplete);
Tyler Scott93cae872015-07-21 14:58:23 -0600122 }
123 break;
124
125 case 9: //Tab
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600126 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfe8e4932015-07-28 17:45:45 -0600127 e.preventDefault(); //Don't print tab or select a different element.
Tyler Scott93cae872015-07-21 14:58:23 -0600128 break;
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600129
130 default:
Tyler Scottfc990392015-08-10 13:48:03 -0600131 updateFromList();
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600132
Tyler Scott93cae872015-07-21 14:58:23 -0600133 }
134
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600135 })
136 .keyup(function(e){
Tyler Scottfc990392015-08-10 13:48:03 -0600137 switch (e.which){
138
139 case 191:
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600140 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfc990392015-08-10 13:48:03 -0600141
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 Scott9eb6abd2015-08-04 14:48:23 -0600152 }
Tyler Scott93cae872015-07-21 14:58:23 -0600153 });
154
Tyler Scottc55879f2015-07-28 14:56:37 -0600155 this.on('autoComplete', function(){
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600156 getSuggestions(getValue(), setAutoComplete);
Tyler Scottc55879f2015-07-28 14:56:37 -0600157 });
158
Tyler Scott93cae872015-07-21 14:58:23 -0600159 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 Scott03854852015-08-07 15:45:10 -0600169 */