2 * Angular Material Design
3 * https://github.com/angular/material
7 goog.provide('ngmaterial.components.checkbox');
8 goog.require('ngmaterial.core');
11 * @name material.components.checkbox
12 * @description Checkbox module!
14 MdCheckboxDirective['$inject'] = ["inputDirective", "$mdAria", "$mdConstant", "$mdTheming", "$mdUtil", "$mdInteraction"];
16 .module('material.components.checkbox', ['material.core'])
17 .directive('mdCheckbox', MdCheckboxDirective);
22 * @module material.components.checkbox
26 * The checkbox directive is used like the normal [angular checkbox](https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D).
28 * As per the [material design spec](http://www.google.com/design/spec/style/color.html#color-color-schemes)
29 * the checkbox is in the accent color by default. The primary color palette may be used with
30 * the `md-primary` class.
32 * @param {string} ng-model Assignable angular expression to data-bind to.
33 * @param {string=} name Property name of the form under which the control is published.
34 * @param {expression=} ng-true-value The value to which the expression should be set when selected.
35 * @param {expression=} ng-false-value The value to which the expression should be set when not selected.
36 * @param {string=} ng-change Angular expression to be executed when input changes due to user interaction with the input element.
37 * @param {boolean=} md-no-ink Use of attribute indicates use of ripple ink effects
38 * @param {string=} aria-label Adds label to checkbox for accessibility.
39 * Defaults to checkbox's text. If no default text is found, a warning will be logged.
40 * @param {expression=} md-indeterminate This determines when the checkbox should be rendered as 'indeterminate'.
41 * If a truthy expression or no value is passed in the checkbox renders in the md-indeterminate state.
42 * If falsy expression is passed in it just looks like a normal unchecked checkbox.
43 * The indeterminate, checked, and unchecked states are mutually exclusive. A box cannot be in any two states at the same time.
44 * Adding the 'md-indeterminate' attribute overrides any checked/unchecked rendering logic.
45 * When using the 'md-indeterminate' attribute use 'ng-checked' to define rendering logic instead of using 'ng-model'.
46 * @param {expression=} ng-checked If this expression evaluates as truthy, the 'md-checked' css class is added to the checkbox and it
47 * will appear checked.
51 * <md-checkbox ng-model="isChecked" aria-label="Finished?">
55 * <md-checkbox md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
59 * <md-checkbox ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
66 function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $mdInteraction) {
67 inputDirective = inputDirective[0];
72 require: ['^?mdInputContainer', '?ngModel', '?^form'],
73 priority: $mdConstant.BEFORE_NG_ARIA,
75 '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
76 '<div class="md-icon"></div>' +
78 '<div ng-transclude class="md-label"></div>',
82 // **********************************************************
84 // **********************************************************
86 function compile (tElement, tAttrs) {
87 tAttrs.$set('tabindex', tAttrs.tabindex || '0');
88 tAttrs.$set('type', 'checkbox');
89 tAttrs.$set('role', tAttrs.type);
92 pre: function(scope, element) {
93 // Attach a click handler during preLink, in order to immediately stop propagation
94 // (especially for ng-click) when the checkbox is disabled.
95 element.on('click', function(e) {
96 if (this.hasAttribute('disabled')) {
97 e.stopImmediatePropagation();
104 function postLink(scope, element, attr, ctrls) {
106 var containerCtrl = ctrls[0];
107 var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
108 var formCtrl = ctrls[2];
111 var isErrorGetter = containerCtrl.isErrorGetter || function() {
112 return ngModelCtrl.$invalid && (ngModelCtrl.$touched || (formCtrl && formCtrl.$submitted));
115 containerCtrl.input = element;
117 scope.$watch(isErrorGetter, containerCtrl.setInvalid);
122 // Redirect focus events to the root element, because IE11 is always focusing the container element instead
123 // of the md-checkbox element. This causes issues when using ngModelOptions: `updateOnBlur`
124 element.children().on('focus', function() {
128 if ($mdUtil.parseAttributeBoolean(attr.mdIndeterminate)) {
129 setIndeterminateState();
130 scope.$watch(attr.mdIndeterminate, setIndeterminateState);
133 if (attr.ngChecked) {
134 scope.$watch(scope.$eval.bind(scope, attr.ngChecked), function(value) {
135 ngModelCtrl.$setViewValue(value);
136 ngModelCtrl.$render();
140 $$watchExpr('ngDisabled', 'tabindex', {
145 $mdAria.expectWithText(element, 'aria-label');
147 // Reuse the original input[type=checkbox] directive from Angular core.
148 // This is a bit hacky as we need our own event listener and own render
150 inputDirective.link.pre(scope, {
153 }, attr, [ngModelCtrl]);
155 element.on('click', listener)
156 .on('keypress', keypressHandler)
157 .on('focus', function() {
158 if ($mdInteraction.getLastInteractionType() === 'keyboard') {
159 element.addClass('md-focused');
162 .on('blur', function() {
163 element.removeClass('md-focused');
166 ngModelCtrl.$render = render;
168 function $$watchExpr(expr, htmlAttr, valueOpts) {
170 scope.$watch(attr[expr], function(val) {
171 if (valueOpts[val]) {
172 element.attr(htmlAttr, valueOpts[val]);
178 function keypressHandler(ev) {
179 var keyCode = ev.which || ev.keyCode;
180 if (keyCode === $mdConstant.KEY_CODE.SPACE || keyCode === $mdConstant.KEY_CODE.ENTER) {
182 element.addClass('md-focused');
187 function listener(ev) {
188 // skipToggle boolean is used by the switch directive to prevent the click event
189 // when releasing the drag. There will be always a click if releasing the drag over the checkbox
190 if (element[0].hasAttribute('disabled') || scope.skipToggle) {
194 scope.$apply(function() {
195 // Toggle the checkbox value...
196 var viewValue = attr.ngChecked ? attr.checked : !ngModelCtrl.$viewValue;
198 ngModelCtrl.$setViewValue(viewValue, ev && ev.type);
199 ngModelCtrl.$render();
204 // Cast the $viewValue to a boolean since it could be undefined
205 element.toggleClass('md-checked', !!ngModelCtrl.$viewValue && !isIndeterminate);
208 function setIndeterminateState(newValue) {
209 isIndeterminate = newValue !== false;
210 if (isIndeterminate) {
211 element.attr('aria-checked', 'mixed');
213 element.toggleClass('md-indeterminate', isIndeterminate);
219 ngmaterial.components.checkbox = angular.module("material.components.checkbox");