@@ -31,10 +31,10 @@ angular
31
31
* @param {expression= } md-indeterminate This determines when the checkbox should be rendered as 'indeterminate'.
32
32
* If a truthy expression or no value is passed in the checkbox renders in the md-indeterminate state.
33
33
* If falsy expression is passed in it just looks like a normal unchecked checkbox.
34
- * The indeterminate, checked, and unchecked states are mutually exclusive. A box cannot be in any two states at the same time.
35
- * Adding the 'md-indeterminate' attribute overrides any checked/unchecked rendering logic.
34
+ * The indeterminate, checked, and unchecked states are mutually exclusive. A box cannot be in any two states at the same time.
35
+ * Adding the 'md-indeterminate' attribute overrides any checked/unchecked rendering logic.
36
36
* When using the 'md-indeterminate' attribute use 'ng-checked' to define rendering logic instead of using 'ng-model'.
37
- * @param {expression= } ng-checked If this expression evaluates as truthy, the 'md-checked' css class is added to the checkbox and it
37
+ * @param {expression= } ng-checked If this expression evaluates as truthy, the 'md-checked' css class is added to the checkbox and it
38
38
* will appear checked.
39
39
*
40
40
* @usage
@@ -56,7 +56,6 @@ angular
56
56
*/
57
57
function MdCheckboxDirective ( inputDirective , $mdAria , $mdConstant , $mdTheming , $mdUtil , $timeout ) {
58
58
inputDirective = inputDirective [ 0 ] ;
59
- var CHECKED_CSS = 'md-checked' ;
60
59
61
60
return {
62
61
restrict : 'E' ,
@@ -76,32 +75,35 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
76
75
// **********************************************************
77
76
78
77
function compile ( tElement , tAttrs ) {
79
- var container = tElement . children ( ) ;
80
- var mdIndeterminateStateEnabled = $mdUtil . parseAttributeBoolean ( tAttrs . mdIndeterminate ) ;
81
-
82
78
tAttrs . $set ( 'tabindex' , tAttrs . tabindex || '0' ) ;
83
79
tAttrs . $set ( 'type' , 'checkbox' ) ;
84
80
tAttrs . $set ( 'role' , tAttrs . type ) ;
85
81
86
- // Attach a click handler in compile in order to immediately stop propagation
87
- // (especially for ng-click) when the checkbox is disabled.
88
- tElement . on ( 'click' , function ( event ) {
89
- if ( this . hasAttribute ( 'disabled' ) ) {
90
- event . stopImmediatePropagation ( ) ;
91
- }
92
- } ) ;
93
-
94
- // Redirect focus events to the root element, because IE11 is always focusing the container element instead
95
- // of the md-checkbox element. This causes issues when using ngModelOptions: `updateOnBlur`
96
- container . on ( 'focus' , function ( ) {
97
- tElement . focus ( ) ;
98
- } ) ;
82
+ return {
83
+ pre : function ( scope , element ) {
84
+ // Attach a click handler during preLink, in order to immediately stop propagation
85
+ // (especially for ng-click) when the checkbox is disabled.
86
+ element . on ( 'click' , function ( e ) {
87
+ if ( this . hasAttribute ( 'disabled' ) ) {
88
+ e . stopImmediatePropagation ( ) ;
89
+ }
90
+ } ) ;
91
+ } ,
92
+ post : postLink
93
+ } ;
99
94
100
- return function postLink ( scope , element , attr , ngModelCtrl ) {
95
+ function postLink ( scope , element , attr , ngModelCtrl ) {
101
96
var isIndeterminate ;
102
97
ngModelCtrl = ngModelCtrl || $mdUtil . fakeNgModel ( ) ;
103
98
$mdTheming ( element ) ;
104
- if ( mdIndeterminateStateEnabled ) {
99
+
100
+ // Redirect focus events to the root element, because IE11 is always focusing the container element instead
101
+ // of the md-checkbox element. This causes issues when using ngModelOptions: `updateOnBlur`
102
+ element . children ( ) . on ( 'focus' , function ( ) {
103
+ element . focus ( ) ;
104
+ } ) ;
105
+
106
+ if ( $mdUtil . parseAttributeBoolean ( attr . mdIndeterminate ) ) {
105
107
setIndeterminateState ( ) ;
106
108
scope . $watch ( attr . mdIndeterminate , setIndeterminateState ) ;
107
109
}
@@ -162,11 +164,7 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
162
164
var keyCode = ev . which || ev . keyCode ;
163
165
if ( keyCode === $mdConstant . KEY_CODE . SPACE || keyCode === $mdConstant . KEY_CODE . ENTER ) {
164
166
ev . preventDefault ( ) ;
165
-
166
- if ( ! element . hasClass ( 'md-focused' ) ) {
167
- element . addClass ( 'md-focused' ) ;
168
- }
169
-
167
+ element . addClass ( 'md-focused' ) ;
170
168
listener ( ev ) ;
171
169
}
172
170
}
@@ -182,17 +180,13 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
182
180
// Toggle the checkbox value...
183
181
var viewValue = attr . ngChecked ? attr . checked : ! ngModelCtrl . $viewValue ;
184
182
185
- ngModelCtrl . $setViewValue ( viewValue , ev && ev . type ) ;
183
+ ngModelCtrl . $setViewValue ( viewValue , ev && ev . type ) ;
186
184
ngModelCtrl . $render ( ) ;
187
185
} ) ;
188
186
}
189
187
190
188
function render ( ) {
191
- if ( ngModelCtrl . $viewValue && ! isIndeterminate ) {
192
- element . addClass ( CHECKED_CSS ) ;
193
- } else {
194
- element . removeClass ( CHECKED_CSS ) ;
195
- }
189
+ element . toggleClass ( 'md-checked' , ngModelCtrl . $viewValue && ! isIndeterminate ) ;
196
190
}
197
191
198
192
function setIndeterminateState ( newValue ) {
0 commit comments