11import { Injectable } from 'angular2/src/core/di' ;
22import { ViewMetadata } from '../metadata/view' ;
3+ import { ComponentMetadata } from '../metadata/directives' ;
34
4- import { Type , stringify , isBlank } from 'angular2/src/core/facade/lang' ;
5+ import { Type , stringify , isBlank , isPresent } from 'angular2/src/core/facade/lang' ;
56import { BaseException } from 'angular2/src/core/facade/exceptions' ;
67import { Map , MapWrapper , ListWrapper } from 'angular2/src/core/facade/collection' ;
78
@@ -24,13 +25,70 @@ export class ViewResolver {
2425 }
2526
2627 _resolve ( component : Type ) : ViewMetadata {
27- var annotations = reflector . annotations ( component ) ;
28- for ( var i = 0 ; i < annotations . length ; i ++ ) {
29- var annotation = annotations [ i ] ;
30- if ( annotation instanceof ViewMetadata ) {
31- return annotation ;
28+ var compMeta : ComponentMetadata ;
29+ var viewMeta : ViewMetadata ;
30+
31+ reflector . annotations ( component ) . forEach ( m => {
32+ if ( m instanceof ViewMetadata ) {
33+ viewMeta = m ;
34+ }
35+ if ( m instanceof ComponentMetadata ) {
36+ compMeta = m ;
37+ }
38+ } ) ;
39+
40+ if ( isPresent ( compMeta ) ) {
41+ if ( isBlank ( compMeta . template ) && isBlank ( compMeta . templateUrl ) && isBlank ( viewMeta ) ) {
42+ throw new BaseException (
43+ `Component '${ stringify ( component ) } ' must have either 'template', 'templateUrl', or '@View' set.` ) ;
44+
45+ } else if ( isPresent ( compMeta . template ) && isPresent ( viewMeta ) ) {
46+ this . _throwMixingViewAndComponent ( "template" , component ) ;
47+
48+ } else if ( isPresent ( compMeta . templateUrl ) && isPresent ( viewMeta ) ) {
49+ this . _throwMixingViewAndComponent ( "templateUrl" , component ) ;
50+
51+ } else if ( isPresent ( compMeta . directives ) && isPresent ( viewMeta ) ) {
52+ this . _throwMixingViewAndComponent ( "directives" , component ) ;
53+
54+ } else if ( isPresent ( compMeta . pipes ) && isPresent ( viewMeta ) ) {
55+ this . _throwMixingViewAndComponent ( "pipes" , component ) ;
56+
57+ } else if ( isPresent ( compMeta . encapsulation ) && isPresent ( viewMeta ) ) {
58+ this . _throwMixingViewAndComponent ( "encapsulation" , component ) ;
59+
60+ } else if ( isPresent ( compMeta . styles ) && isPresent ( viewMeta ) ) {
61+ this . _throwMixingViewAndComponent ( "styles" , component ) ;
62+
63+ } else if ( isPresent ( compMeta . styleUrls ) && isPresent ( viewMeta ) ) {
64+ this . _throwMixingViewAndComponent ( "styleUrls" , component ) ;
65+
66+ } else if ( isPresent ( viewMeta ) ) {
67+ return viewMeta ;
68+
69+ } else {
70+ return new ViewMetadata ( {
71+ templateUrl : compMeta . templateUrl ,
72+ template : compMeta . template ,
73+ directives : compMeta . directives ,
74+ pipes : compMeta . pipes ,
75+ encapsulation : compMeta . encapsulation ,
76+ styles : compMeta . styles ,
77+ styleUrls : compMeta . styleUrls
78+ } ) ;
79+ }
80+ } else {
81+ if ( isBlank ( viewMeta ) ) {
82+ throw new BaseException ( `No View decorator found on component '${ stringify ( component ) } '` ) ;
83+ } else {
84+ return viewMeta ;
3285 }
3386 }
34- throw new BaseException ( `No View annotation found on component ${ stringify ( component ) } ` ) ;
87+ return null ;
88+ }
89+
90+ _throwMixingViewAndComponent ( propertyName : string , component : Type ) : void {
91+ throw new BaseException (
92+ `Component '${ stringify ( component ) } ' cannot have both '${ propertyName } ' and '@View' set at the same time"` ) ;
3593 }
3694}
0 commit comments