Skip to content
Snippets Groups Projects
drop-list.ts 2.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Type } from '@angular/core';
    
    rhenck's avatar
    rhenck committed
      InputElement,
    
      BasicStyles, PositionProperties,
    
    rhenck's avatar
    rhenck committed
      AnswerScheme, AnswerSchemeValue, UIElement
    
    } from 'common/models/elements/element';
    
    import { ElementComponent } from 'common/directives/element-component.directive';
    import { DropListComponent } from 'common/components/input-elements/drop-list.component';
    
    
    rhenck's avatar
    rhenck committed
    export class DropListElement extends InputElement {
    
      value: DragNDropValueObject[];
    
      onlyOneItem: boolean = false;
    
    rhenck's avatar
    rhenck committed
      isSortList: boolean = false;
    
      connectedTo: string[] = [];
      copyOnDrop: boolean = false;
      orientation: 'vertical' | 'horizontal' | 'flex' = 'vertical';
      highlightReceivingDropList: boolean = false;
      highlightReceivingDropListColor: string = '#006064';
    
    rhenck's avatar
    rhenck committed
      position: PositionProperties | undefined;
    
      styling: BasicStyles & {
        itemBackgroundColor: string;
      };
    
    
    rhenck's avatar
    rhenck committed
      constructor(element: Partial<DropListElement>) {
        super({ height: 100, ...element });
    
    rhenck's avatar
    rhenck committed
        this.value = element.value !== undefined ? [...element.value] : [];
    
    rhenck's avatar
    rhenck committed
        if (element.onlyOneItem) this.onlyOneItem = element.onlyOneItem;
    
    rhenck's avatar
    rhenck committed
        if (element.isSortList) this.isSortList = element.isSortList;
    
    rhenck's avatar
    rhenck committed
        if (element.connectedTo) this.connectedTo = element.connectedTo;
        if (element.copyOnDrop) this.copyOnDrop = element.copyOnDrop;
        if (element.orientation) this.orientation = element.orientation;
        if (element.highlightReceivingDropList) this.highlightReceivingDropList = element.highlightReceivingDropList;
    
    rhenck's avatar
    rhenck committed
        if (element.highlightReceivingDropListColor) {
          this.highlightReceivingDropListColor = element.highlightReceivingDropListColor;
        }
    
    rhenck's avatar
    rhenck committed
        this.position = element.position ?
          UIElement.initPositionProps({ useMinHeight: true, ...element.position as Partial<PositionProperties> }) :
          undefined;
        this.styling = UIElement.initStylingProps({
          backgroundColor: '#f4f4f2',
          itemBackgroundColor: '#c9e0e0',
          ...element.styling
        });
    
      hasAnswerScheme(): boolean {
    
        return Boolean(this.getAnswerScheme);
    
    rhenck's avatar
    rhenck committed
      getAnswerScheme(options: Array<DropListElement>): AnswerScheme {
    
        return {
          id: this.id,
          type: 'string',
          format: '',
          multiple: true,
          nullable: false,
    
          values: this.getAnswerSchemeValues(options),
    
    rhenck's avatar
    rhenck committed
      private getAnswerSchemeValues(dropLists: Array<DropListElement>): AnswerSchemeValue[] {
    
        const valueDropLists = dropLists.filter(dropList => dropList.connectedTo.includes(this.id));
    
        if (valueDropLists.length || this.isSortingList()) {
    
          return [this, ...valueDropLists]
            .map(dropList => dropList.value as DragNDropValueObject[])
            .flat()
    
            .map(option => ({ value: option.id, label: option.text as string }));
    
        return [];
      }
    
      private isSortingList(): boolean {
        return (!this.connectedTo.length && (this.value as DragNDropValueObject[]).length > 1);
    
      getElementComponent(): Type<ElementComponent> {
    
        return DropListComponent;
      }
    }