@@ -72,6 +72,7 @@ angular.module('ui.select', [])
72
72
lhs ) ;
73
73
}
74
74
75
+ // Unused for now
75
76
var valueIdentifier = match [ 3 ] || match [ 1 ] ;
76
77
var keyIdentifier = match [ 2 ] ;
77
78
@@ -115,9 +116,9 @@ angular.module('ui.select', [])
115
116
ctrl . resetSearchInput = undefined ; // Initialized inside uiSelect directive link function
116
117
ctrl . refreshDelay = undefined ; // Initialized inside choices directive link function
117
118
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 ) ;
121
122
}
122
123
123
124
// 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', [])
135
136
136
137
// Give it time to appear before focus
137
138
$timeout ( function ( ) {
138
- ctrl . searchInput [ 0 ] . focus ( ) ;
139
+ _searchInput [ 0 ] . focus ( ) ;
139
140
} ) ;
140
141
}
141
142
} ;
@@ -172,13 +173,14 @@ angular.module('ui.select', [])
172
173
}
173
174
} ;
174
175
175
- // When the user clicks on an item inside the dropdown list
176
+ // When the user clicks on an item inside the dropdown
176
177
ctrl . select = function ( item ) {
177
178
ctrl . selected = item ;
178
179
ctrl . close ( ) ;
179
180
// Using a watch instead of $scope.ngModel.$setViewValue(item)
180
181
} ;
181
182
183
+ // Closes the dropdown
182
184
ctrl . close = function ( ) {
183
185
if ( ctrl . open ) {
184
186
_resetSearchInput ( ) ;
@@ -194,7 +196,7 @@ angular.module('ui.select', [])
194
196
Escape : 27
195
197
} ;
196
198
197
- ctrl . onKeydown = function ( key ) {
199
+ function _onKeydown ( key ) {
198
200
var processed = true ;
199
201
switch ( key ) {
200
202
case Key . Down :
@@ -214,12 +216,56 @@ angular.module('ui.select', [])
214
216
processed = false ;
215
217
}
216
218
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
+ } ) ;
218
264
} ] )
219
265
220
266
. directive ( 'uiSelect' ,
221
- [ '$document' , 'uiSelectConfig' , 'uiSelectMinErr' ,
222
- function ( $document , uiSelectConfig , uiSelectMinErr ) {
267
+ [ '$document' , 'uiSelectConfig' ,
268
+ function ( $document , uiSelectConfig ) {
223
269
224
270
return {
225
271
restrict : 'EA' ,
@@ -260,37 +306,6 @@ angular.module('ui.select', [])
260
306
$select . selected = ngModel . $viewValue ;
261
307
} ;
262
308
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
-
294
309
// See Click everywhere but here event http://stackoverflow.com/questions/12931369
295
310
$document . on ( 'mousedown' , function ( e ) {
296
311
var contains = false ;
@@ -310,7 +325,6 @@ angular.module('ui.select', [])
310
325
} ) ;
311
326
312
327
scope . $on ( '$destroy' , function ( ) {
313
- $select . searchInput . off ( 'keydown' ) ;
314
328
$document . off ( 'mousedown' ) ;
315
329
} ) ;
316
330
0 commit comments