input.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  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. .directive('ngMessages', ngMessagesDirective);
  23. /**
  24. * @ngdoc directive
  25. * @name mdInputContainer
  26. * @module material.components.input
  27. *
  28. * @restrict E
  29. *
  30. * @description
  31. * `<md-input-container>` is the parent of any input or textarea element.
  32. *
  33. * Input and textarea elements will not behave properly unless the md-input-container
  34. * parent is provided.
  35. *
  36. * @param md-is-error {expression=} When the given expression evaluates to true, the input container
  37. * will go into error state. Defaults to erroring if the input has been touched and is invalid.
  38. * @param md-no-float {boolean=} When present, placeholders will not be converted to floating
  39. * 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. function postLink(scope, element, attr) {
  64. $mdTheming(element);
  65. }
  66. function ContainerCtrl($scope, $element, $attrs) {
  67. var self = this;
  68. self.isErrorGetter = $attrs.mdIsError && $parse($attrs.mdIsError);
  69. self.delegateClick = function() {
  70. self.input.focus();
  71. };
  72. self.element = $element;
  73. self.setFocused = function(isFocused) {
  74. $element.toggleClass('md-input-focused', !!isFocused);
  75. };
  76. self.setHasValue = function(hasValue) {
  77. $element.toggleClass('md-input-has-value', !!hasValue);
  78. };
  79. self.setHasMessages = function(hasMessages) {
  80. $element.toggleClass('md-input-has-messages', !!hasMessages);
  81. };
  82. self.setHasPlaceholder = function(hasPlaceholder) {
  83. $element.toggleClass('md-input-has-placeholder', !!hasPlaceholder);
  84. };
  85. self.setInvalid = function(isInvalid) {
  86. $element.toggleClass('md-input-invalid', !!isInvalid);
  87. };
  88. $scope.$watch(function() {
  89. return self.label && self.input;
  90. }, function(hasLabelAndInput) {
  91. if (hasLabelAndInput && !self.label.attr('for')) {
  92. self.label.attr('for', self.input.attr('id'));
  93. }
  94. });
  95. }
  96. }
  97. mdInputContainerDirective.$inject = ["$mdTheming", "$parse"];
  98. function labelDirective() {
  99. return {
  100. restrict: 'E',
  101. require: '^?mdInputContainer',
  102. link: function(scope, element, attr, containerCtrl) {
  103. if (!containerCtrl || attr.mdNoFloat) return;
  104. containerCtrl.label = element;
  105. scope.$on('$destroy', function() {
  106. containerCtrl.label = null;
  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
  121. * specified, a character counter will be shown underneath the input.<br/><br/>
  122. * The purpose of **`md-maxlength`** is exactly to show the max length counter text. If you don't
  123. * want the counter text and only need "plain" validation, you can use the "simple" `ng-maxlength`
  124. * or maxlength attributes.
  125. * @param {string=} aria-label Aria-label is required when no label is present. A warning message
  126. * will be logged in the console if not present.
  127. * @param {string=} placeholder An alternative approach to using aria-label when the label is not
  128. * PRESENT. The placeholder text is copied to the aria-label attribute.
  129. * @param md-no-autogrow {boolean=} When present, textareas will not grow automatically.
  130. * @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.
  131. *
  132. * @usage
  133. * <hljs lang="html">
  134. * <md-input-container>
  135. * <label>Color</label>
  136. * <input type="text" ng-model="color" required md-maxlength="10">
  137. * </md-input-container>
  138. * </hljs>
  139. * <h3>With Errors</h3>
  140. *
  141. * <hljs lang="html">
  142. * <form name="userForm">
  143. * <md-input-container>
  144. * <label>Last Name</label>
  145. * <input name="lastName" ng-model="lastName" required md-maxlength="10" minlength="4">
  146. * <div ng-messages="userForm.lastName.$error" ng-show="userForm.lastName.$dirty">
  147. * <div ng-message="required">This is required!</div>
  148. * <div ng-message="md-maxlength">That's too long!</div>
  149. * <div ng-message="minlength">That's too short!</div>
  150. * </div>
  151. * </md-input-container>
  152. * <md-input-container>
  153. * <label>Biography</label>
  154. * <textarea name="bio" ng-model="biography" required md-maxlength="150"></textarea>
  155. * <div ng-messages="userForm.bio.$error" ng-show="userForm.bio.$dirty">
  156. * <div ng-message="required">This is required!</div>
  157. * <div ng-message="md-maxlength">That's too long!</div>
  158. * </div>
  159. * </md-input-container>
  160. * <md-input-container>
  161. * <input aria-label='title' ng-model='title'>
  162. * </md-input-container>
  163. * <md-input-container>
  164. * <input placeholder='title' ng-model='title'>
  165. * </md-input-container>
  166. * </form>
  167. * </hljs>
  168. *
  169. * Requires [ngMessages](https://docs.angularjs.org/api/ngMessages).
  170. * Behaves like the [AngularJS input directive](https://docs.angularjs.org/api/ng/directive/input).
  171. *
  172. */
  173. function inputTextareaDirective($mdUtil, $window, $mdAria) {
  174. return {
  175. restrict: 'E',
  176. require: ['^?mdInputContainer', '?ngModel'],
  177. link: postLink
  178. };
  179. function postLink(scope, element, attr, ctrls) {
  180. var containerCtrl = ctrls[0];
  181. var hasNgModel = !!ctrls[1];
  182. var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();
  183. var isReadonly = angular.isDefined(attr.readonly);
  184. if (!containerCtrl) return;
  185. if (containerCtrl.input) {
  186. throw new Error("<md-input-container> can only have *one* <input>, <textarea> or <md-select> child element!");
  187. }
  188. containerCtrl.input = element;
  189. if (!containerCtrl.label) {
  190. $mdAria.expect(element, 'aria-label', element.attr('placeholder'));
  191. }
  192. element.addClass('md-input');
  193. if (!element.attr('id')) {
  194. element.attr('id', 'input_' + $mdUtil.nextUid());
  195. }
  196. if (element[0].tagName.toLowerCase() === 'textarea') {
  197. setupTextarea();
  198. }
  199. // If the input doesn't have an ngModel, it may have a static value. For that case,
  200. // we have to do one initial check to determine if the container should be in the
  201. // "has a value" state.
  202. if (!hasNgModel) {
  203. inputCheckValue();
  204. }
  205. var isErrorGetter = containerCtrl.isErrorGetter || function() {
  206. return ngModelCtrl.$invalid && ngModelCtrl.$touched;
  207. };
  208. scope.$watch(isErrorGetter, containerCtrl.setInvalid);
  209. ngModelCtrl.$parsers.push(ngModelPipelineCheckValue);
  210. ngModelCtrl.$formatters.push(ngModelPipelineCheckValue);
  211. element.on('input', inputCheckValue);
  212. if (!isReadonly) {
  213. element
  214. .on('focus', function(ev) {
  215. containerCtrl.setFocused(true);
  216. })
  217. .on('blur', function(ev) {
  218. containerCtrl.setFocused(false);
  219. inputCheckValue();
  220. });
  221. }
  222. //ngModelCtrl.$setTouched();
  223. //if( ngModelCtrl.$invalid ) containerCtrl.setInvalid();
  224. scope.$on('$destroy', function() {
  225. containerCtrl.setFocused(false);
  226. containerCtrl.setHasValue(false);
  227. containerCtrl.input = null;
  228. });
  229. /**
  230. *
  231. */
  232. function ngModelPipelineCheckValue(arg) {
  233. containerCtrl.setHasValue(!ngModelCtrl.$isEmpty(arg));
  234. return arg;
  235. }
  236. function inputCheckValue() {
  237. // An input's value counts if its length > 0,
  238. // or if the input's validity state says it has bad input (eg string in a number input)
  239. containerCtrl.setHasValue(element.val().length > 0 || (element[0].validity || {}).badInput);
  240. }
  241. function setupTextarea() {
  242. if (angular.isDefined(element.attr('md-no-autogrow'))) {
  243. return;
  244. }
  245. var node = element[0];
  246. var container = containerCtrl.element[0];
  247. var min_rows = NaN;
  248. var lineHeight = null;
  249. // can't check if height was or not explicity set,
  250. // so rows attribute will take precedence if present
  251. if (node.hasAttribute('rows')) {
  252. min_rows = parseInt(node.getAttribute('rows'));
  253. }
  254. var onChangeTextarea = $mdUtil.debounce(growTextarea, 1);
  255. function pipelineListener(value) {
  256. onChangeTextarea();
  257. return value;
  258. }
  259. if (ngModelCtrl) {
  260. ngModelCtrl.$formatters.push(pipelineListener);
  261. ngModelCtrl.$viewChangeListeners.push(pipelineListener);
  262. } else {
  263. onChangeTextarea();
  264. }
  265. element.on('keydown input', onChangeTextarea);
  266. if (isNaN(min_rows)) {
  267. element.attr('rows', '1');
  268. element.on('scroll', onScroll);
  269. }
  270. angular.element($window).on('resize', onChangeTextarea);
  271. scope.$on('$destroy', function() {
  272. angular.element($window).off('resize', onChangeTextarea);
  273. });
  274. function growTextarea() {
  275. // sets the md-input-container height to avoid jumping around
  276. container.style.height = container.offsetHeight + 'px';
  277. // temporarily disables element's flex so its height 'runs free'
  278. element.addClass('md-no-flex');
  279. if (isNaN(min_rows)) {
  280. node.style.height = "auto";
  281. node.scrollTop = 0;
  282. var height = getHeight();
  283. if (height) node.style.height = height + 'px';
  284. } else {
  285. node.setAttribute("rows", 1);
  286. if (!lineHeight) {
  287. node.style.minHeight = '0';
  288. lineHeight = element.prop('clientHeight');
  289. node.style.minHeight = null;
  290. }
  291. var rows = Math.max(min_rows, Math.round(node.scrollHeight / lineHeight));
  292. node.setAttribute("rows", rows);
  293. }
  294. // reset everything back to normal
  295. element.removeClass('md-no-flex');
  296. container.style.height = 'auto';
  297. }
  298. function getHeight() {
  299. var line = node.scrollHeight - node.offsetHeight;
  300. return node.offsetHeight + (line > 0 ? line : 0);
  301. }
  302. function onScroll(e) {
  303. node.scrollTop = 0;
  304. // for smooth new line adding
  305. var line = node.scrollHeight - node.offsetHeight;
  306. var height = node.offsetHeight + line;
  307. node.style.height = height + 'px';
  308. }
  309. // Attach a watcher to detect when the textarea gets shown.
  310. if (angular.isDefined(element.attr('md-detect-hidden'))) {
  311. var handleHiddenChange = function() {
  312. var wasHidden = false;
  313. return function() {
  314. var isHidden = node.offsetHeight === 0;
  315. if (isHidden === false && wasHidden === true) {
  316. growTextarea();
  317. }
  318. wasHidden = isHidden;
  319. };
  320. }();
  321. // Check every digest cycle whether the visibility of the textarea has changed.
  322. // Queue up to run after the digest cycle is complete.
  323. scope.$watch(function() {
  324. $mdUtil.nextTick(handleHiddenChange, false);
  325. return true;
  326. });
  327. }
  328. }
  329. }
  330. }
  331. inputTextareaDirective.$inject = ["$mdUtil", "$window", "$mdAria"];
  332. function mdMaxlengthDirective($animate) {
  333. return {
  334. restrict: 'A',
  335. require: ['ngModel', '^mdInputContainer'],
  336. link: postLink
  337. };
  338. function postLink(scope, element, attr, ctrls) {
  339. var maxlength;
  340. var ngModelCtrl = ctrls[0];
  341. var containerCtrl = ctrls[1];
  342. var charCountEl = angular.element('<div class="md-char-counter">');
  343. var input = angular.element(containerCtrl.element[0].querySelector('[md-maxlength]'));
  344. // Stop model from trimming. This makes it so whitespace
  345. // over the maxlength still counts as invalid.
  346. attr.$set('ngTrim', 'false');
  347. var ngMessagesSelectors = [
  348. 'ng-messages',
  349. 'data-ng-messages',
  350. 'x-ng-messages',
  351. '[ng-messages]',
  352. '[data-ng-messages]',
  353. '[x-ng-messages]'
  354. ];
  355. var ngMessages = containerCtrl.element[0].querySelector(ngMessagesSelectors.join(','));
  356. // If we have an ngMessages container, put the counter at the top; otherwise, put it after the
  357. // input so it will be positioned properly in the SCSS
  358. if (ngMessages) {
  359. angular.element(ngMessages).prepend(charCountEl);
  360. } else {
  361. input.after(charCountEl);
  362. }
  363. ngModelCtrl.$formatters.push(renderCharCount);
  364. ngModelCtrl.$viewChangeListeners.push(renderCharCount);
  365. element.on('input keydown keyup', function() {
  366. renderCharCount(); //make sure it's called with no args
  367. });
  368. scope.$watch(attr.mdMaxlength, function(value) {
  369. maxlength = value;
  370. if (angular.isNumber(value) && value > 0) {
  371. if (!charCountEl.parent().length) {
  372. $animate.enter(charCountEl, containerCtrl.element, input);
  373. }
  374. renderCharCount();
  375. } else {
  376. $animate.leave(charCountEl);
  377. }
  378. });
  379. ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
  380. if (!angular.isNumber(maxlength) || maxlength < 0) {
  381. return true;
  382. }
  383. return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
  384. };
  385. function renderCharCount(value) {
  386. // Force the value into a string since it may be a number,
  387. // which does not have a length property.
  388. charCountEl.text(String(element.val() || value || '').length + '/' + maxlength);
  389. return value;
  390. }
  391. }
  392. }
  393. mdMaxlengthDirective.$inject = ["$animate"];
  394. function placeholderDirective($log) {
  395. return {
  396. restrict: 'A',
  397. require: '^^?mdInputContainer',
  398. priority: 200,
  399. link: postLink
  400. };
  401. function postLink(scope, element, attr, inputContainer) {
  402. // If there is no input container, just return
  403. if (!inputContainer) return;
  404. var label = inputContainer.element.find('label');
  405. var hasNoFloat = angular.isDefined(inputContainer.element.attr('md-no-float'));
  406. // If we have a label, or they specify the md-no-float attribute, just return
  407. if ((label && label.length) || hasNoFloat) {
  408. // Add a placeholder class so we can target it in the CSS
  409. inputContainer.setHasPlaceholder(true);
  410. return;
  411. }
  412. // Otherwise, grab/remove the placeholder
  413. var placeholderText = attr.placeholder;
  414. element.removeAttr('placeholder');
  415. // And add the placeholder text as a separate label
  416. if (inputContainer.input && inputContainer.input[0].nodeName != 'MD-SELECT') {
  417. var placeholder = '<label ng-click="delegateClick()">' + placeholderText + '</label>';
  418. inputContainer.element.addClass('md-icon-float');
  419. inputContainer.element.prepend(placeholder);
  420. }
  421. }
  422. }
  423. placeholderDirective.$inject = ["$log"];
  424. function ngMessagesDirective() {
  425. return {
  426. restrict: 'EA',
  427. link: postLink,
  428. // This is optional because we don't want target *all* ngMessage instances, just those inside of
  429. // mdInputContainer.
  430. require: '^^?mdInputContainer'
  431. };
  432. function postLink(scope, element, attr, inputContainer) {
  433. // If we are not a child of an input container, don't do anything
  434. if (!inputContainer) return;
  435. // Tell our parent input container we have messages so we can set the proper classes
  436. inputContainer.setHasMessages(true);
  437. // When destroyed, inform our input container
  438. scope.$on('$destroy', function() {
  439. inputContainer.setHasMessages(false);
  440. });
  441. }
  442. }
  443. ng.material.components.input = angular.module("material.components.input");