Skip to content
Snippets Groups Projects
id.service.ts 1.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • export class IdService {
    
      private static instance: IdService;
    
    
      private givenIDs: string[] = [];
      private idCounter: Record<string, number> = {
    
        text: 0,
    
        button: 0,
        'text-field': 0,
    
        'number-field': 0,
    
        'text-area': 0,
    
        checkbox: 0,
        dropdown: 0,
        radio: 0,
        image: 0,
        audio: 0,
        video: 0,
    
        likert: 0,
    
        likert_row: 0,
    
    mechtelm's avatar
    mechtelm committed
        slider: 0,
    
        'spell-correct': 0,
    
    rhenck's avatar
    rhenck committed
        'radio-group-images': 0,
    
    rhenck's avatar
    rhenck committed
        'drop-list': 0,
    
    rhenck's avatar
    rhenck committed
        frame: 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);
      }
    
    
    rhenck's avatar
    rhenck committed
      /* Remove ID from givenIDs, so it can be used again. */
    
      removeId(id: string): void {
        const index = this.givenIDs.indexOf(id, 0);
        if (index > -1) {
          this.givenIDs.splice(index, 1);
        }
      }
    }