PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/bower_components/angular-material/modules/closure/input/input.js

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