X Tutup
Skip to content
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
52 changes: 26 additions & 26 deletions modules/angular2/src/core/compiler/runtime_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {MapWrapper, StringMapWrapper, ListWrapper} from 'angular2/src/core/facade/collection';
import * as cpl from './directive_metadata';
import * as dirAnn from 'angular2/src/core/metadata/directives';
import * as md from 'angular2/src/core/metadata/directives';
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
import {ViewMetadata} from 'angular2/src/core/metadata/view';
Expand All @@ -33,35 +33,35 @@ export class RuntimeMetadataResolver {
getMetadata(directiveType: Type): cpl.CompileDirectiveMetadata {
var meta = this._cache.get(directiveType);
if (isBlank(meta)) {
var directiveAnnotation = this._directiveResolver.resolve(directiveType);
var moduleUrl = calcModuleUrl(directiveType, directiveAnnotation);
var dirMeta = this._directiveResolver.resolve(directiveType);
var moduleUrl = calcModuleUrl(directiveType, dirMeta);
var templateMeta = null;
var changeDetectionStrategy = null;

if (directiveAnnotation instanceof dirAnn.ComponentMetadata) {
var compAnnotation = <dirAnn.ComponentMetadata>directiveAnnotation;
var viewAnnotation = this._viewResolver.resolve(directiveType);
if (dirMeta instanceof md.ComponentMetadata) {
var cmpMeta = <md.ComponentMetadata>dirMeta;
var viewMeta = this._viewResolver.resolve(directiveType);
templateMeta = new cpl.CompileTemplateMetadata({
encapsulation: viewAnnotation.encapsulation,
template: viewAnnotation.template,
templateUrl: viewAnnotation.templateUrl,
styles: viewAnnotation.styles,
styleUrls: viewAnnotation.styleUrls
encapsulation: viewMeta.encapsulation,
template: viewMeta.template,
templateUrl: viewMeta.templateUrl,
styles: viewMeta.styles,
styleUrls: viewMeta.styleUrls
});
changeDetectionStrategy = compAnnotation.changeDetection;
changeDetectionStrategy = cmpMeta.changeDetection;
}
meta = cpl.CompileDirectiveMetadata.create({
selector: directiveAnnotation.selector,
exportAs: directiveAnnotation.exportAs,
selector: dirMeta.selector,
exportAs: dirMeta.exportAs,
isComponent: isPresent(templateMeta),
dynamicLoadable: true,
type: new cpl.CompileTypeMetadata(
{name: stringify(directiveType), moduleUrl: moduleUrl, runtime: directiveType}),
template: templateMeta,
changeDetection: changeDetectionStrategy,
inputs: directiveAnnotation.inputs,
outputs: directiveAnnotation.outputs,
host: directiveAnnotation.host,
inputs: dirMeta.inputs,
outputs: dirMeta.outputs,
host: dirMeta.host,
lifecycleHooks: ListWrapper.filter(LIFECYCLE_HOOKS_VALUES,
hook => hasLifecycleHook(hook, directiveType))
});
Expand All @@ -79,15 +79,15 @@ export class RuntimeMetadataResolver {
`Unexpected directive value '${stringify(directives[i])}' on the View of component '${stringify(component)}'`);
}
}
return removeDuplicatedDirectives(directives.map(type => this.getMetadata(type)));

return removeDuplicates(directives).map(type => this.getMetadata(type));
}
}

function removeDuplicatedDirectives(directives: cpl.CompileDirectiveMetadata[]):
cpl.CompileDirectiveMetadata[] {
var directivesMap = new Map<Type, cpl.CompileDirectiveMetadata>();
directives.forEach((dirMeta) => { directivesMap.set(dirMeta.type.runtime, dirMeta); });
return MapWrapper.values(directivesMap);
function removeDuplicates(items: any[]): any[] {
let m = new Map<any, any>();
items.forEach(i => m.set(i, null));
return MapWrapper.keys(m);
}

function flattenDirectives(view: ViewMetadata): Type[] {
Expand All @@ -112,9 +112,9 @@ function isValidDirective(value: Type): boolean {
return isPresent(value) && (value instanceof Type);
}

function calcModuleUrl(type: Type, directiveAnnotation: dirAnn.DirectiveMetadata): string {
if (isPresent(directiveAnnotation.moduleId)) {
return `package:${directiveAnnotation.moduleId}${MODULE_SUFFIX}`;
function calcModuleUrl(type: Type, dirMeta: md.DirectiveMetadata): string {
if (isPresent(dirMeta.moduleId)) {
return `package:${dirMeta.moduleId}${MODULE_SUFFIX}`;
} else {
return reflector.importUri(type);
}
Expand Down
38 changes: 16 additions & 22 deletions modules/angular2/src/core/compiler/template_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,22 @@ export class TemplateCompiler {
// For non components there is nothing to be normalized yet.
return PromiseWrapper.resolve(directive);
}
var normalizedTemplatePromise;
if (directive.isComponent) {
normalizedTemplatePromise =
this._templateNormalizer.normalizeTemplate(directive.type, directive.template);
} else {
normalizedTemplatePromise = PromiseWrapper.resolve(null);
}
return normalizedTemplatePromise.then(
(normalizedTemplate) => new CompileDirectiveMetadata({
type: directive.type,
isComponent: directive.isComponent,
dynamicLoadable: directive.dynamicLoadable,
selector: directive.selector,
exportAs: directive.exportAs,
changeDetection: directive.changeDetection,
inputs: directive.inputs,
outputs: directive.outputs,
hostListeners: directive.hostListeners,
hostProperties: directive.hostProperties,
hostAttributes: directive.hostAttributes,
lifecycleHooks: directive.lifecycleHooks, template: normalizedTemplate
}));

return this._templateNormalizer.normalizeTemplate(directive.type, directive.template)
.then((normalizedTemplate: CompileTemplateMetadata) => new CompileDirectiveMetadata({
type: directive.type,
isComponent: directive.isComponent,
dynamicLoadable: directive.dynamicLoadable,
selector: directive.selector,
exportAs: directive.exportAs,
changeDetection: directive.changeDetection,
inputs: directive.inputs,
outputs: directive.outputs,
hostListeners: directive.hostListeners,
hostProperties: directive.hostProperties,
hostAttributes: directive.hostAttributes,
lifecycleHooks: directive.lifecycleHooks, template: normalizedTemplate
}));
}

compileHostComponentRuntime(type: Type): Promise<CompiledHostTemplate> {
Expand Down
2 changes: 2 additions & 0 deletions modules/angular2/src/core/compiler/template_normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export class TemplateNormalizer {
.concat(templateMeta.styleUrls.map(
url => this._urlResolver.resolve(directiveType.moduleUrl, url)));
allStyleAbsUrls = ListWrapper.filter(allStyleAbsUrls, isStyleUrlResolvable);

var allResolvedStyles = allStyles.map(style => {
var styleWithImports = extractStyleUrls(this._urlResolver, templateAbsUrl, style);
styleWithImports.styleUrls.forEach(styleUrl => allStyleAbsUrls.push(styleUrl));
return styleWithImports.style;
});

var encapsulation = templateMeta.encapsulation;
if (encapsulation === ViewEncapsulation.Emulated && allResolvedStyles.length === 0 &&
allStyleAbsUrls.length === 0) {
Expand Down
15 changes: 7 additions & 8 deletions modules/angular2/src/core/linker/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Injectable} from 'angular2/src/core/di';
import {Type, isBlank, stringify} from 'angular2/src/core/facade/lang';
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {reflector} from 'angular2/src/core/reflection/reflection';
import {CompiledHostTemplate} from 'angular2/src/core/linker/template_commands';

Expand All @@ -20,20 +21,18 @@ export abstract class Compiler {
abstract clearCache();
}

function _isCompiledHostTemplate(type: any): boolean {
return type instanceof CompiledHostTemplate;
}

@Injectable()
export class Compiler_ extends Compiler {
constructor(private _protoViewFactory: ProtoViewFactory) { super(); }

compileInHost(componentType: Type): Promise<ProtoViewRef> {
var metadatas = reflector.annotations(componentType);
var compiledHostTemplate = null;
for (var i = 0; i < metadatas.length; i++) {
var metadata = metadatas[i];
if (metadata instanceof CompiledHostTemplate) {
compiledHostTemplate = metadata;
break;
}
}
var compiledHostTemplate = ListWrapper.find(metadatas, _isCompiledHostTemplate);

if (isBlank(compiledHostTemplate)) {
throw new BaseException(
`No precompiled template for component ${stringify(componentType)} found`);
Expand Down
15 changes: 9 additions & 6 deletions modules/angular2/src/core/linker/directive_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
} from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection';

function _isDirectiveMetadata(type: any): boolean {
return type instanceof DirectiveMetadata;
}

/*
* Resolve a `Type` for {@link DirectiveMetadata}.
*
Expand All @@ -31,14 +35,13 @@ export class DirectiveResolver {
resolve(type: Type): DirectiveMetadata {
var typeMetadata = reflector.annotations(resolveForwardRef(type));
if (isPresent(typeMetadata)) {
for (var i = 0; i < typeMetadata.length; i++) {
var metadata = typeMetadata[i];
if (metadata instanceof DirectiveMetadata) {
var propertyMetadata = reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
}
var metadata = ListWrapper.find(typeMetadata, _isDirectiveMetadata);
if (isPresent(metadata)) {
var propertyMetadata = reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata);
}
}

throw new BaseException(`No Directive annotation found on ${stringify(type)}`);
}

Expand Down
74 changes: 25 additions & 49 deletions modules/angular2/src/core/linker/dynamic_component_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,35 @@ export abstract class DynamicComponentLoader {
*
* Returns a promise for the {@link ComponentRef} representing the newly created Component.
*
*
* ### Example
*
* ```
* @ng.Component({
* selector: 'child-component'
* })
* @ng.View({
* @Component({
* selector: 'child-component',
* template: 'Child'
* })
* class ChildComponent {
* }
*
*
*
* @ng.Component({
* selector: 'my-app'
* })
* @ng.View({
* template: `
* Parent (<child id="child"></child>)
* `
* @Component({
* selector: 'my-app',
* template: 'Parent (<child id="child"></child>)'
* })
* class MyApp {
* constructor(dynamicComponentLoader: ng.DynamicComponentLoader, injector: ng.Injector) {
* dynamicComponentLoader.loadAsRoot(ChildComponent, '#child', injector);
* constructor(dcl: DynamicComponentLoader, injector: Injector) {
* dcl.loadAsRoot(ChildComponent, '#child', injector);
* }
* }
*
* ng.bootstrap(MyApp);
* bootstrap(MyApp);
* ```
*
* Resulting DOM:
*
* ```
* <my-app>
* Parent (
* <child id="child">
* Child
* </child>
* <child id="child">Child</child>
* )
* </my-app>
* ```
Expand All @@ -166,35 +155,27 @@ export abstract class DynamicComponentLoader {
*
* Returns a promise for the {@link ComponentRef} representing the newly created Component.
*
*
* ### Example
*
* ```
* @ng.Component({
* selector: 'child-component'
* })
* @ng.View({
* @Component({
* selector: 'child-component',
* template: 'Child'
* })
* class ChildComponent {
* }
*
*
* @ng.Component({
* selector: 'my-app'
* })
* @ng.View({
* template: `
* Parent (<div #child></div>)
* `
* @Component({
* selector: 'my-app',
* template: 'Parent (<div #child></div>)'
* })
* class MyApp {
* constructor(dynamicComponentLoader: ng.DynamicComponentLoader, elementRef: ng.ElementRef) {
* dynamicComponentLoader.loadIntoLocation(ChildComponent, elementRef, 'child');
* constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) {
* dcl.loadIntoLocation(ChildComponent, elementRef, 'child');
* }
* }
*
* ng.bootstrap(MyApp);
* bootstrap(MyApp);
* ```
*
* Resulting DOM:
Expand Down Expand Up @@ -224,29 +205,24 @@ export abstract class DynamicComponentLoader {
* ### Example
*
* ```
* @ng.Component({
* selector: 'child-component'
* })
* @ng.View({
* @Component({
* selector: 'child-component',
* template: 'Child'
* })
* class ChildComponent {
* }
*
*
* @ng.Component({
* selector: 'my-app'
* })
* @ng.View({
* template: `Parent`
* @Component({
* selector: 'my-app',
* template: 'Parent'
* })
* class MyApp {
* constructor(dynamicComponentLoader: ng.DynamicComponentLoader, elementRef: ng.ElementRef) {
* dynamicComponentLoader.loadNextToLocation(ChildComponent, elementRef);
* constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) {
* dcl.loadNextToLocation(ChildComponent, elementRef);
* }
* }
*
* ng.bootstrap(MyApp);
* bootstrap(MyApp);
* ```
*
* Resulting DOM:
Expand Down
13 changes: 8 additions & 5 deletions modules/angular2/src/core/linker/pipe_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {resolveForwardRef, Injectable} from 'angular2/src/core/di';
import {Type, isPresent, stringify} from 'angular2/src/core/facade/lang';
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {BaseException} from 'angular2/src/core/facade/exceptions';
import {PipeMetadata} from 'angular2/src/core/metadata';
import {reflector} from 'angular2/src/core/reflection/reflection';

function _isPipeMetadata(type: any): boolean {
return type instanceof PipeMetadata;
}

/**
* Resolve a `Type` for {@link PipeMetadata}.
*
Expand All @@ -19,11 +24,9 @@ export class PipeResolver {
resolve(type: Type): PipeMetadata {
var metas = reflector.annotations(resolveForwardRef(type));
if (isPresent(metas)) {
for (var i = 0; i < metas.length; i++) {
var annotation = metas[i];
if (annotation instanceof PipeMetadata) {
return annotation;
}
var annotation = ListWrapper.find(metas, _isPipeMetadata);
if (isPresent(annotation)) {
return annotation;
}
}
throw new BaseException(`No Pipe decorator found on ${stringify(type)}`);
Expand Down
X Tutup