-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-autocomplete.js
183 lines (157 loc) · 7.52 KB
/
angular-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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([ 'module', 'angular' ], function (module, angular) {
module.exports = factory(angular);
});
} else if (typeof module === 'object') {
module.exports = factory(require('angular'));
} else {
if (!root.mp) {
root.mp = {};
}
root.mp.autocomplete = factory(root.angular);
}
}(this, function (angular) {
'use strict';
return angular.module('mp.autocomplete', []).directive('autocomplete', [
'$window', '$q', '$compile', '$parse', '$timeout',
function($window, $q, $compile, $parse, $timeout) {
// Introduce custom elements for IE8
$window.document.createElement('autocomplete');
var TEMPLATE = '' +
'<div class="angular-autocomplete" ng-if="isOpen()">' +
' <ul class="_suggestions">' +
' <li ng-repeat="match in matches" ' +
' class="_suggestion"' +
' ng-class="{ \'-selected\': selectedIndex == $index }" ' +
// use mousedown to stay clear of input losing focus when suggestion is clicked
' ng-mousedown="handleSelect($index, $event)" ' +
' ng-mousemove="setSelectedIndex($index)">{{ render({ value: match }) }}<a class="delete" href="javascript:void(0)" ng-if="suggestionIsDeletable({ value: match })">Delete</a></li>' +
' </ul>' +
'</div>';
var HOT_KEYS = [
9, // tab
13, // enter
27, // esc
38, // up arrow
40 // down arrow
];
return {
restrict: 'E',
template: TEMPLATE,
replace: true,
scope: {
inputElement: '=',
onSelectionComplete: '&',
render: '&',
query: '&',
deleteSuggestion: '&',
suggestionIsDeletable: '&'
},
link: function (scope, element, attributes) {
scope.matches = []
scope.selectedIndex = -1;
var resetMatches = function () {
scope.matches = [];
scope.selectedIndex = -1;
};
function select(selectedIndex) {
// called from within the $digest() cycle
var selectedValue = scope.matches[selectedIndex];
resetMatches();
// notify observer of selection complete
scope.onSelectionComplete({ value: selectedValue });
};
scope.isOpen = function () {
return scope.matches.length > 0;
};
scope.handleSelect = function (selectedIndex, evt) {
// don't do selection on delete
if (!evt.target.classList.contains('delete')) {
select(selectedIndex);
} else {
scope.deleteSuggestion({ value: scope.matches[selectedIndex] });
scope.matches.splice(selectedIndex, 1);
evt.preventDefault();
}
};
scope.selectNext = function () {
scope.selectedIndex = (scope.selectedIndex + 1) % scope.matches.length;
};
scope.selectPrev = function () {
scope.selectedIndex = (scope.selectedIndex > 0
? scope.selectedIndex
: scope.matches.length) - 1;
};
scope.setSelectedIndex = function(index) {
if (scope.selectedIndex !== index) {
scope.selectedIndex = index;
}
};
// 0. Initialize to empty state
resetMatches();
var latestQuery = null;
// 1. Watch model for changes
scope.$parent.$watch(attributes.queryText, function (queryText) {
// clear out existing displayed suggestions
resetMatches();
var inputValue = queryText.replace(/^\s+/, '').replace(/\s+$/, '');
var currentQuery =
$timeout(function () {
return inputValue;
}, 350)
.then(function (v) {
// 2. Fetch suggestions
if (latestQuery === currentQuery) {
return scope.query({ input: v });
}
})
.then(function (suggestions) {
// 3. Present suggestions
if (latestQuery === currentQuery) {
scope.matches = suggestions;
scope.selectedIndex = suggestions.length ? 0 : -1;
}
});
latestQuery = currentQuery;
});
// 4. Handle mouse selection or keypresses
scope.inputElement.bind('keydown', function (evt) {
// we have matches and an "interesting" key was pressed
if (scope.matches.length === 0 || HOT_KEYS.indexOf(evt.which) === -1) {
return;
}
// if there's nothing selected and enter/tab is hit, don't do anything
if (scope.selectedIndex == -1 && (evt.which === 13 || evt.which === 9)) {
return;
}
evt.preventDefault();
scope.$apply(function () {
if (!scope.isOpen()) {
return;
}
if (evt.which === 40) { // Down Arrow
scope.selectNext();
} else if (evt.which === 38) { // Up Arrow
scope.selectPrev();
} else if (evt.which === 13 || evt.which === 9) { // Enter or Tab
select(scope.selectedIndex);
} else if (evt.which === 27) { // Esc
// 5. Handle cancelling of the dialog
resetMatches();
evt.stopPropagation();
}
});
}).bind('blur', function () {
if (scope.matches.length) {
// Close the autocomplete dialog when the element loses focus
scope.$apply(function () {
resetMatches();
});
}
});
}
};
}
]);
}));