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

Removed all $ static fields, using @NgDirective and @NgComponent annotations. #51

Merged
merged 1 commit into from
Jul 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ class Block implements ElementWrapper {
directiveRefs.forEach((DirectiveRef ref) {
Type type = ref.directive.type;
var visibility = elementOnly;
if (ref.directive.$visibility == DirectiveVisibility.CHILDREN) {
if (ref.directive.$visibility == NgDirective.CHILDREN_VISIBILITY) {
visibility = null;
} else if (ref.directive.$visibility == DirectiveVisibility.DIRECT_CHILDREN) {
} else if (ref.directive.$visibility == NgDirective.DIRECT_CHILDREN_VISIBILITY) {
visibility = elementDirectChildren;
}
if (ref.directive.isComponent) {
Expand Down Expand Up @@ -269,9 +269,9 @@ class ComponentFactory {
this.compiler = compiler;
shadowDom = element.createShadowRoot();
shadowDom.applyAuthorStyles =
directive.$shadowRootOptions.$applyAuthorStyles;
directive.$shadowRootOptions.applyAuthorStyles;
shadowDom.resetStyleInheritance =
directive.$shadowRootOptions.$resetStyleInheritance;
directive.$shadowRootOptions.resetStyleInheritance;

shadowScope = scope.$new(true);
createAttributeMapping(scope, shadowScope, parser);
Expand Down
129 changes: 90 additions & 39 deletions lib/directive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,72 @@ String _COMPONENT = '-component';
String _DIRECTIVE = '-directive';
String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE;

class NgComponent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think NgComponent should inherit from NgDirective?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but they don't have anything in common except visibility...

final String template;
final String templateUrl;
final String cssUrl;
final String visibility;
final Map<String, String> map;
final String publishAs;
final bool applyAuthorStyles;
final bool resetStyleInheritance;

const NgComponent({
this.template,
this.templateUrl,
this.cssUrl,
this.visibility: NgDirective.LOCAL_VISIBILITY,
this.map,
this.publishAs,
this.applyAuthorStyles,
this.resetStyleInheritance
});
}

class NgDirective {
static const String LOCAL_VISIBILITY = 'local';
static const String CHILDREN_VISIBILITY = 'children';
static const String DIRECT_CHILDREN_VISIBILITY = 'direct_children';

final String selector;
final String transclude;
final int priority;
final String visibility;

const NgDirective({
this.selector,
this.transclude,
this.priority : 0,
this.visibility: LOCAL_VISIBILITY
});
}

/**
* See:
* http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc-style-inheriting
*/
class NgShadowRootOptions {
final bool applyAuthorStyles;
final bool resetStyleInheritance;
const NgShadowRootOptions([this.applyAuthorStyles = false,
this.resetStyleInheritance = false]);
}

// TODO(pavelgj): Get rid of Directive and use NgComponent/NgDirective directly.
class Directive {
Type type;
// TODO(misko): this should be renamed to selector once we change over to meta-data.
String $name;
Function $generate;
String $transclude;
int $priority = 0;
Type $controllerType;
String $template;
String $templateUrl;
String $cssUrl;
String $publishAs;
Map<String, String> $map;
String $visibility;
ShadowRootOptions $shadowRootOptions;
NgShadowRootOptions $shadowRootOptions = new NgShadowRootOptions();

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

var $selector = reflectStaticField(type, r'$selector');
if ($selector != null) {
$name = $selector;
var directive = _reflectSingleMetadata(type, NgDirective);
var component = _reflectSingleMetadata(type, NgComponent);
if (directive != null && component != null) {
throw 'Cannot have both NgDirective and NgComponent annotations.';
}

var selector;
if (directive != null) {
selector = directive.selector;
$transclude = directive.transclude;
$priority = directive.priority;
$visibility = directive.visibility;
}
if (component != null) {
$template = component.template;
$templateUrl = component.templateUrl;
$cssUrl = component.cssUrl;
$visibility = component.visibility;
$map = component.map;
$publishAs = component.publishAs;
$shadowRootOptions = new NgShadowRootOptions(component.applyAuthorStyles,
component.resetStyleInheritance);
}

if (selector != null) {
$name = selector;
} else if ($name.endsWith(_ATTR_DIRECTIVE)) {
$name = '[${$name.substring(0, $name.length - _ATTR_DIRECTIVE.length)}]';
} else if ($name.endsWith(_DIRECTIVE)) {
Expand All @@ -45,30 +119,24 @@ class Directive {
throw "Directive name '$name' must end with $_DIRECTIVE, $_ATTR_DIRECTIVE, $_COMPONENT or have a \$selector field.";
}

// Check the $transclude.
// TODO(deboer): I'm not a fan of 'null' as a configuration value.
// It would be awesome if $transclude could be an enum.
$transclude = reflectStaticField(type, '\$transclude');
$template = reflectStaticField(type, '\$template');
$templateUrl = reflectStaticField(type, '\$templateUrl');
$cssUrl = reflectStaticField(type, '\$cssUrl');
$priority = _defaultIfNull(reflectStaticField(type, '\$priority'), 0);
$visibility = _defaultIfNull(
reflectStaticField(type, '\$visibility'), DirectiveVisibility.LOCAL);
$map = reflectStaticField(type, '\$map');
$publishAs = reflectStaticField(type, r'$publishAs');
isStructural = $transclude != null;
if (isComponent && $map == null) {
$map = new Map<String, String>();
}
if (isComponent) {
$shadowRootOptions = _defaultIfNull(
reflectStaticField(type, '\$shadowRootOptions'),
new ShadowRootOptions());
}
}
}

_reflectSingleMetadata(Type type, Type metadataType) {
var metadata = reflectMetadata(type, metadataType);
if (metadata.length == 0) {
return null;
}
if (metadata.length > 1) {
throw 'Expecting not more than one annotation of type $metadataType';
}
return metadata.first;
}

dynamic _defaultIfNull(dynamic value, dynamic defaultValue) =>
value == null ? defaultValue : value;

Expand Down Expand Up @@ -113,23 +181,6 @@ class DirectiveRegistry {
}
}

abstract class DirectiveVisibility {
static const String LOCAL = 'local';
static const String CHILDREN = 'children';
static const String DIRECT_CHILDREN = 'direct_children';
}

/**
* See:
* http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/#toc-style-inheriting
*/
class ShadowRootOptions {
bool $applyAuthorStyles = false;
bool $resetStyleInheritance = false;
ShadowRootOptions([this.$applyAuthorStyles = false,
this.$resetStyleInheritance = false]);
}

class Controller {

}
2 changes: 1 addition & 1 deletion lib/directives/ng_controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of angular;

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

Symbol ctrlSymbol;
Expand Down
4 changes: 1 addition & 3 deletions lib/directives/ng_if.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
part of angular;


@NgDirective(transclude: '.', priority: 100)
class NgIfAttrDirective {
static var $priority = 100;
static String $transclude = 'element';

NgIfAttrDirective(NodeAttrs attrs, Injector injector, BlockList blockList, dom.Node node, Scope scope) {
var _block;
Expand Down
3 changes: 1 addition & 2 deletions lib/directives/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ part of angular;
* knwos how to (in)validate the model and the form in which it is declared
* (to be implemented)
*/
@NgDirective(selector: '[ng-model]')
class NgModel {
static String $selector = '[ng-model]';

Scope scope;
ParsedFn getter;
ParsedAssignFn setter;
Expand Down
5 changes: 2 additions & 3 deletions lib/directives/ng_mustache.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
part of angular;

@NgDirective(selector: r':contains(/{{.*}}/)')
class NgTextMustacheDirective {
static String $selector = r':contains(/{{.*}}/)';

dom.Node element;
ParsedFn interpolateFn;

Expand All @@ -14,8 +13,8 @@ class NgTextMustacheDirective {

}

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

NgAttrMustacheDirective(dom.Element element, NodeAttrs attrs, Interpolate interpolate, Scope scope) {
Expand Down
3 changes: 1 addition & 2 deletions lib/directives/ng_repeat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class Row {
Row(this.id);
}

@NgDirective(transclude: '.')
class NgRepeatAttrDirective {
static var $transclude = ".";

static RegExp SYNTAX = new RegExp(r'^\s*(.+)\s+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?$');
static RegExp LHS_SYNTAX = new RegExp(r'^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$');

Expand Down
6 changes: 6 additions & 0 deletions lib/mirrors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ reflectStaticField(Type type, String field) {
if (fieldReflection == null) return null;
return fieldReflection.reflectee;
}

// TODO(pavelgj): cache.
Iterable reflectMetadata(Type type, Type metadata) =>
fastReflectClass(type).metadata.where(
(InstanceMirror im) => im.reflectee.runtimeType == metadata)
.map((InstanceMirror im) => im.reflectee);
3 changes: 1 addition & 2 deletions test/_test_bed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ beforeEachTestBed(assign) {
*
* rootScope.myProbe.directive(SomeAttrDirective);
*/
@NgDirective(selector: '[probe]')
class Probe {
static String $selector = '[probe]';

Scope scope;
Injector injector;
Element element;
Expand Down
11 changes: 5 additions & 6 deletions test/block_spec.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import "_specs.dart";

@NgDirective(transclude: '.', selector: 'foo')
class LoggerBlockDirective {
static String $name = 'foo';
static String $transclude = '.';
LoggerBlockDirective(BlockList list, Logger logger) {
if (list == null) {
throw new ArgumentError('BlockList must be injected.');
Expand All @@ -12,16 +11,16 @@ class LoggerBlockDirective {
}

class ReplaceBlockDirective {
ReplaceBlockDirective(BlockList list, Node node) {
var block = list.newBlock();
ReplaceBlockDirective(BlockList list, Node node, Scope scope) {
var block = list.newBlock(scope);
block.insertAfter(list);
node.remove();
}
}

class ShadowBlockDirective {
ShadowBlockDirective(BlockList list, Element element) {
var block = list.newBlock();
ShadowBlockDirective(BlockList list, Element element, Scope scope) {
var block = list.newBlock(scope);
var shadowRoot = element.createShadowRoot();
for (var i = 0, ii = block.elements.length; i < ii; i++) {
shadowRoot.append(block.elements[i]);
Expand Down
Loading