All files / src/lib/common/drag-drop draggable.directive.ts

100% Statements 19/19
100% Branches 2/2
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 381x               1x         1x 1x 1x     6x       1x 3x   3x 1x 1x         1x 1x 1x   1x  
import {Directive, Input, HostBinding, HostListener} from '@angular/core';
 
/**
 * @description A wrapper around the HTML 5 Drag API.
 */
@Directive({
    selector: '[anjDraggable]'
})
export class DraggableDirective {
 
    /**
     * @description Binds to arbitrary data to be passed via anjDroppable's onDrop event emitter.
     */
    @Input() public anjDraggable: any;
    @HostBinding('attr.draggable') public readonly isDraggable: boolean;
    @HostBinding('class.dragging') public isDragging: boolean;
 
    constructor() {
        this.isDraggable = true;
    }
 
    @HostListener('dragstart', ['$event'])
    public handleDragStart(event: DragEvent) {
        this.isDragging = true;
 
        if (event.dataTransfer) {
            event.dataTransfer.effectAllowed = 'move';
            event.dataTransfer.setData('data', JSON.stringify(this.anjDraggable));
        }
    }
 
    @HostListener('dragend', ['$event'])
    public handleDragEnd(event: DragEvent) {
        event.preventDefault();
        this.isDragging = false;
    }
}