foundation.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license
  3. * Copyright 2020 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, strings } from './constants';
  26. /** MDC Circular Progress Foundation */
  27. var MDCCircularProgressFoundation = /** @class */ (function (_super) {
  28. __extends(MDCCircularProgressFoundation, _super);
  29. function MDCCircularProgressFoundation(adapter) {
  30. return _super.call(this, __assign(__assign({}, MDCCircularProgressFoundation.defaultAdapter), adapter)) || this;
  31. }
  32. Object.defineProperty(MDCCircularProgressFoundation, "cssClasses", {
  33. get: function () {
  34. return cssClasses;
  35. },
  36. enumerable: false,
  37. configurable: true
  38. });
  39. Object.defineProperty(MDCCircularProgressFoundation, "strings", {
  40. get: function () {
  41. return strings;
  42. },
  43. enumerable: false,
  44. configurable: true
  45. });
  46. Object.defineProperty(MDCCircularProgressFoundation, "defaultAdapter", {
  47. get: function () {
  48. return {
  49. addClass: function () { return undefined; },
  50. getDeterminateCircleAttribute: function () { return null; },
  51. hasClass: function () { return false; },
  52. removeClass: function () { return undefined; },
  53. removeAttribute: function () { return undefined; },
  54. setAttribute: function () { return undefined; },
  55. setDeterminateCircleAttribute: function () { return undefined; },
  56. };
  57. },
  58. enumerable: false,
  59. configurable: true
  60. });
  61. MDCCircularProgressFoundation.prototype.init = function () {
  62. this.closed = this.adapter.hasClass(cssClasses.CLOSED_CLASS);
  63. this.determinate = !this.adapter.hasClass(cssClasses.INDETERMINATE_CLASS);
  64. this.progress = 0;
  65. if (this.determinate) {
  66. this.adapter.setAttribute(strings.ARIA_VALUENOW, this.progress.toString());
  67. }
  68. this.radius =
  69. Number(this.adapter.getDeterminateCircleAttribute(strings.RADIUS));
  70. };
  71. /**
  72. * Sets whether the progress indicator is in determinate mode.
  73. * @param determinate Whether the indicator should be determinate.
  74. */
  75. MDCCircularProgressFoundation.prototype.setDeterminate = function (determinate) {
  76. this.determinate = determinate;
  77. if (this.determinate) {
  78. this.adapter.removeClass(cssClasses.INDETERMINATE_CLASS);
  79. this.setProgress(this.progress);
  80. }
  81. else {
  82. this.adapter.addClass(cssClasses.INDETERMINATE_CLASS);
  83. this.adapter.removeAttribute(strings.ARIA_VALUENOW);
  84. }
  85. };
  86. MDCCircularProgressFoundation.prototype.isDeterminate = function () {
  87. return this.determinate;
  88. };
  89. /**
  90. * Sets the current progress value. In indeterminate mode, this has no
  91. * visual effect but will be reflected if the indicator is switched to
  92. * determinate mode.
  93. * @param value The current progress value, which must be between 0 and 1.
  94. */
  95. MDCCircularProgressFoundation.prototype.setProgress = function (value) {
  96. this.progress = value;
  97. if (this.determinate) {
  98. var unfilledArcLength = (1 - this.progress) * (2 * Math.PI * this.radius);
  99. this.adapter.setDeterminateCircleAttribute(strings.STROKE_DASHOFFSET, "" + unfilledArcLength);
  100. this.adapter.setAttribute(strings.ARIA_VALUENOW, this.progress.toString());
  101. }
  102. };
  103. MDCCircularProgressFoundation.prototype.getProgress = function () {
  104. return this.progress;
  105. };
  106. /**
  107. * Shows the progress indicator.
  108. */
  109. MDCCircularProgressFoundation.prototype.open = function () {
  110. this.closed = false;
  111. this.adapter.removeClass(cssClasses.CLOSED_CLASS);
  112. this.adapter.removeAttribute(strings.ARIA_HIDDEN);
  113. };
  114. /**
  115. * Hides the progress indicator
  116. */
  117. MDCCircularProgressFoundation.prototype.close = function () {
  118. this.closed = true;
  119. this.adapter.addClass(cssClasses.CLOSED_CLASS);
  120. this.adapter.setAttribute(strings.ARIA_HIDDEN, 'true');
  121. };
  122. /**
  123. * @return Returns whether the progress indicator is hidden.
  124. */
  125. MDCCircularProgressFoundation.prototype.isClosed = function () {
  126. return this.closed;
  127. };
  128. return MDCCircularProgressFoundation;
  129. }(MDCFoundation));
  130. export { MDCCircularProgressFoundation };
  131. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  132. export default MDCCircularProgressFoundation;
  133. //# sourceMappingURL=foundation.js.map