-
Notifications
You must be signed in to change notification settings - Fork 146
/
make-draggable.directive.ts
37 lines (32 loc) · 1.15 KB
/
make-draggable.directive.ts
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
import {Directive, Input, ElementRef} from '@angular/core';
import {StoreModel} from "../../models/StoreModel";
@Directive({
selector: '[makeDraggable]'
})
export class MakeDraggable {
@Input('makeDraggable') data:StoreModel;
constructor(private _elementRef:ElementRef) {
}
ngOnInit() {
// Get the current element
let el = this._elementRef.nativeElement.querySelector('div');
if (!el)
return
// Set the draggable attribute to the element
el.draggable = 'true';
// Set up the dragstart event and add the drag-src CSS class
// to change the visual appearance. Set the current todo as the data
// payload by stringifying the object first
el.addEventListener('dragstart', (e) => {
console.log('Start');
el.classList.add('drag-src')
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text', this.data.getKey('modelId'));
});
// Remove the drag-src class
el.addEventListener('dragend', (e) => {
e.preventDefault();
el.classList.remove('drag-src')
});
}
}