import { DomSource , EventsFnOptions , StandardEvents } from '../types' ; import { EventDelegator } from './EventDelegator' ; import { MotorcycleDomSource } from './MotorcycleDomSource' ; import { SCOPE_PREFIX } from './common' ; import { Stream } from 'most' ; import { VNode } from 'mostly-dom' ; import { domEvent } from '@most/dom-event' ; import { elementMap } from './elementMap' ; import { shouldUseCapture } from './shouldUseCapture' ; export class ElementDomSource implements DomSource { protected _rootElement$ : Stream < Element > ; protected _namespace : Array < string > ; protected _delegator : EventDelegator ; protected _element : Element ; constructor ( rootElement$ , namespace , delegator = new EventDelegator ( ) , element ) { this . _rootElement$ = rootElement$ ; this . _namespace = namespace ; this . _delegator = delegator ; this . _element = element ; } public namespace ( ) { return this . _namespace ; } public select ( cssSelector ) { const trimmedSelector = cssSelector . trim ( ) ; if ( elementMap . has ( trimmedSelector ) ) return new ElementDomSource ( this . _rootElement$ , this . _namespace , this . _delegator , elementMap . get ( trimmedSelector ) as Element ) ; const amendedNamespace = trimmedSelector === `:root` ? this . _namespace : this . _namespace . concat ( trimmedSelector ) ; return new MotorcycleDomSource ( this . _rootElement$ , amendedNamespace , this . _delegator ) ; } public elements < T extends Element > ( ) { return this . _rootElement$ . constant ( [ this . _element as T ] ) ; } public events < T extends Event > ( eventType , options = { } ) { const useCapture = shouldUseCapture ( eventType , options . useCapture || false ) ; const event$ = domEvent ( eventType , this . _element , useCapture ) as Stream < T > ; return this . _rootElement$ . constant ( event$ ) . switch ( ) . multicast ( ) ; } public isolateSource ( source , scope ) { return source . select ( SCOPE_PREFIX + scope ) ; } public isolateSink ( sink , scope ) { return sink . tap ( ( vNode ) => { if ( ! vNode . scope ) vNode . scope = SCOPE_PREFIX + scope ; if ( ! vNode . key ) vNode . key = SCOPE_PREFIX + scope ; } ) ; } }