Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 5890a88

Browse files
committed
Move ensureHighlightVisible inside the controller
1 parent 7c2a209 commit 5890a88

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

src/select.js

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ angular.module('ui.select', [])
7272
lhs);
7373
}
7474

75+
// Unused for now
7576
var valueIdentifier = match[3] || match[1];
7677
var keyIdentifier = match[2];
7778

@@ -115,9 +116,9 @@ angular.module('ui.select', [])
115116
ctrl.resetSearchInput = undefined; // Initialized inside uiSelect directive link function
116117
ctrl.refreshDelay = undefined; // Initialized inside choices directive link function
117118

118-
ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
119-
if (ctrl.searchInput.length !== 1) {
120-
throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length);
119+
var _searchInput = $element.querySelectorAll('input.ui-select-search');
120+
if (_searchInput.length !== 1) {
121+
throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", _searchInput.length);
121122
}
122123

123124
// Most of the time the user does not want to empty the search input when in typeahead mode
@@ -135,7 +136,7 @@ angular.module('ui.select', [])
135136

136137
// Give it time to appear before focus
137138
$timeout(function() {
138-
ctrl.searchInput[0].focus();
139+
_searchInput[0].focus();
139140
});
140141
}
141142
};
@@ -172,13 +173,14 @@ angular.module('ui.select', [])
172173
}
173174
};
174175

175-
// When the user clicks on an item inside the dropdown list
176+
// When the user clicks on an item inside the dropdown
176177
ctrl.select = function(item) {
177178
ctrl.selected = item;
178179
ctrl.close();
179180
// Using a watch instead of $scope.ngModel.$setViewValue(item)
180181
};
181182

183+
// Closes the dropdown
182184
ctrl.close = function() {
183185
if (ctrl.open) {
184186
_resetSearchInput();
@@ -194,7 +196,7 @@ angular.module('ui.select', [])
194196
Escape: 27
195197
};
196198

197-
ctrl.onKeydown = function(key) {
199+
function _onKeydown(key) {
198200
var processed = true;
199201
switch (key) {
200202
case Key.Down:
@@ -214,12 +216,56 @@ angular.module('ui.select', [])
214216
processed = false;
215217
}
216218
return processed;
217-
};
219+
}
220+
221+
// Bind to keyboard shortcuts
222+
// Cannot specify a namespace: not supported by jqLite
223+
_searchInput.on('keydown', function(e) {
224+
var key = e.which;
225+
226+
$scope.$apply(function() {
227+
var processed = _onKeydown(key);
228+
if (processed) {
229+
e.preventDefault();
230+
e.stopPropagation();
231+
}
232+
});
233+
234+
switch (key) {
235+
case Key.Down:
236+
case Key.Up:
237+
_ensureHighlightVisible();
238+
break;
239+
}
240+
});
241+
242+
// See https://github.com/ivaynberg/select2/blob/70873abe9d/select2.js#L1431
243+
function _ensureHighlightVisible() {
244+
var container = $element.querySelectorAll('.ui-select-choices-content');
245+
var rows = container.querySelectorAll('.ui-select-choices-row');
246+
if (rows.length < 1) {
247+
throw uiSelectMinErr('rows', "Expected multiple .ui-select-choices-row but got '{0}'.", rows.length);
248+
}
249+
250+
var highlighted = rows[ctrl.activeIndex];
251+
var posY = highlighted.offsetTop + highlighted.clientHeight - container[0].scrollTop;
252+
var height = container[0].offsetHeight;
253+
254+
if (posY > height) {
255+
container[0].scrollTop += posY - height;
256+
} else if (posY < highlighted.clientHeight) {
257+
container[0].scrollTop -= highlighted.clientHeight - posY;
258+
}
259+
}
260+
261+
$scope.$on('$destroy', function() {
262+
_searchInput.off('keydown');
263+
});
218264
}])
219265

220266
.directive('uiSelect',
221-
['$document', 'uiSelectConfig', 'uiSelectMinErr',
222-
function($document, uiSelectConfig, uiSelectMinErr) {
267+
['$document', 'uiSelectConfig',
268+
function($document, uiSelectConfig) {
223269

224270
return {
225271
restrict: 'EA',
@@ -260,37 +306,6 @@ angular.module('ui.select', [])
260306
$select.selected = ngModel.$viewValue;
261307
};
262308

263-
function ensureHighlightVisible() {
264-
var container = element.querySelectorAll('.ui-select-choices-content');
265-
var rows = container.querySelectorAll('.ui-select-choices-row');
266-
if (rows.length < 1) {
267-
throw uiSelectMinErr('rows', "Expected multiple .ui-select-choices-row but got '{0}'.", rows.length);
268-
}
269-
270-
var highlighted = rows[$select.activeIndex];
271-
var posY = highlighted.offsetTop + highlighted.clientHeight - container[0].scrollTop;
272-
var height = container[0].offsetHeight;
273-
274-
if (posY > height) {
275-
container[0].scrollTop += posY - height;
276-
} else if (posY < highlighted.clientHeight) {
277-
container[0].scrollTop -= highlighted.clientHeight - posY;
278-
}
279-
}
280-
281-
// Bind to keyboard shortcuts
282-
$select.searchInput.on('keydown', function(e) {
283-
scope.$apply(function() {
284-
var processed = $select.onKeydown(e.which);
285-
if (processed) {
286-
e.preventDefault();
287-
e.stopPropagation();
288-
289-
ensureHighlightVisible();
290-
}
291-
});
292-
});
293-
294309
// See Click everywhere but here event http://stackoverflow.com/questions/12931369
295310
$document.on('mousedown', function(e) {
296311
var contains = false;
@@ -310,7 +325,6 @@ angular.module('ui.select', [])
310325
});
311326

312327
scope.$on('$destroy', function() {
313-
$select.searchInput.off('keydown');
314328
$document.off('mousedown');
315329
});
316330

0 commit comments

Comments
 (0)