Newer
Older
private static instance: IdService;
private givenIDs: string[] = [];
private idCounter: Record<string, number> = {
checkbox: 0,
dropdown: 0,
radio: 0,
image: 0,
audio: 0,
video: 0,
static getInstance(): IdService {
if (!IdService.instance) {
IdService.instance = new IdService();
}
return IdService.instance;
}
getNewID(type: string): string {
if (!type) {
throw Error('ID-Service: No type given!');
}
do {
this.idCounter[type] += 1;
}
while (!this.isIdAvailable(`${type}_${this.idCounter[type]}`));
this.givenIDs.push(`${type}_${this.idCounter[type]}`);
return `${type}_${this.idCounter[type]}`;
}
addID(id: string): void {
this.givenIDs.push(id);
}
isIdAvailable(value: string): boolean {
return !this.givenIDs.includes(value);
}
addId(id: string): void {
this.givenIDs.push(id);
}
/* Remove ID from givenIDs, so it can be used again. */