@@ -7,9 +7,9 @@ var ITEM_HEIGHT = 41,
7
7
MENU_PADDING = 8 ;
8
8
9
9
function MdAutocompleteCtrl ( $scope , $element , $mdUtil , $mdConstant , $timeout , $mdTheming , $window ,
10
- $animate , $rootElement ) {
10
+ $animate , $rootElement , $attrs ) {
11
11
//-- private variables
12
- var self = this ,
12
+ var ctrl = this ,
13
13
itemParts = $scope . itemsExpr . split ( / i n / i) ,
14
14
itemExpr = itemParts [ 1 ] ,
15
15
elements = null ,
@@ -24,28 +24,30 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
24
24
defineProperty ( 'hidden' , handleHiddenChange , true ) ;
25
25
26
26
//-- public variables
27
- self . scope = $scope ;
28
- self . parent = $scope . $parent ;
29
- self . itemName = itemParts [ 0 ] ;
30
- self . matches = [ ] ;
31
- self . loading = false ;
32
- self . hidden = true ;
33
- self . index = null ;
34
- self . messages = [ ] ;
35
- self . id = $mdUtil . nextUid ( ) ;
27
+ ctrl . scope = $scope ;
28
+ ctrl . parent = $scope . $parent ;
29
+ ctrl . itemName = itemParts [ 0 ] ;
30
+ ctrl . matches = [ ] ;
31
+ ctrl . loading = false ;
32
+ ctrl . hidden = true ;
33
+ ctrl . index = null ;
34
+ ctrl . messages = [ ] ;
35
+ ctrl . id = $mdUtil . nextUid ( ) ;
36
+ ctrl . isDisabled = null ;
37
+ ctrl . isRequired = null ;
36
38
37
39
//-- public methods
38
- self . keydown = keydown ;
39
- self . blur = blur ;
40
- self . focus = focus ;
41
- self . clear = clearValue ;
42
- self . select = select ;
43
- self . listEnter = onListEnter ;
44
- self . listLeave = onListLeave ;
45
- self . mouseUp = onMouseup ;
46
- self . getCurrentDisplayValue = getCurrentDisplayValue ;
47
- self . registerSelectedItemWatcher = registerSelectedItemWatcher ;
48
- self . unregisterSelectedItemWatcher = unregisterSelectedItemWatcher ;
40
+ ctrl . keydown = keydown ;
41
+ ctrl . blur = blur ;
42
+ ctrl . focus = focus ;
43
+ ctrl . clear = clearValue ;
44
+ ctrl . select = select ;
45
+ ctrl . listEnter = onListEnter ;
46
+ ctrl . listLeave = onListLeave ;
47
+ ctrl . mouseUp = onMouseup ;
48
+ ctrl . getCurrentDisplayValue = getCurrentDisplayValue ;
49
+ ctrl . registerSelectedItemWatcher = registerSelectedItemWatcher ;
50
+ ctrl . unregisterSelectedItemWatcher = unregisterSelectedItemWatcher ;
49
51
50
52
return init ( ) ;
51
53
@@ -55,6 +57,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
55
57
* Initialize the controller, setup watchers, gather elements
56
58
*/
57
59
function init ( ) {
60
+ $mdUtil . initOptionalProperties ( $scope , $attrs , { searchText : null , selectedItem : null } ) ;
61
+ $mdTheming ( $element ) ;
58
62
configureWatchers ( ) ;
59
63
$timeout ( function ( ) {
60
64
gatherElements ( ) ;
@@ -129,9 +133,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
129
133
*/
130
134
function configureWatchers ( ) {
131
135
var wait = parseInt ( $scope . delay , 10 ) || 0 ;
132
- $scope . $watch ( 'searchText ', wait
133
- ? $mdUtil . debounce ( handleSearchText , wait )
134
- : handleSearchText ) ;
136
+ $attrs . $observe ( 'disabled ', function ( value ) { ctrl . isDisabled = value ; } ) ;
137
+ $attrs . $observe ( 'required' , function ( value ) { ctrl . isRequired = value !== null ; } ) ;
138
+ $scope . $watch ( 'searchText' , wait ? $mdUtil . debounce ( handleSearchText , wait ) : handleSearchText ) ;
135
139
registerSelectedItemWatcher ( selectedItemChange ) ;
136
140
$scope . $watch ( 'selectedItem' , handleSelectedItemChange ) ;
137
141
angular . element ( $window ) . on ( 'resize' , positionDropdown ) ;
@@ -195,6 +199,11 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
195
199
*/
196
200
function handleHiddenChange ( hidden , oldHidden ) {
197
201
if ( ! hidden && oldHidden ) positionDropdown ( ) ;
202
+ if ( ! hidden ) {
203
+ if ( elements ) $mdUtil . disableScrollAround ( elements . ul ) ;
204
+ } else {
205
+ $mdUtil . enableScrolling ( ) ;
206
+ }
198
207
}
199
208
200
209
/**
@@ -209,7 +218,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
209
218
*/
210
219
function onListLeave ( ) {
211
220
noBlur = false ;
212
- if ( ! hasFocus ) self . hidden = true ;
221
+ if ( ! hasFocus ) ctrl . hidden = true ;
213
222
}
214
223
215
224
/**
@@ -271,7 +280,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
271
280
* @param previousSearchText
272
281
*/
273
282
function handleSearchText ( searchText , previousSearchText ) {
274
- self . index = getDefaultIndex ( ) ;
283
+ ctrl . index = getDefaultIndex ( ) ;
275
284
//-- do nothing on init
276
285
if ( searchText === previousSearchText ) return ;
277
286
//-- clear selected item if search text no longer matches it
@@ -282,9 +291,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
282
291
$scope . textChange ( getItemScope ( $scope . selectedItem ) ) ;
283
292
//-- cancel results if search text is not long enough
284
293
if ( ! isMinLengthMet ( ) ) {
285
- self . loading = false ;
286
- self . matches = [ ] ;
287
- self . hidden = shouldHide ( ) ;
294
+ ctrl . loading = false ;
295
+ ctrl . matches = [ ] ;
296
+ ctrl . hidden = shouldHide ( ) ;
288
297
updateMessages ( ) ;
289
298
} else {
290
299
handleQuery ( ) ;
@@ -296,7 +305,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
296
305
*/
297
306
function blur ( ) {
298
307
hasFocus = false ;
299
- if ( ! noBlur ) self . hidden = true ;
308
+ if ( ! noBlur ) ctrl . hidden = true ;
300
309
}
301
310
302
311
/**
@@ -307,8 +316,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
307
316
//-- if searchText is null, let's force it to be a string
308
317
if ( ! angular . isString ( $scope . searchText ) ) $scope . searchText = '' ;
309
318
if ( $scope . minLength > 0 ) return ;
310
- self . hidden = shouldHide ( ) ;
311
- if ( ! self . hidden ) handleQuery ( ) ;
319
+ ctrl . hidden = shouldHide ( ) ;
320
+ if ( ! ctrl . hidden ) handleQuery ( ) ;
312
321
}
313
322
314
323
/**
@@ -318,29 +327,29 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
318
327
function keydown ( event ) {
319
328
switch ( event . keyCode ) {
320
329
case $mdConstant . KEY_CODE . DOWN_ARROW :
321
- if ( self . loading ) return ;
330
+ if ( ctrl . loading ) return ;
322
331
event . preventDefault ( ) ;
323
- self . index = Math . min ( self . index + 1 , self . matches . length - 1 ) ;
332
+ ctrl . index = Math . min ( ctrl . index + 1 , ctrl . matches . length - 1 ) ;
324
333
updateScroll ( ) ;
325
334
updateMessages ( ) ;
326
335
break ;
327
336
case $mdConstant . KEY_CODE . UP_ARROW :
328
- if ( self . loading ) return ;
337
+ if ( ctrl . loading ) return ;
329
338
event . preventDefault ( ) ;
330
- self . index = self . index < 0 ? self . matches . length - 1 : Math . max ( 0 , self . index - 1 ) ;
339
+ ctrl . index = ctrl . index < 0 ? ctrl . matches . length - 1 : Math . max ( 0 , ctrl . index - 1 ) ;
331
340
updateScroll ( ) ;
332
341
updateMessages ( ) ;
333
342
break ;
334
343
case $mdConstant . KEY_CODE . TAB :
335
344
case $mdConstant . KEY_CODE . ENTER :
336
- if ( self . hidden || self . loading || self . index < 0 || self . matches . length < 1 ) return ;
345
+ if ( ctrl . hidden || ctrl . loading || ctrl . index < 0 || ctrl . matches . length < 1 ) return ;
337
346
event . preventDefault ( ) ;
338
- select ( self . index ) ;
347
+ select ( ctrl . index ) ;
339
348
break ;
340
349
case $mdConstant . KEY_CODE . ESCAPE :
341
- self . matches = [ ] ;
342
- self . hidden = true ;
343
- self . index = getDefaultIndex ( ) ;
350
+ ctrl . matches = [ ] ;
351
+ ctrl . hidden = true ;
352
+ ctrl . index = getDefaultIndex ( ) ;
344
353
break ;
345
354
default :
346
355
}
@@ -373,7 +382,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
373
382
function getItemScope ( item ) {
374
383
if ( ! item ) return ;
375
384
var locals = { } ;
376
- if ( self . itemName ) locals [ self . itemName ] = item ;
385
+ if ( ctrl . itemName ) locals [ ctrl . itemName ] = item ;
377
386
return locals ;
378
387
}
379
388
@@ -398,7 +407,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
398
407
* @returns {* }
399
408
*/
400
409
function getCurrentDisplayValue ( ) {
401
- return getDisplayValue ( self . matches [ self . index ] ) ;
410
+ return getDisplayValue ( ctrl . matches [ ctrl . index ] ) ;
402
411
}
403
412
404
413
/**
@@ -418,7 +427,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
418
427
* @param value
419
428
*/
420
429
function defineProperty ( key , handler , value ) {
421
- Object . defineProperty ( self , key , {
430
+ Object . defineProperty ( ctrl , key , {
422
431
get : function ( ) { return value ; } ,
423
432
set : function ( newValue ) {
424
433
var oldValue = value ;
@@ -433,15 +442,15 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
433
442
* @param index
434
443
*/
435
444
function select ( index ) {
436
- $scope . selectedItem = self . matches [ index ] ;
437
- self . hidden = true ;
438
- self . index = 0 ;
439
- self . matches = [ ] ;
445
+ $scope . selectedItem = ctrl . matches [ index ] ;
446
+ ctrl . hidden = true ;
447
+ ctrl . index = 0 ;
448
+ ctrl . matches = [ ] ;
440
449
//-- force form to update state for validation
441
450
$timeout ( function ( ) {
442
451
elements . $ . input . controller ( 'ngModel' ) . $setViewValue ( getDisplayValue ( $scope . selectedItem ) ||
443
452
$scope . searchText ) ;
444
- self . hidden = true ;
453
+ ctrl . hidden = true ;
445
454
} ) ;
446
455
}
447
456
@@ -470,18 +479,18 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
470
479
if ( angular . isArray ( items ) ) {
471
480
handleResults ( items ) ;
472
481
} else {
473
- self . loading = true ;
482
+ ctrl . loading = true ;
474
483
if ( items . success ) items . success ( handleResults ) ;
475
484
if ( items . then ) items . then ( handleResults ) ;
476
- if ( items . error ) items . error ( function ( ) { self . loading = false ; } ) ;
485
+ if ( items . error ) items . error ( function ( ) { ctrl . loading = false ; } ) ;
477
486
}
478
487
function handleResults ( matches ) {
479
488
cache [ term ] = matches ;
480
489
if ( searchText !== $scope . searchText ) return ; //-- just cache the results if old request
481
- self . loading = false ;
490
+ ctrl . loading = false ;
482
491
promise = null ;
483
- self . matches = matches ;
484
- self . hidden = shouldHide ( ) ;
492
+ ctrl . matches = matches ;
493
+ ctrl . hidden = shouldHide ( ) ;
485
494
updateMessages ( ) ;
486
495
positionDropdown ( ) ;
487
496
}
@@ -491,29 +500,29 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
491
500
* Updates the ARIA messages
492
501
*/
493
502
function updateMessages ( ) {
494
- self . messages = [ getCountMessage ( ) , getCurrentDisplayValue ( ) ] ;
503
+ ctrl . messages = [ getCountMessage ( ) , getCurrentDisplayValue ( ) ] ;
495
504
}
496
505
497
506
/**
498
507
* Returns the ARIA message for how many results match the current query.
499
508
* @returns {* }
500
509
*/
501
510
function getCountMessage ( ) {
502
- if ( lastCount === self . matches . length ) return '' ;
503
- lastCount = self . matches . length ;
504
- switch ( self . matches . length ) {
511
+ if ( lastCount === ctrl . matches . length ) return '' ;
512
+ lastCount = ctrl . matches . length ;
513
+ switch ( ctrl . matches . length ) {
505
514
case 0 : return 'There are no matches available.' ;
506
515
case 1 : return 'There is 1 match available.' ;
507
- default : return 'There are ' + self . matches . length + ' matches available.' ;
516
+ default : return 'There are ' + ctrl . matches . length + ' matches available.' ;
508
517
}
509
518
}
510
519
511
520
/**
512
521
* Makes sure that the focused element is within view.
513
522
*/
514
523
function updateScroll ( ) {
515
- if ( ! elements . li [ self . index ] ) return ;
516
- var li = elements . li [ self . index ] ,
524
+ if ( ! elements . li [ ctrl . index ] ) return ;
525
+ var li = elements . li [ ctrl . index ] ,
517
526
top = li . offsetTop ,
518
527
bot = top + li . offsetHeight ,
519
528
hgt = elements . ul . clientHeight ;
@@ -538,12 +547,12 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
538
547
}
539
548
//-- if results are cached, pull in cached results
540
549
if ( ! $scope . noCache && cache [ term ] ) {
541
- self . matches = cache [ term ] ;
550
+ ctrl . matches = cache [ term ] ;
542
551
updateMessages ( ) ;
543
552
} else {
544
553
fetchResults ( searchText ) ;
545
554
}
546
- if ( hasFocus ) self . hidden = shouldHide ( ) ;
555
+ if ( hasFocus ) ctrl . hidden = shouldHide ( ) ;
547
556
}
548
557
549
558
}
0 commit comments