Skip to content

Added support for drag handles that contain nested elements. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions angular-drag-and-drop-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,39 @@ angular.module('dndLists', [])
});
}

/**
* Search up the DOM from the specified element and find an ancestor (parent, grand-parent, etc)
* that has the requested class name.
*/
var findAncestorWithClass = function (element, className) {

if (!element) {
throw new Error("findAncestorWithClass: Invalid value for parameter element");
}

if (!className) {
throw new Error("findAncestorWithClass: Invalid value for parameter className");
}

var ancestor = angular.element(element).parent();
while (ancestor.length > 0) {
if (ancestor.hasClass(className)) {
return ancestor;
}

ancestor = ancestor.parent();
}

return null;
}

/**
* Keep track of the mouseTarget so we can use a custom handle.
*/
element.on('mousedown', function (event) {
mouseTarget = event.target;

if (!attr.dndHandleClass || mouseTarget.classList.contains(attr.dndHandleClass)) {
if (!attr.dndHandleClass || findAncestorWithClass(mouseTarget, attr.dndHandleClass)) {
// Set the HTML5 draggable attribute on the element
element.attr("draggable", "true");
} else {
Expand All @@ -95,7 +121,7 @@ angular.module('dndLists', [])
* which is the primary way we communicate with the target element
*/
element.on('dragstart', function(event) {
if (attr.dndHandleClass && !mouseTarget.classList.contains(attr.dndHandleClass)) {
if (attr.dndHandleClass && !findAncestorWithClass(mouseTarget, attr.dndHandleClass)) {
event.preventDefault();
return;
}
Expand Down