2015-09-14 18:59:09 -04:00
|
|
|
import {resolveForwardRef} from 'angular2/src/core/di';
|
|
|
|
import {
|
|
|
|
Type,
|
|
|
|
isBlank,
|
|
|
|
isPresent,
|
|
|
|
isArray,
|
|
|
|
stringify,
|
|
|
|
RegExpWrapper
|
2015-11-06 20:34:07 -05:00
|
|
|
} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException} from 'angular2/src/facade/exceptions';
|
2015-09-14 18:59:09 -04:00
|
|
|
import * as cpl from './directive_metadata';
|
2015-10-23 18:55:48 -04:00
|
|
|
import * as md from 'angular2/src/core/metadata/directives';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
|
|
|
|
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {ViewMetadata} from 'angular2/src/core/metadata/view';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {hasLifecycleHook} from 'angular2/src/core/linker/directive_lifecycle_reflector';
|
|
|
|
import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from 'angular2/src/core/linker/interfaces';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {reflector} from 'angular2/src/core/reflection/reflection';
|
2015-11-02 19:03:42 -05:00
|
|
|
import {Injectable, Inject, Optional} from 'angular2/src/core/di';
|
2015-11-09 17:33:22 -05:00
|
|
|
import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes';
|
2015-10-01 13:07:49 -04:00
|
|
|
import {MODULE_SUFFIX} from './util';
|
2015-12-05 05:21:38 -05:00
|
|
|
import {getUrlScheme} from 'angular2/src/compiler/url_resolver';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RuntimeMetadataResolver {
|
2015-09-29 14:11:06 -04:00
|
|
|
private _cache = new Map<Type, cpl.CompileDirectiveMetadata>();
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-11-02 19:03:42 -05:00
|
|
|
constructor(private _directiveResolver: DirectiveResolver, private _viewResolver: ViewResolver,
|
2015-11-09 17:33:22 -05:00
|
|
|
@Optional() @Inject(PLATFORM_DIRECTIVES) private _platformDirectives: Type[]) {}
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
getMetadata(directiveType: Type): cpl.CompileDirectiveMetadata {
|
2015-09-14 18:59:09 -04:00
|
|
|
var meta = this._cache.get(directiveType);
|
|
|
|
if (isBlank(meta)) {
|
2015-10-23 18:55:48 -04:00
|
|
|
var dirMeta = this._directiveResolver.resolve(directiveType);
|
|
|
|
var moduleUrl = calcModuleUrl(directiveType, dirMeta);
|
2015-09-14 18:59:09 -04:00
|
|
|
var templateMeta = null;
|
|
|
|
var changeDetectionStrategy = null;
|
|
|
|
|
2015-10-23 18:55:48 -04:00
|
|
|
if (dirMeta instanceof md.ComponentMetadata) {
|
|
|
|
var cmpMeta = <md.ComponentMetadata>dirMeta;
|
|
|
|
var viewMeta = this._viewResolver.resolve(directiveType);
|
2015-09-18 13:33:23 -04:00
|
|
|
templateMeta = new cpl.CompileTemplateMetadata({
|
2015-10-23 18:55:48 -04:00
|
|
|
encapsulation: viewMeta.encapsulation,
|
|
|
|
template: viewMeta.template,
|
|
|
|
templateUrl: viewMeta.templateUrl,
|
|
|
|
styles: viewMeta.styles,
|
|
|
|
styleUrls: viewMeta.styleUrls
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
2015-10-23 18:55:48 -04:00
|
|
|
changeDetectionStrategy = cmpMeta.changeDetection;
|
2015-09-14 18:59:09 -04:00
|
|
|
}
|
2015-09-18 13:33:23 -04:00
|
|
|
meta = cpl.CompileDirectiveMetadata.create({
|
2015-10-23 18:55:48 -04:00
|
|
|
selector: dirMeta.selector,
|
|
|
|
exportAs: dirMeta.exportAs,
|
2015-09-14 18:59:09 -04:00
|
|
|
isComponent: isPresent(templateMeta),
|
2015-09-18 13:33:23 -04:00
|
|
|
dynamicLoadable: true,
|
2015-09-28 13:30:33 -04:00
|
|
|
type: new cpl.CompileTypeMetadata(
|
2015-10-01 13:07:49 -04:00
|
|
|
{name: stringify(directiveType), moduleUrl: moduleUrl, runtime: directiveType}),
|
2015-09-14 18:59:09 -04:00
|
|
|
template: templateMeta,
|
2015-09-18 13:33:23 -04:00
|
|
|
changeDetection: changeDetectionStrategy,
|
2015-10-23 18:55:48 -04:00
|
|
|
inputs: dirMeta.inputs,
|
|
|
|
outputs: dirMeta.outputs,
|
|
|
|
host: dirMeta.host,
|
2015-10-09 12:07:58 -04:00
|
|
|
lifecycleHooks: LIFECYCLE_HOOKS_VALUES.filter(hook => hasLifecycleHook(hook, directiveType))
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
|
|
|
this._cache.set(directiveType, meta);
|
|
|
|
}
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
2015-09-18 13:33:23 -04:00
|
|
|
getViewDirectivesMetadata(component: Type): cpl.CompileDirectiveMetadata[] {
|
2015-09-14 18:59:09 -04:00
|
|
|
var view = this._viewResolver.resolve(component);
|
2015-11-09 17:33:22 -05:00
|
|
|
var directives = flattenDirectives(view, this._platformDirectives);
|
2015-09-14 18:59:09 -04:00
|
|
|
for (var i = 0; i < directives.length; i++) {
|
|
|
|
if (!isValidDirective(directives[i])) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Unexpected directive value '${stringify(directives[i])}' on the View of component '${stringify(component)}'`);
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 19:39:35 -04:00
|
|
|
|
2015-11-24 14:26:37 -05:00
|
|
|
return directives.map(type => this.getMetadata(type));
|
2015-09-14 18:59:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 17:33:22 -05:00
|
|
|
function flattenDirectives(view: ViewMetadata, platformDirectives: any[]): Type[] {
|
2015-11-02 19:03:42 -05:00
|
|
|
let directives = [];
|
2015-11-09 17:33:22 -05:00
|
|
|
if (isPresent(platformDirectives)) {
|
|
|
|
flattenArray(platformDirectives, directives);
|
2015-11-02 19:03:42 -05:00
|
|
|
}
|
|
|
|
if (isPresent(view.directives)) {
|
|
|
|
flattenArray(view.directives, directives);
|
|
|
|
}
|
2015-09-14 18:59:09 -04:00
|
|
|
return directives;
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:03:42 -05:00
|
|
|
function flattenArray(tree: any[], out: Array<Type | any[]>): void {
|
2015-09-14 18:59:09 -04:00
|
|
|
for (var i = 0; i < tree.length; i++) {
|
|
|
|
var item = resolveForwardRef(tree[i]);
|
|
|
|
if (isArray(item)) {
|
2015-11-02 19:03:42 -05:00
|
|
|
flattenArray(item, out);
|
2015-09-14 18:59:09 -04:00
|
|
|
} else {
|
|
|
|
out.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValidDirective(value: Type): boolean {
|
|
|
|
return isPresent(value) && (value instanceof Type);
|
|
|
|
}
|
|
|
|
|
2015-10-23 18:55:48 -04:00
|
|
|
function calcModuleUrl(type: Type, dirMeta: md.DirectiveMetadata): string {
|
2015-12-05 05:21:38 -05:00
|
|
|
var moduleId = dirMeta.moduleId;
|
|
|
|
if (isPresent(moduleId)) {
|
|
|
|
var scheme = getUrlScheme(moduleId);
|
|
|
|
return isPresent(scheme) && scheme.length > 0 ? moduleId :
|
|
|
|
`package:${moduleId}${MODULE_SUFFIX}`;
|
2015-09-14 18:59:09 -04:00
|
|
|
} else {
|
2015-10-01 13:07:49 -04:00
|
|
|
return reflector.importUri(type);
|
2015-09-14 18:59:09 -04:00
|
|
|
}
|
|
|
|
}
|