-
Notifications
You must be signed in to change notification settings - Fork 233
/
autocomplete.js
312 lines (263 loc) · 8.79 KB
/
autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* --- Made by justgoscha and licensed under MIT license --- */
var app = angular.module('autocomplete', []);
app.directive('autocomplete', function() {
var index = -1;
return {
restrict: 'E',
scope: {
searchParam: '=ngModel',
suggestions: '=data',
onType: '=onType',
onSelect: '=onSelect',
autocompleteRequired: '=',
noAutoSort: '=noAutoSort'
},
controller: ['$scope', function($scope){
// the index of the suggestions that's currently selected
$scope.selectedIndex = -1;
$scope.initLock = true;
// set new index
$scope.setIndex = function(i){
$scope.selectedIndex = parseInt(i);
};
this.setIndex = function(i){
$scope.setIndex(i);
$scope.$apply();
};
$scope.getIndex = function(i){
return $scope.selectedIndex;
};
// watches if the parameter filter should be changed
var watching = true;
// autocompleting drop down on/off
$scope.completing = false;
// starts autocompleting on typing in something
$scope.$watch('searchParam', function(newValue, oldValue){
if (oldValue === newValue || (!oldValue && $scope.initLock)) {
return;
}
if(watching && typeof $scope.searchParam !== 'undefined' && $scope.searchParam !== null) {
$scope.completing = true;
$scope.searchFilter = $scope.searchParam;
$scope.selectedIndex = -1;
}
// function thats passed to on-type attribute gets executed
if($scope.onType)
$scope.onType($scope.searchParam);
});
// for hovering over suggestions
this.preSelect = function(suggestion){
watching = false;
// this line determines if it is shown
// in the input field before it's selected:
//$scope.searchParam = suggestion;
$scope.$apply();
watching = true;
};
$scope.preSelect = this.preSelect;
this.preSelectOff = function(){
watching = true;
};
$scope.preSelectOff = this.preSelectOff;
// selecting a suggestion with RIGHT ARROW or ENTER
$scope.select = function(suggestion){
if(suggestion){
$scope.searchParam = suggestion;
$scope.searchFilter = suggestion;
if($scope.onSelect)
$scope.onSelect(suggestion);
}
watching = false;
$scope.completing = false;
setTimeout(function(){watching = true;},1000);
$scope.setIndex(-1);
};
}],
link: function(scope, element, attrs){
console.log(scope.noAutoSort)
setTimeout(function() {
scope.initLock = false;
scope.$apply();
}, 250);
var attr = '';
// Default atts
scope.attrs = {
"placeholder": "start typing...",
"class": "",
"id": "",
"inputclass": "",
"inputid": ""
};
for (var a in attrs) {
attr = a.replace('attr', '').toLowerCase();
// add attribute overriding defaults
// and preventing duplication
if (a.indexOf('attr') === 0) {
scope.attrs[attr] = attrs[a];
}
}
if (attrs.clickActivation) {
element[0].onclick = function(e){
if(!scope.searchParam){
setTimeout(function() {
scope.completing = true;
scope.$apply();
}, 200);
}
};
}
var key = {left: 37, up: 38, right: 39, down: 40 , enter: 13, esc: 27, tab: 9};
document.addEventListener("keydown", function(e){
var keycode = e.keyCode || e.which;
switch (keycode){
case key.esc:
// disable suggestions on escape
scope.select();
scope.setIndex(-1);
scope.$apply();
e.preventDefault();
}
}, true);
document.addEventListener("blur", function(e){
// disable suggestions on blur
// we do a timeout to prevent hiding it before a click event is registered
setTimeout(function() {
scope.select();
scope.setIndex(-1);
scope.$apply();
}, 150);
}, true);
element[0].addEventListener("keydown",function (e){
var keycode = e.keyCode || e.which;
var l = angular.element(this).find('li').length;
// this allows submitting forms by pressing Enter in the autocompleted field
if(!scope.completing || l == 0) return;
// implementation of the up and down movement in the list of suggestions
switch (keycode){
case key.up:
index = scope.getIndex()-1;
if(index<-1){
index = l-1;
} else if (index >= l ){
index = -1;
scope.setIndex(index);
scope.preSelectOff();
break;
}
scope.setIndex(index);
if(index!==-1)
scope.preSelect(angular.element(angular.element(this).find('li')[index]).text());
scope.$apply();
break;
case key.down:
index = scope.getIndex()+1;
if(index<-1){
index = l-1;
} else if (index >= l ){
index = -1;
scope.setIndex(index);
scope.preSelectOff();
scope.$apply();
break;
}
scope.setIndex(index);
if(index!==-1)
scope.preSelect(angular.element(angular.element(this).find('li')[index]).text());
break;
case key.left:
break;
case key.right:
case key.enter:
case key.tab:
index = scope.getIndex();
// scope.preSelectOff();
if(index !== -1) {
scope.select(angular.element(angular.element(this).find('li')[index]).text());
if(keycode == key.enter) {
e.preventDefault();
}
} else {
if(keycode == key.enter) {
scope.select();
}
}
scope.setIndex(-1);
scope.$apply();
break;
case key.esc:
// disable suggestions on escape
scope.select();
scope.setIndex(-1);
scope.$apply();
e.preventDefault();
break;
default:
return;
}
});
},
template: '\
<div class="autocomplete {{ attrs.class }}" id="{{ attrs.id }}">\
<input\
type="text"\
ng-model="searchParam"\
placeholder="{{ attrs.placeholder }}"\
class="{{ attrs.inputclass }}"\
tabindex="{{ attrs.tabindex }}"\
id="{{ attrs.inputid }}"\
name="{{ attrs.name }}"\
ng-required="{{ autocompleteRequired }}" />\
<ul ng-if="!noAutoSort" ng-show="completing && (suggestions | filter:searchFilter).length > 0">\
<li\
suggestion\
ng-repeat="suggestion in suggestions | filter:searchFilter | orderBy:\'toString()\' track by $index"\
index="{{ $index }}"\
val="{{ suggestion }}"\
ng-class="{ active: ($index === selectedIndex) }"\
ng-click="select(suggestion)"\
ng-bind-html="suggestion | highlight:searchParam"></li>\
</ul>\
<ul ng-if="noAutoSort" ng-show="completing && (suggestions | filter:searchFilter).length > 0">\
<li\
suggestion\
ng-repeat="suggestion in suggestions | filter:searchFilter track by $index"\
index="{{ $index }}"\
val="{{ suggestion }}"\
ng-class="{ active: ($index === selectedIndex) }"\
ng-click="select(suggestion)"\
ng-bind-html="suggestion | highlight:searchParam"></li>\
</ul>\
</div>'
};
});
app.filter('highlight', ['$sce', function ($sce) {
return function (input, searchParam) {
if (typeof input === 'function') return '';
if (searchParam) {
var words = '(' +
searchParam.split(/\ /).join(' |') + '|' +
searchParam.split(/\ /).join('|') +
')',
exp = new RegExp(words, 'gi');
if (words.length) {
input = input.replace(exp, "<span class=\"highlight\">$1</span>");
}
}
return $sce.trustAsHtml(input);
};
}]);
app.directive('suggestion', function(){
return {
restrict: 'A',
require: '^autocomplete', // ^look for controller on parents element
link: function(scope, element, attrs, autoCtrl){
element.bind('mouseenter', function() {
autoCtrl.preSelect(attrs.val);
autoCtrl.setIndex(attrs.index);
});
element.bind('mouseleave', function() {
autoCtrl.preSelectOff();
});
}
};
});