File

projects/i-components/src/lib/directives/draggable.directive.ts

Description

Directive to allow Draggable behavior on items

Implements

OnInit

Metadata

Selector [wmDraggable]

Index

Properties
Methods
Inputs
Outputs
HostBindings
HostListeners

Constructor

constructor(elementRef: ElementRef, dragDropService: DragDropService)

Creates an instance of DraggableDirective

Parameters :
Name Type Optional
elementRef ElementRef No
dragDropService DragDropService No

Inputs

wmDraggable
Type : literal type

Pair of values representing the target node and desire drop position

Outputs

dragEnd
Type : EventEmitter

Events that get triggered when dragend event is called

dragStart
Type : EventEmitter

Events that get triggered when dragstart event is called

HostBindings

class.draggable
Type : boolean
Default value : true

Default class applied to elements that use this directive

class.dragging
Type : boolean
Default value : false

Default class applied to elements that are being dragged

HostListeners

dragend
Arguments : '$event'

Item drag ended event handler

Parameters :
Name Optional
event No
dragover
Arguments : '$event'

Item drag over event handler, its called a lot of times

Parameters :
Name Optional
event No
dragstart
Arguments : '$event'

Item drag started event handler

Parameters :
Name Optional
event No

Methods

ngOnInit
ngOnInit()

Angular lifecycle hook

Returns : void
onDragEnd
onDragEnd(event: DragEvent)
Decorators :
@HostListener('dragend', ['$event'])

Item drag ended event handler

Parameters :
Name Type Optional
event DragEvent No
Returns : void
onDragOver
onDragOver(event: DragEvent)
Decorators :
@HostListener('dragover', ['$event'])

Item drag over event handler, its called a lot of times

Parameters :
Name Type Optional
event DragEvent No
Returns : void
onDragStart
onDragStart(event: DragEvent)
Decorators :
@HostListener('dragstart', ['$event'])

Item drag started event handler

Parameters :
Name Type Optional
event DragEvent No
Returns : void

Properties

dragEnd
Default value : new EventEmitter<DragEvent>()
Decorators :
@Output()

Events that get triggered when dragend event is called

draggable
Default value : true
Decorators :
@HostBinding('class.draggable')

Default class applied to elements that use this directive

dragging
Default value : false
Decorators :
@HostBinding('class.dragging')

Default class applied to elements that are being dragged

dragStart
Default value : new EventEmitter<DragEvent>()
Decorators :
@Output()

Events that get triggered when dragstart event is called

Public wmDraggable
Type : literal type
Decorators :
@Input()

Pair of values representing the target node and desire drop position

import {
  Directive,
  ElementRef,
  EventEmitter,
  HostBinding,
  HostListener,
  Input,
  OnInit,
  Output,
} from '@angular/core';
import { DragDropService } from '../services/drag-drop.service';

/**
 * Directive to allow Draggable behavior on items
 *
 * @export
 * @class ContainerDirective
 */
@Directive({
  selector: '[wmDraggable]',
})
export class DraggableDirective implements OnInit {
  /**
   * Default class applied to elements that use this directive
   *
   * @type {boolean}
   * @memberof DraggableDirective
   */
  @HostBinding('class.draggable') draggable = true;

  /**
   * Default class applied to elements that are being dragged
   *
   * @type {boolean}
   * @memberof DraggableDirective
   */
  @HostBinding('class.dragging') dragging = false;

  /**
   * Pair of values representing the target node and desire drop position
   *
   * @type {[any, boolean]}
   * @memberof DraggableDirective
   */
  @Input() public wmDraggable: { source: any; draggable: boolean };

  /**
   * Events that get triggered when dragstart event is called
   *
   * @type {EventEmitter<DragEvent>}
   * @memberof DraggableDirective
   */
  @Output() dragStart = new EventEmitter<DragEvent>();

  /**
   * Events that get triggered when dragend event is called
   *
   * @type {EventEmitter<DragEvent>}
   * @memberof DraggableDirective
   */
  @Output() dragEnd = new EventEmitter<DragEvent>();

  /**
   * Creates an instance of DraggableDirective
   *
   * @param {ElementRef} elementRef
   * @param {DragDropService} dragDropService
   * @memberof DraggableDirective
   */
  constructor(
    private elementRef: ElementRef,
    private dragDropService: DragDropService
  ) {}

  /**
   * Angular lifecycle hook
   *
   * @memberof DraggableDirective
   */
  ngOnInit(): void {
    this.draggable = this.wmDraggable.draggable;
    this.elementRef.nativeElement.draggable = this.draggable;
  }

  /**
   * Item drag started event handler
   *
   * @param {DragEvent} event
   * @memberof DraggableDirective
   */
  @HostListener('dragstart', ['$event'])
  onDragStart(event: DragEvent): void {
    if (!this.draggable) return;
    event.stopPropagation();
    this.dragging = true;
    this.dragDropService.currentDragging = this;
    const customEvent: any = event;
    customEvent.dragData = this.wmDraggable;
    this.dragStart.emit(customEvent);
  }

  /**
   * Item drag ended event handler
   *
   * @param {DragEvent} event
   * @memberof DraggableDirective
   */
  @HostListener('dragend', ['$event'])
  onDragEnd(event: DragEvent): void {
    if (!this.dragging) return;
    this.dragging = false;
    this.dragDropService.currentDragging = null;
    this.dragEnd.emit(event);
  }

  /**
   * Item drag over event handler, its called a lot of times
   *
   * @param {DragEvent} event
   * @memberof DraggableDirective
   */
  @HostListener('dragover', ['$event'])
  onDragOver(event: DragEvent): void {
    event.preventDefault();
    event.dataTransfer.dropEffect = 'copy';
  }
}

result-matching ""

    No results matching ""