2 * Angular Material Design
3 * https://github.com/angular/material
7 (function( window, angular, undefined ){
12 * @name material.components.checkbox
13 * @description Checkbox module!
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-ui-color-application)
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.
43 * <md-checkbox ng-model="isChecked" aria-label="Finished?">
47 * <md-checkbox md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
51 * <md-checkbox ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
58 function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
59 inputDirective = inputDirective[0];
60 var CHECKED_CSS = 'md-checked';
66 priority:210, // Run before ngAria
68 '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
69 '<div class="md-icon"></div>' +
71 '<div ng-transclude class="md-label"></div>',
75 // **********************************************************
77 // **********************************************************
79 function compile (tElement, tAttrs) {
81 tAttrs.type = 'checkbox';
82 tAttrs.tabindex = tAttrs.tabindex || '0';
83 tElement.attr('role', tAttrs.type);
85 return function postLink(scope, element, attr, ngModelCtrl) {
86 ngModelCtrl = ngModelCtrl || $mdUtil.fakeNgModel();
91 scope.$eval.bind(scope, attr.ngChecked),
92 ngModelCtrl.$setViewValue.bind(ngModelCtrl)
95 $$watchExpr('ngDisabled', 'tabindex', {
99 $mdAria.expectWithText(element, 'aria-label');
101 // Reuse the original input[type=checkbox] directive from Angular core.
102 // This is a bit hacky as we need our own event listener and own render
104 inputDirective.link.pre(scope, {
107 }, attr, [ngModelCtrl]);
109 scope.mouseActive = false;
110 element.on('click', listener)
111 .on('keypress', keypressHandler)
112 .on('mousedown', function() {
113 scope.mouseActive = true;
115 scope.mouseActive = false;
118 .on('focus', function() {
119 if(scope.mouseActive === false) { element.addClass('md-focused'); }
121 .on('blur', function() { element.removeClass('md-focused'); });
123 ngModelCtrl.$render = render;
125 function $$watchExpr(expr, htmlAttr, valueOpts) {
127 scope.$watch(attr[expr], function(val) {
128 if (valueOpts[val]) {
129 element.attr(htmlAttr, valueOpts[val]);
135 function keypressHandler(ev) {
136 var keyCode = ev.which || ev.keyCode;
137 if (keyCode === $mdConstant.KEY_CODE.SPACE || keyCode === $mdConstant.KEY_CODE.ENTER) {
139 if (!element.hasClass('md-focused')) { element.addClass('md-focused'); }
143 function listener(ev) {
144 if (element[0].hasAttribute('disabled')) return;
146 scope.$apply(function() {
147 // Toggle the checkbox value...
148 var viewValue = attr.ngChecked ? attr.checked : !ngModelCtrl.$viewValue;
150 ngModelCtrl.$setViewValue( viewValue, ev && ev.type);
151 ngModelCtrl.$render();
156 if(ngModelCtrl.$viewValue) {
157 element.addClass(CHECKED_CSS);
159 element.removeClass(CHECKED_CSS);
165 MdCheckboxDirective.$inject = ["inputDirective", "$mdInkRipple", "$mdAria", "$mdConstant", "$mdTheming", "$mdUtil", "$timeout"];
167 })(window, window.angular);