@@ -4,11 +4,20 @@ String _COMPONENT = '-component';
4
4
String _DIRECTIVE = '-directive' ;
5
5
String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE ;
6
6
7
- class NgComponent {
7
+ class NgAnnotationBase {
8
+ final String visibility;
9
+ final List <Type > publishTypes;
10
+
11
+ const NgAnnotationBase ({
12
+ this .visibility: NgDirective .LOCAL_VISIBILITY ,
13
+ this .publishTypes
14
+ });
15
+ }
16
+
17
+ class NgComponent extends NgAnnotationBase {
8
18
final String template;
9
19
final String templateUrl;
10
20
final String cssUrl;
11
- final String visibility;
12
21
final Map <String , String > map;
13
22
final String publishAs;
14
23
final bool applyAuthorStyles;
@@ -18,30 +27,31 @@ class NgComponent {
18
27
this .template,
19
28
this .templateUrl,
20
29
this .cssUrl,
21
- this . visibility: NgDirective . LOCAL_VISIBILITY ,
30
+ visibility,
22
31
this .map,
23
32
this .publishAs,
24
33
this .applyAuthorStyles,
25
- this .resetStyleInheritance
26
- });
34
+ this .resetStyleInheritance,
35
+ publishTypes : const < Type > []
36
+ }) : super (visibility: visibility, publishTypes: publishTypes);
27
37
}
28
38
29
- class NgDirective {
39
+ class NgDirective extends NgAnnotationBase {
30
40
static const String LOCAL_VISIBILITY = 'local' ;
31
41
static const String CHILDREN_VISIBILITY = 'children' ;
32
42
static const String DIRECT_CHILDREN_VISIBILITY = 'direct_children' ;
33
43
34
44
final String selector;
35
45
final String transclude;
36
46
final int priority;
37
- final String visibility;
38
47
39
48
const NgDirective ({
40
49
this .selector,
41
50
this .transclude,
42
51
this .priority : 0 ,
43
- this .visibility: LOCAL_VISIBILITY
44
- });
52
+ visibility,
53
+ publishTypes : const < Type > []
54
+ }) : super (visibility: visibility, publishTypes: publishTypes);
45
55
}
46
56
47
57
/**
@@ -55,6 +65,8 @@ class NgShadowRootOptions {
55
65
this .resetStyleInheritance = false ]);
56
66
}
57
67
68
+ Map <Type , Directive > _directiveCache = new Map <Type , Directive >();
69
+
58
70
// TODO(pavelgj): Get rid of Directive and use NgComponent/NgDirective directly.
59
71
class Directive {
60
72
Type type;
@@ -70,14 +82,23 @@ class Directive {
70
82
Map <String , String > $map;
71
83
String $visibility;
72
84
NgShadowRootOptions $shadowRootOptions = new NgShadowRootOptions ();
85
+ List <Type > $publishTypes = < Type > [];
73
86
74
87
bool isComponent = false ;
75
88
bool isStructural = false ;
76
89
77
- Directive (this .type) {
90
+ Directive ._new (Type this .type);
91
+
92
+ factory Directive (Type type) {
93
+ var instance = _directiveCache[type];
94
+ if (instance != null ) {
95
+ return instance;
96
+ }
97
+
98
+ instance = new Directive ._new (type);
78
99
var name = type.toString ();
79
100
var isAttr = false ;
80
- $name = name.splitMapJoin (
101
+ instance. $name = name.splitMapJoin (
81
102
new RegExp (r'[A-Z]' ),
82
103
onMatch: (m) => '-' + m.group (0 ).toLowerCase ())
83
104
.substring (1 );
@@ -91,38 +112,48 @@ class Directive {
91
112
var selector;
92
113
if (directive != null ) {
93
114
selector = directive.selector;
94
- $transclude = directive.transclude;
95
- $priority = directive.priority;
96
- $visibility = directive.visibility;
115
+ instance.$transclude = directive.transclude;
116
+ instance.$priority = directive.priority;
117
+ instance.$visibility = directive.visibility;
118
+ instance.$publishTypes = directive.publishTypes;
97
119
}
98
120
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);
121
+ instance.$template = component.template;
122
+ instance.$templateUrl = component.templateUrl;
123
+ instance.$cssUrl = component.cssUrl;
124
+ instance.$visibility = component.visibility;
125
+ instance.$map = component.map;
126
+ instance.$publishAs = component.publishAs;
127
+ instance.$shadowRootOptions =
128
+ new NgShadowRootOptions (component.applyAuthorStyles,
129
+ component.resetStyleInheritance);
130
+ instance.$publishTypes = component.publishTypes;
107
131
}
108
132
109
133
if (selector != null ) {
110
- $name = selector;
111
- } else if ($name.endsWith (_ATTR_DIRECTIVE )) {
112
- $name = '[${$name .substring (0 , $name .length - _ATTR_DIRECTIVE .length )}]' ;
113
- } else if ($name.endsWith (_DIRECTIVE )) {
114
- $name = $name.substring (0 , $name.length - _DIRECTIVE .length);
115
- } else if ($name.endsWith (_COMPONENT )) {
116
- isComponent = true ;
117
- $name = $name.substring (0 , $name.length - _COMPONENT .length);
134
+ instance.$name = selector;
135
+ } else if (instance.$name.endsWith (_ATTR_DIRECTIVE )) {
136
+ var attrName = instance.$name.
137
+ substring (0 , instance.$name.length - _ATTR_DIRECTIVE .length);
138
+ instance.$name = '[$attrName ]' ;
139
+ } else if (instance.$name.endsWith (_DIRECTIVE )) {
140
+ instance.$name = instance.$name.
141
+ substring (0 , instance.$name.length - _DIRECTIVE .length);
142
+ } else if (instance.$name.endsWith (_COMPONENT )) {
143
+ instance.isComponent = true ;
144
+ instance.$name = instance.$name.
145
+ substring (0 , instance.$name.length - _COMPONENT .length);
118
146
} else {
119
- throw "Directive name '$name ' must end with $_DIRECTIVE , $_ATTR_DIRECTIVE , $_COMPONENT or have a \$ selector field." ;
147
+ throw "Directive name '$name ' must end with $_DIRECTIVE , "
148
+ "$_ATTR_DIRECTIVE , $_COMPONENT or have a \$ selector field." ;
120
149
}
121
150
122
- isStructural = $transclude != null ;
123
- if (isComponent && $map == null ) {
124
- $map = new Map <String , String >();
151
+ instance. isStructural = instance. $transclude != null ;
152
+ if (instance. isComponent && instance. $map == null ) {
153
+ instance. $map = new Map <String , String >();
125
154
}
155
+ _directiveCache[type] = instance;
156
+ return instance;
126
157
}
127
158
}
128
159
0 commit comments