Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 75a38b8

Browse files
committed
Removed all $ static fields, using @ngdirective and @NgComponent annotations.
1 parent 17be163 commit 75a38b8

14 files changed

+168
-97
lines changed

lib/block.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ class Block implements ElementWrapper {
9898
directiveRefs.forEach((DirectiveRef ref) {
9999
Type type = ref.directive.type;
100100
var visibility = elementOnly;
101-
if (ref.directive.$visibility == DirectiveVisibility.CHILDREN) {
101+
if (ref.directive.$visibility == NgDirective.CHILDREN_VISIBILITY) {
102102
visibility = null;
103-
} else if (ref.directive.$visibility == DirectiveVisibility.DIRECT_CHILDREN) {
103+
} else if (ref.directive.$visibility == NgDirective.DIRECT_CHILDREN_VISIBILITY) {
104104
visibility = elementDirectChildren;
105105
}
106106
if (ref.directive.isComponent) {
@@ -269,9 +269,9 @@ class ComponentFactory {
269269
this.compiler = compiler;
270270
shadowDom = element.createShadowRoot();
271271
shadowDom.applyAuthorStyles =
272-
directive.$shadowRootOptions.$applyAuthorStyles;
272+
directive.$shadowRootOptions.applyAuthorStyles;
273273
shadowDom.resetStyleInheritance =
274-
directive.$shadowRootOptions.$resetStyleInheritance;
274+
directive.$shadowRootOptions.resetStyleInheritance;
275275

276276
shadowScope = scope.$new(true);
277277
createAttributeMapping(scope, shadowScope, parser);

lib/directive.dart

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,72 @@ String _COMPONENT = '-component';
44
String _DIRECTIVE = '-directive';
55
String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE;
66

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.
759
class Directive {
860
Type type;
961
// TODO(misko): this should be renamed to selector once we change over to meta-data.
1062
String $name;
1163
Function $generate;
1264
String $transclude;
1365
int $priority = 0;
14-
Type $controllerType;
1566
String $template;
1667
String $templateUrl;
1768
String $cssUrl;
1869
String $publishAs;
1970
Map<String, String> $map;
2071
String $visibility;
21-
ShadowRootOptions $shadowRootOptions;
72+
NgShadowRootOptions $shadowRootOptions = new NgShadowRootOptions();
2273

2374
bool isComponent = false;
2475
bool isStructural = false;
@@ -31,9 +82,32 @@ class Directive {
3182
onMatch: (m) => '-' + m.group(0).toLowerCase())
3283
.substring(1);
3384

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;
37111
} else if ($name.endsWith(_ATTR_DIRECTIVE)) {
38112
$name = '[${$name.substring(0, $name.length - _ATTR_DIRECTIVE.length)}]';
39113
} else if ($name.endsWith(_DIRECTIVE)) {
@@ -45,30 +119,24 @@ class Directive {
45119
throw "Directive name '$name' must end with $_DIRECTIVE, $_ATTR_DIRECTIVE, $_COMPONENT or have a \$selector field.";
46120
}
47121

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');
60122
isStructural = $transclude != null;
61123
if (isComponent && $map == null) {
62124
$map = new Map<String, String>();
63125
}
64-
if (isComponent) {
65-
$shadowRootOptions = _defaultIfNull(
66-
reflectStaticField(type, '\$shadowRootOptions'),
67-
new ShadowRootOptions());
68-
}
69126
}
70127
}
71128

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+
72140
dynamic _defaultIfNull(dynamic value, dynamic defaultValue) =>
73141
value == null ? defaultValue : value;
74142

@@ -113,23 +181,6 @@ class DirectiveRegistry {
113181
}
114182
}
115183

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-
133184
class Controller {
134185

135186
}

lib/directives/ng_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of angular;
22

3+
@NgDirective(transclude: '.')
34
class NgControllerAttrDirective {
4-
static String $transclude = 'element';
55
static RegExp CTRL_REGEXP = new RegExp(r'^(\S+)(\s+as\s+(\w+))?$');
66

77
Symbol ctrlSymbol;

lib/directives/ng_if.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
part of angular;
22

3-
3+
@NgDirective(transclude: '.', priority: 100)
44
class NgIfAttrDirective {
5-
static var $priority = 100;
6-
static String $transclude = 'element';
75

86
NgIfAttrDirective(NodeAttrs attrs, Injector injector, BlockList blockList, dom.Node node, Scope scope) {
97
var _block;

lib/directives/ng_model.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ part of angular;
1010
* knwos how to (in)validate the model and the form in which it is declared
1111
* (to be implemented)
1212
*/
13+
@NgDirective(selector: '[ng-model]')
1314
class NgModel {
14-
static String $selector = '[ng-model]';
15-
1615
Scope scope;
1716
ParsedFn getter;
1817
ParsedAssignFn setter;

lib/directives/ng_mustache.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
part of angular;
22

3+
@NgDirective(selector: r':contains(/{{.*}}/)')
34
class NgTextMustacheDirective {
4-
static String $selector = r':contains(/{{.*}}/)';
5-
65
dom.Node element;
76
ParsedFn interpolateFn;
87

@@ -14,8 +13,8 @@ class NgTextMustacheDirective {
1413

1514
}
1615

16+
@NgDirective(selector: r'[*=/{{.*}}/]')
1717
class NgAttrMustacheDirective {
18-
static String $selector = r'[*=/{{.*}}/]';
1918
static RegExp ATTR_NAME_VALUE_REGEXP = new RegExp(r'^([^=]+)=(.*)$');
2019

2120
NgAttrMustacheDirective(dom.Element element, NodeAttrs attrs, Interpolate interpolate, Scope scope) {

lib/directives/ng_repeat.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class Row {
1111
Row(this.id);
1212
}
1313

14+
@NgDirective(transclude: '.')
1415
class NgRepeatAttrDirective {
15-
static var $transclude = ".";
16-
1716
static RegExp SYNTAX = new RegExp(r'^\s*(.+)\s+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?$');
1817
static RegExp LHS_SYNTAX = new RegExp(r'^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$');
1918

lib/mirrors.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ reflectStaticField(Type type, String field) {
2828
if (fieldReflection == null) return null;
2929
return fieldReflection.reflectee;
3030
}
31+
32+
// TODO(pavelgj): cache.
33+
Iterable reflectMetadata(Type type, Type metadata) =>
34+
fastReflectClass(type).metadata.where(
35+
(InstanceMirror im) => im.reflectee.runtimeType == metadata)
36+
.map((InstanceMirror im) => im.reflectee);

test/_test_bed.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ beforeEachTestBed(assign) {
5151
*
5252
* rootScope.myProbe.directive(SomeAttrDirective);
5353
*/
54+
@NgDirective(selector: '[probe]')
5455
class Probe {
55-
static String $selector = '[probe]';
56-
5756
Scope scope;
5857
Injector injector;
5958
Element element;

test/block_spec.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import "_specs.dart";
22

3+
@NgDirective(transclude: '.', selector: 'foo')
34
class LoggerBlockDirective {
4-
static String $name = 'foo';
5-
static String $transclude = '.';
65
LoggerBlockDirective(BlockList list, Logger logger) {
76
if (list == null) {
87
throw new ArgumentError('BlockList must be injected.');
@@ -12,16 +11,16 @@ class LoggerBlockDirective {
1211
}
1312

1413
class ReplaceBlockDirective {
15-
ReplaceBlockDirective(BlockList list, Node node) {
16-
var block = list.newBlock();
14+
ReplaceBlockDirective(BlockList list, Node node, Scope scope) {
15+
var block = list.newBlock(scope);
1716
block.insertAfter(list);
1817
node.remove();
1918
}
2019
}
2120

2221
class ShadowBlockDirective {
23-
ShadowBlockDirective(BlockList list, Element element) {
24-
var block = list.newBlock();
22+
ShadowBlockDirective(BlockList list, Element element, Scope scope) {
23+
var block = list.newBlock(scope);
2524
var shadowRoot = element.createShadowRoot();
2625
for (var i = 0, ii = block.elements.length; i < ii; i++) {
2726
shadowRoot.append(block.elements[i]);

0 commit comments

Comments
 (0)