component.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 * as tslib_1 from "tslib";
  24. import { MDCFoundation } from './foundation';
  25. var MDCComponent = /** @class */ (function () {
  26. function MDCComponent(root, foundation) {
  27. var args = [];
  28. for (var _i = 2; _i < arguments.length; _i++) {
  29. args[_i - 2] = arguments[_i];
  30. }
  31. this.root_ = root;
  32. this.initialize.apply(this, tslib_1.__spread(args));
  33. // Note that we initialize foundation here and not within the constructor's default param so that
  34. // this.root_ is defined and can be used within the foundation class.
  35. this.foundation_ = foundation === undefined ? this.getDefaultFoundation() : foundation;
  36. this.foundation_.init();
  37. this.initialSyncWithDOM();
  38. }
  39. MDCComponent.attachTo = function (root) {
  40. // Subclasses which extend MDCBase should provide an attachTo() method that takes a root element and
  41. // returns an instantiated component with its root set to that element. Also note that in the cases of
  42. // subclasses, an explicit foundation class will not have to be passed in; it will simply be initialized
  43. // from getDefaultFoundation().
  44. return new MDCComponent(root, new MDCFoundation({}));
  45. };
  46. /* istanbul ignore next: method param only exists for typing purposes; it does not need to be unit tested */
  47. MDCComponent.prototype.initialize = function () {
  48. var _args = [];
  49. for (var _i = 0; _i < arguments.length; _i++) {
  50. _args[_i] = arguments[_i];
  51. }
  52. // Subclasses can override this to do any additional setup work that would be considered part of a
  53. // "constructor". Essentially, it is a hook into the parent constructor before the foundation is
  54. // initialized. Any additional arguments besides root and foundation will be passed in here.
  55. };
  56. MDCComponent.prototype.getDefaultFoundation = function () {
  57. // Subclasses must override this method to return a properly configured foundation class for the
  58. // component.
  59. throw new Error('Subclasses must override getDefaultFoundation to return a properly configured ' +
  60. 'foundation class');
  61. };
  62. MDCComponent.prototype.initialSyncWithDOM = function () {
  63. // Subclasses should override this method if they need to perform work to synchronize with a host DOM
  64. // object. An example of this would be a form control wrapper that needs to synchronize its internal state
  65. // to some property or attribute of the host DOM. Please note: this is *not* the place to perform DOM
  66. // reads/writes that would cause layout / paint, as this is called synchronously from within the constructor.
  67. };
  68. MDCComponent.prototype.destroy = function () {
  69. // Subclasses may implement this method to release any resources / deregister any listeners they have
  70. // attached. An example of this might be deregistering a resize event from the window object.
  71. this.foundation_.destroy();
  72. };
  73. MDCComponent.prototype.listen = function (evtType, handler, options) {
  74. this.root_.addEventListener(evtType, handler, options);
  75. };
  76. MDCComponent.prototype.unlisten = function (evtType, handler, options) {
  77. this.root_.removeEventListener(evtType, handler, options);
  78. };
  79. /**
  80. * Fires a cross-browser-compatible custom event from the component root of the given type, with the given data.
  81. */
  82. MDCComponent.prototype.emit = function (evtType, evtData, shouldBubble) {
  83. if (shouldBubble === void 0) { shouldBubble = false; }
  84. var evt;
  85. if (typeof CustomEvent === 'function') {
  86. evt = new CustomEvent(evtType, {
  87. bubbles: shouldBubble,
  88. detail: evtData,
  89. });
  90. }
  91. else {
  92. evt = document.createEvent('CustomEvent');
  93. evt.initCustomEvent(evtType, shouldBubble, false, evtData);
  94. }
  95. this.root_.dispatchEvent(evt);
  96. };
  97. return MDCComponent;
  98. }());
  99. export { MDCComponent };
  100. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  101. export default MDCComponent;
  102. //# sourceMappingURL=component.js.map