2016-06-08 16:38:52 -07:00
|
|
|
import {Injectable, ViewMetadata, ComponentMetadata,} from '@angular/core';
|
2015-02-12 14:44:59 +01:00
|
|
|
|
2016-05-25 15:00:05 -07:00
|
|
|
import {ReflectorReader, reflector} from '../core_private';
|
2015-02-12 14:44:59 +01:00
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
import {Type, stringify, isBlank, isPresent} from '../src/facade/lang';
|
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
|
|
|
import {Map} from '../src/facade/collection';
|
2015-02-12 14:44:59 +01:00
|
|
|
|
2015-12-03 15:49:09 -08:00
|
|
|
/**
|
|
|
|
* Resolves types to {@link ViewMetadata}.
|
|
|
|
*/
|
2015-03-16 14:44:14 -07:00
|
|
|
@Injectable()
|
2015-06-24 10:54:04 +02:00
|
|
|
export class ViewResolver {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_cache = new Map<Type, ViewMetadata>();
|
2015-02-12 14:44:59 +01:00
|
|
|
|
2016-06-17 10:57:50 -07:00
|
|
|
constructor(private _reflector: ReflectorReader = reflector) {}
|
2016-03-24 13:32:47 -07:00
|
|
|
|
2015-08-14 10:03:45 -07:00
|
|
|
resolve(component: Type): ViewMetadata {
|
2015-06-17 16:21:40 -07:00
|
|
|
var view = this._cache.get(component);
|
2015-02-12 14:44:59 +01:00
|
|
|
|
2015-04-09 21:20:11 +02:00
|
|
|
if (isBlank(view)) {
|
|
|
|
view = this._resolve(component);
|
2015-06-17 16:21:40 -07:00
|
|
|
this._cache.set(component, view);
|
2015-02-12 14:44:59 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:20:11 +02:00
|
|
|
return view;
|
2015-02-12 14:44:59 +01:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-14 10:03:45 -07:00
|
|
|
_resolve(component: Type): ViewMetadata {
|
2015-10-06 17:03:37 -07:00
|
|
|
var compMeta: ComponentMetadata;
|
|
|
|
|
2016-03-24 13:32:47 -07:00
|
|
|
this._reflector.annotations(component).forEach(m => {
|
2015-10-06 17:03:37 -07:00
|
|
|
if (m instanceof ComponentMetadata) {
|
|
|
|
compMeta = m;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isPresent(compMeta)) {
|
2016-06-05 04:46:55 +02:00
|
|
|
if (isBlank(compMeta.template) && isBlank(compMeta.templateUrl)) {
|
2015-10-06 17:03:37 -07:00
|
|
|
throw new BaseException(
|
2016-03-08 13:36:48 -08:00
|
|
|
`Component '${stringify(component)}' must have either 'template' or 'templateUrl' set.`);
|
2015-10-06 17:03:37 -07:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return new ViewMetadata({
|
|
|
|
templateUrl: compMeta.templateUrl,
|
|
|
|
template: compMeta.template,
|
|
|
|
directives: compMeta.directives,
|
|
|
|
pipes: compMeta.pipes,
|
|
|
|
encapsulation: compMeta.encapsulation,
|
|
|
|
styles: compMeta.styles,
|
2016-05-25 12:46:22 -07:00
|
|
|
styleUrls: compMeta.styleUrls,
|
|
|
|
animations: compMeta.animations
|
2015-10-06 17:03:37 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-08 16:38:52 -07:00
|
|
|
throw new BaseException(
|
|
|
|
`Could not compile '${stringify(component)}' because it is not a component.`);
|
2015-02-12 14:44:59 +01:00
|
|
|
}
|
2015-10-06 17:03:37 -07:00
|
|
|
}
|
2015-02-12 14:44:59 +01:00
|
|
|
}
|