123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /*!
- * Angular Material Design
- * https://github.com/angular/material
- * @license MIT
- * v0.11.4
- */
- (function( window, angular, undefined ){
- "use strict";
- /**
- * @ngdoc module
- * @name material.components.input
- */
- angular.module('material.components.input', [
- 'material.core'
- ])
- .directive('mdInputContainer', mdInputContainerDirective)
- .directive('label', labelDirective)
- .directive('input', inputTextareaDirective)
- .directive('textarea', inputTextareaDirective)
- .directive('mdMaxlength', mdMaxlengthDirective)
- .directive('placeholder', placeholderDirective)
- .directive('ngMessages', ngMessagesDirective);
- /**
- * @ngdoc directive
- * @name mdInputContainer
- * @module material.components.input
- *
- * @restrict E
- *
- * @description
- * `<md-input-container>` is the parent of any input or textarea element.
- *
- * Input and textarea elements will not behave properly unless the md-input-container
- * parent is provided.
- *
- * @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.
- * @param md-no-float {boolean=} When present, placeholders will not be converted to floating
- * labels.
- *
- * @usage
- * <hljs lang="html">
- *
- * <md-input-container>
- * <label>Username</label>
- * <input type="text" ng-model="user.name">
- * </md-input-container>
- *
- * <md-input-container>
- * <label>Description</label>
- * <textarea ng-model="user.description"></textarea>
- * </md-input-container>
- *
- * </hljs>
- */
- function mdInputContainerDirective($mdTheming, $parse) {
- ContainerCtrl.$inject = ["$scope", "$element", "$attrs"];
- return {
- restrict: 'E',
- link: postLink,
- controller: ContainerCtrl
- };
- function postLink(scope, element, attr) {
- $mdTheming(element);
- }
- function ContainerCtrl($scope, $element, $attrs) {
- var self = this;
- self.isErrorGetter = $attrs.mdIsError && $parse($attrs.mdIsError);
- self.delegateClick = function() {
- self.input.focus();
- };
- self.element = $element;
- self.setFocused = function(isFocused) {
- $element.toggleClass('md-input-focused', !!isFocused);
- };
- self.setHasValue = function(hasValue) {
- $element.toggleClass('md-input-has-value', !!hasValue);
- };
- self.setHasMessages = function(hasMessages) {
- $element.toggleClass('md-input-has-messages', !!hasMessages);
- };
- self.setHasPlaceholder = function(hasPlaceholder) {
- $element.toggleClass('md-input-has-placeholder', !!hasPlaceholder);
- };
- self.setInvalid = function(isInvalid) {
- $element.toggleClass('md-input-invalid', !!isInvalid);
- };
- $scope.$watch(function() {
- return self.label && self.input;
- }, function(hasLabelAndInput) {
- if (hasLabelAndInput && !self.label.attr('for')) {
- self.label.attr('for', self.input.attr('id'));
- }
- });
- }
- }
- mdInputContainerDirective.$inject = ["$mdTheming", "$parse"];
- function labelDirective() {
- return {
- restrict: 'E',
- require: '^?mdInputContainer',
- link: function(scope, element, attr, containerCtrl) {
- if (!containerCtrl || attr.mdNoFloat) return;
- containerCtrl.label = element;
- scope.$on('$destroy', function() {
- containerCtrl.label = null;
- });
- }
- };
- }
- /**
- * @ngdoc directive
- * @name mdInput
- * @restrict E
- * @module material.components.input
- *
- * @description
- * Use the `<input>` or the `<textarea>` as a child of an `<md-input-container>`.
- *
- * @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/>
- * 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.
- * @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.
- * @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.
- * @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
- * @param md-detect-hidden {boolean=} When present, textareas will be sized properly when they are revealed after being hidden. This is off by default for performance reasons because it guarantees a reflow every digest cycle.
- *
- * @usage
- * <hljs lang="html">
- * <md-input-container>
- * <label>Color</label>
- * <input type="text" ng-model="color" required md-maxlength="10">
- * </md-input-container>
- * </hljs>
- * <h3>With Errors</h3>
- *
- * <hljs lang="html">
- * <form name="userForm">
- * <md-input-container>
- * <label>Last Name</label>
- * <input name="lastName" ng-model="lastName" required md-maxlength="10" minlength="4">
- * <div ng-messages="userForm.lastName.$error" ng-show="userForm.lastName.$dirty">
- * <div ng-message="required">This is required!</div>
- * <div ng-message="md-maxlength">That's too long!</div>
- * <div ng-message="minlength">That's too short!</div>
- * </div>
- * </md-input-container>
- * <md-input-container>
- * <label>Biography</label>
- * <textarea name="bio" ng-model="biography" required md-maxlength="150"></textarea>
- * <div ng-messages="userForm.bio.$error" ng-show="userForm.bio.$dirty">
- * <div ng-message="required">This is required!</div>
- * <div ng-message="md-maxlength">That's too long!</div>
- * </div>
- * </md-input-container>
- * <md-input-container>
- * <input aria-label='title' ng-model='title'>
- * </md-input-container>
- * <md-input-container>
- * <input placeholder='title' ng-model='title'>
- * </md-input-container>
- * </form>
- * </hljs>
- *
- * Requires [ngMessages](https://docs.angularjs.org/api/ngMessages).
- * Behaves like the [AngularJS input directive](https://docs.angularjs.org/api/ng/directive/input).
- *
- */
- function inputTextareaDirective($mdUtil, $window, $mdAria) {
- return {
- restrict: 'E',
- require: ['^?mdInputContainer', '?ngModel'],
- link: postLink
- };
- function postLink(scope, element, attr, ctrls) {
- var containerCtrl = ctrls[0];
- var hasNgModel = !!ctrls[1];
- var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
- var isReadonly = angular.isDefined(attr.readonly);
- if (!containerCtrl) return;
- if (containerCtrl.input) {
- throw new Error("<md-input-container> can only have *one* <input>, <textarea> or <md-select> child element!");
- }
- containerCtrl.input = element;
- if (!containerCtrl.label) {
- $mdAria.expect(element, 'aria-label', element.attr('placeholder'));
- }
- element.addClass('md-input');
- if (!element.attr('id')) {
- element.attr('id', 'input_' + $mdUtil.nextUid());
- }
- if (element[0].tagName.toLowerCase() === 'textarea') {
- setupTextarea();
- }
- // If the input doesn't have an ngModel, it may have a static value. For that case,
- // we have to do one initial check to determine if the container should be in the
- // "has a value" state.
- if (!hasNgModel) {
- inputCheckValue();
- }
- var isErrorGetter = containerCtrl.isErrorGetter || function() {
- return ngModelCtrl.$invalid && ngModelCtrl.$touched;
- };
- scope.$watch(isErrorGetter, containerCtrl.setInvalid);
- ngModelCtrl.$parsers.push(ngModelPipelineCheckValue);
- ngModelCtrl.$formatters.push(ngModelPipelineCheckValue);
- element.on('input', inputCheckValue);
- if (!isReadonly) {
- element
- .on('focus', function(ev) {
- containerCtrl.setFocused(true);
- })
- .on('blur', function(ev) {
- containerCtrl.setFocused(false);
- inputCheckValue();
- });
- }
- //ngModelCtrl.$setTouched();
- //if( ngModelCtrl.$invalid ) containerCtrl.setInvalid();
- scope.$on('$destroy', function() {
- containerCtrl.setFocused(false);
- containerCtrl.setHasValue(false);
- containerCtrl.input = null;
- });
- /**
- *
- */
- function ngModelPipelineCheckValue(arg) {
- containerCtrl.setHasValue(!ngModelCtrl.$isEmpty(arg));
- return arg;
- }
- function inputCheckValue() {
- // An input's value counts if its length > 0,
- // or if the input's validity state says it has bad input (eg string in a number input)
- containerCtrl.setHasValue(element.val().length > 0 || (element[0].validity || {}).badInput);
- }
- function setupTextarea() {
- if (angular.isDefined(element.attr('md-no-autogrow'))) {
- return;
- }
- var node = element[0];
- var container = containerCtrl.element[0];
- var min_rows = NaN;
- var lineHeight = null;
- // can't check if height was or not explicity set,
- // so rows attribute will take precedence if present
- if (node.hasAttribute('rows')) {
- min_rows = parseInt(node.getAttribute('rows'));
- }
- var onChangeTextarea = $mdUtil.debounce(growTextarea, 1);
- function pipelineListener(value) {
- onChangeTextarea();
- return value;
- }
- if (ngModelCtrl) {
- ngModelCtrl.$formatters.push(pipelineListener);
- ngModelCtrl.$viewChangeListeners.push(pipelineListener);
- } else {
- onChangeTextarea();
- }
- element.on('keydown input', onChangeTextarea);
- if (isNaN(min_rows)) {
- element.attr('rows', '1');
- element.on('scroll', onScroll);
- }
- angular.element($window).on('resize', onChangeTextarea);
- scope.$on('$destroy', function() {
- angular.element($window).off('resize', onChangeTextarea);
- });
- function growTextarea() {
- // sets the md-input-container height to avoid jumping around
- container.style.height = container.offsetHeight + 'px';
- // temporarily disables element's flex so its height 'runs free'
- element.addClass('md-no-flex');
- if (isNaN(min_rows)) {
- node.style.height = "auto";
- node.scrollTop = 0;
- var height = getHeight();
- if (height) node.style.height = height + 'px';
- } else {
- node.setAttribute("rows", 1);
- if (!lineHeight) {
- node.style.minHeight = '0';
- lineHeight = element.prop('clientHeight');
- node.style.minHeight = null;
- }
- var rows = Math.max(min_rows, Math.round(node.scrollHeight / lineHeight));
- node.setAttribute("rows", rows);
- }
- // reset everything back to normal
- element.removeClass('md-no-flex');
- container.style.height = 'auto';
- }
- function getHeight() {
- var line = node.scrollHeight - node.offsetHeight;
- return node.offsetHeight + (line > 0 ? line : 0);
- }
- function onScroll(e) {
- node.scrollTop = 0;
- // for smooth new line adding
- var line = node.scrollHeight - node.offsetHeight;
- var height = node.offsetHeight + line;
- node.style.height = height + 'px';
- }
- // Attach a watcher to detect when the textarea gets shown.
- if (angular.isDefined(element.attr('md-detect-hidden'))) {
- var handleHiddenChange = function() {
- var wasHidden = false;
- return function() {
- var isHidden = node.offsetHeight === 0;
- if (isHidden === false && wasHidden === true) {
- growTextarea();
- }
- wasHidden = isHidden;
- };
- }();
- // Check every digest cycle whether the visibility of the textarea has changed.
- // Queue up to run after the digest cycle is complete.
- scope.$watch(function() {
- $mdUtil.nextTick(handleHiddenChange, false);
- return true;
- });
- }
- }
- }
- }
- inputTextareaDirective.$inject = ["$mdUtil", "$window", "$mdAria"];
- function mdMaxlengthDirective($animate) {
- return {
- restrict: 'A',
- require: ['ngModel', '^mdInputContainer'],
- link: postLink
- };
- function postLink(scope, element, attr, ctrls) {
- var maxlength;
- var ngModelCtrl = ctrls[0];
- var containerCtrl = ctrls[1];
- var charCountEl = angular.element('<div class="md-char-counter">');
- var input = angular.element(containerCtrl.element[0].querySelector('[md-maxlength]'));
- // Stop model from trimming. This makes it so whitespace
- // over the maxlength still counts as invalid.
- attr.$set('ngTrim', 'false');
- var ngMessagesSelectors = [
- 'ng-messages',
- 'data-ng-messages',
- 'x-ng-messages',
- '[ng-messages]',
- '[data-ng-messages]',
- '[x-ng-messages]'
- ];
- var ngMessages = containerCtrl.element[0].querySelector(ngMessagesSelectors.join(','));
- // If we have an ngMessages container, put the counter at the top; otherwise, put it after the
- // input so it will be positioned properly in the SCSS
- if (ngMessages) {
- angular.element(ngMessages).prepend(charCountEl);
- } else {
- input.after(charCountEl);
- }
- ngModelCtrl.$formatters.push(renderCharCount);
- ngModelCtrl.$viewChangeListeners.push(renderCharCount);
- element.on('input keydown keyup', function() {
- renderCharCount(); //make sure it's called with no args
- });
- scope.$watch(attr.mdMaxlength, function(value) {
- maxlength = value;
- if (angular.isNumber(value) && value > 0) {
- if (!charCountEl.parent().length) {
- $animate.enter(charCountEl, containerCtrl.element, input);
- }
- renderCharCount();
- } else {
- $animate.leave(charCountEl);
- }
- });
- ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
- if (!angular.isNumber(maxlength) || maxlength < 0) {
- return true;
- }
- return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
- };
- function renderCharCount(value) {
- // Force the value into a string since it may be a number,
- // which does not have a length property.
- charCountEl.text(String(element.val() || value || '').length + '/' + maxlength);
- return value;
- }
- }
- }
- mdMaxlengthDirective.$inject = ["$animate"];
- function placeholderDirective($log) {
- return {
- restrict: 'A',
- require: '^^?mdInputContainer',
- priority: 200,
- link: postLink
- };
- function postLink(scope, element, attr, inputContainer) {
- // If there is no input container, just return
- if (!inputContainer) return;
- var label = inputContainer.element.find('label');
- var hasNoFloat = angular.isDefined(inputContainer.element.attr('md-no-float'));
- // If we have a label, or they specify the md-no-float attribute, just return
- if ((label && label.length) || hasNoFloat) {
- // Add a placeholder class so we can target it in the CSS
- inputContainer.setHasPlaceholder(true);
- return;
- }
- // Otherwise, grab/remove the placeholder
- var placeholderText = attr.placeholder;
- element.removeAttr('placeholder');
- // And add the placeholder text as a separate label
- if (inputContainer.input && inputContainer.input[0].nodeName != 'MD-SELECT') {
- var placeholder = '<label ng-click="delegateClick()">' + placeholderText + '</label>';
- inputContainer.element.addClass('md-icon-float');
- inputContainer.element.prepend(placeholder);
- }
- }
- }
- placeholderDirective.$inject = ["$log"];
- function ngMessagesDirective() {
- return {
- restrict: 'EA',
- link: postLink,
- // This is optional because we don't want target *all* ngMessage instances, just those inside of
- // mdInputContainer.
- require: '^^?mdInputContainer'
- };
- function postLink(scope, element, attr, inputContainer) {
- // If we are not a child of an input container, don't do anything
- if (!inputContainer) return;
- // Tell our parent input container we have messages so we can set the proper classes
- inputContainer.setHasMessages(true);
- // When destroyed, inform our input container
- scope.$on('$destroy', function() {
- inputContainer.setHasMessages(false);
- });
- }
- }
- })(window, window.angular);
|