blob: ea71fa0c17c5a1da1ef96333c9faa54db2c4a74c [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 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(){
73 input.val($(this).text());
74 });
75
76 this.keydown(function(e){
77 switch(e.which){
78 case 38: //up
79 var active = element.find('.active');
80 if (active.length === 0){
81 element.find(':first-child').addClass('active');
82 } else {
83 if (!active.is(':first-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060084 var top = active.removeClass('active').prev().addClass('active').offset().top;
85 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060086 }
87 }
88 e.preventDefault();
89 break;
90
91 case 40: //down
92 var active = element.find('.active');
93 if (active.length === 0){
94 element.find(':first-child').addClass('active');
95 } else {
96 if (!active.is(':last-child')){
Tyler Scottfe8e4932015-07-28 17:45:45 -060097 var top = active.removeClass('active').next().addClass('active').offset().top;
98 active.parent().stop().animate({scrollTop: top}, 500);
Tyler Scott93cae872015-07-21 14:58:23 -060099 }
100 }
101 e.preventDefault();
102 break;
103
104 case 13: //Enter
105 var active = element.find('.active');
106 if (active.length === 1){
107 $(this).val(active.text());
108 e.preventDefault();
109 }
110 break;
111
112 case 9: //Tab
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600113 getSuggestions(getValue(), setAutoComplete);
Tyler Scottfe8e4932015-07-28 17:45:45 -0600114 e.preventDefault(); //Don't print tab or select a different element.
Tyler Scott93cae872015-07-21 14:58:23 -0600115 break;
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600116
117 default:
118 var val = input.val(); //Needs to be unfiltered, for filtering existing results.
119 setAutoComplete(lastList.reduce(function(prev, current){
120 if (current.indexOf(val) === 0){
121 prev.push(current);
122 }
123 return prev;
124 }, []));
125
Tyler Scott93cae872015-07-21 14:58:23 -0600126 }
127
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600128 })
129 .keyup(function(e){
130 if (e.which === 191){
131 getSuggestions(getValue(), setAutoComplete);
132 }
Tyler Scott93cae872015-07-21 14:58:23 -0600133 });
134
Tyler Scottc55879f2015-07-28 14:56:37 -0600135 this.on('autoComplete', function(){
Tyler Scott9eb6abd2015-08-04 14:48:23 -0600136 getSuggestions(getValue(), setAutoComplete);
Tyler Scottc55879f2015-07-28 14:56:37 -0600137 });
138
Tyler Scott93cae872015-07-21 14:58:23 -0600139 return this;
140
141 }
142 });
143})();
144
145/**
146 * @callback getSuggestions
147 * @param {string} current - The current value of the input field.
148 * @param {function}
Tyler Scott03854852015-08-07 15:45:10 -0600149 */