feat(core): @Attribute annotation

Closes #1091
Fixes #622
This commit is contained in:
Marc Laval
2015-03-25 09:42:19 +01:00
parent 3ce0f1146f
commit b1dc6239ef
9 changed files with 114 additions and 6 deletions

View File

@ -26,3 +26,15 @@ export class PropertySetter extends DependencyAnnotation {
this.propName = propName;
}
}
/**
* The directive can inject the value of an attribute of the host element
*/
export class Attribute extends DependencyAnnotation {
attributeName: string;
@CONST()
constructor(attributeName) {
super();
this.attributeName = attributeName;
}
}

View File

@ -3,7 +3,7 @@ import {Math} from 'angular2/src/facade/math';
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {Injector, Key, Dependency, bind, Binding, NoProviderError, ProviderError, CyclicDependencyError} from 'angular2/di';
import {Parent, Ancestor} from 'angular2/src/core/annotations/visibility';
import {EventEmitter, PropertySetter} from 'angular2/src/core/annotations/di';
import {EventEmitter, PropertySetter, Attribute} from 'angular2/src/core/annotations/di';
import * as viewModule from 'angular2/src/core/compiler/view';
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
import {NgElement} from 'angular2/src/core/dom/element';
@ -90,19 +90,22 @@ export class DirectiveDependency extends Dependency {
depth:int;
eventEmitterName:string;
propSetterName:string;
attributeName:string;
constructor(key:Key, asPromise:boolean, lazy:boolean, optional:boolean,
properties:List, depth:int, eventEmitterName: string, propSetterName: string) {
properties:List, depth:int, eventEmitterName: string, propSetterName: string, attributeName:string) {
super(key, asPromise, lazy, optional, properties);
this.depth = depth;
this.eventEmitterName = eventEmitterName;
this.propSetterName = propSetterName;
this.attributeName = attributeName;
}
static createFrom(d:Dependency):Dependency {
var depth = 0;
var eventName = null;
var propName = null;
var attributeName = null;
var properties = d.properties;
for (var i = 0; i < properties.length; i++) {
@ -115,11 +118,13 @@ export class DirectiveDependency extends Dependency {
eventName = property.eventName;
} else if (property instanceof PropertySetter) {
propName = property.propName;
} else if (property instanceof Attribute) {
attributeName = property.attributeName;
}
}
return new DirectiveDependency(d.key, d.asPromise, d.lazy, d.optional, d.properties, depth,
eventName, propName);
eventName, propName, attributeName);
}
}
@ -209,6 +214,7 @@ export class ProtoElementInjector {
index:int;
view:viewModule.View;
distanceToParent:number;
attributes:Map;
/** Whether the element is exported as $implicit. */
exportElement:boolean;
@ -514,6 +520,7 @@ export class ElementInjector extends TreeNode {
_getByDependency(dep:DirectiveDependency, requestor:Key) {
if (isPresent(dep.eventEmitterName)) return this._buildEventEmitter(dep);
if (isPresent(dep.propSetterName)) return this._buildPropSetter(dep);
if (isPresent(dep.attributeName)) return this._buildAttribute(dep);
return this._getByKey(dep.key, dep.depth, dep.optional, requestor);
}
@ -531,6 +538,15 @@ export class ElementInjector extends TreeNode {
return function(v) { setter(domElement, v) };
}
_buildAttribute(dep): string {
var attributes = this._proto.attributes;
if (isPresent(attributes) && MapWrapper.contains(attributes, dep.attributeName)) {
return MapWrapper.get(attributes, dep.attributeName);
} else {
return null;
}
}
/*
* It is fairly easy to annotate keys with metadata.
* For example, key.metadata = 'directive'.

View File

@ -22,6 +22,7 @@ export class CompileElement {
textNodeBindings:Map;
propertyBindings:Map;
eventBindings:Map;
attributes:Map;
/// Store directive name to template name mapping.
/// Directive name is what the directive exports the variable as
@ -144,6 +145,13 @@ export class CompileElement {
MapWrapper.set(this.eventBindings, eventName, expression);
}
addAttribute(attributeName:string, attributeValue:string) {
if (isBlank(this.attributes)) {
this.attributes = MapWrapper.create();
}
MapWrapper.set(this.attributes, attributeName, attributeValue);
}
addDirective(directive:DirectiveMetadata) {
var annotation = directive.annotation;
this._allDirectives = null;

View File

@ -72,6 +72,8 @@ export class PropertyBindingParser extends CompileStep {
var ast = this._parseInterpolation(attrValue, desc);
if (isPresent(ast)) {
current.addPropertyBinding(attrName, ast);
} else {
current.addAttribute(attrName, attrValue);
}
}
});

View File

@ -59,6 +59,7 @@ export class ProtoElementInjectorBuilder extends CompileStep {
current.inheritedProtoElementInjector.exportImplicitName = exportImplicitName;
}
}
current.inheritedProtoElementInjector.attributes = current.attributes;
} else {
current.inheritedProtoElementInjector = parentProtoElementInjector;

View File

@ -136,6 +136,8 @@ function _extractToken(typeOrFunc, annotations) {
} else if (paramAnnotation instanceof DependencyAnnotation) {
ListWrapper.push(depProps, paramAnnotation);
} else if (paramAnnotation.name === "string") {
token = paramAnnotation;
}
}