11import { resolveForwardRef , Injectable } from 'angular2/di' ;
22import { Type , isPresent , BaseException , stringify } from 'angular2/src/core/facade/lang' ;
3- import { DirectiveMetadata } from 'angular2/metadata' ;
3+ import { ListWrapper , StringMapWrapper } from 'angular2/src/core/facade/collection' ;
4+ import {
5+ DirectiveMetadata ,
6+ ComponentMetadata ,
7+ PropertyMetadata ,
8+ EventMetadata
9+ } from 'angular2/metadata' ;
410import { reflector } from 'angular2/src/core/reflection/reflection' ;
511
612/**
@@ -16,15 +22,78 @@ export class DirectiveResolver {
1622 * Return {@link DirectiveMetadata} for a given `Type`.
1723 */
1824 resolve ( type : Type ) : DirectiveMetadata {
19- var annotations = reflector . annotations ( resolveForwardRef ( type ) ) ;
20- if ( isPresent ( annotations ) ) {
21- for ( var i = 0 ; i < annotations . length ; i ++ ) {
22- var annotation = annotations [ i ] ;
23- if ( annotation instanceof DirectiveMetadata ) {
24- return annotation ;
25+ var typeMetadata = reflector . annotations ( resolveForwardRef ( type ) ) ;
26+ if ( isPresent ( typeMetadata ) ) {
27+ for ( var i = 0 ; i < typeMetadata . length ; i ++ ) {
28+ var metadata = typeMetadata [ i ] ;
29+ if ( metadata instanceof DirectiveMetadata ) {
30+ var propertyMetadata = reflector . propMetadata ( type ) ;
31+ return this . _mergeWithPropertyMetadata ( metadata , propertyMetadata ) ;
2532 }
2633 }
2734 }
2835 throw new BaseException ( `No Directive annotation found on ${ stringify ( type ) } ` ) ;
2936 }
37+
38+ private _mergeWithPropertyMetadata ( dm : DirectiveMetadata ,
39+ propertyMetadata :
40+ StringMap < string , any [ ] > ) : DirectiveMetadata {
41+ var properties = [ ] ;
42+ var events = [ ] ;
43+
44+ StringMapWrapper . forEach ( propertyMetadata , ( metadata : any [ ] , propName : string ) => {
45+ metadata . forEach ( a => {
46+ if ( a instanceof PropertyMetadata ) {
47+ if ( isPresent ( a . bindingPropertyName ) ) {
48+ properties . push ( `${ propName } : ${ a . bindingPropertyName } ` ) ;
49+ } else {
50+ properties . push ( propName ) ;
51+ }
52+ }
53+
54+ if ( a instanceof EventMetadata ) {
55+ if ( isPresent ( a . bindingPropertyName ) ) {
56+ events . push ( `${ propName } : ${ a . bindingPropertyName } ` ) ;
57+ } else {
58+ events . push ( propName ) ;
59+ }
60+ }
61+ } ) ;
62+ } ) ;
63+
64+ return this . _merge ( dm , properties , events ) ;
65+ }
66+
67+ private _merge ( dm : DirectiveMetadata , properties : string [ ] , events : string [ ] ) : DirectiveMetadata {
68+ var mergedProperties =
69+ isPresent ( dm . properties ) ? ListWrapper . concat ( dm . properties , properties ) : properties ;
70+ var mergedEvents = isPresent ( dm . events ) ? ListWrapper . concat ( dm . events , events ) : events ;
71+
72+ if ( dm instanceof ComponentMetadata ) {
73+ return new ComponentMetadata ( {
74+ selector : dm . selector ,
75+ properties : mergedProperties ,
76+ events : mergedEvents ,
77+ host : dm . host ,
78+ lifecycle : dm . lifecycle ,
79+ bindings : dm . bindings ,
80+ exportAs : dm . exportAs ,
81+ compileChildren : dm . compileChildren ,
82+ changeDetection : dm . changeDetection ,
83+ viewBindings : dm . viewBindings
84+ } ) ;
85+
86+ } else {
87+ return new DirectiveMetadata ( {
88+ selector : dm . selector ,
89+ properties : mergedProperties ,
90+ events : mergedEvents ,
91+ host : dm . host ,
92+ lifecycle : dm . lifecycle ,
93+ bindings : dm . bindings ,
94+ exportAs : dm . exportAs ,
95+ compileChildren : dm . compileChildren
96+ } ) ;
97+ }
98+ }
3099}
0 commit comments