@@ -116,6 +116,8 @@ angular.module('ui.select', [])
116
116
ctrl . items = [ ] ;
117
117
ctrl . selected = undefined ;
118
118
ctrl . open = false ;
119
+ ctrl . focus = false ;
120
+ ctrl . focusser = undefined ; //Reference to input element used to handle focus events
119
121
ctrl . disabled = undefined ; // Initialized inside uiSelect directive link function
120
122
ctrl . resetSearchInput = undefined ; // Initialized inside uiSelect directive link function
121
123
ctrl . refreshDelay = undefined ; // Initialized inside uiSelectChoices directive link function
@@ -137,13 +139,14 @@ angular.module('ui.select', [])
137
139
}
138
140
139
141
// When the user clicks on ui-select, displays the dropdown list
140
- ctrl . activate = function ( ) {
142
+ ctrl . activate = function ( initSearchValue ) {
141
143
if ( ! ctrl . disabled ) {
142
144
_resetSearchInput ( ) ;
143
145
ctrl . open = true ;
144
146
145
147
// Give it time to appear before focus
146
148
$timeout ( function ( ) {
149
+ ctrl . search = initSearchValue || ctrl . search ;
147
150
_searchInput [ 0 ] . focus ( ) ;
148
151
} ) ;
149
152
}
@@ -206,6 +209,7 @@ angular.module('ui.select', [])
206
209
if ( ctrl . open ) {
207
210
_resetSearchInput ( ) ;
208
211
ctrl . open = false ;
212
+ ctrl . focusser [ 0 ] . focus ( ) ;
209
213
}
210
214
} ;
211
215
@@ -288,8 +292,8 @@ angular.module('ui.select', [])
288
292
} ] )
289
293
290
294
. directive ( 'uiSelect' ,
291
- [ '$document' , 'uiSelectConfig' , 'uiSelectMinErr' ,
292
- function ( $document , uiSelectConfig , uiSelectMinErr ) {
295
+ [ '$document' , 'uiSelectConfig' , 'uiSelectMinErr' , '$compile' ,
296
+ function ( $document , uiSelectConfig , uiSelectMinErr , $compile ) {
293
297
294
298
return {
295
299
restrict : 'EA' ,
@@ -309,6 +313,96 @@ angular.module('ui.select', [])
309
313
var $select = ctrls [ 0 ] ;
310
314
var ngModel = ctrls [ 1 ] ;
311
315
316
+ //Idea from: https://github.com/ivaynberg/select2/blob/79b5bf6db918d7560bdd959109b7bcfb47edaf43/select2.js#L1954
317
+ var focusser = angular . element ( "<input ng-disabled='$select.disabled' class='ui-select-focusser ui-select-offscreen' type='text' aria-haspopup='true' role='button' />" ) ;
318
+ $compile ( focusser ) ( scope ) ;
319
+ $select . focusser = focusser ;
320
+
321
+ element . append ( focusser ) ;
322
+ focusser . bind ( "focus" , function ( ) {
323
+ scope . $evalAsync ( function ( ) {
324
+ $select . focus = true ;
325
+ } ) ;
326
+ } ) ;
327
+ focusser . bind ( "blur" , function ( ) {
328
+ scope . $evalAsync ( function ( ) {
329
+ $select . focus = false ;
330
+ } ) ;
331
+ } ) ;
332
+ focusser . bind ( "keydown" , function ( e ) {
333
+
334
+ if ( e . which === KEY . TAB || KEY . isControl ( e ) || KEY . isFunctionKey ( e ) || e . which === KEY . ESC ) {
335
+ return ;
336
+ }
337
+
338
+ if ( e . which == KEY . DOWN || e . which == KEY . UP || e . which == KEY . ENTER ) {
339
+ $select . activate ( ) ;
340
+ }
341
+
342
+ scope . $digest ( ) ;
343
+ } ) ;
344
+
345
+ focusser . bind ( "keyup input" , function ( e ) {
346
+
347
+ if ( e . which === KEY . TAB || KEY . isControl ( e ) || KEY . isFunctionKey ( e ) || e . which === KEY . ESC || e . which == KEY . ENTER ) {
348
+ return ;
349
+ }
350
+
351
+ $select . activate ( focusser . val ( ) ) ; //User pressed some regualar key, so we pass it to the search input
352
+ focusser . val ( '' ) ;
353
+ scope . $digest ( ) ;
354
+
355
+ } ) ;
356
+
357
+ //TODO Refactor to reuse the KEY object from uiSelectCtrl
358
+ var KEY = {
359
+ TAB : 9 ,
360
+ ENTER : 13 ,
361
+ ESC : 27 ,
362
+ SPACE : 32 ,
363
+ LEFT : 37 ,
364
+ UP : 38 ,
365
+ RIGHT : 39 ,
366
+ DOWN : 40 ,
367
+ SHIFT : 16 ,
368
+ CTRL : 17 ,
369
+ ALT : 18 ,
370
+ PAGE_UP : 33 ,
371
+ PAGE_DOWN : 34 ,
372
+ HOME : 36 ,
373
+ END : 35 ,
374
+ BACKSPACE : 8 ,
375
+ DELETE : 46 ,
376
+ isArrow : function ( k ) {
377
+ k = k . which ? k . which : k ;
378
+ switch ( k ) {
379
+ case KEY . LEFT :
380
+ case KEY . RIGHT :
381
+ case KEY . UP :
382
+ case KEY . DOWN :
383
+ return true ;
384
+ }
385
+ return false ;
386
+ } ,
387
+ isControl : function ( e ) {
388
+ var k = e . which ;
389
+ switch ( k ) {
390
+ case KEY . SHIFT :
391
+ case KEY . CTRL :
392
+ case KEY . ALT :
393
+ return true ;
394
+ }
395
+
396
+ if ( e . metaKey ) return true ;
397
+
398
+ return false ;
399
+ } ,
400
+ isFunctionKey : function ( k ) {
401
+ k = k . which ? k . which : k ;
402
+ return k >= 112 && k <= 123 ;
403
+ }
404
+ } ;
405
+
312
406
attrs . $observe ( 'disabled' , function ( ) {
313
407
// No need to use $eval() (thanks to ng-disabled) since we already get a boolean instead of a string
314
408
$select . disabled = attrs . disabled !== undefined ? attrs . disabled : false ;
0 commit comments