projects/i-components/src/lib/directives/draggable.directive.ts
Directive to allow Draggable behavior on items
Selector | [wmDraggable] |
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
HostListeners |
constructor(elementRef: ElementRef, dragDropService: DragDropService)
|
|||||||||
Creates an instance of DraggableDirective
Parameters :
|
wmDraggable | |
Type : literal type
|
|
Pair of values representing the target node and desire drop position |
dragEnd | |
Type : EventEmitter
|
|
Events that get triggered when dragend event is called |
dragStart | |
Type : EventEmitter
|
|
Events that get triggered when dragstart event is called |
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 |
dragend | ||||
Arguments : '$event'
|
||||
Item drag ended event handler
Parameters :
|
dragover | ||||
Arguments : '$event'
|
||||
Item drag over event handler, its called a lot of times
Parameters :
|
dragstart | ||||
Arguments : '$event'
|
||||
Item drag started event handler
Parameters :
|
ngOnInit |
ngOnInit()
|
Angular lifecycle hook
Returns :
void
|
onDragEnd | ||||||
onDragEnd(event: DragEvent)
|
||||||
Decorators :
@HostListener('dragend', ['$event'])
|
||||||
Item drag ended event handler
Parameters :
Returns :
void
|
onDragOver | ||||||
onDragOver(event: DragEvent)
|
||||||
Decorators :
@HostListener('dragover', ['$event'])
|
||||||
Item drag over event handler, its called a lot of times
Parameters :
Returns :
void
|
onDragStart | ||||||
onDragStart(event: DragEvent)
|
||||||
Decorators :
@HostListener('dragstart', ['$event'])
|
||||||
Item drag started event handler
Parameters :
Returns :
void
|
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';
}
}