import { Injectable } from '@angular/core' ; import { Connection } from '../../channel/connection' ; import { MessageFactory } from '../../../communication' ; import { Route } from '../../../backend/utils' ; import { matchNode , matchRoute , matchString } from '../../utils' ; import { ExpandState , ComponentViewState } from '../../state' ; import { MutableTree , Node , Path } from '../../../tree' ; @ Injectable ( ) export class UserActions { constructor ( private connection , private viewState ) { } toggle ( node , defaultState ) { let state = this . viewState . expandState ( node ) ; if ( state == null ) { state = defaultState ; } switch ( state ) { case ExpandState . Collapsed : this . viewState . expandState ( node , ExpandState . Expanded ) ; break ; case ExpandState . Expanded : default : this . viewState . expandState ( node , ExpandState . Collapsed ) ; break ; } } updateProperty ( path , newValue ) { return this . connection . send ( MessageFactory . updateProperty ( path , newValue ) ) ; } updateProvider ( path , providerKey , newValue ) { const [ token , ... propertyPath ] = providerKey ; return this . connection . send ( MessageFactory . updateProviderProperty ( path , token , propertyPath , newValue ) ) ; } emitValue ( path , value ? ) { return this . connection . send ( MessageFactory . emitValue ( path , value ) ) ; } searchComponents ( tree , query ) { return new Promise ( ( resolve , reject ) => { const results = tree . filter ( node => matchNode ( node , query ) ) ; if ( results . length > 0 ) { resolve ( results ) ; } else { reject ( new Error ( 'No results found' ) ) ; } } ) ; } searchRouter ( routerTree , query ) { return new Promise ( ( resolve , reject ) => { const recurse = ( node , fn ) => { fn ( node ) ; if ( node . children ) { node . children . forEach ( child => recurse ( child , fn ) ) ; } } ; const matches = new Array < Route > ( ) ; routerTree . forEach ( root => recurse ( root , node => { if ( matchString ( query , node . name ) || matchString ( query , node . path ) ) { matches . push ( node ) ; } } ) ) ; if ( matches . length > 0 ) { resolve ( matches ) ; } else { reject ( new Error ( 'No matching routes were found' ) ) ; } } ) ; } clearHighlight ( ) { return this . connection . send ( MessageFactory . highlight ( [ ] ) ) ; } highlight ( node ) { return this . connection . send ( MessageFactory . highlight ( [ node ] ) ) ; } findElement ( ) { return this . connection . send ( MessageFactory . findDOMElement ( ) ) ; } cancelFindElement ( ) { return this . connection . send ( MessageFactory . foundDOMElement ( null ) ) ; } }