2016-06-23 16:47:54 +00: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-06-08 23:38:52 +00:00
|
|
|
import {Injectable, ViewMetadata, ComponentMetadata,} from '@angular/core';
|
2016-05-25 22:00:05 +00:00
|
|
|
import {ReflectorReader, reflector} from '../core_private';
|
2016-04-29 00:50:03 +00:00
|
|
|
import {Type, stringify, isBlank, isPresent} from '../src/facade/lang';
|
|
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
2016-07-11 22:27:57 +00:00
|
|
|
|
|
|
|
|
function _isComponentMetadata(obj: any): obj is ComponentMetadata {
|
|
|
|
|
return obj instanceof ComponentMetadata;
|
|
|
|
|
}
|
2015-02-12 13:44:59 +00:00
|
|
|
|
2015-12-03 23:49:09 +00:00
|
|
|
/**
|
|
|
|
|
* Resolves types to {@link ViewMetadata}.
|
|
|
|
|
*/
|
2015-03-16 21:44:14 +00:00
|
|
|
@Injectable()
|
2015-06-24 08:54:04 +00:00
|
|
|
export class ViewResolver {
|
2016-06-17 17:57:50 +00:00
|
|
|
constructor(private _reflector: ReflectorReader = reflector) {}
|
2016-03-24 20:32:47 +00:00
|
|
|
|
2015-08-14 17:03:45 +00:00
|
|
|
resolve(component: Type): ViewMetadata {
|
2016-07-11 22:27:57 +00:00
|
|
|
const compMeta: ComponentMetadata =
|
|
|
|
|
this._reflector.annotations(component).find(_isComponentMetadata);
|
2015-10-07 00:03:37 +00:00
|
|
|
|
|
|
|
|
if (isPresent(compMeta)) {
|
2016-06-05 02:46:55 +00:00
|
|
|
if (isBlank(compMeta.template) && isBlank(compMeta.templateUrl)) {
|
2015-10-07 00:03:37 +00:00
|
|
|
throw new BaseException(
|
2016-03-08 21:36:48 +00:00
|
|
|
`Component '${stringify(component)}' must have either 'template' or 'templateUrl' set.`);
|
2015-10-07 00:03:37 +00: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 19:46:22 +00:00
|
|
|
styleUrls: compMeta.styleUrls,
|
2016-06-20 16:52:41 +00:00
|
|
|
animations: compMeta.animations,
|
|
|
|
|
interpolation: compMeta.interpolation
|
2015-10-07 00:03:37 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2016-06-08 23:38:52 +00:00
|
|
|
throw new BaseException(
|
|
|
|
|
`Could not compile '${stringify(component)}' because it is not a component.`);
|
2015-02-12 13:44:59 +00:00
|
|
|
}
|
2015-10-07 00:03:37 +00:00
|
|
|
}
|
2015-02-12 13:44:59 +00:00
|
|
|
}
|