blob: b160dcb9902d7a9ac9d63de5e25b947bf8f4c340 [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.
3 *
4 * Usage:
5 *
6 * Then simply call $('.someClass').autoComplete(getSuggestions) on it to enable auto completion.
7 *
Tyler Scott93cae872015-07-21 14:58:23 -06008 * getSuggestions returns by calling its callback parameter with an array of valid strings.
9 *
Tyler Scottc55879f2015-07-28 14:56:37 -060010 * Autocomplete can be manually triggered by triggering the autoComplete event.
11 *
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 Scottfe8e4932015-07-28 17:45:45 -060031// this.focus(function(){
32// element.slideDown();
33// }).blur(function(){
34// element.slideUp();
35// }).before(element);
36
37 this.after(element);
Tyler Scott93cae872015-07-21 14:58:23 -060038
39 var getSuggestions = function(current, callback){
40 callback(suggestions.reduce(function(prev, suggestion){
41 if (current.toLowerCase().indexOf(suggestion.substr(0, current.length).toLowerCase()) === 0){
42 prev.push(suggestion);
43 }
44 return prev;
45 }, []));
46 }
47
48 var setAutoComplete = function(list){
49 element.empty();
50
51 element.html(list.reduce(function(prev, current){
52 return [prev, '<a href="#" class="list-group-item">', current, '</a>'].join("");
53 }, ""));
54
55 }
56
57 if (suggestions instanceof Function){
58 getSuggestions = suggestions;
59 }
60
61 var input = this;
62
63 element.bind('click', 'a', function(){
64 input.val($(this).text());
65 });
66
67 this.keydown(function(e){
68 switch(e.which){
69 case 38: //up
70 var active = element.find('.active');
71 if (active.length === 0){
72 element.find(':first-child').addClass('active');
73 } else {
74 if (!active.is(':first-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060075 var top = active.removeClass('active').prev().addClass('active').offset().top;
76 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060077 }
78 }
79 e.preventDefault();
80 break;
81
82 case 40: //down
83 var active = element.find('.active');
84 if (active.length === 0){
85 element.find(':first-child').addClass('active');
86 } else {
87 if (!active.is(':last-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060088 var top = active.removeClass('active').next().addClass('active').offset().top;
89 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060090 }
91 }
92 e.preventDefault();
93 break;
94
95 case 13: //Enter
96 var active = element.find('.active');
97 if (active.length === 1){
98 $(this).val(active.text());
99 e.preventDefault();
100 }
101 break;
102
103 case 9: //Tab
104 getSuggestions(input.val(), setAutoComplete);
Tyler Scottfe8e4932015-07-28 17:45:45 -0600105 e.preventDefault(); //Don't print tab or select a different element.
Tyler Scott93cae872015-07-21 14:58:23 -0600106 break;
107 }
108
109 });
110
Tyler Scottc55879f2015-07-28 14:56:37 -0600111 this.on('autoComplete', function(){
112 getSuggestions(input.val(), setAutoComplete);
113 });
114
Tyler Scott93cae872015-07-21 14:58:23 -0600115 return this;
116
117 }
118 });
119})();
120
121/**
122 * @callback getSuggestions
123 * @param {string} current - The current value of the input field.
124 * @param {function}
125 */