import { getPrefix } from '../prefix' ; import { CustomDirective , TgDirective , TgElementExtraData } from '../type' ; const SHOULD_NOT_INHERIT_DIRECTIVES = [ `tg-follow` , `tg-ref` ] ; const directives = { } ; const importDir = require . context ( './' , false , /tg-[\S]+\.ts$/ ) ; importDir . keys ( ) . map ( ( key ) => { const formatKey = key . match ( /tg-[^.]+/ ) ?. [ 0 ] as TgDirective | null ; if ( formatKey ) { directives [ formatKey ] = importDir ( key ) ; } } ) ; export function extractValues ( element , directive , data ? ) { let directiveKey = directive as TgDirective ; if ( directiveKey . substring ( 0 , 3 ) !== 'tg-' ) { const newPrefix = getPrefix ( ) ; directiveKey = `tg-${ directiveKey . replace ( newPrefix , '' ) }` ; } if ( typeof directives [ directiveKey ] === 'undefined' ) { return null ; } const targetElement = selfOrInheritFromParent ( element , directive , directiveKey ) ; const value = targetElement . hasAttribute ( directive ) ? targetElement . getAttribute ( directive ) : null ; return directives [ directiveKey ] . get ( value , data ) ; } function selfOrInheritFromParent ( el , directive , directiveKey ) { if ( el . hasAttribute ( directive ) || SHOULD_NOT_INHERIT_DIRECTIVES . includes ( directiveKey ) ) { return el ; } let currentEl = el ; while ( true ) { currentEl = currentEl . parentElement ! ; if ( currentEl === document . body ) { break ; } if ( currentEl . hasAttribute ( directive ) ) { return currentEl ; } } return el ; }