Skip to content

Instantly share code, notes, and snippets.

@jakedowns
Last active February 14, 2017 22:28
Show Gist options
  • Save jakedowns/ed421ee3ee57af869fabb594531e6182 to your computer and use it in GitHub Desktop.
Save jakedowns/ed421ee3ee57af869fabb594531e6182 to your computer and use it in GitHub Desktop.
SortableGroup Class modifications
checkList = () => {
let lists = this.getRefs();
let {target, current, delta, pageX, pageY} = this.dragInfo;
let t = this.center(target);
let closest = this.closestList(t.x, t.y, lists);
// closest list is not the current list
if(current != closest){
if(this.shouldCancelListJump && this.shouldCancelListJump(current, closest)){
console.info('list jump cancelled');
return false;
}
//console.log('doing list jump...', current, closest);
t = this.center(target);
let itemContainer = lists[closest].getSortableItemContainer(); // TODO: default to list.container
let newIndex = this.closestNodeIndex(t.x, t.y, itemContainer.childNodes);
//console.log('stop dragging:', current);
// stop dragging from the prev list (calls onSortEnd)
lists[current].handleSortEnd({});
//console.log('start dragging:', closest);
// start dragging from the closest list
this.startDragging(closest, newIndex, delta, pageX, pageY);
this.dragInfo.current = closest;
}
}
closestList(x, y, lists)
{
let list;
let d = 0;
let sd = 999999999;
let listName;
_.each(lists, (c, key) = > {
// ORIGINAL
//list = this.center(c.container.getBoundingClientRect());
//d = this.distance(x, y, list.x, list.y);
// MODIFIED
let list_rect = c.container.getBoundingClientRect();
let rect = {
min: {x: list_rect.left, y: list_rect.top},
max: {x: list_rect.right, y: list_rect.bottom}
}
let point = {x, y}
d = this.distance_rect_point(rect, point);
//console.log(key, d, sd);
if (d < sd) {
sd = d;
listName = key;
}
});
return listName;
}
distance_rect_point(rect, p)
{
// cred: http://stackoverflow.com/questions/5254838/calculating-distance-between-a-point-and-a-rectangular-box-nearest-point
// Let rect = {max:{x:_, y:_}, min:{x:_, y:_}} and p={x:_, y:_}
var dx = Math.max(rect.min.x - p.x, 0, p.x - rect.max.x);
var dy = Math.max(rect.min.y - p.y, 0, p.y - rect.max.y);
return Math.sqrt(dx * dx + dy * dy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment