Skip to content
Snippets Groups Projects
element-model-properties.component.ts 3.16 KiB
Newer Older
rhenck's avatar
rhenck committed
import {
  Component, EventEmitter,
rhenck's avatar
rhenck committed
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
rhenck's avatar
rhenck committed
import { CdkDragDrop } from '@angular/cdk/drag-drop/drag-events';
import { moveItemInArray } from '@angular/cdk/drag-drop';
import {
jojohoch's avatar
jojohoch committed
  InputElementValue, TextLabel, TextImageLabel, UIElement, Hotspot
} from 'common/models/elements/element';
import { LikertRowElement } from 'common/models/elements/compound-elements/likert/likert-row';
import { FileService } from 'common/services/file.service';
import { CombinedProperties } from 'editor/src/app/components/properties-panel/element-properties-panel.component';
jojohoch's avatar
jojohoch committed
import { firstValueFrom } from 'rxjs';
import { UnitService } from '../../../services/unit.service';
import { SelectionService } from '../../../services/selection.service';
import { DialogService } from '../../../services/dialog.service';
rhenck's avatar
rhenck committed

@Component({
  selector: 'aspect-element-model-properties-component',
rhenck's avatar
rhenck committed
  templateUrl: './element-model-properties.component.html',
  styleUrls: ['./element-model-properties.component.css']
})
export class ElementModelPropertiesComponent {
  @Input() combinedProperties!: CombinedProperties;
rhenck's avatar
rhenck committed
  @Input() selectedElements: UIElement[] = [];
  @Output() updateModel = new EventEmitter<{
    property: string;
jojohoch's avatar
jojohoch committed
    value: InputElementValue | TextImageLabel[] | LikertRowElement[] | TextLabel[] | Hotspot[]
rhenck's avatar
rhenck committed
    isInputValid?: boolean | null
  }>();

  constructor(public unitService: UnitService,
              public selectionService: SelectionService,
              public dialogService: DialogService,
              public sanitizer: DomSanitizer) { }
  addListValue(property: string, value: string): void {
    this.updateModel.emit({
      property: property,
      value: [...(this.combinedProperties[property] as string[]), value]
  moveListValue(property: string, event: CdkDragDrop<string[]>): void {
rhenck's avatar
rhenck committed
    moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
    this.updateModel.emit({ property: property, value: event.container.data });
  }

  async editTextOption(property: string, optionIndex: number): Promise<void> {
    const oldOptions = this.selectionService.getSelectedElements()[0][property] as string[];
    await this.dialogService.showTextEditDialog(oldOptions[optionIndex])
      .subscribe((result: string) => {
        if (result) {
          oldOptions[optionIndex] = result;
          this.updateModel.emit({ property, value: oldOptions });
        }
      });
  async changeMediaSrc(elementType: string) {
    let mediaSrc = '';
    switch (elementType) {
      case 'image':
        mediaSrc = await FileService.loadImage();
        break;
      case 'audio':
        mediaSrc = await FileService.loadAudio();
        break;
      case 'video':
        mediaSrc = await FileService.loadVideo();
        break;
      // no default
    }
    this.updateModel.emit({ property: 'src', value: mediaSrc });
  }

  async showGeogebraAppDefDialog() {
    const appDefinition = await firstValueFrom(this.dialogService.showGeogebraAppDefinitionDialog());
    if (appDefinition) this.updateModel.emit({ property: 'appDefinition', value: appDefinition });
  }
rhenck's avatar
rhenck committed
}