Drag and Drop with Angular

In my last post, I implemented drag and drop with clone using JQuery. Now, it was time to integrate it into an application I am building, and thus had to “angularize” the implementation.

There are many drag and drop implementations on Angular out there.

The Angular docs include a simple example on directives that add/handle event listeners to implement dragging. Unfortunately, it is pretty rudimentary and it does not support cloning the draggable.

This one by Ben Parker builds from scratch on top of angular without using JQuery UI. It is an interesting approach but it is more pedagogical than anything else since it misses out on all the goodies provided by JQuery UI and its plugins.

This one by Amit Gharat combines the power of JQuery UI and Angular declarative clarity. However, it was too constraining for my needs. To use it, the drag source and drop target both have to have an underlying model and all manipulation happens to the model. If you needed to further manipulate to the dropped object, you would have to write some custom handlers. Besides, I found some oddities when trying it out, like behaving in a strange way if you did not add certain attributes.

Other implementations such as ngDraggable had similar issues.

After this tour, I decided to roll my own. I did not need something generic so I wrote a narrow solution that fit my needs. The code is basically the same that I posted in my last post but wrapped in directives and controllers.

angular.module(‘coach.field’, [])
.directive(“cloneDraggable”, function() {
return {
link: function(scope, element, attrs, controller) {
element.draggable({ helper:’clone’, 
                                                          revert: ‘invalid’, 
                                                          cursor: “crosshair”});
}
}
})
.controller(“soccerFieldCtrl”, function(tacticsService, $scope) {
$scope.idCounter = 0;
this.nextId = function() {
return $scope.idCounter++;
};
})
.directive(“soccerField”, function(tacticsService) {
return {
templateUrl: “field/field.html”,
controller: “soccerFieldCtrl”,
                link: function(scope, element, attrs, controller) {
          element.droppable({
drop: function(ev, ui) {
if (! ui.helper.hasClass(“toolbar-gadget”))
return;

var dropped=$(ui.draggable).clone();
var newId = “player” + controller.nextId();
dropped.attr(“id”, newId);

var pos=$(ui.helper).offset();

var newX = pos.left- element.offset().left;
var newY = pos.top – element.offset().top;

dropped.css({“left”:newX,”top”:newY, “z-index”: 100});
dropped.removeClass(“toolbar-gadget”);
dropped.addClass(“player”);

$(this).append(dropped);
       
                dropped.draggable({ cursor: “crosshair”, 
                                                                          containment: ‘parent’});
}
     });
}
}
});


jQuery UI Drag & Drop with "Clone"

The problem I was trying to solve is how to drag an element, make a clone on drop, then make the dropped element draggable with different parameters. Seemed like a simple task, given the basics that are built into jQuery UI. It turns out that there a lot of posts out there that give part of the answer, but I have not found one that gives a complete example. Marcos Placona came close with his example, but it seemed overly complex and it also had an interesting bug:

  1. Drag one of the widgets on the left and drop in the grid (fine).
  2. Drag the widget you just dropped (fine).
  3. Now, drag any widget on the left to an invalid place, so as to force an animation back, and see what happens!

There seems to be some phantom association between the previously dropped element and the draggable element used by jQuery UI, either through direct reference or an ID.

I was about to adopt his code, but having found this bug I thought I should try on my own, given all the examples I have seen. I ended up building a much simpler working example with half the code.

A few notes on the code:

  1. The condition in the beginning of the drop() function filters out the case where you drag a widget that has already been dropped in the drop area.
  2. I set the position of the dropped element to “absolute” so that it can float inside the drop area.
  3. To calculate the drop position within the drop area, you need to calculate the offset of the dropped element from its parent, rather than from the page origin (which are the coordinates you get in the event).
  4. Note that you have to call css() on element before you append it or else the position may not take effect!
  5. Finally, for the code nazis out there, I deal with the repetition in the CSS via SASS when I develop locally! 

Padding and Margins with Bootstrap Grid

While constructing a page using the Bootstrap grid system, I noticed some extra padding in one of the rows. Documentation said that each column is given a 15px horizontal padding to create a gutter. It also said that rows have a -15 margin on both ends to offset this effect for the first and last columns.

This is all fine. However, I still noticed an extra padding even though my rows and columns seemed to be nested properly. After trying to remove various pieces from the HTML, it turns out that any element with the “container” or “container-fluid” classes adds yet another 15px horizontal padding. Removing the extra container fixed my issue.