foundation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license
  3. * Copyright 2018 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 Switch Foundation */
  27. var MDCSwitchFoundation = /** @class */ (function (_super) {
  28. __extends(MDCSwitchFoundation, _super);
  29. function MDCSwitchFoundation(adapter) {
  30. return _super.call(this, __assign(__assign({}, MDCSwitchFoundation.defaultAdapter), adapter)) || this;
  31. }
  32. Object.defineProperty(MDCSwitchFoundation, "strings", {
  33. /** The string constants used by the switch. */
  34. get: function () {
  35. return strings;
  36. },
  37. enumerable: false,
  38. configurable: true
  39. });
  40. Object.defineProperty(MDCSwitchFoundation, "cssClasses", {
  41. /** The CSS classes used by the switch. */
  42. get: function () {
  43. return cssClasses;
  44. },
  45. enumerable: false,
  46. configurable: true
  47. });
  48. Object.defineProperty(MDCSwitchFoundation, "defaultAdapter", {
  49. /** The default Adapter for the switch. */
  50. get: function () {
  51. return {
  52. addClass: function () { return undefined; },
  53. removeClass: function () { return undefined; },
  54. setNativeControlChecked: function () { return undefined; },
  55. setNativeControlDisabled: function () { return undefined; },
  56. setNativeControlAttr: function () { return undefined; },
  57. };
  58. },
  59. enumerable: false,
  60. configurable: true
  61. });
  62. /** Sets the checked state of the switch. */
  63. MDCSwitchFoundation.prototype.setChecked = function (checked) {
  64. this.adapter.setNativeControlChecked(checked);
  65. this.updateAriaChecked(checked);
  66. this.updateCheckedStyling(checked);
  67. };
  68. /** Sets the disabled state of the switch. */
  69. MDCSwitchFoundation.prototype.setDisabled = function (disabled) {
  70. this.adapter.setNativeControlDisabled(disabled);
  71. if (disabled) {
  72. this.adapter.addClass(cssClasses.DISABLED);
  73. }
  74. else {
  75. this.adapter.removeClass(cssClasses.DISABLED);
  76. }
  77. };
  78. /** Handles the change event for the switch native control. */
  79. MDCSwitchFoundation.prototype.handleChange = function (evt) {
  80. var nativeControl = evt.target;
  81. this.updateAriaChecked(nativeControl.checked);
  82. this.updateCheckedStyling(nativeControl.checked);
  83. };
  84. /** Updates the styling of the switch based on its checked state. */
  85. MDCSwitchFoundation.prototype.updateCheckedStyling = function (checked) {
  86. if (checked) {
  87. this.adapter.addClass(cssClasses.CHECKED);
  88. }
  89. else {
  90. this.adapter.removeClass(cssClasses.CHECKED);
  91. }
  92. };
  93. MDCSwitchFoundation.prototype.updateAriaChecked = function (checked) {
  94. this.adapter.setNativeControlAttr(strings.ARIA_CHECKED_ATTR, "" + !!checked);
  95. };
  96. return MDCSwitchFoundation;
  97. }(MDCFoundation));
  98. export { MDCSwitchFoundation };
  99. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  100. export default MDCSwitchFoundation;
  101. //# sourceMappingURL=foundation.js.map