nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-material / modules / js / input / input.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.input
13  */
14
15 angular.module('material.components.input', [
16   'material.core'
17 ])
18   .directive('mdInputContainer', mdInputContainerDirective)
19   .directive('label', labelDirective)
20   .directive('input', inputTextareaDirective)
21   .directive('textarea', inputTextareaDirective)
22   .directive('mdMaxlength', mdMaxlengthDirective)
23   .directive('placeholder', placeholderDirective);
24
25 /**
26  * @ngdoc directive
27  * @name mdInputContainer
28  * @module material.components.input
29  *
30  * @restrict E
31  *
32  * @description
33  * `<md-input-container>` is the parent of any input or textarea element.
34  *
35  * Input and textarea elements will not behave properly unless the md-input-container
36  * parent is provided.
37  *
38  * @param md-is-error {expression=} When the given expression evaluates to true, the input container will go into error state. Defaults to erroring if the input has been touched and is invalid.
39  * @param md-no-float {boolean=} When present, placeholders will not be converted to floating labels
40  *
41  * @usage
42  * <hljs lang="html">
43  *
44  * <md-input-container>
45  *   <label>Username</label>
46  *   <input type="text" ng-model="user.name">
47  * </md-input-container>
48  *
49  * <md-input-container>
50  *   <label>Description</label>
51  *   <textarea ng-model="user.description"></textarea>
52  * </md-input-container>
53  *
54  * </hljs>
55  */
56 function mdInputContainerDirective($mdTheming, $parse) {
57   ContainerCtrl.$inject = ["$scope", "$element", "$attrs"];
58   return {
59     restrict: 'E',
60     link: postLink,
61     controller: ContainerCtrl
62   };
63
64   function postLink(scope, element, attr) {
65     $mdTheming(element);
66   }
67   function ContainerCtrl($scope, $element, $attrs) {
68     var self = this;
69
70     self.isErrorGetter = $attrs.mdIsError && $parse($attrs.mdIsError);
71
72     self.delegateClick = function() {
73       self.input.focus();
74     };
75     self.element = $element;
76     self.setFocused = function(isFocused) {
77       $element.toggleClass('md-input-focused', !!isFocused);
78     };
79     self.setHasValue = function(hasValue) {
80       $element.toggleClass('md-input-has-value', !!hasValue);
81     };
82     self.setInvalid = function(isInvalid) {
83       $element.toggleClass('md-input-invalid', !!isInvalid);
84     };
85     $scope.$watch(function() {
86       return self.label && self.input;
87     }, function(hasLabelAndInput) {
88       if (hasLabelAndInput && !self.label.attr('for')) {
89         self.label.attr('for', self.input.attr('id'));
90       }
91     });
92   }
93 }
94 mdInputContainerDirective.$inject = ["$mdTheming", "$parse"];
95
96 function labelDirective() {
97   return {
98     restrict: 'E',
99     require: '^?mdInputContainer',
100     link: function(scope, element, attr, containerCtrl) {
101       if (!containerCtrl || attr.mdNoFloat) return;
102
103       containerCtrl.label = element;
104       scope.$on('$destroy', function() {
105         containerCtrl.label = null;
106       });
107     }
108   };
109 }
110
111 /**
112  * @ngdoc directive
113  * @name mdInput
114  * @restrict E
115  * @module material.components.input
116  *
117  * @description
118  * Use the `<input>` or the  `<textarea>` as a child of an `<md-input-container>`.
119  *
120  * @param {number=} md-maxlength The maximum number of characters allowed in this input. If this is specified, a character counter will be shown underneath the input.<br/><br/>
121  * The purpose of **`md-maxlength`** is exactly to show the max length counter text. If you don't want the counter text and only need "plain" validation, you can use the "simple" `ng-maxlength` or maxlength attributes.
122  * @param {string=} aria-label Aria-label is required when no label is present.  A warning message will be logged in the console if not present.
123  * @param {string=} placeholder An alternative approach to using aria-label when the label is not present.  The placeholder text is copied to the aria-label attribute.
124  *
125  * @usage
126  * <hljs lang="html">
127  * <md-input-container>
128  *   <label>Color</label>
129  *   <input type="text" ng-model="color" required md-maxlength="10">
130  * </md-input-container>
131  * </hljs>
132  * <h3>With Errors</h3>
133  *
134  * <hljs lang="html">
135  * <form name="userForm">
136  *   <md-input-container>
137  *     <label>Last Name</label>
138  *     <input name="lastName" ng-model="lastName" required md-maxlength="10" minlength="4">
139  *     <div ng-messages="userForm.lastName.$error" ng-show="userForm.lastName.$dirty">
140  *       <div ng-message="required">This is required!</div>
141  *       <div ng-message="md-maxlength">That's too long!</div>
142  *       <div ng-message="minlength">That's too short!</div>
143  *     </div>
144  *   </md-input-container>
145  *   <md-input-container>
146  *     <label>Biography</label>
147  *     <textarea name="bio" ng-model="biography" required md-maxlength="150"></textarea>
148  *     <div ng-messages="userForm.bio.$error" ng-show="userForm.bio.$dirty">
149  *       <div ng-message="required">This is required!</div>
150  *       <div ng-message="md-maxlength">That's too long!</div>
151  *     </div>
152  *   </md-input-container>
153  *   <md-input-container>
154  *     <input aria-label='title' ng-model='title'>
155  *   </md-input-container>
156  *   <md-input-container>
157  *     <input placeholder='title' ng-model='title'>
158  *   </md-input-container>
159  * </form>
160  * </hljs>
161  *
162  * Requires [ngMessages](https://docs.angularjs.org/api/ngMessages).
163  * Behaves like the [AngularJS input directive](https://docs.angularjs.org/api/ng/directive/input).
164  *
165  */
166
167 function inputTextareaDirective($mdUtil, $window, $mdAria) {
168   return {
169     restrict: 'E',
170     require: ['^?mdInputContainer', '?ngModel'],
171     link: postLink
172   };
173
174   function postLink(scope, element, attr, ctrls) {
175
176     var containerCtrl = ctrls[0];
177     var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
178     var isReadonly = angular.isDefined(attr.readonly);
179
180     if ( !containerCtrl ) return;
181     if (containerCtrl.input) {
182       throw new Error("<md-input-container> can only have *one* <input> or <textarea> child element!");
183     }
184     containerCtrl.input = element;
185
186     if(!containerCtrl.label) {
187       $mdAria.expect(element, 'aria-label', element.attr('placeholder'));
188     }
189
190     element.addClass('md-input');
191     if (!element.attr('id')) {
192       element.attr('id', 'input_' + $mdUtil.nextUid());
193     }
194
195     if (element[0].tagName.toLowerCase() === 'textarea') {
196       setupTextarea();
197     }
198
199     var isErrorGetter = containerCtrl.isErrorGetter || function() {
200       return ngModelCtrl.$invalid && ngModelCtrl.$touched;
201     };
202     scope.$watch(isErrorGetter, containerCtrl.setInvalid);
203
204     ngModelCtrl.$parsers.push(ngModelPipelineCheckValue);
205     ngModelCtrl.$formatters.push(ngModelPipelineCheckValue);
206
207     element.on('input', inputCheckValue);
208
209     if (!isReadonly) {
210       element
211         .on('focus', function(ev) {
212           containerCtrl.setFocused(true);
213         })
214         .on('blur', function(ev) {
215           containerCtrl.setFocused(false);
216           inputCheckValue();
217         });
218
219     }
220
221     //ngModelCtrl.$setTouched();
222     //if( ngModelCtrl.$invalid ) containerCtrl.setInvalid();
223
224     scope.$on('$destroy', function() {
225       containerCtrl.setFocused(false);
226       containerCtrl.setHasValue(false);
227       containerCtrl.input = null;
228     });
229
230     /**
231      *
232      */
233     function ngModelPipelineCheckValue(arg) {
234       containerCtrl.setHasValue(!ngModelCtrl.$isEmpty(arg));
235       return arg;
236     }
237     function inputCheckValue() {
238       // An input's value counts if its length > 0,
239       // or if the input's validity state says it has bad input (eg string in a number input)
240       containerCtrl.setHasValue(element.val().length > 0 || (element[0].validity||{}).badInput);
241     }
242
243     function setupTextarea() {
244       var node = element[0];
245       var onChangeTextarea = $mdUtil.debounce(growTextarea, 1);
246
247       function pipelineListener(value) {
248         onChangeTextarea();
249         return value;
250       }
251
252       if (ngModelCtrl) {
253         ngModelCtrl.$formatters.push(pipelineListener);
254         ngModelCtrl.$viewChangeListeners.push(pipelineListener);
255       } else {
256         onChangeTextarea();
257       }
258       element.on('keydown input', onChangeTextarea);
259       element.on('scroll', onScroll);
260       angular.element($window).on('resize', onChangeTextarea);
261
262       scope.$on('$destroy', function() {
263         angular.element($window).off('resize', onChangeTextarea);
264       });
265
266       function growTextarea() {
267         node.style.height = "auto";
268         node.scrollTop = 0;
269         var height = getHeight();
270         if (height) node.style.height = height + 'px';
271       }
272
273       function getHeight () {
274         var line = node.scrollHeight - node.offsetHeight;
275         return node.offsetHeight + (line > 0 ? line : 0);
276       }
277
278       function onScroll(e) {
279         node.scrollTop = 0;
280         // for smooth new line adding
281         var line = node.scrollHeight - node.offsetHeight;
282         var height = node.offsetHeight + line;
283         node.style.height = height + 'px';
284       }
285     }
286   }
287 }
288 inputTextareaDirective.$inject = ["$mdUtil", "$window", "$mdAria"];
289
290 function mdMaxlengthDirective($animate) {
291   return {
292     restrict: 'A',
293     require: ['ngModel', '^mdInputContainer'],
294     link: postLink
295   };
296
297   function postLink(scope, element, attr, ctrls) {
298     var maxlength;
299     var ngModelCtrl = ctrls[0];
300     var containerCtrl = ctrls[1];
301     var charCountEl = angular.element('<div class="md-char-counter">');
302
303     // Stop model from trimming. This makes it so whitespace
304     // over the maxlength still counts as invalid.
305     attr.$set('ngTrim', 'false');
306     containerCtrl.element.append(charCountEl);
307
308     ngModelCtrl.$formatters.push(renderCharCount);
309     ngModelCtrl.$viewChangeListeners.push(renderCharCount);
310     element.on('input keydown', function() {
311       renderCharCount(); //make sure it's called with no args
312     });
313
314     scope.$watch(attr.mdMaxlength, function(value) {
315       maxlength = value;
316       if (angular.isNumber(value) && value > 0) {
317         if (!charCountEl.parent().length) {
318           $animate.enter(charCountEl, containerCtrl.element,
319                          angular.element(containerCtrl.element[0].lastElementChild));
320         }
321         renderCharCount();
322       } else {
323         $animate.leave(charCountEl);
324       }
325     });
326
327     ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
328       if (!angular.isNumber(maxlength) || maxlength < 0) {
329         return true;
330       }
331       return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
332     };
333
334     function renderCharCount(value) {
335       charCountEl.text( ( element.val() || value || '' ).length + '/' + maxlength );
336       return value;
337     }
338   }
339 }
340 mdMaxlengthDirective.$inject = ["$animate"];
341
342 function placeholderDirective($log) {
343   var blackListElements = ['MD-SELECT'];
344   return {
345     restrict: 'A',
346     require: '^^?mdInputContainer',
347     priority: 200,
348     link: postLink
349   };
350
351   function postLink(scope, element, attr, inputContainer) {
352     if (!inputContainer) return;
353     if (blackListElements.indexOf(element[0].nodeName) != -1) return;
354     if (angular.isDefined(inputContainer.element.attr('md-no-float'))) return;
355
356     var placeholderText = attr.placeholder;
357     element.removeAttr('placeholder');
358
359     if ( inputContainer.element.find('label').length == 0 ) {
360       var placeholder = '<label ng-click="delegateClick()">' + placeholderText + '</label>';
361
362       inputContainer.element.addClass('md-icon-float');
363       inputContainer.element.prepend(placeholder);
364     } else {
365       $log.warn("The placeholder='" + placeholderText + "' will be ignored since this md-input-container has a child label element.");
366     }
367
368   }
369 }
370 placeholderDirective.$inject = ["$log"];
371
372 })(window, window.angular);