2019-04-23 06:29:23 +00:00
|
|
|
import ngModule from '../module';
|
2019-09-30 12:57:54 +00:00
|
|
|
import './droppable.scss';
|
2019-04-23 06:29:23 +00:00
|
|
|
|
|
|
|
export function directive($parse) {
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function($scope, $element, $attrs) {
|
|
|
|
const element = $element[0];
|
|
|
|
const onDropEvent = $parse($attrs.onDrop);
|
2019-04-29 09:49:43 +00:00
|
|
|
const isDroppable = $attrs.vnDroppable === 'true';
|
|
|
|
|
|
|
|
if (!isDroppable) return;
|
2019-04-23 06:29:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Captures current dragging element
|
|
|
|
*/
|
|
|
|
element.addEventListener('dragstart', () => {
|
|
|
|
this.dragged = element;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter droppable area event
|
|
|
|
*/
|
|
|
|
element.addEventListener('dragenter', event => {
|
2019-09-30 12:57:54 +00:00
|
|
|
element.classList.add('active');
|
2019-04-23 06:29:23 +00:00
|
|
|
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exit droppable area event
|
|
|
|
*/
|
|
|
|
element.addEventListener('dragleave', event => {
|
2019-09-30 12:57:54 +00:00
|
|
|
element.classList.remove('active');
|
2019-04-23 06:29:23 +00:00
|
|
|
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent dragover for allowing
|
|
|
|
* dispatch drop event
|
|
|
|
*/
|
|
|
|
element.addEventListener('dragover', event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires when a drop events
|
|
|
|
*/
|
|
|
|
element.addEventListener('drop', event => {
|
2019-09-30 12:57:54 +00:00
|
|
|
element.classList.remove('active');
|
2019-04-23 06:29:23 +00:00
|
|
|
|
2019-09-30 12:57:54 +00:00
|
|
|
onDropEvent($scope, {event});
|
2019-04-23 06:29:23 +00:00
|
|
|
|
2019-09-30 12:57:54 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
2019-04-23 06:29:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
directive.$inject = ['$parse'];
|
|
|
|
|
|
|
|
ngModule.directive('vnDroppable', directive);
|