foundation.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { __assign, __extends } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { cssClasses } from './constants';
  26. /** MDC Floating Label Foundation */
  27. var MDCFloatingLabelFoundation = /** @class */ (function (_super) {
  28. __extends(MDCFloatingLabelFoundation, _super);
  29. function MDCFloatingLabelFoundation(adapter) {
  30. var _this = _super.call(this, __assign(__assign({}, MDCFloatingLabelFoundation.defaultAdapter), adapter)) || this;
  31. _this.shakeAnimationEndHandler = function () {
  32. _this.handleShakeAnimationEnd();
  33. };
  34. return _this;
  35. }
  36. Object.defineProperty(MDCFloatingLabelFoundation, "cssClasses", {
  37. get: function () {
  38. return cssClasses;
  39. },
  40. enumerable: false,
  41. configurable: true
  42. });
  43. Object.defineProperty(MDCFloatingLabelFoundation, "defaultAdapter", {
  44. /**
  45. * See {@link MDCFloatingLabelAdapter} for typing information on parameters
  46. * and return types.
  47. */
  48. get: function () {
  49. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  50. return {
  51. addClass: function () { return undefined; },
  52. removeClass: function () { return undefined; },
  53. hasClass: function () { return false; },
  54. getWidth: function () { return 0; },
  55. registerInteractionHandler: function () { return undefined; },
  56. deregisterInteractionHandler: function () { return undefined; },
  57. };
  58. // tslint:enable:object-literal-sort-keys
  59. },
  60. enumerable: false,
  61. configurable: true
  62. });
  63. MDCFloatingLabelFoundation.prototype.init = function () {
  64. this.adapter.registerInteractionHandler('animationend', this.shakeAnimationEndHandler);
  65. };
  66. MDCFloatingLabelFoundation.prototype.destroy = function () {
  67. this.adapter.deregisterInteractionHandler('animationend', this.shakeAnimationEndHandler);
  68. };
  69. /**
  70. * Returns the width of the label element.
  71. */
  72. MDCFloatingLabelFoundation.prototype.getWidth = function () {
  73. return this.adapter.getWidth();
  74. };
  75. /**
  76. * Styles the label to produce a shake animation to indicate an error.
  77. * @param shouldShake If true, adds the shake CSS class; otherwise, removes
  78. * shake class.
  79. */
  80. MDCFloatingLabelFoundation.prototype.shake = function (shouldShake) {
  81. var LABEL_SHAKE = MDCFloatingLabelFoundation.cssClasses.LABEL_SHAKE;
  82. if (shouldShake) {
  83. this.adapter.addClass(LABEL_SHAKE);
  84. }
  85. else {
  86. this.adapter.removeClass(LABEL_SHAKE);
  87. }
  88. };
  89. /**
  90. * Styles the label to float or dock.
  91. * @param shouldFloat If true, adds the float CSS class; otherwise, removes
  92. * float and shake classes to dock the label.
  93. */
  94. MDCFloatingLabelFoundation.prototype.float = function (shouldFloat) {
  95. var _a = MDCFloatingLabelFoundation.cssClasses, LABEL_FLOAT_ABOVE = _a.LABEL_FLOAT_ABOVE, LABEL_SHAKE = _a.LABEL_SHAKE;
  96. if (shouldFloat) {
  97. this.adapter.addClass(LABEL_FLOAT_ABOVE);
  98. }
  99. else {
  100. this.adapter.removeClass(LABEL_FLOAT_ABOVE);
  101. this.adapter.removeClass(LABEL_SHAKE);
  102. }
  103. };
  104. /**
  105. * Styles the label as required.
  106. * @param isRequired If true, adds an asterisk to the label, indicating that
  107. * it is required.
  108. */
  109. MDCFloatingLabelFoundation.prototype.setRequired = function (isRequired) {
  110. var LABEL_REQUIRED = MDCFloatingLabelFoundation.cssClasses.LABEL_REQUIRED;
  111. if (isRequired) {
  112. this.adapter.addClass(LABEL_REQUIRED);
  113. }
  114. else {
  115. this.adapter.removeClass(LABEL_REQUIRED);
  116. }
  117. };
  118. MDCFloatingLabelFoundation.prototype.setHideRequiredMarker = function (hideRequiredMarker) {
  119. var LABEL_HIDE_REQUIRED_MARKER = MDCFloatingLabelFoundation.cssClasses.LABEL_HIDE_REQUIRED_MARKER;
  120. if (hideRequiredMarker) {
  121. this.adapter.addClass(LABEL_HIDE_REQUIRED_MARKER);
  122. }
  123. else {
  124. this.adapter.removeClass(LABEL_HIDE_REQUIRED_MARKER);
  125. }
  126. };
  127. MDCFloatingLabelFoundation.prototype.getHideRequiredMarker = function () {
  128. var LABEL_HIDE_REQUIRED_MARKER = MDCFloatingLabelFoundation.cssClasses.LABEL_HIDE_REQUIRED_MARKER;
  129. return this.adapter.hasClass(LABEL_HIDE_REQUIRED_MARKER);
  130. };
  131. MDCFloatingLabelFoundation.prototype.handleShakeAnimationEnd = function () {
  132. var LABEL_SHAKE = MDCFloatingLabelFoundation.cssClasses.LABEL_SHAKE;
  133. this.adapter.removeClass(LABEL_SHAKE);
  134. };
  135. return MDCFloatingLabelFoundation;
  136. }(MDCFoundation));
  137. export { MDCFloatingLabelFoundation };
  138. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  139. export default MDCFloatingLabelFoundation;
  140. //# sourceMappingURL=foundation.js.map