forked from hannaholl/angular-input-dropdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputDropdown.js
247 lines (222 loc) · 7.54 KB
/
inputDropdown.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
angular.module('inputDropdown', []).directive('inputDropdown', [function() {
var templateString =
'<div class="input-dropdown">' +
'<input type="text"' +
'name="{{inputName}}"' +
'placeholder="{{inputPlaceholder}}"' +
'autocomplete="off"' +
'ng-model="inputValue"' +
'class="{{inputClassName}}"' +
'ng-required="inputRequired"' +
'ng-change="inputChange()"' +
'ng-focus="inputFocus()"' +
'ng-blur="inputBlur($event)"' +
'input-dropdown-validator>' +
'<ul ng-show="dropdownVisible">' +
'<li ng-repeat="item in dropdownItems"' +
'ng-click="selectItem(item)"' +
'ng-mouseenter="setActive($index)"' +
'ng-mousedown="dropdownPressed()"' +
'ng-class="{\'active\': activeItemIndex === $index}"' +
'>' +
'<span ng-if="item.readableName">{{item.readableName}}</span>' +
'<span ng-if="!item.readableName">{{item}}</span>' +
'</li>' +
'</ul>' +
'</div>';
return {
restrict: 'E',
scope: {
defaultDropdownItems: '=',
selectedItem: '=',
allowCustomInput: '=',
inputRequired: '=',
inputName: '@',
inputClassName: '@',
inputPlaceholder: '@',
filterListMethod: '&',
itemSelectedMethod: '&'
},
template: templateString,
controller: function($scope) {
this.getSelectedItem = function() {
return $scope.selectedItem;
};
this.isRequired = function() {
return $scope.inputRequired;
};
this.customInputAllowed = function() {
return $scope.allowCustomInput;
};
this.getInput = function() {
return $scope.inputValue;
};
},
link: function(scope, element) {
var pressedDropdown = false;
var inputScope = element.find('input').isolateScope();
scope.activeItemIndex = 0;
scope.inputValue = '';
scope.dropdownVisible = false;
scope.dropdownItems = scope.defaultDropdownItems || [];
scope.$watch('dropdownItems', function(newValue, oldValue) {
if (!angular.equals(newValue, oldValue)) {
// If new dropdownItems were retrieved, reset active item
if (scope.allowCustomInput) {
scope.setInputActive();
}
else {
scope.setActive(0);
}
}
});
scope.$watch('selectedItem', function(newValue, oldValue) {
inputScope.updateInputValidity();
if (!angular.equals(newValue, oldValue)) {
if (newValue) {
// Update value in input field to match readableName of selected item
if (typeof newValue === 'string') {
scope.inputValue = newValue;
}
else {
scope.inputValue = newValue.readableName;
}
}
else {
// Uncomment to clear input field when editing it after making a selection
// scope.inputValue = '';
}
}
});
scope.setInputActive = function() {
scope.setActive(-1);
//TODO: Add active/selected class to input field for styling
};
scope.setActive = function(itemIndex) {
scope.activeItemIndex = itemIndex;
};
scope.inputChange = function() {
scope.selectedItem = null;
showDropdown();
if (!scope.inputValue) {
scope.dropdownItems = scope.defaultDropdownItems || [];
return;
}
else if (scope.allowCustomInput) {
inputScope.updateInputValidity();
}
if (scope.filterListMethod) {
var promise = scope.filterListMethod({userInput: scope.inputValue});
if (promise) {
promise.then(function(dropdownItems) {
scope.dropdownItems = dropdownItems;
});
}
}
};
scope.inputFocus = function() {
if (scope.allowCustomInput) {
scope.setInputActive();
}
else {
scope.setActive(0);
}
showDropdown();
};
scope.inputBlur = function(event) {
if (pressedDropdown) {
// Blur event is triggered before click event, which means a click on a dropdown item wont be triggered if we hide the dropdown list here.
pressedDropdown = false;
return;
}
hideDropdown();
};
scope.dropdownPressed = function() {
pressedDropdown = true;
};
scope.selectItem = function(item) {
scope.selectedItem = item;
hideDropdown();
scope.dropdownItems = [item];
if (scope.itemSelectedMethod) {
scope.itemSelectedMethod({item: item});
}
};
var showDropdown = function () {
scope.dropdownVisible = true;
};
var hideDropdown = function() {
scope.dropdownVisible = false;
};
var selectPreviousItem = function() {
var prevIndex = scope.activeItemIndex - 1;
if (prevIndex >= 0) {
scope.setActive(prevIndex);
}
else if (scope.allowCustomInput) {
scope.setInputActive();
}
};
var selectNextItem = function() {
var nextIndex = scope.activeItemIndex + 1;
if (nextIndex < scope.dropdownItems.length) {
scope.setActive(nextIndex);
}
};
var selectActiveItem = function() {
if (scope.activeItemIndex >= 0 && scope.activeItemIndex < scope.dropdownItems.length) {
scope.selectItem(scope.dropdownItems[scope.activeItemIndex]);
}
else if (scope.allowCustomInput && scope.activeItemIndex === -1) {
//TODO: Select user input. Do we need to call the controller here (ie scope.itemSelectedMethod()) or is it enough to just leave the input value in the field?
}
};
element.bind("keydown keypress", function (event) {
switch (event.which) {
case 38: //up
scope.$apply(selectPreviousItem);
break;
case 40: //down
scope.$apply(selectNextItem);
break;
case 13: // return
if (scope.dropdownVisible && scope.dropdownItems && scope.dropdownItems.length > 0 && scope.activeItemIndex !== -1) {
// only preventDefault when there is a list so that we can submit form with return key after a selection is made
event.preventDefault();
scope.$apply(selectActiveItem);
}
break;
}
});
}
}
}]);
angular.module('inputDropdown').directive('inputDropdownValidator', function() {
return {
require: ['^inputDropdown', 'ngModel'],
restrict: 'A',
scope: {},
link: function(scope, element, attrs, ctrls) {
var inputDropdownCtrl = ctrls[0];
var ngModelCtrl = ctrls[1];
var validatorName = 'itemSelectedValid';
scope.updateInputValidity = function() {
var selection = inputDropdownCtrl.getSelectedItem();
var isValid = false;
if (!inputDropdownCtrl.isRequired()) {
// Input isn't required, so it's always valid
isValid = true;
}
else if (inputDropdownCtrl.customInputAllowed() && inputDropdownCtrl.getInput()) {
// Custom input is allowed so we just need to make sure the input field isn't empty
isValid = true;
}
else if (selection) {
// Input is required and custom input is not allowed, so only validate if an item is selected
isValid = true;
}
ngModelCtrl.$setValidity(validatorName, isValid);
};
}
};
});