license import { MDCComponent } from '@material/base/component' ; import { SpecificEventListener } from '@material/base/types' ; import { closest } from '@material/dom/ponyfill' ; import { MDCRippleAdapter } from '@material/ripple/adapter' ; import { MDCRipple , MDCRippleFactory } from '@material/ripple/component' ; import { MDCRippleFoundation } from '@material/ripple/foundation' ; import { MDCRippleCapableSurface } from '@material/ripple/types' ; import { MDCChipActionAdapter } from './adapter' ; import { computePrimaryActionRippleClientRect , GRAPHIC_SELECTED_WIDTH_STYLE_PROP } from './component-ripple' ; import { MDCChipActionCssClasses , MDCChipActionFocusBehavior , MDCChipActionType } from './constants' ; import { MDCChipActionFoundation } from './foundation' ; import { MDCChipPrimaryActionFoundation } from './primary-foundation' ; import { MDCChipTrailingActionFoundation } from './trailing-foundation' ; /**
 * MDCChipActionFactory is used by the parent MDCChip component to initialize
 * chip actions.
 */ export type MDCChipActionFactory = ( el : Element , foundation ? : MDCChipActionFoundation ) => MDCChipAction ; /**
 * MDCChipAction provides component encapsulation of the different foundation
 * implementations.
 */ export class MDCChipAction extends MDCComponent < MDCChipActionFoundation > implements MDCRippleCapableSurface { static override attachTo ( root ) { return new MDCChipAction ( root ) ; } private readonly rootHTML = this . root as HTMLElement ; private rippleInstance ! : MDCRipple ; private handleClick ! : SpecificEventListener < 'click' > ; private handleKeydown ! : SpecificEventListener < 'keydown' > ; get ripple ( ) : MDCRipple { return this . rippleInstance ; } override initialize ( rippleFactory = ( el , foundation ) => new MDCRipple ( el , foundation ) ) { const rippleAdapter = { ... MDCRipple . createAdapter ( this ) , computeBoundingRect : ( ) => this . computeRippleClientRect ( ) , } ; this . rippleInstance = rippleFactory ( this . root , new MDCRippleFoundation ( rippleAdapter ) ) ; } override initialSyncWithDOM ( ) { this . handleClick = ( ) => { this . foundation . handleClick ( ) ; } ; this . handleKeydown = ( event ) => { this . foundation . handleKeydown ( event ) ; } ; this . listen ( 'click' , this . handleClick ) ; this . listen ( 'keydown' , this . handleKeydown ) ; } override destroy ( ) { this . ripple . destroy ( ) ; this . unlisten ( 'click' , this . handleClick ) ; this . unlisten ( 'keydown' , this . handleKeydown ) ; super . destroy ( ) ; } override getDefaultFoundation ( ) { const adapter = { emitEvent : ( eventName , eventDetail ) => { this . emit ( eventName , eventDetail , true ) ; } , focus : ( ) => { this . rootHTML . focus ( ) ; } , getAttribute : ( attrName ) => this . root . getAttribute ( attrName ) , getElementID : ( ) => this . root . id , removeAttribute : ( name ) => { this . root . removeAttribute ( name ) ; } , setAttribute : ( name , value ) => { this . root . setAttribute ( name , value ) ; } , } ; if ( this . root . classList . contains ( MDCChipActionCssClasses . TRAILING_ACTION ) ) { return new MDCChipTrailingActionFoundation ( adapter ) ; } return new MDCChipPrimaryActionFoundation ( adapter ) ; } setDisabled ( isDisabled ) { this . foundation . setDisabled ( isDisabled ) ; } isDisabled ( ) { return this . foundation . isDisabled ( ) ; } setFocus ( behavior ) { this . foundation . setFocus ( behavior ) ; } isFocusable ( ) { return this . foundation . isFocusable ( ) ; } setSelected ( isSelected ) { this . foundation . setSelected ( isSelected ) ; } isSelected ( ) { return this . foundation . isSelected ( ) ; } isSelectable ( ) { return this . foundation . isSelectable ( ) ; } actionType ( ) { return this . foundation . actionType ( ) ; } private computeRippleClientRect ( ) { if ( this . root . classList . contains ( MDCChipActionCssClasses . PRIMARY_ACTION ) ) { const chipRoot = closest ( this . root , `.${ MDCChipActionCssClasses . CHIP_ROOT }` ) ; if ( ! chipRoot ) return this . root . getBoundingClientRect ( ) ; const graphicWidth = window . getComputedStyle ( chipRoot ) . getPropertyValue ( GRAPHIC_SELECTED_WIDTH_STYLE_PROP ) ; return computePrimaryActionRippleClientRect ( chipRoot . getBoundingClientRect ( ) , graphicWidth ) ; } return this . root . getBoundingClientRect ( ) ; } }