2016-04-28 20:50:03 -04:00
|
|
|
import {
|
|
|
|
Injectable,
|
|
|
|
ViewMetadata,
|
|
|
|
ComponentMetadata,
|
|
|
|
} from '@angular/core';
|
2015-02-12 08:44:59 -05:00
|
|
|
|
2016-05-25 18:00:05 -04:00
|
|
|
import {ReflectorReader, reflector} from '../core_private';
|
2015-02-12 08:44:59 -05:00
|
|
|
|
2016-04-28 20:50:03 -04: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 08:44:59 -05:00
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* Resolves types to {@link ViewMetadata}.
|
|
|
|
*/
|
2015-03-16 17:44:14 -04:00
|
|
|
@Injectable()
|
2015-06-24 04:54:04 -04:00
|
|
|
export class ViewResolver {
|
2016-03-24 16:32:47 -04:00
|
|
|
private _reflector: ReflectorReader;
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-29 14:11:06 -04:00
|
|
|
_cache = new Map<Type, ViewMetadata>();
|
2015-02-12 08:44:59 -05:00
|
|
|
|
2016-03-24 16:32:47 -04:00
|
|
|
constructor(_reflector?: ReflectorReader) {
|
|
|
|
if (isPresent(_reflector)) {
|
|
|
|
this._reflector = _reflector;
|
|
|
|
} else {
|
|
|
|
this._reflector = reflector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 13:03:45 -04:00
|
|
|
resolve(component: Type): ViewMetadata {
|
2015-06-17 19:21:40 -04:00
|
|
|
var view = this._cache.get(component);
|
2015-02-12 08:44:59 -05:00
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
if (isBlank(view)) {
|
|
|
|
view = this._resolve(component);
|
2015-06-17 19:21:40 -04:00
|
|
|
this._cache.set(component, view);
|
2015-02-12 08:44:59 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
return view;
|
2015-02-12 08:44:59 -05:00
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-08-14 13:03:45 -04:00
|
|
|
_resolve(component: Type): ViewMetadata {
|
2015-10-06 20:03:37 -04:00
|
|
|
var compMeta: ComponentMetadata;
|
|
|
|
var viewMeta: ViewMetadata;
|
|
|
|
|
2016-03-24 16:32:47 -04:00
|
|
|
this._reflector.annotations(component).forEach(m => {
|
2015-10-06 20:03:37 -04:00
|
|
|
if (m instanceof ViewMetadata) {
|
|
|
|
viewMeta = m;
|
|
|
|
}
|
|
|
|
if (m instanceof ComponentMetadata) {
|
|
|
|
compMeta = m;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isPresent(compMeta)) {
|
|
|
|
if (isBlank(compMeta.template) && isBlank(compMeta.templateUrl) && isBlank(viewMeta)) {
|
|
|
|
throw new BaseException(
|
2016-03-08 16:36:48 -05:00
|
|
|
`Component '${stringify(component)}' must have either 'template' or 'templateUrl' set.`);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.template) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("template", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.templateUrl) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("templateUrl", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.directives) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("directives", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.pipes) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("pipes", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.encapsulation) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("encapsulation", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.styles) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("styles", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(compMeta.styleUrls) && isPresent(viewMeta)) {
|
2016-04-12 12:40:37 -04:00
|
|
|
this._throwMixingViewAndComponent("styleUrls", component);
|
2015-10-06 20:03:37 -04:00
|
|
|
|
|
|
|
} else if (isPresent(viewMeta)) {
|
|
|
|
return viewMeta;
|
|
|
|
|
|
|
|
} 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 15:46:22 -04:00
|
|
|
styleUrls: compMeta.styleUrls,
|
|
|
|
animations: compMeta.animations
|
2015-10-06 20:03:37 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isBlank(viewMeta)) {
|
2016-03-08 16:36:48 -05:00
|
|
|
throw new BaseException(
|
|
|
|
`Could not compile '${stringify(component)}' because it is not a component.`);
|
2015-10-06 20:03:37 -04:00
|
|
|
} else {
|
|
|
|
return viewMeta;
|
2015-02-12 08:44:59 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-06 20:03:37 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-10-06 20:03:37 -04:00
|
|
|
_throwMixingViewAndComponent(propertyName: string, component: Type): void {
|
|
|
|
throw new BaseException(
|
|
|
|
`Component '${stringify(component)}' cannot have both '${propertyName}' and '@View' set at the same time"`);
|
2015-02-12 08:44:59 -05:00
|
|
|
}
|
|
|
|
}
|