104 lines
5.3 KiB
JavaScript
104 lines
5.3 KiB
JavaScript
|
/**
|
||
|
* @license
|
||
|
* Copyright 2016 Google Inc.
|
||
|
*
|
||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
* of this software and associated documentation files (the "Software"), to deal
|
||
|
* in the Software without restriction, including without limitation the rights
|
||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
* copies of the Software, and to permit persons to whom the Software is
|
||
|
* furnished to do so, subject to the following conditions:
|
||
|
*
|
||
|
* The above copyright notice and this permission notice shall be included in
|
||
|
* all copies or substantial portions of the Software.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
|
* THE SOFTWARE.
|
||
|
*/
|
||
|
import { __read, __spreadArray } from "tslib";
|
||
|
import { MDCFoundation } from './foundation';
|
||
|
var MDCComponent = /** @class */ (function () {
|
||
|
function MDCComponent(root, foundation) {
|
||
|
var args = [];
|
||
|
for (var _i = 2; _i < arguments.length; _i++) {
|
||
|
args[_i - 2] = arguments[_i];
|
||
|
}
|
||
|
this.root = root;
|
||
|
this.initialize.apply(this, __spreadArray([], __read(args)));
|
||
|
// Note that we initialize foundation here and not within the constructor's
|
||
|
// default param so that this.root is defined and can be used within the
|
||
|
// foundation class.
|
||
|
this.foundation =
|
||
|
foundation === undefined ? this.getDefaultFoundation() : foundation;
|
||
|
this.foundation.init();
|
||
|
this.initialSyncWithDOM();
|
||
|
}
|
||
|
MDCComponent.attachTo = function (root) {
|
||
|
// Subclasses which extend MDCBase should provide an attachTo() method that takes a root element and
|
||
|
// returns an instantiated component with its root set to that element. Also note that in the cases of
|
||
|
// subclasses, an explicit foundation class will not have to be passed in; it will simply be initialized
|
||
|
// from getDefaultFoundation().
|
||
|
return new MDCComponent(root, new MDCFoundation({}));
|
||
|
};
|
||
|
/* istanbul ignore next: method param only exists for typing purposes; it does not need to be unit tested */
|
||
|
MDCComponent.prototype.initialize = function () {
|
||
|
var _args = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
_args[_i] = arguments[_i];
|
||
|
}
|
||
|
// Subclasses can override this to do any additional setup work that would be considered part of a
|
||
|
// "constructor". Essentially, it is a hook into the parent constructor before the foundation is
|
||
|
// initialized. Any additional arguments besides root and foundation will be passed in here.
|
||
|
};
|
||
|
MDCComponent.prototype.getDefaultFoundation = function () {
|
||
|
// Subclasses must override this method to return a properly configured foundation class for the
|
||
|
// component.
|
||
|
throw new Error('Subclasses must override getDefaultFoundation to return a properly configured ' +
|
||
|
'foundation class');
|
||
|
};
|
||
|
MDCComponent.prototype.initialSyncWithDOM = function () {
|
||
|
// Subclasses should override this method if they need to perform work to synchronize with a host DOM
|
||
|
// object. An example of this would be a form control wrapper that needs to synchronize its internal state
|
||
|
// to some property or attribute of the host DOM. Please note: this is *not* the place to perform DOM
|
||
|
// reads/writes that would cause layout / paint, as this is called synchronously from within the constructor.
|
||
|
};
|
||
|
MDCComponent.prototype.destroy = function () {
|
||
|
// Subclasses may implement this method to release any resources / deregister any listeners they have
|
||
|
// attached. An example of this might be deregistering a resize event from the window object.
|
||
|
this.foundation.destroy();
|
||
|
};
|
||
|
MDCComponent.prototype.listen = function (evtType, handler, options) {
|
||
|
this.root.addEventListener(evtType, handler, options);
|
||
|
};
|
||
|
MDCComponent.prototype.unlisten = function (evtType, handler, options) {
|
||
|
this.root.removeEventListener(evtType, handler, options);
|
||
|
};
|
||
|
/**
|
||
|
* Fires a cross-browser-compatible custom event from the component root of the given type, with the given data.
|
||
|
*/
|
||
|
MDCComponent.prototype.emit = function (evtType, evtData, shouldBubble) {
|
||
|
if (shouldBubble === void 0) { shouldBubble = false; }
|
||
|
var evt;
|
||
|
if (typeof CustomEvent === 'function') {
|
||
|
evt = new CustomEvent(evtType, {
|
||
|
bubbles: shouldBubble,
|
||
|
detail: evtData,
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
evt = document.createEvent('CustomEvent');
|
||
|
evt.initCustomEvent(evtType, shouldBubble, false, evtData);
|
||
|
}
|
||
|
this.root.dispatchEvent(evt);
|
||
|
};
|
||
|
return MDCComponent;
|
||
|
}());
|
||
|
export { MDCComponent };
|
||
|
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
|
||
|
export default MDCComponent;
|
||
|
//# sourceMappingURL=component.js.map
|