Skip to content
Snippets Groups Projects
id-manager.ts 1.34 KiB
Newer Older
  • Learn to ignore specific revisions
  • rhenck's avatar
    rhenck committed
    export class IDManager {
      private static instance: IDManager;
    
    rhenck's avatar
    rhenck committed
      givenIDs: string[] = [];
    
      private idCounter: Record<string, number> = {
    
        text: 0,
    
        button: 0,
        'text-field': 0,
    
        'text-field-simple': 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,
    
        'drop-list-simple': 0,
    
    rhenck's avatar
    rhenck committed
        frame: 0,
    
    rhenck's avatar
    rhenck committed
        'toggle-button': 0,
    
    rhenck's avatar
    rhenck committed
      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;
    
    rhenck's avatar
    rhenck committed
        } 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);
      }
    
    
    rhenck's avatar
    rhenck committed
      /* Remove ID from givenIDs, so it can be used again. */
    
      removeId(id: string): void {
    
        this.givenIDs.splice(this.givenIDs.indexOf(id, 0), 1);
    
        Object.keys(this.idCounter).forEach(counter => {
          this.idCounter[counter] = 0;
        });