license import { MDCFoundation } from '@material/base/foundation' ; import { MDCCheckboxAdapter } from './adapter' ; import { cssClasses , numbers , strings } from './constants' ; export class MDCCheckboxFoundation extends MDCFoundation < MDCCheckboxAdapter > { static override get cssClasses ( ) { return cssClasses ; } static override get strings ( ) { return strings ; } static override get numbers ( ) { return numbers ; } static override get defaultAdapter ( ) : MDCCheckboxAdapter { return { addClass : ( ) => undefined , forceLayout : ( ) => undefined , hasNativeControl : ( ) => false , isAttachedToDOM : ( ) => false , isChecked : ( ) => false , isIndeterminate : ( ) => false , removeClass : ( ) => undefined , removeNativeControlAttr : ( ) => undefined , setNativeControlAttr : ( ) => undefined , setNativeControlDisabled : ( ) => undefined , } ; } private currentCheckState = strings . TRANSITION_STATE_INIT ; private currentAnimationClass = '' ; private animEndLatchTimer = 0 ; private enableAnimationEndHandler = false ; constructor ( adapter ? ) { super ( { ... MDCCheckboxFoundation . defaultAdapter , ... adapter } ) ; } override init ( ) { this . currentCheckState = this . determineCheckState ( ) ; this . updateAriaChecked ( ) ; this . adapter . addClass ( cssClasses . UPGRADED ) ; } override destroy ( ) { clearTimeout ( this . animEndLatchTimer ) ; } setDisabled ( disabled ) { this . adapter . setNativeControlDisabled ( disabled ) ; if ( disabled ) { this . adapter . addClass ( cssClasses . DISABLED ) ; } else { this . adapter . removeClass ( cssClasses . DISABLED ) ; } } /**
     * Handles the animationend event for the checkbox
     */ handleAnimationEnd ( ) { if ( ! this . enableAnimationEndHandler ) { return ; } clearTimeout ( this . animEndLatchTimer ) ; this . animEndLatchTimer = setTimeout ( ( ) => { this . adapter . removeClass ( this . currentAnimationClass ) ; this . enableAnimationEndHandler = false ; } , numbers . ANIM_END_LATCH_MS ) ; } /**
     * Handles the change event for the checkbox
     */ handleChange ( ) { this . transitionCheckState ( ) ; } private transitionCheckState ( ) { if ( ! this . adapter . hasNativeControl ( ) ) { return ; } const oldState = this . currentCheckState ; const newState = this . determineCheckState ( ) ; if ( oldState === newState ) { return ; } this . updateAriaChecked ( ) ; const { TRANSITION_STATE_UNCHECKED } = strings ; const { SELECTED } = cssClasses ; if ( newState === TRANSITION_STATE_UNCHECKED ) { this . adapter . removeClass ( SELECTED ) ; } else { this . adapter . addClass ( SELECTED ) ; } if ( this . currentAnimationClass . length > 0 ) { clearTimeout ( this . animEndLatchTimer ) ; this . adapter . forceLayout ( ) ; this . adapter . removeClass ( this . currentAnimationClass ) ; } this . currentAnimationClass = this . getTransitionAnimationClass ( oldState , newState ) ; this . currentCheckState = newState ; if ( this . adapter . isAttachedToDOM ( ) && this . currentAnimationClass . length > 0 ) { this . adapter . addClass ( this . currentAnimationClass ) ; this . enableAnimationEndHandler = true ; } } private determineCheckState ( ) { const { TRANSITION_STATE_INDETERMINATE , TRANSITION_STATE_CHECKED , TRANSITION_STATE_UNCHECKED , } = strings ; if ( this . adapter . isIndeterminate ( ) ) { return TRANSITION_STATE_INDETERMINATE ; } return this . adapter . isChecked ( ) ? TRANSITION_STATE_CHECKED : TRANSITION_STATE_UNCHECKED ; } private getTransitionAnimationClass ( oldState , newState ) { const { TRANSITION_STATE_INIT , TRANSITION_STATE_CHECKED , TRANSITION_STATE_UNCHECKED , } = strings ; const { ANIM_UNCHECKED_CHECKED , ANIM_UNCHECKED_INDETERMINATE , ANIM_CHECKED_UNCHECKED , ANIM_CHECKED_INDETERMINATE , ANIM_INDETERMINATE_CHECKED , ANIM_INDETERMINATE_UNCHECKED , } = MDCCheckboxFoundation . cssClasses ; switch ( oldState ) { case TRANSITION_STATE_INIT : if ( newState === TRANSITION_STATE_UNCHECKED ) { return '' ; } return newState === TRANSITION_STATE_CHECKED ? ANIM_INDETERMINATE_CHECKED : ANIM_INDETERMINATE_UNCHECKED ; case TRANSITION_STATE_UNCHECKED : return newState === TRANSITION_STATE_CHECKED ? ANIM_UNCHECKED_CHECKED : ANIM_UNCHECKED_INDETERMINATE ; case TRANSITION_STATE_CHECKED : return newState === TRANSITION_STATE_UNCHECKED ? ANIM_CHECKED_UNCHECKED : ANIM_CHECKED_INDETERMINATE ; default : return newState === TRANSITION_STATE_CHECKED ? ANIM_INDETERMINATE_CHECKED : ANIM_INDETERMINATE_UNCHECKED ; } } private updateAriaChecked ( ) { if ( this . adapter . isIndeterminate ( ) ) { this . adapter . setNativeControlAttr ( strings . ARIA_CHECKED_ATTR , strings . ARIA_CHECKED_INDETERMINATE_VALUE ) ; } else { this . adapter . removeNativeControlAttr ( strings . ARIA_CHECKED_ATTR ) ; } } } export default MDCCheckboxFoundation ;