2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
import {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, Injectable, InputMetadata, OutputMetadata, QueryMetadata, Type, resolveForwardRef} from '@angular/core';
|
2016-08-30 21:07:40 -04:00
|
|
|
|
2016-07-11 19:01:49 -04:00
|
|
|
import {StringMapWrapper} from './facade/collection';
|
2016-08-10 21:21:28 -04:00
|
|
|
import {isPresent, stringify} from './facade/lang';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {ReflectorReader, reflector} from './private_import_core';
|
2016-07-11 19:01:49 -04:00
|
|
|
import {splitAtColon} from './util';
|
2014-11-11 20:33:47 -05:00
|
|
|
|
2016-07-11 18:27:57 -04:00
|
|
|
function _isDirectiveMetadata(type: any): type is DirectiveMetadata {
|
2015-10-23 02:35:53 -04:00
|
|
|
return type instanceof DirectiveMetadata;
|
|
|
|
}
|
|
|
|
|
2015-09-19 21:39:35 -04:00
|
|
|
/*
|
2015-08-14 13:03:45 -04:00
|
|
|
* Resolve a `Type` for {@link DirectiveMetadata}.
|
2015-07-07 02:15:58 -04:00
|
|
|
*
|
|
|
|
* This interface can be overridden by the application developer to create custom behavior.
|
|
|
|
*
|
|
|
|
* See {@link Compiler}
|
|
|
|
*/
|
2015-03-16 17:44:14 -04:00
|
|
|
@Injectable()
|
2015-05-11 20:59:39 -04:00
|
|
|
export class DirectiveResolver {
|
2016-06-17 13:57:50 -04:00
|
|
|
constructor(private _reflector: ReflectorReader = reflector) {}
|
2016-03-24 16:32:47 -04:00
|
|
|
|
2015-07-07 02:15:58 -04:00
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Return {@link DirectiveMetadata} for a given `Type`.
|
2015-07-07 02:15:58 -04:00
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): DirectiveMetadata {
|
2016-03-24 16:32:47 -04:00
|
|
|
var typeMetadata = this._reflector.annotations(resolveForwardRef(type));
|
2015-09-03 18:10:48 -04:00
|
|
|
if (isPresent(typeMetadata)) {
|
2015-10-09 12:07:58 -04:00
|
|
|
var metadata = typeMetadata.find(_isDirectiveMetadata);
|
2015-10-23 02:35:53 -04:00
|
|
|
if (isPresent(metadata)) {
|
2016-03-24 16:32:47 -04:00
|
|
|
var propertyMetadata = this._reflector.propMetadata(type);
|
2015-11-04 04:08:51 -05:00
|
|
|
return this._mergeWithPropertyMetadata(metadata, propertyMetadata, type);
|
2014-11-11 20:33:47 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 06:50:31 -04:00
|
|
|
if (throwIfNotFound) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`No Directive annotation found on ${stringify(type)}`);
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
|
|
|
return null;
|
2014-11-11 20:33:47 -05:00
|
|
|
}
|
2015-09-03 18:10:48 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
private _mergeWithPropertyMetadata(
|
|
|
|
dm: DirectiveMetadata, propertyMetadata: {[key: string]: any[]},
|
2016-08-10 21:21:28 -04:00
|
|
|
directiveType: Type<any>): DirectiveMetadata {
|
2016-06-17 13:57:50 -04:00
|
|
|
var inputs: string[] = [];
|
|
|
|
var outputs: string[] = [];
|
2015-10-02 20:33:21 -04:00
|
|
|
var host: {[key: string]: string} = {};
|
|
|
|
var queries: {[key: string]: any} = {};
|
2015-09-03 18:10:48 -04:00
|
|
|
|
|
|
|
StringMapWrapper.forEach(propertyMetadata, (metadata: any[], propName: string) => {
|
|
|
|
metadata.forEach(a => {
|
2015-09-30 23:59:23 -04:00
|
|
|
if (a instanceof InputMetadata) {
|
2015-09-03 18:10:48 -04:00
|
|
|
if (isPresent(a.bindingPropertyName)) {
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs.push(`${propName}: ${a.bindingPropertyName}`);
|
2015-09-03 18:10:48 -04:00
|
|
|
} else {
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs.push(propName);
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
2016-07-13 14:01:32 -04:00
|
|
|
} else if (a instanceof OutputMetadata) {
|
2016-09-12 12:44:20 -04:00
|
|
|
const output: OutputMetadata = a;
|
|
|
|
if (isPresent(output.bindingPropertyName)) {
|
|
|
|
outputs.push(`${propName}: ${output.bindingPropertyName}`);
|
2015-09-03 18:10:48 -04:00
|
|
|
} else {
|
2015-09-30 23:59:23 -04:00
|
|
|
outputs.push(propName);
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
2016-07-13 14:01:32 -04:00
|
|
|
} else if (a instanceof HostBindingMetadata) {
|
2016-09-12 12:44:20 -04:00
|
|
|
const hostBinding: HostBindingMetadata = a;
|
|
|
|
if (isPresent(hostBinding.hostPropertyName)) {
|
|
|
|
host[`[${hostBinding.hostPropertyName}]`] = propName;
|
2015-09-04 17:07:16 -04:00
|
|
|
} else {
|
|
|
|
host[`[${propName}]`] = propName;
|
|
|
|
}
|
2016-07-13 14:01:32 -04:00
|
|
|
} else if (a instanceof HostListenerMetadata) {
|
2016-09-12 12:44:20 -04:00
|
|
|
const hostListener: HostListenerMetadata = a;
|
|
|
|
var args = isPresent(hostListener.args) ? (<any[]>hostListener.args).join(', ') : '';
|
|
|
|
host[`(${hostListener.eventName})`] = `${propName}(${args})`;
|
2016-07-13 14:01:32 -04:00
|
|
|
} else if (a instanceof QueryMetadata) {
|
2015-09-19 21:39:35 -04:00
|
|
|
queries[propName] = a;
|
|
|
|
}
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
});
|
2015-11-04 04:08:51 -05:00
|
|
|
return this._merge(dm, inputs, outputs, host, queries, directiveType);
|
2015-09-03 18:10:48 -04:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:01:49 -04:00
|
|
|
private _extractPublicName(def: string) { return splitAtColon(def, [null, def])[1].trim(); }
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
private _merge(
|
|
|
|
dm: DirectiveMetadata, inputs: string[], outputs: string[], host: {[key: string]: string},
|
2016-08-10 21:21:28 -04:00
|
|
|
queries: {[key: string]: any}, directiveType: Type<any>): DirectiveMetadata {
|
2016-07-11 19:01:49 -04:00
|
|
|
let mergedInputs: string[];
|
|
|
|
|
|
|
|
if (isPresent(dm.inputs)) {
|
|
|
|
const inputNames: string[] =
|
|
|
|
dm.inputs.map((def: string): string => this._extractPublicName(def));
|
|
|
|
inputs.forEach((inputDef: string) => {
|
|
|
|
const publicName = this._extractPublicName(inputDef);
|
|
|
|
if (inputNames.indexOf(publicName) > -1) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(
|
2016-07-11 19:01:49 -04:00
|
|
|
`Input '${publicName}' defined multiple times in '${stringify(directiveType)}'`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mergedInputs = dm.inputs.concat(inputs);
|
|
|
|
} else {
|
|
|
|
mergedInputs = inputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mergedOutputs: string[];
|
2015-11-04 04:08:51 -05:00
|
|
|
|
|
|
|
if (isPresent(dm.outputs)) {
|
2016-07-11 19:01:49 -04:00
|
|
|
const outputNames: string[] =
|
|
|
|
dm.outputs.map((def: string): string => this._extractPublicName(def));
|
|
|
|
|
|
|
|
outputs.forEach((outputDef: string) => {
|
|
|
|
const publicName = this._extractPublicName(outputDef);
|
|
|
|
if (outputNames.indexOf(publicName) > -1) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(
|
2016-07-11 19:01:49 -04:00
|
|
|
`Output event '${publicName}' defined multiple times in '${stringify(directiveType)}'`);
|
2015-11-04 04:08:51 -05:00
|
|
|
}
|
|
|
|
});
|
2016-07-11 19:01:49 -04:00
|
|
|
mergedOutputs = dm.outputs.concat(outputs);
|
2015-11-04 04:08:51 -05:00
|
|
|
} else {
|
|
|
|
mergedOutputs = outputs;
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:07:16 -04:00
|
|
|
var mergedHost = isPresent(dm.host) ? StringMapWrapper.merge(dm.host, host) : host;
|
2015-09-17 21:45:49 -04:00
|
|
|
var mergedQueries =
|
|
|
|
isPresent(dm.queries) ? StringMapWrapper.merge(dm.queries, queries) : queries;
|
2015-09-03 18:10:48 -04:00
|
|
|
|
|
|
|
if (dm instanceof ComponentMetadata) {
|
|
|
|
return new ComponentMetadata({
|
|
|
|
selector: dm.selector,
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs: mergedInputs,
|
|
|
|
outputs: mergedOutputs,
|
2015-09-04 17:07:16 -04:00
|
|
|
host: mergedHost,
|
2015-09-03 18:10:48 -04:00
|
|
|
exportAs: dm.exportAs,
|
2015-09-14 18:59:09 -04:00
|
|
|
moduleId: dm.moduleId,
|
2015-09-17 21:45:49 -04:00
|
|
|
queries: mergedQueries,
|
2015-09-03 18:10:48 -04:00
|
|
|
changeDetection: dm.changeDetection,
|
2015-10-11 01:11:13 -04:00
|
|
|
providers: dm.providers,
|
2016-06-22 17:06:23 -04:00
|
|
|
viewProviders: dm.viewProviders,
|
2016-07-28 09:31:26 -04:00
|
|
|
entryComponents: dm.entryComponents,
|
|
|
|
template: dm.template,
|
|
|
|
templateUrl: dm.templateUrl,
|
|
|
|
styles: dm.styles,
|
|
|
|
styleUrls: dm.styleUrls,
|
|
|
|
encapsulation: dm.encapsulation,
|
|
|
|
animations: dm.animations,
|
|
|
|
interpolation: dm.interpolation
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return new DirectiveMetadata({
|
|
|
|
selector: dm.selector,
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs: mergedInputs,
|
|
|
|
outputs: mergedOutputs,
|
2015-09-04 17:07:16 -04:00
|
|
|
host: mergedHost,
|
2015-09-03 18:10:48 -04:00
|
|
|
exportAs: dm.exportAs,
|
2015-10-11 01:11:13 -04:00
|
|
|
queries: mergedQueries,
|
|
|
|
providers: dm.providers
|
2015-09-03 18:10:48 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 20:33:47 -05:00
|
|
|
}
|