commonroutesApp/www/lib/angular-material/modules/closure/checkbox/checkbox.js
www/lib/angular-material/modules/closure/checkbox/checkbox.jsRaw
1/*!
2 * Angular Material Design
3 * https://github.com/angular/material
4 * @license MIT
5 * v1.1.1
6 */
7goog.provide('ngmaterial.components.checkbox');
8goog.require('ngmaterial.core');
9/**
10 * @ngdoc module
11 * @name material.components.checkbox
12 * @description Checkbox module!
13 */
14MdCheckboxDirective.$inject = ["inputDirective", "$mdAria", "$mdConstant", "$mdTheming", "$mdUtil", "$timeout"];
15angular
16 .module('material.components.checkbox', ['material.core'])
17 .directive('mdCheckbox', MdCheckboxDirective);
18
19/**
20 * @ngdoc directive
21 * @name mdCheckbox
22 * @module material.components.checkbox
23 * @restrict E
24 *
25 * @description
26 * The checkbox directive is used like the normal [angular checkbox](https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D).
27 *
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.
31 *
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.
48 *
49 * @usage
50 * <hljs lang="html">
51 * <md-checkbox ng-model="isChecked" aria-label="Finished?">
52 * Finished ?
53 * </md-checkbox>
54 *
55 * <md-checkbox md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
56 * No Ink Effects
57 * </md-checkbox>
58 *
59 * <md-checkbox ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
60 * Disabled
61 * </md-checkbox>
62 *
63 * </hljs>
64 *
65 */
66function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
67 inputDirective = inputDirective[0];
68
69 return {
70 restrict: 'E',
71 transclude: true,
72 require: '?ngModel',
73 priority: 210, // Run before ngAria
74 template:
75 '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
76 '<div class="md-icon"></div>' +
77 '</div>' +
78 '<div ng-transclude class="md-label"></div>',
79 compile: compile
80 };
81
82 // **********************************************************
83 // Private Methods
84 // **********************************************************
85
86 function compile (tElement, tAttrs) {
87 tAttrs.$set('tabindex', tAttrs.tabindex || '0');
88 tAttrs.$set('type', 'checkbox');
89 tAttrs.$set('role', tAttrs.type);
90
91 return {
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();
98 }
99 });
100 },
101 post: postLink
102 };
103
104 function postLink(scope, element, attr, ngModelCtrl) {
105 var isIndeterminate;
106 ngModelCtrl = ngModelCtrl || $mdUtil.fakeNgModel();
107 $mdTheming(element);
108
109 // Redirect focus events to the root element, because IE11 is always focusing the container element instead
110 // of the md-checkbox element. This causes issues when using ngModelOptions: `updateOnBlur`
111 element.children().on('focus', function() {
112 element.focus();
113 });
114
115 if ($mdUtil.parseAttributeBoolean(attr.mdIndeterminate)) {
116 setIndeterminateState();
117 scope.$watch(attr.mdIndeterminate, setIndeterminateState);
118 }
119
120 if (attr.ngChecked) {
121 scope.$watch(scope.$eval.bind(scope, attr.ngChecked), function(value) {
122 ngModelCtrl.$setViewValue(value);
123 ngModelCtrl.$render();
124 });
125 }
126
127 $$watchExpr('ngDisabled', 'tabindex', {
128 true: '-1',
129 false: attr.tabindex
130 });
131
132 $mdAria.expectWithText(element, 'aria-label');
133
134 // Reuse the original input[type=checkbox] directive from Angular core.
135 // This is a bit hacky as we need our own event listener and own render
136 // function.
137 inputDirective.link.pre(scope, {
138 on: angular.noop,
139 0: {}
140 }, attr, [ngModelCtrl]);
141
142 scope.mouseActive = false;
143 element.on('click', listener)
144 .on('keypress', keypressHandler)
145 .on('mousedown', function() {
146 scope.mouseActive = true;
147 $timeout(function() {
148 scope.mouseActive = false;
149 }, 100);
150 })
151 .on('focus', function() {
152 if (scope.mouseActive === false) {
153 element.addClass('md-focused');
154 }
155 })
156 .on('blur', function() {
157 element.removeClass('md-focused');
158 });
159
160 ngModelCtrl.$render = render;
161
162 function $$watchExpr(expr, htmlAttr, valueOpts) {
163 if (attr[expr]) {
164 scope.$watch(attr[expr], function(val) {
165 if (valueOpts[val]) {
166 element.attr(htmlAttr, valueOpts[val]);
167 }
168 });
169 }
170 }
171
172 function keypressHandler(ev) {
173 var keyCode = ev.which || ev.keyCode;
174 if (keyCode === $mdConstant.KEY_CODE.SPACE || keyCode === $mdConstant.KEY_CODE.ENTER) {
175 ev.preventDefault();
176 element.addClass('md-focused');
177 listener(ev);
178 }
179 }
180
181 function listener(ev) {
182 // skipToggle boolean is used by the switch directive to prevent the click event
183 // when releasing the drag. There will be always a click if releasing the drag over the checkbox
184 if (element[0].hasAttribute('disabled') || scope.skipToggle) {
185 return;
186 }
187
188 scope.$apply(function() {
189 // Toggle the checkbox value...
190 var viewValue = attr.ngChecked ? attr.checked : !ngModelCtrl.$viewValue;
191
192 ngModelCtrl.$setViewValue(viewValue, ev && ev.type);
193 ngModelCtrl.$render();
194 });
195 }
196
197 function render() {
198 // Cast the $viewValue to a boolean since it could be undefined
199 element.toggleClass('md-checked', !!ngModelCtrl.$viewValue && !isIndeterminate);
200 }
201
202 function setIndeterminateState(newValue) {
203 isIndeterminate = newValue !== false;
204 if (isIndeterminate) {
205 element.attr('aria-checked', 'mixed');
206 }
207 element.toggleClass('md-indeterminate', isIndeterminate);
208 }
209 }
210 }
211}
212
213ngmaterial.components.checkbox = angular.module("material.components.checkbox");