Last active
May 24, 2016 13:21
-
-
Save mithun-daa/bf857ed48b50a24a1c528385e7cb09e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Directive({ | |
selector: '[makeDroppable]' | |
}) | |
export class MakeDroppable implements OnInit { | |
@Output() dropped: EventEmitter<any> = new EventEmitter(); | |
constructor(private _elementRef: ElementRef) {} | |
ngOnInit() { | |
let el = this._elementRef.nativeElement; | |
// Add a style to indicate that this element is a drop target | |
el.addEventListener('dragenter', (e) => { | |
el.classList.add('over'); | |
}); | |
// Remove the style | |
el.addEventListener('dragleave', (e) => { | |
el.classList.remove('over'); | |
}); | |
el.addEventListener('dragover', (e) => { | |
if (e.preventDefault) { | |
e.preventDefault(); | |
} | |
e.dataTransfer.dropEffect = 'move'; | |
return false; | |
}); | |
// On drop, get the data and convert it back to a JSON object | |
// and fire off an event passing the data | |
el.addEventListener('drop', (e) => { | |
if (e.stopPropagation) { | |
e.stopPropagation(); // Stops some browsers from redirecting. | |
} | |
el.classList.remove('over'); | |
let data = JSON.parse(e.dataTransfer.getData('text')); | |
this.dropped.emit(data); | |
return false; | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment