Nested Drag and Drop in with Angular 7 Material CDK












0














I've got a nested tree (Not the tree component) of drag and drop lists.



When dragging items around in drop lists that are contained inside of another drop list - Enter / Exit events are firing for both drop lists, meaning that when an item is dropped it could either be dropped into the inner drop list or the container drop list depending where it was dropped (Note: These lists are all linked to each other)



I'm thinking at the moment that the best solution will to be suppress events firing for the container list if the drag is currently over an inner list but I'm not sure if this is the best solution or exactly how to do it at the moment.










share|improve this question
























  • I think material's drag and drop still have many features need to do.may be you could search in official github issues.
    – junk
    Nov 13 '18 at 1:49










  • @junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
    – anewtonlevey
    Nov 13 '18 at 2:22
















0














I've got a nested tree (Not the tree component) of drag and drop lists.



When dragging items around in drop lists that are contained inside of another drop list - Enter / Exit events are firing for both drop lists, meaning that when an item is dropped it could either be dropped into the inner drop list or the container drop list depending where it was dropped (Note: These lists are all linked to each other)



I'm thinking at the moment that the best solution will to be suppress events firing for the container list if the drag is currently over an inner list but I'm not sure if this is the best solution or exactly how to do it at the moment.










share|improve this question
























  • I think material's drag and drop still have many features need to do.may be you could search in official github issues.
    – junk
    Nov 13 '18 at 1:49










  • @junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
    – anewtonlevey
    Nov 13 '18 at 2:22














0












0








0







I've got a nested tree (Not the tree component) of drag and drop lists.



When dragging items around in drop lists that are contained inside of another drop list - Enter / Exit events are firing for both drop lists, meaning that when an item is dropped it could either be dropped into the inner drop list or the container drop list depending where it was dropped (Note: These lists are all linked to each other)



I'm thinking at the moment that the best solution will to be suppress events firing for the container list if the drag is currently over an inner list but I'm not sure if this is the best solution or exactly how to do it at the moment.










share|improve this question















I've got a nested tree (Not the tree component) of drag and drop lists.



When dragging items around in drop lists that are contained inside of another drop list - Enter / Exit events are firing for both drop lists, meaning that when an item is dropped it could either be dropped into the inner drop list or the container drop list depending where it was dropped (Note: These lists are all linked to each other)



I'm thinking at the moment that the best solution will to be suppress events firing for the container list if the drag is currently over an inner list but I'm not sure if this is the best solution or exactly how to do it at the moment.







angular drag-and-drop angular7 angular-cdk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:41









Goncalo Peres

1,3261318




1,3261318










asked Nov 12 '18 at 23:57









anewtonlevey

527




527












  • I think material's drag and drop still have many features need to do.may be you could search in official github issues.
    – junk
    Nov 13 '18 at 1:49










  • @junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
    – anewtonlevey
    Nov 13 '18 at 2:22


















  • I think material's drag and drop still have many features need to do.may be you could search in official github issues.
    – junk
    Nov 13 '18 at 1:49










  • @junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
    – anewtonlevey
    Nov 13 '18 at 2:22
















I think material's drag and drop still have many features need to do.may be you could search in official github issues.
– junk
Nov 13 '18 at 1:49




I think material's drag and drop still have many features need to do.may be you could search in official github issues.
– junk
Nov 13 '18 at 1:49












@junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
– anewtonlevey
Nov 13 '18 at 2:22




@junk I did manage to get this working, although i'm sure as Material's drag and drop CDK evolves so will the solution.
– anewtonlevey
Nov 13 '18 at 2:22












1 Answer
1






active

oldest

votes


















0














I did manage to find a solution to this, although it's definitely hacky and involves accessing a private value with the Angular drag and drop CDK.



I use the cdkDropListEnterPredicate function to check which list it should be trying to drop into, which i assign the canDropPredicate function.



I'm also forced to get access to the pointer position via: _pointerPositionAtLastDirectionChange which isn't great as not all the values I'd like to see passed into the cdkDropListEnterPredicate get passed.



canDropPredicate(): Function {
const me = this;
return (drag: CdkDrag<ResourceNode>, drop: CdkDropList<ResourceNode>): boolean => {
const fromBounds = drag.dropContainer.element.nativeElement.getBoundingClientRect();
const toBounds = drop.element.nativeElement.getBoundingClientRect();

if (!me.intersect(fromBounds, toBounds)) {
return true;
}

// This gross but allows us to access a private field for now.
const pointerPosition: Point = drag['_pointerPositionAtLastDirectionChange'];
// They Intersect with each other so we need to do some calculations here.
if (me.insideOf(fromBounds, toBounds)) {
return !me.pointInsideOf(pointerPosition, fromBounds);
}

if (me.insideOf(toBounds, fromBounds) && me.pointInsideOf(pointerPosition, toBounds)) {
return true;
}
return false;
};
}

intersect(r1: DOMRect | ClientRect, r2: DOMRect | ClientRect): boolean {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}

insideOf(innerRect: DOMRect | ClientRect, outerRect: DOMRect | ClientRect): boolean {
return innerRect.left >= outerRect.left &&
innerRect.right <= outerRect.right &&
innerRect.top >= outerRect.top &&
innerRect.bottom <= outerRect.bottom &&
!(
innerRect.left === outerRect.left &&
innerRect.right === outerRect.right &&
innerRect.top === outerRect.top &&
innerRect.bottom === outerRect.bottom
);
}

pointInsideOf(position: Point, rect: DOMRect | ClientRect) {
return position.x >= rect.left &&
position.x <= rect.right &&
position.y >= rect.top &&
position.y <= rect.bottom;
}





share|improve this answer





















  • you extend the component?
    – junk
    Nov 13 '18 at 2:29










  • This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
    – anewtonlevey
    Nov 13 '18 at 2:36












  • @anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
    – Andre Elrico
    Dec 7 '18 at 14:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271834%2fnested-drag-and-drop-in-with-angular-7-material-cdk%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I did manage to find a solution to this, although it's definitely hacky and involves accessing a private value with the Angular drag and drop CDK.



I use the cdkDropListEnterPredicate function to check which list it should be trying to drop into, which i assign the canDropPredicate function.



I'm also forced to get access to the pointer position via: _pointerPositionAtLastDirectionChange which isn't great as not all the values I'd like to see passed into the cdkDropListEnterPredicate get passed.



canDropPredicate(): Function {
const me = this;
return (drag: CdkDrag<ResourceNode>, drop: CdkDropList<ResourceNode>): boolean => {
const fromBounds = drag.dropContainer.element.nativeElement.getBoundingClientRect();
const toBounds = drop.element.nativeElement.getBoundingClientRect();

if (!me.intersect(fromBounds, toBounds)) {
return true;
}

// This gross but allows us to access a private field for now.
const pointerPosition: Point = drag['_pointerPositionAtLastDirectionChange'];
// They Intersect with each other so we need to do some calculations here.
if (me.insideOf(fromBounds, toBounds)) {
return !me.pointInsideOf(pointerPosition, fromBounds);
}

if (me.insideOf(toBounds, fromBounds) && me.pointInsideOf(pointerPosition, toBounds)) {
return true;
}
return false;
};
}

intersect(r1: DOMRect | ClientRect, r2: DOMRect | ClientRect): boolean {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}

insideOf(innerRect: DOMRect | ClientRect, outerRect: DOMRect | ClientRect): boolean {
return innerRect.left >= outerRect.left &&
innerRect.right <= outerRect.right &&
innerRect.top >= outerRect.top &&
innerRect.bottom <= outerRect.bottom &&
!(
innerRect.left === outerRect.left &&
innerRect.right === outerRect.right &&
innerRect.top === outerRect.top &&
innerRect.bottom === outerRect.bottom
);
}

pointInsideOf(position: Point, rect: DOMRect | ClientRect) {
return position.x >= rect.left &&
position.x <= rect.right &&
position.y >= rect.top &&
position.y <= rect.bottom;
}





share|improve this answer





















  • you extend the component?
    – junk
    Nov 13 '18 at 2:29










  • This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
    – anewtonlevey
    Nov 13 '18 at 2:36












  • @anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
    – Andre Elrico
    Dec 7 '18 at 14:09
















0














I did manage to find a solution to this, although it's definitely hacky and involves accessing a private value with the Angular drag and drop CDK.



I use the cdkDropListEnterPredicate function to check which list it should be trying to drop into, which i assign the canDropPredicate function.



I'm also forced to get access to the pointer position via: _pointerPositionAtLastDirectionChange which isn't great as not all the values I'd like to see passed into the cdkDropListEnterPredicate get passed.



canDropPredicate(): Function {
const me = this;
return (drag: CdkDrag<ResourceNode>, drop: CdkDropList<ResourceNode>): boolean => {
const fromBounds = drag.dropContainer.element.nativeElement.getBoundingClientRect();
const toBounds = drop.element.nativeElement.getBoundingClientRect();

if (!me.intersect(fromBounds, toBounds)) {
return true;
}

// This gross but allows us to access a private field for now.
const pointerPosition: Point = drag['_pointerPositionAtLastDirectionChange'];
// They Intersect with each other so we need to do some calculations here.
if (me.insideOf(fromBounds, toBounds)) {
return !me.pointInsideOf(pointerPosition, fromBounds);
}

if (me.insideOf(toBounds, fromBounds) && me.pointInsideOf(pointerPosition, toBounds)) {
return true;
}
return false;
};
}

intersect(r1: DOMRect | ClientRect, r2: DOMRect | ClientRect): boolean {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}

insideOf(innerRect: DOMRect | ClientRect, outerRect: DOMRect | ClientRect): boolean {
return innerRect.left >= outerRect.left &&
innerRect.right <= outerRect.right &&
innerRect.top >= outerRect.top &&
innerRect.bottom <= outerRect.bottom &&
!(
innerRect.left === outerRect.left &&
innerRect.right === outerRect.right &&
innerRect.top === outerRect.top &&
innerRect.bottom === outerRect.bottom
);
}

pointInsideOf(position: Point, rect: DOMRect | ClientRect) {
return position.x >= rect.left &&
position.x <= rect.right &&
position.y >= rect.top &&
position.y <= rect.bottom;
}





share|improve this answer





















  • you extend the component?
    – junk
    Nov 13 '18 at 2:29










  • This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
    – anewtonlevey
    Nov 13 '18 at 2:36












  • @anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
    – Andre Elrico
    Dec 7 '18 at 14:09














0












0








0






I did manage to find a solution to this, although it's definitely hacky and involves accessing a private value with the Angular drag and drop CDK.



I use the cdkDropListEnterPredicate function to check which list it should be trying to drop into, which i assign the canDropPredicate function.



I'm also forced to get access to the pointer position via: _pointerPositionAtLastDirectionChange which isn't great as not all the values I'd like to see passed into the cdkDropListEnterPredicate get passed.



canDropPredicate(): Function {
const me = this;
return (drag: CdkDrag<ResourceNode>, drop: CdkDropList<ResourceNode>): boolean => {
const fromBounds = drag.dropContainer.element.nativeElement.getBoundingClientRect();
const toBounds = drop.element.nativeElement.getBoundingClientRect();

if (!me.intersect(fromBounds, toBounds)) {
return true;
}

// This gross but allows us to access a private field for now.
const pointerPosition: Point = drag['_pointerPositionAtLastDirectionChange'];
// They Intersect with each other so we need to do some calculations here.
if (me.insideOf(fromBounds, toBounds)) {
return !me.pointInsideOf(pointerPosition, fromBounds);
}

if (me.insideOf(toBounds, fromBounds) && me.pointInsideOf(pointerPosition, toBounds)) {
return true;
}
return false;
};
}

intersect(r1: DOMRect | ClientRect, r2: DOMRect | ClientRect): boolean {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}

insideOf(innerRect: DOMRect | ClientRect, outerRect: DOMRect | ClientRect): boolean {
return innerRect.left >= outerRect.left &&
innerRect.right <= outerRect.right &&
innerRect.top >= outerRect.top &&
innerRect.bottom <= outerRect.bottom &&
!(
innerRect.left === outerRect.left &&
innerRect.right === outerRect.right &&
innerRect.top === outerRect.top &&
innerRect.bottom === outerRect.bottom
);
}

pointInsideOf(position: Point, rect: DOMRect | ClientRect) {
return position.x >= rect.left &&
position.x <= rect.right &&
position.y >= rect.top &&
position.y <= rect.bottom;
}





share|improve this answer












I did manage to find a solution to this, although it's definitely hacky and involves accessing a private value with the Angular drag and drop CDK.



I use the cdkDropListEnterPredicate function to check which list it should be trying to drop into, which i assign the canDropPredicate function.



I'm also forced to get access to the pointer position via: _pointerPositionAtLastDirectionChange which isn't great as not all the values I'd like to see passed into the cdkDropListEnterPredicate get passed.



canDropPredicate(): Function {
const me = this;
return (drag: CdkDrag<ResourceNode>, drop: CdkDropList<ResourceNode>): boolean => {
const fromBounds = drag.dropContainer.element.nativeElement.getBoundingClientRect();
const toBounds = drop.element.nativeElement.getBoundingClientRect();

if (!me.intersect(fromBounds, toBounds)) {
return true;
}

// This gross but allows us to access a private field for now.
const pointerPosition: Point = drag['_pointerPositionAtLastDirectionChange'];
// They Intersect with each other so we need to do some calculations here.
if (me.insideOf(fromBounds, toBounds)) {
return !me.pointInsideOf(pointerPosition, fromBounds);
}

if (me.insideOf(toBounds, fromBounds) && me.pointInsideOf(pointerPosition, toBounds)) {
return true;
}
return false;
};
}

intersect(r1: DOMRect | ClientRect, r2: DOMRect | ClientRect): boolean {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}

insideOf(innerRect: DOMRect | ClientRect, outerRect: DOMRect | ClientRect): boolean {
return innerRect.left >= outerRect.left &&
innerRect.right <= outerRect.right &&
innerRect.top >= outerRect.top &&
innerRect.bottom <= outerRect.bottom &&
!(
innerRect.left === outerRect.left &&
innerRect.right === outerRect.right &&
innerRect.top === outerRect.top &&
innerRect.bottom === outerRect.bottom
);
}

pointInsideOf(position: Point, rect: DOMRect | ClientRect) {
return position.x >= rect.left &&
position.x <= rect.right &&
position.y >= rect.top &&
position.y <= rect.bottom;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 2:20









anewtonlevey

527




527












  • you extend the component?
    – junk
    Nov 13 '18 at 2:29










  • This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
    – anewtonlevey
    Nov 13 '18 at 2:36












  • @anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
    – Andre Elrico
    Dec 7 '18 at 14:09


















  • you extend the component?
    – junk
    Nov 13 '18 at 2:29










  • This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
    – anewtonlevey
    Nov 13 '18 at 2:36












  • @anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
    – Andre Elrico
    Dec 7 '18 at 14:09
















you extend the component?
– junk
Nov 13 '18 at 2:29




you extend the component?
– junk
Nov 13 '18 at 2:29












This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
– anewtonlevey
Nov 13 '18 at 2:36






This is within my tree component (No relation to the Angular Tree component) and I set the cdkDropListEnterPredicate to canDropPredicate
– anewtonlevey
Nov 13 '18 at 2:36














@anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
– Andre Elrico
Dec 7 '18 at 14:09




@anewtonlevey is there any progress regarding nested lists? would you like to share a stackblitz?
– Andre Elrico
Dec 7 '18 at 14:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271834%2fnested-drag-and-drop-in-with-angular-7-material-cdk%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python