blob: 2cda884ca40b18f4e0fbb1e21863b157fa1c1d69 [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.after(element);
Tyler Scott93cae872015-07-21 14:58:23 -060032
33 var getSuggestions = function(current, callback){
34 callback(suggestions.reduce(function(prev, suggestion){
35 if (current.toLowerCase().indexOf(suggestion.substr(0, current.length).toLowerCase()) === 0){
36 prev.push(suggestion);
37 }
38 return prev;
39 }, []));
40 }
41
Tyler Scott9eb6abd2015-08-04 14:48:23 -060042 var lastList = [];
43
Tyler Scott93cae872015-07-21 14:58:23 -060044 var setAutoComplete = function(list){
Tyler Scott9eb6abd2015-08-04 14:48:23 -060045 lastList = list;
Tyler Scott93cae872015-07-21 14:58:23 -060046 element.empty();
47
48 element.html(list.reduce(function(prev, current){
49 return [prev, '<a href="#" class="list-group-item">', current, '</a>'].join("");
50 }, ""));
51
52 }
53
54 if (suggestions instanceof Function){
55 getSuggestions = suggestions;
56 }
57
58 var input = this;
59
Tyler Scott9eb6abd2015-08-04 14:48:23 -060060 var matcher = /^\/([-_\w]+\/)*/; //Returns only the absolute path.
61
62 var getValue = function(){
63 var res = matcher.exec(input.val());
64 if (res){
65 return res[0]; //Return the absolute match
66 } else {
67 throw new Error("Empty or incorrectly formatted path.");
68 }
69 }
70
Tyler Scott93cae872015-07-21 14:58:23 -060071 element.bind('click', 'a', function(){
72 input.val($(this).text());
73 });
74
75 this.keydown(function(e){
76 switch(e.which){
77 case 38: //up
78 var active = element.find('.active');
79 if (active.length === 0){
80 element.find(':first-child').addClass('active');
81 } else {
82 if (!active.is(':first-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060083 var top = active.removeClass('active').prev().addClass('active').offset().top;
84 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060085 }
86 }
87 e.preventDefault();
88 break;
89
90 case 40: //down
91 var active = element.find('.active');
92 if (active.length === 0){
93 element.find(':first-child').addClass('active');
94 } else {
95 if (!active.is(':last-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060096 var top = active.removeClass('active').next().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 13: //Enter
104 var active = element.find('.active');
105 if (active.length === 1){
106 $(this).val(active.text());
107 e.preventDefault();
108 }
109 break;
110
111 case 9: //Tab
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600112 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfe8e4932015-07-28 17:45:45 -0600113 e.preventDefault(); //Don't print tab or select a different element.
Tyler Scott93cae872015-07-21 14:58:23 -0600114 break;
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600115
116 default:
117 var val = input.val(); //Needs to be unfiltered, for filtering existing results.
118 setAutoComplete(lastList.reduce(function(prev, current){
119 if (current.indexOf(val) === 0){
120 prev.push(current);
121 }
122 return prev;
123 }, []));
124
Tyler Scott93cae872015-07-21 14:58:23 -0600125 }
126
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600127 })
128 .keyup(function(e){
129 if (e.which === 191){
130 getSuggestions(getValue(), setAutoComplete);
131 }
Tyler Scott93cae872015-07-21 14:58:23 -0600132 });
133
Tyler Scottc55879f2015-07-28 14:56:37 -0600134 this.on('autoComplete', function(){
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600135 getSuggestions(getValue(), setAutoComplete);
Tyler Scottc55879f2015-07-28 14:56:37 -0600136 });
137
Tyler Scott93cae872015-07-21 14:58:23 -0600138 return this;
139
140 }
141 });
142})();
143
144/**
145 * @callback getSuggestions
146 * @param {string} current - The current value of the input field.
147 * @param {function}
148 */