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-08-11 01:21:28 +00:00
|
|
|
import {Injectable, PipeMetadata, Type, resolveForwardRef} from '@angular/core';
|
2016-04-29 00:50:03 +00:00
|
|
|
|
2016-08-11 01:21:28 +00:00
|
|
|
import {isPresent, stringify} from './facade/lang';
|
2016-08-31 01:07:40 +00:00
|
|
|
import {ReflectorReader, reflector} from './private_import_core';
|
2015-08-07 18:41:38 +00:00
|
|
|
|
2015-10-23 06:35:53 +00:00
|
|
|
function _isPipeMetadata(type: any): boolean {
|
|
|
|
|
return type instanceof PipeMetadata;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-07 18:41:38 +00:00
|
|
|
/**
|
2015-08-14 17:03:45 +00:00
|
|
|
* Resolve a `Type` for {@link PipeMetadata}.
|
2015-08-07 18:41:38 +00:00
|
|
|
*
|
|
|
|
|
* This interface can be overridden by the application developer to create custom behavior.
|
|
|
|
|
*
|
|
|
|
|
* See {@link Compiler}
|
|
|
|
|
*/
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class PipeResolver {
|
2016-06-17 17:57:50 +00:00
|
|
|
constructor(private _reflector: ReflectorReader = reflector) {}
|
2016-03-24 20:32:47 +00:00
|
|
|
|
2015-08-07 18:41:38 +00:00
|
|
|
/**
|
2015-08-14 17:03:45 +00:00
|
|
|
* Return {@link PipeMetadata} for a given `Type`.
|
2015-08-07 18:41:38 +00:00
|
|
|
*/
|
2016-08-11 01:21:28 +00:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): PipeMetadata {
|
2016-03-24 20:32:47 +00:00
|
|
|
var metas = this._reflector.annotations(resolveForwardRef(type));
|
2015-08-07 18:41:38 +00:00
|
|
|
if (isPresent(metas)) {
|
2015-10-09 16:07:58 +00:00
|
|
|
var annotation = metas.find(_isPipeMetadata);
|
2015-10-23 06:35:53 +00:00
|
|
|
if (isPresent(annotation)) {
|
|
|
|
|
return annotation;
|
2015-08-07 18:41:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-07-18 10:50:31 +00:00
|
|
|
if (throwIfNotFound) {
|
2016-08-25 07:50:16 +00:00
|
|
|
throw new Error(`No Pipe decorator found on ${stringify(type)}`);
|
2016-07-18 10:50:31 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
2015-08-07 18:41:38 +00:00
|
|
|
}
|
2016-07-18 10:50:31 +00:00
|
|
|
}
|