@@ -40,6 +40,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
40
40
ctrl . isRequired = null ;
41
41
ctrl . isReadonly = null ;
42
42
ctrl . hasNotFound = false ;
43
+ ctrl . selectedMessage = $scope . selectedMessage || 'selected' ;
43
44
44
45
// Public Exported Methods
45
46
ctrl . keydown = keydown ;
@@ -321,8 +322,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
321
322
if ( ! hidden && oldHidden ) {
322
323
positionDropdown ( ) ;
323
324
324
- // Report in polite mode, because the screenreader should finish the default description of
325
- // the input. element.
325
+ // Report in polite mode, because the screen reader should finish the default description of
326
+ // the input element.
326
327
reportMessages ( true , ReportType . Count | ReportType . Selected ) ;
327
328
328
329
if ( elements ) {
@@ -406,14 +407,17 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
406
407
} ) ;
407
408
}
408
409
409
- if ( selectedItem !== previousSelectedItem ) announceItemChange ( ) ;
410
+ if ( selectedItem !== previousSelectedItem ) {
411
+ announceItemChange ( ) ;
412
+ }
410
413
}
411
414
412
415
/**
413
416
* Use the user-defined expression to announce changes each time a new item is selected
414
417
*/
415
418
function announceItemChange ( ) {
416
- angular . isFunction ( $scope . itemChange ) && $scope . itemChange ( getItemAsNameVal ( $scope . selectedItem ) ) ;
419
+ angular . isFunction ( $scope . itemChange ) &&
420
+ $scope . itemChange ( getItemAsNameVal ( $scope . selectedItem ) ) ;
417
421
}
418
422
419
423
/**
@@ -430,15 +434,17 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
430
434
* @param previousSelectedItem
431
435
*/
432
436
function handleSelectedItemChange ( selectedItem , previousSelectedItem ) {
433
- selectedItemWatchers . forEach ( function ( watcher ) { watcher ( selectedItem , previousSelectedItem ) ; } ) ;
437
+ selectedItemWatchers . forEach ( function ( watcher ) {
438
+ watcher ( selectedItem , previousSelectedItem ) ;
439
+ } ) ;
434
440
}
435
441
436
442
/**
437
443
* Register a function to be called when the selected item changes.
438
444
* @param cb
439
445
*/
440
446
function registerSelectedItemWatcher ( cb ) {
441
- if ( selectedItemWatchers . indexOf ( cb ) == - 1 ) {
447
+ if ( selectedItemWatchers . indexOf ( cb ) === - 1 ) {
442
448
selectedItemWatchers . push ( cb ) ;
443
449
}
444
450
}
@@ -449,7 +455,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
449
455
*/
450
456
function unregisterSelectedItemWatcher ( cb ) {
451
457
var i = selectedItemWatchers . indexOf ( cb ) ;
452
- if ( i != - 1 ) {
458
+ if ( i !== - 1 ) {
453
459
selectedItemWatchers . splice ( i , 1 ) ;
454
460
}
455
461
}
@@ -472,9 +478,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
472
478
if ( searchText !== val ) {
473
479
$scope . selectedItem = null ;
474
480
475
-
476
481
// trigger change event if available
477
- if ( searchText !== previousSearchText ) announceTextChange ( ) ;
482
+ if ( searchText !== previousSearchText ) {
483
+ announceTextChange ( ) ;
484
+ }
478
485
479
486
// cancel results if search text is not long enough
480
487
if ( ! isMinLengthMet ( ) ) {
@@ -617,6 +624,8 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
617
624
/**
618
625
* Getter function to invoke user-defined expression (in the directive)
619
626
* to convert your object to a single string.
627
+ * @param item
628
+ * @returns {string|null }
620
629
*/
621
630
function getItemText ( item ) {
622
631
return ( item && $scope . itemText ) ? $scope . itemText ( getItemAsNameVal ( item ) ) : null ;
@@ -626,20 +635,24 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
626
635
/**
627
636
* Returns the locals object for compiling item templates.
628
637
* @param item
629
- * @returns {{} }
638
+ * @returns {Object|undefined }
630
639
*/
631
640
function getItemAsNameVal ( item ) {
632
- if ( ! item ) return undefined ;
641
+ if ( ! item ) {
642
+ return undefined ;
643
+ }
633
644
634
645
var locals = { } ;
635
- if ( ctrl . itemName ) locals [ ctrl . itemName ] = item ;
646
+ if ( ctrl . itemName ) {
647
+ locals [ ctrl . itemName ] = item ;
648
+ }
636
649
637
650
return locals ;
638
651
}
639
652
640
653
/**
641
654
* Returns the default index based on whether or not autoselect is enabled.
642
- * @returns {number }
655
+ * @returns {number } 0 if autoselect is enabled, -1 if not.
643
656
*/
644
657
function getDefaultIndex ( ) {
645
658
return $scope . autoselect ? 0 : - 1 ;
@@ -650,7 +663,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
650
663
* @param value {boolean} Whether or not the component is currently loading.
651
664
*/
652
665
function setLoading ( value ) {
653
- if ( ctrl . loading != value ) {
666
+ if ( ctrl . loading !== value ) {
654
667
ctrl . loading = value ;
655
668
}
656
669
@@ -718,40 +731,36 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
718
731
}
719
732
720
733
/**
721
- * Returns true if the search text has matches.
722
- * @returns {boolean }
734
+ * @returns {boolean } true if the search text has matches.
723
735
*/
724
736
function hasMatches ( ) {
725
737
return ctrl . matches . length ? true : false ;
726
738
}
727
739
728
740
/**
729
- * Returns true if the autocomplete has a valid selection.
730
- * @returns {boolean }
741
+ * @returns {boolean } true if the autocomplete has a valid selection.
731
742
*/
732
743
function hasSelection ( ) {
733
744
return ctrl . scope . selectedItem ? true : false ;
734
745
}
735
746
736
747
/**
737
- * Returns true if the loading indicator is, or should be, visible.
738
- * @returns {boolean }
748
+ * @returns {boolean } true if the loading indicator is, or should be, visible.
739
749
*/
740
750
function loadingIsVisible ( ) {
741
751
return ctrl . loading && ! hasSelection ( ) ;
742
752
}
743
753
744
754
/**
745
- * Returns the display value of the current item.
746
- * @returns {* }
755
+ * @returns {* } the display value of the current item.
747
756
*/
748
757
function getCurrentDisplayValue ( ) {
749
758
return getDisplayValue ( ctrl . matches [ ctrl . index ] ) ;
750
759
}
751
760
752
761
/**
753
762
* Determines if the minimum length is met by the search text.
754
- * @returns {* }
763
+ * @returns {* } true if the minimum length is met by the search text
755
764
*/
756
765
function isMinLengthMet ( ) {
757
766
return ( $scope . searchText || '' ) . length >= getMinLength ( ) ;
@@ -761,9 +770,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
761
770
762
771
/**
763
772
* Defines a public property with a handler and a default value.
764
- * @param key
765
- * @param handler
766
- * @param value
773
+ * @param { string } key
774
+ * @param { Function } handler function
775
+ * @param { * } value default value
767
776
*/
768
777
function defineProperty ( key , handler , value ) {
769
778
Object . defineProperty ( ctrl , key , {
@@ -778,13 +787,14 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
778
787
779
788
/**
780
789
* Selects the item at the given index.
781
- * @param index
790
+ * @param { number } index to select
782
791
*/
783
792
function select ( index ) {
784
793
//-- force form to update state for validation
785
794
$mdUtil . nextTick ( function ( ) {
786
795
getDisplayValue ( ctrl . matches [ index ] ) . then ( function ( val ) {
787
796
var ngModel = elements . $ . input . controller ( 'ngModel' ) ;
797
+ $mdLiveAnnouncer . announce ( val + ' ' + ctrl . selectedMessage , 'assertive' ) ;
788
798
ngModel . $setViewValue ( val ) ;
789
799
ngModel . $render ( ) ;
790
800
} ) . finally ( function ( ) {
@@ -884,12 +894,11 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
884
894
885
895
886
896
/**
887
- * Reports given message types to supported screenreaders .
897
+ * Reports given message types to supported screen readers .
888
898
* @param {boolean } isPolite Whether the announcement should be polite.
889
- * @param {!number } types Message flags to be reported to the screenreader .
899
+ * @param {!number } types Message flags to be reported to the screen reader .
890
900
*/
891
901
function reportMessages ( isPolite , types ) {
892
-
893
902
var politeness = isPolite ? 'polite' : 'assertive' ;
894
903
var messages = [ ] ;
895
904
@@ -904,12 +913,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
904
913
$q . all ( messages ) . then ( function ( data ) {
905
914
$mdLiveAnnouncer . announce ( data . join ( ' ' ) , politeness ) ;
906
915
} ) ;
907
-
908
916
}
909
917
910
918
/**
911
- * Returns the ARIA message for how many results match the current query.
912
- * @returns {* }
919
+ * @returns {string } the ARIA message for how many results match the current query.
913
920
*/
914
921
function getCountMessage ( ) {
915
922
switch ( ctrl . matches . length ) {
@@ -1000,12 +1007,14 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
1000
1007
matches = ctrl . matches ,
1001
1008
item = matches [ 0 ] ;
1002
1009
if ( matches . length === 1 ) getDisplayValue ( item ) . then ( function ( displayValue ) {
1003
- var isMatching = searchText == displayValue ;
1010
+ var isMatching = searchText === displayValue ;
1004
1011
if ( $scope . matchInsensitive && ! isMatching ) {
1005
- isMatching = searchText . toLowerCase ( ) == displayValue . toLowerCase ( ) ;
1012
+ isMatching = searchText . toLowerCase ( ) === displayValue . toLowerCase ( ) ;
1006
1013
}
1007
1014
1008
- if ( isMatching ) select ( 0 ) ;
1015
+ if ( isMatching ) {
1016
+ select ( 0 ) ;
1017
+ }
1009
1018
} ) ;
1010
1019
}
1011
1020
0 commit comments