@@ -4,21 +4,72 @@ String _COMPONENT = '-component';
4
4
String _DIRECTIVE = '-directive' ;
5
5
String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE ;
6
6
7
+ class NgComponent {
8
+ final String template;
9
+ final String templateUrl;
10
+ final String cssUrl;
11
+ final String visibility;
12
+ final Map <String , String > map;
13
+ final String publishAs;
14
+ final bool applyAuthorStyles;
15
+ final bool resetStyleInheritance;
16
+
17
+ const NgComponent ({
18
+ this .template,
19
+ this .templateUrl,
20
+ this .cssUrl,
21
+ this .visibility: NgDirective .LOCAL_VISIBILITY ,
22
+ this .map,
23
+ this .publishAs,
24
+ this .applyAuthorStyles,
25
+ this .resetStyleInheritance
26
+ });
27
+ }
28
+
29
+ class NgDirective {
30
+ static const String LOCAL_VISIBILITY = 'local' ;
31
+ static const String CHILDREN_VISIBILITY = 'children' ;
32
+ static const String DIRECT_CHILDREN_VISIBILITY = 'direct_children' ;
33
+
34
+ final String selector;
35
+ final String transclude;
36
+ final int priority;
37
+ final String visibility;
38
+
39
+ const NgDirective ({
40
+ this .selector,
41
+ this .transclude,
42
+ this .priority : 0 ,
43
+ this .visibility: LOCAL_VISIBILITY
44
+ });
45
+ }
46
+
47
+ /**
48
+ * See:
49
+ * http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc-style-inheriting
50
+ */
51
+ class NgShadowRootOptions {
52
+ final bool applyAuthorStyles;
53
+ final bool resetStyleInheritance;
54
+ const NgShadowRootOptions ([this .applyAuthorStyles = false ,
55
+ this .resetStyleInheritance = false ]);
56
+ }
57
+
58
+ // TODO(pavelgj): Get rid of Directive and use NgComponent/NgDirective directly.
7
59
class Directive {
8
60
Type type;
9
61
// TODO(misko): this should be renamed to selector once we change over to meta-data.
10
62
String $name;
11
63
Function $generate;
12
64
String $transclude;
13
65
int $priority = 0 ;
14
- Type $controllerType;
15
66
String $template;
16
67
String $templateUrl;
17
68
String $cssUrl;
18
69
String $publishAs;
19
70
Map <String , String > $map;
20
71
String $visibility;
21
- ShadowRootOptions $shadowRootOptions;
72
+ NgShadowRootOptions $shadowRootOptions = new NgShadowRootOptions () ;
22
73
23
74
bool isComponent = false ;
24
75
bool isStructural = false ;
@@ -31,9 +82,32 @@ class Directive {
31
82
onMatch: (m) => '-' + m.group (0 ).toLowerCase ())
32
83
.substring (1 );
33
84
34
- var $selector = reflectStaticField (type, r'$selector' );
35
- if ($selector != null ) {
36
- $name = $selector;
85
+ var directive = _reflectSingleMetadata (type, NgDirective );
86
+ var component = _reflectSingleMetadata (type, NgComponent );
87
+ if (directive != null && component != null ) {
88
+ throw 'Cannot have both NgDirective and NgComponent annotations.' ;
89
+ }
90
+
91
+ var selector;
92
+ if (directive != null ) {
93
+ selector = directive.selector;
94
+ $transclude = directive.transclude;
95
+ $priority = directive.priority;
96
+ $visibility = directive.visibility;
97
+ }
98
+ if (component != null ) {
99
+ $template = component.template;
100
+ $templateUrl = component.templateUrl;
101
+ $cssUrl = component.cssUrl;
102
+ $visibility = component.visibility;
103
+ $map = component.map;
104
+ $publishAs = component.publishAs;
105
+ $shadowRootOptions = new NgShadowRootOptions (component.applyAuthorStyles,
106
+ component.resetStyleInheritance);
107
+ }
108
+
109
+ if (selector != null ) {
110
+ $name = selector;
37
111
} else if ($name.endsWith (_ATTR_DIRECTIVE )) {
38
112
$name = '[${$name .substring (0 , $name .length - _ATTR_DIRECTIVE .length )}]' ;
39
113
} else if ($name.endsWith (_DIRECTIVE )) {
@@ -45,30 +119,24 @@ class Directive {
45
119
throw "Directive name '$name ' must end with $_DIRECTIVE , $_ATTR_DIRECTIVE , $_COMPONENT or have a \$ selector field." ;
46
120
}
47
121
48
- // Check the $transclude.
49
- // TODO(deboer): I'm not a fan of 'null' as a configuration value.
50
- // It would be awesome if $transclude could be an enum.
51
- $transclude = reflectStaticField (type, '\$ transclude' );
52
- $template = reflectStaticField (type, '\$ template' );
53
- $templateUrl = reflectStaticField (type, '\$ templateUrl' );
54
- $cssUrl = reflectStaticField (type, '\$ cssUrl' );
55
- $priority = _defaultIfNull (reflectStaticField (type, '\$ priority' ), 0 );
56
- $visibility = _defaultIfNull (
57
- reflectStaticField (type, '\$ visibility' ), DirectiveVisibility .LOCAL );
58
- $map = reflectStaticField (type, '\$ map' );
59
- $publishAs = reflectStaticField (type, r'$publishAs' );
60
122
isStructural = $transclude != null ;
61
123
if (isComponent && $map == null ) {
62
124
$map = new Map <String , String >();
63
125
}
64
- if (isComponent) {
65
- $shadowRootOptions = _defaultIfNull (
66
- reflectStaticField (type, '\$ shadowRootOptions' ),
67
- new ShadowRootOptions ());
68
- }
69
126
}
70
127
}
71
128
129
+ _reflectSingleMetadata (Type type, Type metadataType) {
130
+ var metadata = reflectMetadata (type, metadataType);
131
+ if (metadata.length == 0 ) {
132
+ return null ;
133
+ }
134
+ if (metadata.length > 1 ) {
135
+ throw 'Expecting not more than one annotation of type $metadataType ' ;
136
+ }
137
+ return metadata.first;
138
+ }
139
+
72
140
dynamic _defaultIfNull (dynamic value, dynamic defaultValue) =>
73
141
value == null ? defaultValue : value;
74
142
@@ -113,23 +181,6 @@ class DirectiveRegistry {
113
181
}
114
182
}
115
183
116
- abstract class DirectiveVisibility {
117
- static const String LOCAL = 'local' ;
118
- static const String CHILDREN = 'children' ;
119
- static const String DIRECT_CHILDREN = 'direct_children' ;
120
- }
121
-
122
- /**
123
- * See:
124
- * http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc-style-inheriting
125
- */
126
- class ShadowRootOptions {
127
- bool $applyAuthorStyles = false ;
128
- bool $resetStyleInheritance = false ;
129
- ShadowRootOptions ([this .$applyAuthorStyles = false ,
130
- this .$resetStyleInheritance = false ]);
131
- }
132
-
133
184
class Controller {
134
185
135
186
}
0 commit comments