2015-09-04 01:01:36 -04:00
|
|
|
import {resolveForwardRef, Injectable} from 'angular2/src/core/di';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {Type, isPresent, stringify} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException} from 'angular2/src/facade/exceptions';
|
2015-09-04 01:01:36 -04:00
|
|
|
import {PipeMetadata} from 'angular2/src/core/metadata';
|
2016-03-24 16:32:47 -04:00
|
|
|
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {reflector} from 'angular2/src/core/reflection/reflection';
|
2015-08-07 14:41:38 -04:00
|
|
|
|
2015-10-23 02:35:53 -04:00
|
|
|
function _isPipeMetadata(type: any): boolean {
|
|
|
|
return type instanceof PipeMetadata;
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:38 -04:00
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Resolve a `Type` for {@link PipeMetadata}.
|
2015-08-07 14:41:38 -04:00
|
|
|
*
|
|
|
|
* This interface can be overridden by the application developer to create custom behavior.
|
|
|
|
*
|
|
|
|
* See {@link Compiler}
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class PipeResolver {
|
2016-03-24 16:32:47 -04:00
|
|
|
private _reflector: ReflectorReader;
|
|
|
|
constructor(_reflector?: ReflectorReader) {
|
|
|
|
if (isPresent(_reflector)) {
|
|
|
|
this._reflector = _reflector;
|
|
|
|
} else {
|
|
|
|
this._reflector = reflector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:38 -04:00
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Return {@link PipeMetadata} for a given `Type`.
|
2015-08-07 14:41:38 -04:00
|
|
|
*/
|
2015-08-14 13:03:45 -04:00
|
|
|
resolve(type: Type): PipeMetadata {
|
2016-03-24 16:32:47 -04:00
|
|
|
var metas = this._reflector.annotations(resolveForwardRef(type));
|
2015-08-07 14:41:38 -04:00
|
|
|
if (isPresent(metas)) {
|
2015-10-09 12:07:58 -04:00
|
|
|
var annotation = metas.find(_isPipeMetadata);
|
2015-10-23 02:35:53 -04:00
|
|
|
if (isPresent(annotation)) {
|
|
|
|
return annotation;
|
2015-08-07 14:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new BaseException(`No Pipe decorator found on ${stringify(type)}`);
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 13:35:51 -05:00
|
|
|
|
2016-03-24 16:32:47 -04:00
|
|
|
export var CODEGEN_PIPE_RESOLVER = new PipeResolver(reflector);
|