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

Misc minor changes #1061

Closed
wants to merge 5 commits into from
Closed
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
19 changes: 10 additions & 9 deletions lib/core/annotation_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ RegExp _ATTR_NAME = new RegExp(r'\[([^\]]+)\]$');

const String SHADOW_DOM_INJECTOR_NAME = 'SHADOW_INJECTOR';

skipShadow(Injector injector)
=> injector.name == SHADOW_DOM_INJECTOR_NAME ? injector.parent : injector;
skipShadow(Injector injector) =>
injector.name == SHADOW_DOM_INJECTOR_NAME ? injector.parent : injector;

localVisibility (Injector requesting, Injector defining)
=> identical(skipShadow(requesting), defining);
localVisibility (Injector requesting, Injector defining) =>
identical(skipShadow(requesting), defining);

directChildrenVisibility(Injector requesting, Injector defining) {
requesting = skipShadow(requesting);
return identical(requesting.parent, defining) || localVisibility(requesting, defining);
}

Directive cloneWithNewMap(Directive annotation, map)
=> annotation._cloneWithNewMap(map);
Directive cloneWithNewMap(Directive annotation, map) => annotation._cloneWithNewMap(map);

String mappingSpec(DirectiveAnnotation annotation) => annotation._mappingSpec;

Expand Down Expand Up @@ -223,9 +222,11 @@ abstract class Directive {
this.exportExpressionAttrs: const []
});

toString() => selector;
get hashCode => selector.hashCode;
operator==(other) =>
String toString() => selector;

int get hashCode => selector.hashCode;

bool operator==(other) =>
other is Directive && selector == other.selector;

Directive _cloneWithNewMap(newMap);
Expand Down
4 changes: 2 additions & 2 deletions lib/core/formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ part of angular.core_internal;
class FormatterMap extends AnnotationMap<Formatter> {
Injector _injector;
FormatterMap(Injector injector, MetadataExtractor extractMetadata)
: this._injector = injector,
: _injector = injector,
super(injector, extractMetadata);

call(String name) {
Function call(String name) {
var formatter = new Formatter(name: name);
var formatterType = this[formatter];
return _injector.get(formatterType);
Expand Down
43 changes: 31 additions & 12 deletions lib/core/registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ library angular.core.registry;

import 'package:di/di.dart' show Injector;

/**
* The [AnnotationMap] maps annotations to [Type]s.
*
* The [AnnotationMap] contains all annotated [Type]s provided by the [Injector] passed as a
* constructor argument. Every single annotation maps to only one [Type].
*/
abstract class AnnotationMap<K> {
final Map<K, Type> _map = {};
final _map = <K, Type>{};

AnnotationMap(Injector injector, MetadataExtractor extractMetadata) {
injector.types.forEach((type) {
Expand All @@ -15,16 +21,19 @@ abstract class AnnotationMap<K> {
});
}

/// Returns the [Type] annotated with [annotation].
Type operator[](K annotation) {
var value = _map[annotation];
if (value == null) throw 'No $annotation found!';
return value;
}

void forEach(fn(K, Type)) {
_map.forEach(fn);
/// Executes the [function] for all registered annotations.
void forEach(function(K, Type)) {
_map.forEach(function);
}

/// Returns a list of all the annotations applied to the [Type].
List<K> annotationsFor(Type type) {
final res = <K>[];
forEach((ann, annType) {
Expand All @@ -34,37 +43,47 @@ abstract class AnnotationMap<K> {
}
}

/**
* The [AnnotationsMap] maps annotations to [Type]s.
*
* The [AnnotationsMap] contains all annotated [Type]s provided by the [Injector]
* given in the constructor argument. Every single annotation can maps to only
* multiple [Type]s.
*/
abstract class AnnotationsMap<K> {
final Map<K, List<Type>> map = {};
final _map = <K, List<Type>>{};

AnnotationsMap(Injector injector, MetadataExtractor extractMetadata) {
injector.types.forEach((type) {
extractMetadata(type)
.where((annotation) => annotation is K)
.forEach((annotation) {
map.putIfAbsent(annotation, () => []).add(type);
_map.putIfAbsent(annotation, () => <Type>[]).add(type);
});
});
}

List operator[](K annotation) {
var value = map[annotation];
/// Returns a list of [Type]s annotated with [annotation].
List<Type> operator[](K annotation) {
var value = _map[annotation];
if (value == null) throw 'No $annotation found!';
return value;
}

void forEach(fn(K, Type)) {
map.forEach((annotation, types) {
/// Executes the [function] for all registered (annotation, type) pairs.
void forEach(function(K, Type)) {
_map.forEach((K annotation, List<Type> types) {
types.forEach((type) {
fn(annotation, type);
function(annotation, type);
});
});
}

/// Returns a list of all the annotations applied to the [Type].
List<K> annotationsFor(Type type) {
var res = <K>[];
forEach((ann, annType) {
if (annType == type) res.add(ann);
_map.forEach((K annotation, List<Type> types) {
if (types.contains(type)) res.add(annotation);
});
return res;
}
Expand Down
97 changes: 46 additions & 51 deletions lib/core/registry_dynamic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import 'package:angular/core/registry.dart';
export 'package:angular/core/registry.dart' show
MetadataExtractor;

var _fieldMetadataCache = new Map<Type, Map<String, DirectiveAnnotation>>();

class DynamicMetadataExtractor implements MetadataExtractor {
static final _fieldMetadataCache = <Type, Map<String, DirectiveAnnotation>>{};

final _fieldAnnotations = [
reflectType(NgAttr),
reflectType(NgOneWay),
Expand All @@ -21,71 +21,66 @@ class DynamicMetadataExtractor implements MetadataExtractor {
Iterable call(Type type) {
if (reflectType(type) is TypedefMirror) return [];
var metadata = reflectClass(type).metadata;
if (metadata == null) {
metadata = [];
} else {
metadata = metadata.map((InstanceMirror im) => map(type, im.reflectee));
}
return metadata;
return metadata == null ?
[] : metadata.map((InstanceMirror im) => _mergeFieldAnnotations(type, im.reflectee));
}

map(Type type, obj) {
if (obj is Directive) {
return mapDirectiveAnnotation(type, obj);
} else {
return obj;
}
}

Directive mapDirectiveAnnotation(Type type, Directive annotation) {
/**
* Merges the field annotations with the [AbstractNgAttrAnnotation.map] definition from the
* directive.
*
* [_mergeFieldAnnotations] throws when a field annotation has already been defined via
* [Directive.map]
*/
dynamic _mergeFieldAnnotations(Type type, annotation) {
if (annotation is! Directive) return annotation;
var match;
var fieldMetadata = fieldMetadataExtractor(type);
var fieldMetadata = _fieldMetadataExtractor(type);
if (fieldMetadata.isNotEmpty) {
var newMap = annotation.map == null ? {} : new Map.from(annotation.map);
fieldMetadata.forEach((String fieldName, DirectiveAnnotation ann) {
var attrName = ann.attrName;
if (newMap.containsKey(attrName)) {
throw 'Mapping for attribute $attrName is already defined (while '
'processing annottation for field $fieldName of $type)';
'processing annottation for field $fieldName of $type)';
}
newMap[attrName] = '${mappingSpec(ann)}$fieldName';
newMap[attrName] = mappingSpec(ann) + fieldName;
});
annotation = cloneWithNewMap(annotation, newMap);
}
return annotation;
}


Map<String, DirectiveAnnotation> fieldMetadataExtractor(Type type) =>
_fieldMetadataCache.putIfAbsent(type, () => _fieldMetadataExtractor(reflectType(type)));

Map<String, DirectiveAnnotation> _fieldMetadataExtractor(ClassMirror cm) {
var fields = <String, DirectiveAnnotation>{};
if(cm.superclass != null) {
fields.addAll(_fieldMetadataExtractor(cm.superclass));
} else {
fields = {};
}
Map<Symbol, DeclarationMirror> declarations = cm.declarations;
declarations.forEach((symbol, dm) {
if(dm is VariableMirror ||
dm is MethodMirror && (dm.isGetter || dm.isSetter)) {
var fieldName = MirrorSystem.getName(symbol);
if (dm is MethodMirror && dm.isSetter) {
// Remove "=" from the end of the setter.
fieldName = fieldName.substring(0, fieldName.length - 1);
}
dm.metadata.forEach((InstanceMirror meta) {
if (_fieldAnnotations.contains(meta.type)) {
if (fields.containsKey(fieldName)) {
throw 'Attribute annotation for $fieldName is defined more '
'than once in ${cm.reflectedType}';
}
fields[fieldName] = meta.reflectee as DirectiveAnnotation;
}
});
/// Extract metadata defined on fields via a [DirectiveAnnotation]
Map<String, DirectiveAnnotation> _fieldMetadataExtractor(Type type) {
if (!_fieldMetadataCache.containsKey(type)) {
var fields = <String, DirectiveAnnotation>{};
ClassMirror cm = reflectType(type);
if (cm.superclass != null) {
fields.addAll(_fieldMetadataExtractor(cm.superclass.reflectedType));
}
});
return fields;
Map<Symbol, DeclarationMirror> declarations = cm.declarations;
declarations.forEach((symbol, dm) {
if (dm is VariableMirror ||
dm is MethodMirror && (dm.isGetter || dm.isSetter)) {
var fieldName = MirrorSystem.getName(symbol);
if (dm is MethodMirror && dm.isSetter) {
// Remove "=" from the end of the setter.
fieldName = fieldName.substring(0, fieldName.length - 1);
}
dm.metadata.forEach((InstanceMirror meta) {
if (_fieldAnnotations.contains(meta.type)) {
if (fields.containsKey(fieldName)) {
throw 'Attribute annotation for $fieldName is defined more '
'than once in ${cm.reflectedType}';
}
fields[fieldName] = meta.reflectee as DirectiveAnnotation;
}
});
}
});
_fieldMetadataCache[type] = fields;
}
return _fieldMetadataCache[type];
}
}
2 changes: 1 addition & 1 deletion lib/core/registry_static.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:angular/core/registry.dart';
@Injectable()
class StaticMetadataExtractor extends MetadataExtractor {
final Map<Type, Iterable> metadataMap;
final List empty = const [];
final empty = const [];

StaticMetadataExtractor(this.metadataMap);

Expand Down
2 changes: 1 addition & 1 deletion lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class Scope {
} else if (expression.startsWith(':')) {
expression = expression.substring(1);
fn = (value, last) {
if (value != null) reactionFn(value, last);
if (value != null) reactionFn(value, last);
};
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib/core_dom/common.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
part of angular.core.dom_internal;

List<dom.Node> cloneElements(elements) {
return elements.map((el) => el.clone(true)).toList();
}
List<dom.Node> cloneElements(elements) => elements.map((el) => el.clone(true)).toList();

class MappingParts {
final String attrName;
Expand All @@ -18,7 +16,7 @@ class DirectiveRef {
final Type type;
final Directive annotation;
final String value;
final mappings = new List<MappingParts>();
final mappings = <MappingParts>[];

DirectiveRef(this.element, this.type, this.annotation, [ this.value ]);

Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/directive_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of angular.core.dom_internal;

@Injectable()
class DirectiveMap extends AnnotationsMap<Directive> {
DirectiveSelectorFactory _directiveSelectorFactory;
final DirectiveSelectorFactory _directiveSelectorFactory;
DirectiveSelector _selector;
DirectiveSelector get selector {
if (_selector != null) return _selector;
Expand Down
6 changes: 3 additions & 3 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ElementBinder {
}
}

_bindOneWay(tasks, expression, scope, dstPathFn, controller, formatters) {
void _bindOneWay(tasks, expression, scope, dstPathFn, controller, formatters) {
var taskId = tasks.registerTask();

Expression attrExprFn = _parser(expression);
Expand Down Expand Up @@ -227,9 +227,8 @@ class ElementBinder {
if (directive is AttachAware) {
var taskId = tasks.registerTask();
Watch watch;
watch = scope.watch('1', // Cheat a bit.
watch = scope.watch('::1', // Cheat a bit.
(_, __) {
watch.remove();
tasks.completeTask(taskId);
});
}
Expand Down Expand Up @@ -400,6 +399,7 @@ class TaggedTextBinder {
final int offsetIndex;

TaggedTextBinder(this.binder, this.offsetIndex);

String toString() => "[TaggedTextBinder binder:$binder offset:$offsetIndex]";
}

Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/node_cursor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ class NodeCursor {

NodeCursor remove() => new NodeCursor([elements.removeAt(index)..remove()]);

toString() => "[NodeCursor: $elements $index]";
String toString() => "[NodeCursor: $elements $index]";
}
10 changes: 3 additions & 7 deletions lib/core_dom/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class ViewPort {
dom.Node previousNode = _lastNode(insertAfter);
_viewsInsertAfter(view, insertAfter);

_animate.insert(view.nodes, placeholder.parentNode,
insertBefore: previousNode.nextNode);
_animate.insert(view.nodes, placeholder.parentNode, insertBefore: previousNode.nextNode);
}

void remove(View view) {
Expand All @@ -51,8 +50,7 @@ class ViewPort {
_views.remove(view);
_viewsInsertAfter(view, moveAfter);

_animate.move(view.nodes, placeholder.parentNode,
insertBefore: previousNode.nextNode);
_animate.move(view.nodes, placeholder.parentNode, insertBefore: previousNode.nextNode);
}

void _viewsInsertAfter(View view, View insertAfter) {
Expand All @@ -61,7 +59,5 @@ class ViewPort {
}

dom.Node _lastNode(View insertAfter) =>
insertAfter == null
? placeholder
: insertAfter.nodes.last;
insertAfter == null ? placeholder : insertAfter.nodes.last;
}
3 changes: 1 addition & 2 deletions lib/core_dom/view_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class WalkingViewFactory implements ViewFactory {
eb is ElementBinderTreeRef));
}

BoundViewFactory bind(Injector injector) =>
new BoundViewFactory(this, injector);
BoundViewFactory bind(Injector injector) => new BoundViewFactory(this, injector);

View call(Injector injector, [List<dom.Node> nodes]) {
if (nodes == null) nodes = cloneElements(templateElements);
Expand Down
Loading