import { Renderer2 } from '@angular/core' ; import * as _ from 'lodash' ; export const OF_ATTR = 'dvOf' ; const ALIAS_ATTR = 'dvAlias' ; const CLASS_ATTR = 'class' ; const PARENT_ID_ATTR = 'dvParentIs' ; const COMPONENT_ID_ATTR = '_dvComponentId' ; const RUN_ID_ATTR = '_dvRunId' ; const IS_APP_COMPONENT_ATTR = '_dvIsAppComponent' ; export class NodeUtils { private static GetConceptFromTag ( tag ) { return tag . substring ( 0 , tag . indexOf ( '-' ) ) ; } private static GetComponentFromTag ( tag ) { return tag . substring ( tag . indexOf ( '-' ) + 1 ) ; } private static GetFqTag ( tag , dvAlias , dvOf ) { if ( ! _ . isEmpty ( dvAlias ) ) { return dvAlias ; } else if ( ! _ . isEmpty ( dvOf ) ) { return dvOf + '-' + NodeUtils . GetComponentFromTag ( tag ) ; } else { return tag ; } } static GetTag ( node ) { return node . nodeName . toLowerCase ( ) ; } static GetAttribute ( node , attribute ) { if ( node . hasAttribute && node . hasAttribute ( attribute ) ) { return node . getAttribute ( attribute ) ; } return undefined ; } static SetComponentId ( node , componentId ) { node . setAttribute ( COMPONENT_ID_ATTR , componentId ) ; } static GetComponentId ( node ) { return NodeUtils . GetAttribute ( node , COMPONENT_ID_ATTR ) ; } static SetRunId ( node , runId ) { node . setAttribute ( RUN_ID_ATTR , runId ) ; } static GetRunId ( node ) { return NodeUtils . GetAttribute ( node , RUN_ID_ATTR ) ; } static HasRunId ( node ) { return node . hasAttribute ( RUN_ID_ATTR ) ; } static RemoveRunId ( node ) { node . removeAttribute ( RUN_ID_ATTR ) ; } static IsDvTx ( node ) { return ( node . nodeName . toLowerCase ( ) === 'dv-tx' || ( window [ 'dv-designer' ] && NodeUtils . GetFqTagOfNode ( node ) === 'dv-tx' ) ) ; } static IsComponent ( node ) { if ( window [ 'dv-designer' ] ) { return NodeUtils . GetFqTagOfNode ( node ) === 'dv-tx' || NodeUtils . GetFqTagOfNode ( node ) === 'user-component' || ( _ . includes ( NodeUtils . GetTag ( node ) , '-' ) && ! NodeUtils . GetTag ( node ) . startsWith ( 'mat-' ) && ! NodeUtils . GetTag ( node ) . startsWith ( 'app-' ) && ! NodeUtils . GetTag ( node ) . startsWith ( 'designer-' ) ) ; } return _ . includes ( NodeUtils . GetTag ( node ) , '-' ) && ! NodeUtils . GetTag ( node ) . startsWith ( 'mat-' ) ; } static IsAppComponent ( node ) { return node . hasAttribute ( IS_APP_COMPONENT_ATTR ) ; } static MarkAsAppComponent ( node ) { node . setAttribute ( IS_APP_COMPONENT_ATTR , '' ) ; } static GetConceptAliasOfNode ( node ) { const conceptOfTag = NodeUtils . GetConceptFromTag ( NodeUtils . GetTag ( node ) ) ; const dvOf = NodeUtils . GetAttribute ( node , OF_ATTR ) ; return ! _ . isEmpty ( dvOf ) ? dvOf : conceptOfTag ; } static GetFqTagOfNode ( node ) { const tag = NodeUtils . GetTag ( node ) ; const dvAlias = NodeUtils . GetAttribute ( node , ALIAS_ATTR ) ; const dvOf = NodeUtils . GetAttribute ( node , OF_ATTR ) ; return NodeUtils . GetFqTag ( tag , dvAlias , dvOf ) ; } static GetCssClassesOfNode ( node ) { const classAttr = NodeUtils . GetAttribute ( node , CLASS_ATTR ) ; return _ . isEmpty ( classAttr ) ? [ ] : classAttr ! . split ( ' ' ) ; } static SetOfOfNode ( node , value ) { node . setAttribute ( OF_ATTR , value ) ; } static WalkUpFromNode ( node , renderer , breakFn ? , visitFn ? ) { while ( node && node . getAttribute ) { if ( breakFn && breakFn ( node ) ) { break ; } if ( visitFn ) { visitFn ( node ) ; } const parentAttr = NodeUtils . GetAttribute ( node , PARENT_ID_ATTR ) ; if ( parentAttr !== undefined ) { node = renderer . selectRootElement ( `.dv-parent-${ parentAttr }` ) ; } else { node = renderer . parentNode ( node ) ; } } } static GetAppComponentNodeContainingNode ( node , renderer ) { let appComponentNode = null ; NodeUtils . WalkUpFromNode ( node , renderer , ( visitNode ) => { if ( NodeUtils . IsAppComponent ( visitNode ) ) { appComponentNode = visitNode ; return true ; } return false ; } ) ; return appComponentNode ; } }