JSFiddle - React, Tailwind, and code Playground
HTML
div(ng-controller="MainController")
div.med-list-container(ng-model="data")
h1 Bob, these are your current medications:
div.col-md-5#medList
div.single-med(ng-repeat="med in data")
h4(draggable="true" is-draggable med="med").med-title {{med.Medication}}
div.col-md-2#space
div.col-md-5#schedule
div
h3 Morning <i class="icon-cloud-sun"></i>
div#morning-container(draggable="true" ng-repeat="meds in morningMeds" drop-target="true" )
div
h3 Afternoon <i class="icon-sun"></i>
div#afternoon-container(draggable="true" ng-repeat="meds in afternoonMeds" drop-target="true")
div
h3 Evening <i class="icon-moon"></i>
div#evening-container(draggable="true" ng-repeat="meds in eveningMeds" drop-target="true")
JavaScript
angular.module('pillboxApp.directive')
.directive('isDraggable', function($rootScope) {
return {
restrict: "A",
scope: {
med:"="
},
link: function(scope, element, attrs) {
// dragstart event
// stores scope data as json string format to send
element.bind('dragstart', function(e) {
var sendData = angular.toJson(scope.med);
// console.log(sendData);
e.dataTransfer.setData('Text', sendData);
$rootScope.$emit('ANGULAR_DRAG_START');
})
element.bind('dragover', function(e) {
$rootScope.$emit('ANGULAR_DRAG_END');
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = "copy";
return false;
})
}
}
});
angular.module('pillboxApp.directive')
.directive('dropTarget', function($parse, $rootScope) {
return {
restrict: "A",
scope: {
onDrop: "&"
},
link: function(scope, element, attrs) {
function onDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
if (e.stopPropagation) {
e.stopPropagation();
}
e.dataTransfer.dropEffect = "move";
return false;
}
function onDrop(e) {
if(e.preventDefault) {
e.preventDefault();
}
if(e.stopPropagation) {
e.stopPropagation();
}
var data = e.dataTransfer.getData('text');
data = angular.fromJson(data);
var fn = $parse(attrs.dropTarget);
scope.$apply(function() {
fn(scope, {$data: data, $event: e});
})
console.log(data);
}
$rootScope.$on('ANGULAR_DRAG_START', function() {
element.bind('dragover', onDragOver);
element.bind('drop', onDrop);
})
$rootScope.$on('ANGULAR_DRAG_END', function() {
element.unbind('dragover', onDragOver);
element.unbind('drop', onDrop);
})
}
}
})