Newer
Older
export class IDManager {
private static instance: IDManager;
private idCounter: Record<string, number> = {
checkbox: 0,
dropdown: 0,
radio: 0,
image: 0,
audio: 0,
video: 0,
static getInstance() {
return this.instance || (this.instance = new this());
}
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]}`));
return `${type}_${this.idCounter[type]}`;
}
addID(id: string): void {
this.givenIDs.push(id);
}
isIdAvailable(value: string): boolean {
return !this.givenIDs.includes(value);
}
/* Remove ID from givenIDs, so it can be used again. */
this.givenIDs.splice(this.givenIDs.indexOf(id, 0), 1);
reset(): void {
Object.keys(this.idCounter).forEach(counter => {
this.idCounter[counter] = 0;
});
this.givenIDs = [];
}