license import { MDCFoundation } from './foundation' ; import { CustomEventListener , EventType , SpecificEventListener } from './types' ; export class MDCComponent < FoundationType extends MDCFoundation > { static attachTo ( root ) { return new MDCComponent ( root , new MDCFoundation ( { } ) ) ; } protected foundation : FoundationType ; constructor ( public root , foundation ? , ... args ) { this . initialize ( ... args ) ; this . foundation = foundation === undefined ? this . getDefaultFoundation ( ) : foundation ; this . foundation . init ( ) ; this . initialSyncWithDOM ( ) ; } initialize ( ... _args ) { } getDefaultFoundation ( ) { throw new Error ( 'Subclasses must override getDefaultFoundation to return a properly configured ' + 'foundation class' ) ; } initialSyncWithDOM ( ) { } destroy ( ) { this . foundation . destroy ( ) ; } /**
     * Wrapper method to add an event listener to the component's root element. This is most useful when
     * listening for custom events.
     */ listen < K extends EventType > ( evtType , handler , options ? ) ; listen < E extends Event > ( evtType , handler , options ? ) ; listen ( evtType , handler , options ? ) { this . root . addEventListener ( evtType , handler , options ) ; } /**
     * Wrapper method to remove an event listener to the component's root element. This is most useful when
     * unlistening for custom events.
     */ unlisten < K extends EventType > ( evtType , handler , options ? ) ; unlisten < E extends Event > ( evtType , handler , options ? ) ; unlisten ( 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.
     */ emit < T extends object > ( evtType , evtData , shouldBubble = false ) { let evt ; if ( typeof CustomEvent === 'function' ) { evt = new CustomEvent < T > ( evtType , { bubbles : shouldBubble , detail : evtData , } ) ; } else { evt = document . createEvent ( 'CustomEvent' ) ; evt . initCustomEvent ( evtType , shouldBubble , false , evtData ) ; } this . root . dispatchEvent ( evt ) ; } } export default MDCComponent ;