angular-docs-cn/modules/@angular/compiler/src/directive_resolver.ts

177 lines
6.1 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Component, Directive, HostBinding, HostListener, Injectable, Input, Output, Query, Type, resolveForwardRef} from '@angular/core';
import {StringMapWrapper} from './facade/collection';
2016-09-27 18:41:37 -04:00
import {stringify} from './facade/lang';
import {ReflectorReader, reflector} from './private_import_core';
import {splitAtColon} from './util';
/*
* Resolve a `Type` for {@link Directive}.
*
* This interface can be overridden by the application developer to create custom behavior.
*
* See {@link Compiler}
*/
@Injectable()
export class DirectiveResolver {
2016-06-17 13:57:50 -04:00
constructor(private _reflector: ReflectorReader = reflector) {}
/**
* Return {@link Directive} for a given `Type`.
*/
resolve(type: Type<any>, throwIfNotFound = true): Directive {
2016-09-27 18:41:37 -04:00
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
if (typeMetadata) {
const metadata = typeMetadata.find(isDirectiveMetadata);
if (metadata) {
const propertyMetadata = this._reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata, type);
}
}
2016-09-27 18:41:37 -04:00
if (throwIfNotFound) {
throw new Error(`No Directive annotation found on ${stringify(type)}`);
}
2016-09-27 18:41:37 -04:00
return null;
}
private _mergeWithPropertyMetadata(
dm: Directive, propertyMetadata: {[key: string]: any[]},
directiveType: Type<any>): Directive {
2016-09-27 18:41:37 -04:00
const inputs: string[] = [];
const outputs: string[] = [];
const host: {[key: string]: string} = {};
const queries: {[key: string]: any} = {};
Object.keys(propertyMetadata).forEach((propName: string) => {
2016-09-27 18:41:37 -04:00
propertyMetadata[propName].forEach(a => {
if (a instanceof Input) {
2016-09-27 18:41:37 -04:00
if (a.bindingPropertyName) {
inputs.push(`${propName}: ${a.bindingPropertyName}`);
} else {
inputs.push(propName);
}
} else if (a instanceof Output) {
const output: Output = a;
2016-09-27 18:41:37 -04:00
if (output.bindingPropertyName) {
outputs.push(`${propName}: ${output.bindingPropertyName}`);
} else {
outputs.push(propName);
}
} else if (a instanceof HostBinding) {
const hostBinding: HostBinding = a;
2016-09-27 18:41:37 -04:00
if (hostBinding.hostPropertyName) {
const startWith = hostBinding.hostPropertyName[0];
if (startWith === '(') {
throw new Error(`@HostBinding can not bind to events. Use @HostListener instead.`);
} else if (startWith === '[') {
throw new Error(
`@HostBinding parameter should be a property name, 'class.<name>', or 'attr.<name>'.`);
}
host[`[${hostBinding.hostPropertyName}]`] = propName;
} else {
host[`[${propName}]`] = propName;
}
} else if (a instanceof HostListener) {
const hostListener: HostListener = a;
2016-09-27 18:41:37 -04:00
const args = hostListener.args || [];
host[`(${hostListener.eventName})`] = `${propName}(${args.join(',')})`;
} else if (a instanceof Query) {
queries[propName] = a;
}
});
});
return this._merge(dm, inputs, outputs, host, queries, directiveType);
}
private _extractPublicName(def: string) { return splitAtColon(def, [null, def])[1].trim(); }
private _merge(
2016-09-27 18:41:37 -04:00
directive: Directive, inputs: string[], outputs: string[], host: {[key: string]: string},
queries: {[key: string]: any}, directiveType: Type<any>): Directive {
2016-09-27 18:41:37 -04:00
const mergedInputs: string[] = inputs;
2016-09-27 18:41:37 -04:00
if (directive.inputs) {
const inputNames: string[] =
2016-09-27 18:41:37 -04:00
directive.inputs.map((def: string): string => this._extractPublicName(def));
inputs.forEach((inputDef: string) => {
const publicName = this._extractPublicName(inputDef);
if (inputNames.indexOf(publicName) > -1) {
throw new Error(
`Input '${publicName}' defined multiple times in '${stringify(directiveType)}'`);
}
});
2016-09-27 18:41:37 -04:00
mergedInputs.unshift(...directive.inputs);
}
2016-09-27 18:41:37 -04:00
let mergedOutputs: string[] = outputs;
2016-09-27 18:41:37 -04:00
if (directive.outputs) {
const outputNames: string[] =
2016-09-27 18:41:37 -04:00
directive.outputs.map((def: string): string => this._extractPublicName(def));
outputs.forEach((outputDef: string) => {
const publicName = this._extractPublicName(outputDef);
if (outputNames.indexOf(publicName) > -1) {
throw new Error(
`Output event '${publicName}' defined multiple times in '${stringify(directiveType)}'`);
}
});
2016-09-27 18:41:37 -04:00
mergedOutputs.unshift(...directive.outputs);
}
2016-09-27 18:41:37 -04:00
const mergedHost = directive.host ? StringMapWrapper.merge(directive.host, host) : host;
const mergedQueries =
directive.queries ? StringMapWrapper.merge(directive.queries, queries) : queries;
2016-09-27 18:41:37 -04:00
if (directive instanceof Component) {
return new Component({
2016-09-27 18:41:37 -04:00
selector: directive.selector,
inputs: mergedInputs,
outputs: mergedOutputs,
host: mergedHost,
2016-09-27 18:41:37 -04:00
exportAs: directive.exportAs,
moduleId: directive.moduleId,
queries: mergedQueries,
2016-09-27 18:41:37 -04:00
changeDetection: directive.changeDetection,
providers: directive.providers,
viewProviders: directive.viewProviders,
entryComponents: directive.entryComponents,
template: directive.template,
templateUrl: directive.templateUrl,
styles: directive.styles,
styleUrls: directive.styleUrls,
encapsulation: directive.encapsulation,
animations: directive.animations,
interpolation: directive.interpolation
});
} else {
return new Directive({
2016-09-27 18:41:37 -04:00
selector: directive.selector,
inputs: mergedInputs,
outputs: mergedOutputs,
host: mergedHost,
2016-09-27 18:41:37 -04:00
exportAs: directive.exportAs,
queries: mergedQueries,
2016-09-27 18:41:37 -04:00
providers: directive.providers
});
}
}
}
2016-09-27 18:41:37 -04:00
function isDirectiveMetadata(type: any): type is Directive {
return type instanceof Directive;
}