libs/core/src/lib/services/building-block-meta.service.ts
Service to access any given meta data for a Building Block page injected by BUILDING_BLOCK_META_DATA during the generation of a page. The service is used by Building Block Pages and needs access to the page-local BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance. Working alternative would be injecting it directly into each of the page modules but this lazy approach supports tree shaking way better.
Methods |
Accessors |
constructor(metaData: BuildingBlockPageMetaData)
|
||||||
Parameters :
|
assertBuildingBlockExistence | ||||||
assertBuildingBlockExistence(id: string)
|
||||||
Parameters :
Returns :
void
|
getBusinessEvents | ||||||
getBusinessEvents(buildingBlockId: string)
|
||||||
Parameters :
Returns :
Record<string, BusinessEventConfig>
|
getData | |||||||||
getData(buildingBlockId: string, key)
|
|||||||||
Parameters :
Returns :
Record<string, BuildingBlockMetaData>
|
getNavigationData | ||||||
getNavigationData(buildingBlockId: string)
|
||||||
Parameters :
|
getResources | ||||||
getResources(buildingBlockId: string)
|
||||||
Parameters :
Returns :
Record<string, BuildingBlockMetaData>
|
hasData | |||||||||
hasData(buildingBlockId: string, key)
|
|||||||||
Parameters :
Returns :
boolean
|
hasMetaData | ||||||
hasMetaData(id: string)
|
||||||
Parameters :
Returns :
any
|
hasResources | ||||||
hasResources(id: string)
|
||||||
Parameters :
Returns :
boolean
|
metaData |
getmetaData()
|
import { Inject, Injectable, Optional } from '@angular/core';
import { BUILDING_BLOCK_NAVIGATION_TYPE } from '../building-block/navigation';
import { type ValidationConfigItem } from '../form-support/validation.model';
import { BUILDING_BLOCK_META_DATA } from '../tokens';
import { type BusinessEventConfig } from './taly-business-event.model';
export type BuildingBlockPageMetaData = Record<string, BuildingBlockMetaData>;
export interface BuildingBlockMetaData {
resources: unknown;
businessEvents?: Record<string, string>;
navigation?: Record<string, string>;
validators?: ValidationConfigItem[];
}
/**
* Service to access any given meta data for a Building Block page
* injected by BUILDING_BLOCK_META_DATA during the generation of a page.
* The service is used by Building Block Pages and needs access to the page-local
* BUILDING_BLOCK_META_DATA, hence we chose the any strategy to always create a local service instance.
* Working alternative would be injecting it directly into each of the page modules but this lazy approach
* supports tree shaking way better.
*/
@Injectable({
providedIn: 'any'
})
export class BuildingBlockMetaService {
private _metaData: BuildingBlockPageMetaData = {};
constructor(
@Optional()
@Inject(BUILDING_BLOCK_META_DATA)
metaData: BuildingBlockPageMetaData
) {
this._metaData = metaData;
}
get metaData() {
return this._metaData;
}
getData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
this.assertBuildingBlockExistence(buildingBlockId);
return this.metaData[buildingBlockId]?.[key];
}
hasData(buildingBlockId: string, key: keyof BuildingBlockMetaData) {
return this.hasMetaData(buildingBlockId) && !!this.metaData[buildingBlockId][key];
}
getResources(buildingBlockId: string) {
return this.getData(buildingBlockId, 'resources');
}
getBusinessEvents(buildingBlockId: string) {
return this.getData(buildingBlockId, 'businessEvents') as Record<string, BusinessEventConfig>;
}
getNavigationData(buildingBlockId: string) {
return (this.getData(buildingBlockId, 'navigation') ?? {}) as Record<
string,
BUILDING_BLOCK_NAVIGATION_TYPE
>;
}
hasResources(id: string) {
return this.hasMetaData(id) && !!this.metaData[id].resources;
}
hasMetaData(id: string) {
return Object.keys(this.metaData).includes(id);
}
assertBuildingBlockExistence(id: string) {
if (!this.hasMetaData(id)) {
throw new Error(`Could not find key ${id} in metadata list`);
}
}
}