nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-material / modules / js / checkbox / checkbox.js
1 /*!
2  * Angular Material Design
3  * https://github.com/angular/material
4  * @license MIT
5  * v0.9.8
6  */
7 (function( window, angular, undefined ){
8 "use strict";
9
10 /**
11  * @ngdoc module
12  * @name material.components.checkbox
13  * @description Checkbox module!
14  */
15 angular
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-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.
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  *
41  * @usage
42  * <hljs lang="html">
43  * <md-checkbox ng-model="isChecked" aria-label="Finished?">
44  *   Finished ?
45  * </md-checkbox>
46  *
47  * <md-checkbox md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
48  *   No Ink Effects
49  * </md-checkbox>
50  *
51  * <md-checkbox ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
52  *   Disabled
53  * </md-checkbox>
54  *
55  * </hljs>
56  *
57  */
58 function MdCheckboxDirective(inputDirective, $mdInkRipple, $mdAria, $mdConstant, $mdTheming, $mdUtil, $timeout) {
59   inputDirective = inputDirective[0];
60   var CHECKED_CSS = 'md-checked';
61
62   return {
63     restrict: 'E',
64     transclude: true,
65     require: '?ngModel',
66     priority:210, // Run before ngAria
67     template: 
68       '<div class="md-container" md-ink-ripple md-ink-ripple-checkbox>' +
69         '<div class="md-icon"></div>' +
70       '</div>' +
71       '<div ng-transclude class="md-label"></div>',
72     compile: compile
73   };
74
75   // **********************************************************
76   // Private Methods
77   // **********************************************************
78
79   function compile (tElement, tAttrs) {
80
81     tAttrs.type = 'checkbox';
82     tAttrs.tabindex = tAttrs.tabindex || '0';
83     tElement.attr('role', tAttrs.type);
84
85     return function postLink(scope, element, attr, ngModelCtrl) {
86       ngModelCtrl = ngModelCtrl || $mdUtil.fakeNgModel();
87       $mdTheming(element);
88
89       if (attr.ngChecked) {
90         scope.$watch(
91             scope.$eval.bind(scope, attr.ngChecked),
92             ngModelCtrl.$setViewValue.bind(ngModelCtrl)
93         );
94       }
95       $$watchExpr('ngDisabled', 'tabindex', {
96         true: '-1',
97         false: attr.tabindex
98       });
99       $mdAria.expectWithText(element, 'aria-label');
100
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
103       // function.
104       inputDirective.link.pre(scope, {
105         on: angular.noop,
106         0: {}
107       }, attr, [ngModelCtrl]);
108
109       scope.mouseActive = false;
110       element.on('click', listener)
111         .on('keypress', keypressHandler)
112         .on('mousedown', function() {
113           scope.mouseActive = true;
114           $timeout(function(){
115             scope.mouseActive = false;
116           }, 100);
117         })
118         .on('focus', function() {
119           if(scope.mouseActive === false) { element.addClass('md-focused'); }
120         })
121         .on('blur', function() { element.removeClass('md-focused'); });
122
123       ngModelCtrl.$render = render;
124
125       function $$watchExpr(expr, htmlAttr, valueOpts) {
126         if (attr[expr]) {
127           scope.$watch(attr[expr], function(val) {
128             if (valueOpts[val]) {
129               element.attr(htmlAttr, valueOpts[val]);
130             }
131           });
132         }
133       }
134
135       function keypressHandler(ev) {
136         var keyCode = ev.which || ev.keyCode;
137         if (keyCode === $mdConstant.KEY_CODE.SPACE || keyCode === $mdConstant.KEY_CODE.ENTER) {
138           ev.preventDefault();
139           if (!element.hasClass('md-focused')) { element.addClass('md-focused'); }
140           listener(ev);
141         }
142       }
143       function listener(ev) {
144         if (element[0].hasAttribute('disabled')) return;
145
146         scope.$apply(function() {
147           // Toggle the checkbox value...
148           var viewValue = attr.ngChecked ? attr.checked : !ngModelCtrl.$viewValue;
149
150           ngModelCtrl.$setViewValue( viewValue, ev && ev.type);
151           ngModelCtrl.$render();
152         });
153       }
154
155       function render() {
156         if(ngModelCtrl.$viewValue) {
157           element.addClass(CHECKED_CSS);
158         } else {
159           element.removeClass(CHECKED_CSS);
160         }
161       }
162     };
163   }
164 }
165 MdCheckboxDirective.$inject = ["inputDirective", "$mdInkRipple", "$mdAria", "$mdConstant", "$mdTheming", "$mdUtil", "$timeout"];
166
167 })(window, window.angular);