import type * as d from '../declarations' ; import { addHostEventListeners , doc , getHostRef , nextTick , plt , supportsShadow } from '@platform' ; import { addStyle } from './styles' ; import { attachToAncestor } from './update-component' ; import { BUILD } from '@app-data' ; import { CMP_FLAGS , HOST_FLAGS , MEMBER_FLAGS } from '@utils' ; import { createTime } from './profile' ; import { HYDRATE_ID , NODE_TYPE , PLATFORM_FLAGS } from './runtime-constants' ; import { initializeClientHydrate } from './client-hydrate' ; import { initializeComponent , fireConnectedCallback } from './initialize-component' ; export const connectedCallback = ( elm ) => { if ( ( plt . $flags$ & PLATFORM_FLAGS . isTmpDisconnected ) === 0 ) { const hostRef = getHostRef ( elm ) ; const cmpMeta = hostRef . $cmpMeta$ ; const endConnected = createTime ( 'connectedCallback' , cmpMeta . $tagName$ ) ; if ( BUILD . hostListenerTargetParent ) { addHostEventListeners ( elm , hostRef , cmpMeta . $listeners$ , true ) ; } if ( ! ( hostRef . $flags$ & HOST_FLAGS . hasConnected ) ) { hostRef . $flags$ |= HOST_FLAGS . hasConnected ; let hostId ; if ( BUILD . hydrateClientSide ) { hostId = elm . getAttribute ( HYDRATE_ID ) ; if ( hostId ) { if ( BUILD . shadowDom && supportsShadow && cmpMeta . $flags$ & CMP_FLAGS . shadowDomEncapsulation ) { const scopeId = BUILD . mode ? addStyle ( elm . shadowRoot , cmpMeta , elm . getAttribute ( 's-mode' ) ) : addStyle ( elm . shadowRoot , cmpMeta ) ; elm . classList . remove ( scopeId + '-h' , scopeId + '-s' ) ; } initializeClientHydrate ( elm , cmpMeta . $tagName$ , hostId , hostRef ) ; } } if ( BUILD . slotRelocation && ! hostId ) { if ( BUILD . hydrateServerSide || ( ( BUILD . slot || BUILD . shadowDom ) && cmpMeta . $flags$ & ( CMP_FLAGS . hasSlotRelocation | CMP_FLAGS . needsShadowDomShim ) ) ) { setContentReference ( elm ) ; } } if ( BUILD . asyncLoading ) { let ancestorComponent = elm ; while ( ( ancestorComponent = ( ancestorComponent . parentNode as any ) || ( ancestorComponent . host as any ) ) ) { if ( ( BUILD . hydrateClientSide && ancestorComponent . nodeType === NODE_TYPE . ElementNode && ancestorComponent . hasAttribute ( 's-id' ) && ancestorComponent [ 's-p' ] ) || ancestorComponent [ 's-p' ] ) { attachToAncestor ( hostRef , ( hostRef . $ancestorComponent$ = ancestorComponent ) ) ; break ; } } } if ( BUILD . prop && ! BUILD . hydrateServerSide && cmpMeta . $members$ ) { Object . entries ( cmpMeta . $members$ ) . map ( ( [ memberName , [ memberFlags ] ] ) => { if ( memberFlags & MEMBER_FLAGS . Prop && elm . hasOwnProperty ( memberName ) ) { const value = ( elm as any ) [ memberName ] ; delete ( elm as any ) [ memberName ] ; ( elm as any ) [ memberName ] = value ; } } ) ; } if ( BUILD . initializeNextTick ) { nextTick ( ( ) => initializeComponent ( elm , hostRef , cmpMeta ) ) ; } else { initializeComponent ( elm , hostRef , cmpMeta ) ; } } else { addHostEventListeners ( elm , hostRef , cmpMeta . $listeners$ , false ) ; fireConnectedCallback ( hostRef . $lazyInstance$ ) ; } endConnected ( ) ; } } ; const setContentReference = ( elm ) => { const contentRefElm = ( elm [ 's-cr' ] = doc . createComment ( BUILD . isDebug ? `content-ref (host=${ elm . localName })` : '' ) as any ) ; contentRefElm [ 's-cn' ] = true ; elm . insertBefore ( contentRefElm , elm . firstChild ) ; } ;