Skip to content
Snippets Groups Projects
drop-list.component.ts 8.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • rhenck's avatar
    rhenck committed
      Component, Input
    
    } from '@angular/core';
    
    rhenck's avatar
    rhenck committed
      CdkDrag, CdkDragDrop, CdkDragStart, CdkDropList, moveItemInArray, transferArrayItem
    
    } from '@angular/cdk/drag-drop';
    
    import { DropListElement } from 'common/models/elements/input-elements/drop-list';
    
    rhenck's avatar
    rhenck committed
    import { DragNDropValueObject } from 'common/models/elements/element';
    
    import { FormElementComponent } from '../../directives/form-element-component.directive';
    
    rhenck's avatar
    rhenck committed
    
    @Component({
    
      selector: 'aspect-drop-list',
    
    rhenck's avatar
    rhenck committed
      template: `
    
        <!--         [style.min-height.px]="elementModel.position?.useMinHeight || clozeContext ? elementModel.height : undefined"-->
    
        <div class="list" [id]="elementModel.id"
    
    rhenck's avatar
    rhenck committed
             tabindex="0"
    
    rhenck's avatar
    rhenck committed
             [class.cloze-context]="clozeContext"
    
             [class.vertical-orientation]="elementModel.orientation === 'vertical'"
             [class.horizontal-orientation]="elementModel.orientation === 'horizontal'"
    
    rhenck's avatar
    rhenck committed
             [class.floating-orientation]="elementModel.orientation === 'flex'"
    
    rhenck's avatar
    rhenck committed
             [class.only-one-item]="elementModel.onlyOneItem"
    
             [class.highlight-receiver]="classReference.highlightReceivingDropList"
    
    rhenck's avatar
    rhenck committed
             cdkDropList
             [cdkDropListData]="this" [cdkDropListConnectedTo]="elementModel.connectedTo"
    
             [cdkDropListOrientation]="elementModel.orientation === 'vertical' ? 'vertical' : 'horizontal'"
    
    rhenck's avatar
    rhenck committed
             [cdkDropListEnterPredicate]="validDropPredicate"
    
    rhenck's avatar
    rhenck committed
             (cdkDropListDropped)="drop($event)"
    
             [style.color]="elementModel.styling.fontColor"
             [style.font-family]="elementModel.styling.font"
             [style.font-size.px]="elementModel.styling.fontSize"
             [style.font-weight]="elementModel.styling.bold ? 'bold' : ''"
             [style.font-style]="elementModel.styling.italic ? 'italic' : ''"
             [style.text-decoration]="elementModel.styling.underline ? 'underline' : ''"
             [style.backgroundColor]="elementModel.styling.backgroundColor"
    
             [style.border-color]="elementModel.highlightReceivingDropListColor"
    
             [class.errors]="elementFormControl.errors && elementFormControl.touched"
    
    rhenck's avatar
    rhenck committed
             (focusout)="elementFormControl.markAsTouched()">
    
          <ng-container *ngFor="let dropListValueElement of
    
    rhenck's avatar
    rhenck committed
          parentForm ? elementFormControl.value : elementModel.value; let index = index;">
    
            <div *ngIf="!dropListValueElement.imgSrc"
    
    rhenck's avatar
    rhenck committed
                 cdkDrag [cdkDragData]="dropListValueElement"
                 (cdkDragStarted)="dragStart($event)"
                 (cdkDragEnded)="dragEnd()"
    
                 [style.background-color]="elementModel.styling.itemBackgroundColor">
              <span>{{dropListValueElement.text}}</span>
    
              <div *cdkDragPlaceholder></div>
              <ng-template cdkDragPreview>
    
    rhenck's avatar
    rhenck committed
                <div [style.color]="elementModel.styling.fontColor"
                     [style.font-family]="elementModel.styling.font"
                     [style.font-size.px]="elementModel.styling.fontSize"
                     [style.font-weight]="elementModel.styling.bold ? 'bold' : ''"
                     [style.font-style]="elementModel.styling.italic ? 'italic' : ''"
                     [style.text-decoration]="elementModel.styling.underline ? 'underline' : ''"
                     [style.background-color]="elementModel.styling.itemBackgroundColor">
                  <span>{{dropListValueElement.text}}</span>
                </div>
              </ng-template>
    
            </div>
            <img *ngIf="dropListValueElement.imgSrc"
    
    rhenck's avatar
    rhenck committed
                 class="list-item" cdkDrag
                 [src]="dropListValueElement.imgSrc | safeResourceUrl" alt="Image Placeholder">
    
          </ng-container>
        </div>
        <mat-error *ngIf="elementFormControl.errors && elementFormControl.touched"
    
                   class="error-message" [style.bottom.px]="clozeContext ? -1 : 3"
                   [class.cloze-context-error-messag]="clozeContext">
    
          {{elementFormControl.errors | errorTransform: elementModel}}
        </mat-error>
    
    rhenck's avatar
    rhenck committed
      `,
      styles: [
    
        '.list {width: 100%; height: 100%; background-color: rgb(244, 244, 242); border-radius: 5px;}',
    
    rhenck's avatar
    rhenck committed
        '.list {display: flex; gap: 5px; box-sizing: border-box; padding: 5px}',
        '.list.vertical-orientation {flex-direction: column;}',
        '.list.horizontal-orientation {flex-direction: row;}',
        '.list.floating-orientation {place-content: center space-around; align-items: center; flex-flow: row wrap;}',
    
    rhenck's avatar
    rhenck committed
        '.cloze-context.list {padding: 0;}',
    
    rhenck's avatar
    rhenck committed
        '.list-item {border-radius: 5px;}',
    
        ':not(.cloze-context) .list-item {padding: 10px;}',
    
    rhenck's avatar
    rhenck committed
        '.cloze-context .list-item {padding: 0 5px;}',
    
    rhenck's avatar
    rhenck committed
        '.cloze-context .list-item span {margin-bottom: 3px;}',
        '.only-one-item .list-item {height: 100%; display: flex; align-items: center; justify-content: center;}',
    
    rhenck's avatar
    rhenck committed
        'img.list-item {align-self: start;}',
    
    rhenck's avatar
    rhenck committed
        '.errors {border: 2px solid #f44336 !important;}',
        '.error-message {font-size: 75%; margin-top: 10px; margin-left: 5px; position: absolute; pointer-events: none;}',
        '.cloze-context-error-message {padding: 0 !important;}',
        '.list-item:active {cursor: grabbing;}',
    
        '.cdk-drag-preview {border-radius: 5px; box-shadow: 2px 2px 5px black; padding: 10px;}',
    
    rhenck's avatar
    rhenck committed
        '.cdk-drop-list-dragging .cdk-drag {transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);}',
    
        '.highlight-receiver.cdk-drop-list-receiving {padding: 3px; border: 2px solid;}',
    
    rhenck's avatar
    rhenck committed
        '.cdk-drag-placeholder {background: #ccc; border: dotted 3px #999; min-height: 25px; min-width: 25px;}',
    
        '.cdk-drag-placeholder {transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);}'
    
    rhenck's avatar
    rhenck committed
      ]
    })
    
    rhenck's avatar
    rhenck committed
    export class DropListComponent extends FormElementComponent {
    
      @Input() elementModel!: DropListElement;
    
    rhenck's avatar
    rhenck committed
      @Input() clozeContext: boolean = false;
    
    
      classReference = DropListComponent;
      static highlightReceivingDropList = false;
    
    rhenck's avatar
    rhenck committed
      dragStart(event: CdkDragStart) {
        DropListComponent.setHighlighting(event.source.dropContainer.data.elementModel.highlightReceivingDropList);
        document.body.classList.add('dragging-active');
      }
    
      dragEnd() {
        DropListComponent.setHighlighting(false);
        document.body.classList.remove('dragging-active');
      }
    
      static setHighlighting(showHighlight: boolean) {
        DropListComponent.highlightReceivingDropList = showHighlight;
      }
    
    rhenck's avatar
    rhenck committed
      drop(event: CdkDragDrop<any>) {
    
    rhenck's avatar
    rhenck committed
        if (DropListComponent.isReorderDrop(event)) {
    
          moveItemInArray(event.container.data.elementFormControl.value, event.previousIndex, event.currentIndex);
    
    rhenck's avatar
    rhenck committed
          event.container.data.updateFormvalue();
    
    rhenck's avatar
    rhenck committed
        } else if (DropListComponent.isCopyDrop(event)) {
    
          event.container.data.elementFormControl.value.push(
            event.previousContainer.data.elementFormControl.value[event.previousIndex]);
    
    rhenck's avatar
    rhenck committed
          event.container.data.updateFormvalue();
        } else if (DropListComponent.isPutBack(event)) {
    
          event.previousContainer.data.elementFormControl.value.splice(event.previousIndex, 1);
    
    rhenck's avatar
    rhenck committed
          event.previousContainer.data.updateFormvalue();
    
    rhenck's avatar
    rhenck committed
          transferArrayItem(
    
            event.previousContainer.data.elementFormControl.value,
            event.container.data.elementFormControl.value,
    
    rhenck's avatar
    rhenck committed
            event.previousIndex,
            event.currentIndex
    
    rhenck's avatar
    rhenck committed
          event.previousContainer.data.updateFormvalue();
          event.container.data.updateFormvalue();
    
    rhenck's avatar
    rhenck committed
      /* Move element within the same list to a new index position. */
    
    rhenck's avatar
    rhenck committed
      static isReorderDrop(event: CdkDragDrop<any>): boolean {
        return event.previousContainer === event.container;
      }
    
      static isCopyDrop(event: CdkDragDrop<any>): boolean {
        return event.previousContainer.data.elementModel.copyOnDrop;
      }
    
    
    rhenck's avatar
    rhenck committed
      /* Put a copied element back to the source list. */
    
    rhenck's avatar
    rhenck committed
      static isPutBack(event: CdkDragDrop<any>): boolean {
        return event.container.data.elementModel.copyOnDrop &&
    
          DropListComponent.isItemIDAlreadyPresent(event.item.data.id, event.container.data.elementFormControl.value);
    
    rhenck's avatar
    rhenck committed
      updateFormvalue(): void {
    
        this.elementFormControl.setValue(this.elementFormControl.value);
    
    rhenck's avatar
    rhenck committed
      validDropPredicate = (drag: CdkDrag, drop: CdkDropList): boolean => {
    
        return (!drop.data.elementModel.onlyOneItem || drop.data.elementFormControl.value.length < 1);// &&
    
    rhenck's avatar
    rhenck committed
          // (!DropListComponent.isItemIDAlreadyPresent(drag.data.id, drop.data.value));
      };
    
    rhenck's avatar
    rhenck committed
      // isIDPresentAndNoReturning(): boolean {
      //   return DropListComponent.isItemIDAlreadyPresent(DropListComponent.draggedElement?.id as string, this.elementFormControl.value) &&
      //     !(this.elementModel.deleteDroppedItemWithSameID);
      // }
      //
      // isOnlyOneItemAndNoReplacingOrReturning(): boolean {
      //   return this.elementModel.onlyOneItem && this.viewModel.length > 0 &&
      //     !((this.viewModel[0].returnToOriginOnReplacement && !this.elementModel.isSortList) ||
      //       (this.elementModel.deleteDroppedItemWithSameID &&
      //         DropListComponent.draggedElement?.id === this.viewModel[0].id));
      // }
    
      static isItemIDAlreadyPresent(itemID: string, valueList: DragNDropValueObject[]): boolean {
        const listValueIDs = valueList.map((valueValue: DragNDropValueObject) => valueValue.id);
        return listValueIDs.includes(itemID);
    
    rhenck's avatar
    rhenck committed
    }