Skip to content

Instantly share code, notes, and snippets.

@mithun-daa
Last active May 24, 2016 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithun-daa/e229d144ac5369742740663d4e804cdd to your computer and use it in GitHub Desktop.
Save mithun-daa/e229d144ac5369742740663d4e804cdd to your computer and use it in GitHub Desktop.
@Directive({
selector: '[makeDraggable]'
})
export class MakeDraggable {
@Input('makeDraggable') data: any;
constructor(private _elementRef: ElementRef) {}
ngOnInit() {
// Get the current element
let el = this._elementRef.nativeElement.querySelector('li');
// 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', JSON.stringify(this.data));
});
// Remove the drag-src class
el.addEventListener('dragend', (e) => {
e.preventDefault();
el.classList.remove('drag-src')
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment