2015-02-12 14:56:41 -08:00
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang';
|
2015-06-18 15:40:12 -07:00
|
|
|
import {Pipe, PipeFactory} from './pipe';
|
2015-05-01 14:05:19 -07:00
|
|
|
import {Injectable} from 'angular2/src/di/decorators';
|
2015-04-14 07:47:12 -07:00
|
|
|
import {ChangeDetectorRef} from '../change_detector_ref';
|
2015-02-12 14:56:41 -08:00
|
|
|
|
2015-04-13 09:50:02 -07:00
|
|
|
@Injectable()
|
2015-06-22 17:11:55 -07:00
|
|
|
@CONST()
|
2015-02-12 14:56:41 -08:00
|
|
|
export class PipeRegistry {
|
2015-05-01 14:05:19 -07:00
|
|
|
constructor(public config) {}
|
2015-02-12 14:56:41 -08:00
|
|
|
|
2015-06-18 15:40:12 -07:00
|
|
|
get(type: string, obj, cdRef?: ChangeDetectorRef, existingPipe?: Pipe): Pipe {
|
|
|
|
if (isPresent(existingPipe) && existingPipe.supports(obj)) return existingPipe;
|
|
|
|
|
|
|
|
if (isPresent(existingPipe)) existingPipe.onDestroy();
|
|
|
|
|
|
|
|
var factories = this._getListOfFactories(type, obj);
|
|
|
|
var factory = this._getMatchingFactory(factories, type, obj);
|
2015-02-12 14:56:41 -08:00
|
|
|
|
2015-06-18 15:40:12 -07:00
|
|
|
return factory.create(cdRef);
|
|
|
|
}
|
2015-02-12 14:56:41 -08:00
|
|
|
|
2015-06-18 15:40:12 -07:00
|
|
|
private _getListOfFactories(type: string, obj: any): PipeFactory[] {
|
|
|
|
var listOfFactories = this.config[type];
|
|
|
|
if (isBlank(listOfFactories)) {
|
2015-04-23 15:27:20 +02:00
|
|
|
throw new BaseException(`Cannot find '${type}' pipe supporting object '${obj}'`);
|
2015-02-12 14:56:41 -08:00
|
|
|
}
|
2015-06-18 15:40:12 -07:00
|
|
|
return listOfFactories;
|
|
|
|
}
|
2015-02-12 14:56:41 -08:00
|
|
|
|
2015-06-18 15:40:12 -07:00
|
|
|
private _getMatchingFactory(listOfFactories: PipeFactory[], type: string, obj: any): PipeFactory {
|
|
|
|
var matchingFactory =
|
|
|
|
ListWrapper.find(listOfFactories, pipeFactory => pipeFactory.supports(obj));
|
|
|
|
if (isBlank(matchingFactory)) {
|
|
|
|
throw new BaseException(`Cannot find '${type}' pipe supporting object '${obj}'`);
|
|
|
|
}
|
|
|
|
return matchingFactory;
|
2015-02-12 14:56:41 -08:00
|
|
|
}
|
2015-04-23 15:27:20 +02:00
|
|
|
}
|