OpenSlides/client/src/app/core/services/collectionStringModelMapper.service.ts

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-08-24 13:05:03 +02:00
import { ModelConstructor } from '../../shared/models/base.model';
/**
* Registeres the mapping of collection strings <--> actual types. Every Model should register itself here.
*/
export class CollectionStringModelMapperService {
/**
* Mapps collection strings to model constructors. Accessed by {@method registerCollectionElement} and
* {@method getCollectionStringType}.
*/
private static collectionStringsTypeMapping: { [collectionString: string]: ModelConstructor } = {};
/**
* Registers the type to the collection string
* @param collectionString
* @param type
*/
public static registerCollectionElement(collectionString: string, type: ModelConstructor) {
CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString] = type;
}
/**
* Returns the constructor of the requested collection or undefined, if it is not registered.
* @param collectionString the requested collection
*/
public static getModelConstructor(collectionString: string): ModelConstructor {
2018-08-24 13:05:03 +02:00
return CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString];
}
2018-08-29 13:21:25 +02:00
/**
* Returns the collection string of a given ModelConstructor or undefined, if it is not registered.
* @param ctor
*/
public static getCollectionString(ctor: ModelConstructor): string {
return Object.keys(CollectionStringModelMapperService.collectionStringsTypeMapping).find(
(collectionString: string) => {
return ctor === CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString];
}
);
}
2018-08-29 13:21:25 +02:00
/**
* Constructor to create the NotifyService. Registers itself to the WebsocketService.
* @param websocketService
*/
public constructor() {}
2018-08-24 13:05:03 +02:00
}