Skip to content
Snippets Groups Projects
element-model-element-code-mapping.service.ts 3.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Injectable } from '@angular/core';
    
    import {
    
      InputElement,
      InputElementValue,
      UIElement,
      UIElementType
    } from 'common/models/elements/element';
    import { TextElement } from 'common/models/elements/text/text';
    import { AudioElement } from 'common/models/elements/media-elements/audio';
    import { VideoElement } from 'common/models/elements/media-elements/video';
    import { ImageElement } from 'common/models/elements/media-elements/image';
    
    import { GeometryElement } from 'common/models/elements/geometry/geometry';
    
    import { Hotspot, HotspotImageElement } from 'common/models/elements/input-elements/hotspot-image';
    import { DragNDropValueObject } from 'common/models/elements/label-interfaces';
    
    import { TextMarkingService } from './text-marking.service';
    
    
    @Injectable({
      providedIn: 'root'
    })
    
    
    export class ElementModelElementCodeMappingService {
    
      dragNDropValueObjects: DragNDropValueObject[] = [];
    
      mapToElementModelValue(elementCodeValue: InputElementValue | undefined, elementModel: UIElement): InputElementValue {
    
        switch (elementModel.type) {
    
            return (elementCodeValue !== undefined) ?
              (elementCodeValue as string[]).map(id => this.getDragNDropValueObjectById(id)) as DragNDropValueObject[] :
    
              (elementModel as InputElement).value;
    
    jojohoch's avatar
    jojohoch committed
          case 'hotspot-image':
            return (elementCodeValue !== undefined) ?
              (elementCodeValue as boolean[])
                .map((v, i) => ({ ...(elementModel as HotspotImageElement).value[i], value: v })) :
              (elementModel as HotspotImageElement).value;
    
            return (elementCodeValue !== undefined) ?
    
              TextMarkingService
                .restoreMarkedTextIndices(elementCodeValue as string[], (elementModel as TextElement).text) :
    
              (elementModel as TextElement).text;
    
            return elementCodeValue !== undefined ?
              elementCodeValue as number :
    
              (elementModel as AudioElement).player.playbackTime;
    
            return elementCodeValue !== undefined ?
              elementCodeValue as number :
    
              (elementModel as VideoElement).player.playbackTime;
    
            return elementCodeValue !== undefined ?
              elementCodeValue as boolean :
    
              (elementModel as ImageElement).magnifierUsed;
    
          case 'radio':
          case 'radio-group-images':
          case 'dropdown':
          case 'toggle-button':
          case 'likert-row':
    
            return elementCodeValue !== undefined && elementCodeValue !== null ?
              elementCodeValue as number - 1 : (elementModel as InputElement).value;
    
          case 'geometry':
            return elementCodeValue !== undefined ? elementCodeValue : (elementModel as GeometryElement).appDefinition;
    
            return elementCodeValue !== undefined ? elementCodeValue : (elementModel as InputElement).value;
    
      static mapToElementCodeValue(elementModelValue: InputElementValue, elementType: UIElementType): InputElementValue {
    
          case 'drop-list':
    
            return (elementModelValue as DragNDropValueObject[]).map(object => object.id);
    
    jojohoch's avatar
    jojohoch committed
          case 'hotspot-image':
            return (elementModelValue as Hotspot[]).map(hotspot => hotspot.value);
    
            return TextMarkingService.getMarkedTextIndices(elementModelValue as string);
    
          case 'radio':
          case 'radio-group-images':
          case 'dropdown':
          case 'toggle-button':
          case 'likert-row':
    
            return elementModelValue !== null ? elementModelValue as number + 1 : null;
    
      private getDragNDropValueObjectById(id: string): DragNDropValueObject | undefined {
        return this.dragNDropValueObjects.find(dropListValue => dropListValue.id === id);