From d1d1c6400a00ae8a0e9f53d4a5d1525e4c2ed7d9 Mon Sep 17 00:00:00 2001 From: Mina Nagy Zaki Date: Tue, 2 May 2017 20:25:23 +0200 Subject: [PATCH] Handle positioning correctly when inside a positioned parent and that parent may not be the containment element --- src/angular-sortable-view.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/angular-sortable-view.js b/src/angular-sortable-view.js index 5046909..dacb2e1 100644 --- a/src/angular-sortable-view.js +++ b/src/angular-sortable-view.js @@ -420,6 +420,7 @@ var target = $element; var clientRect = $element[0].getBoundingClientRect(); + var parentRect = getPositionedParentRect($element[0]); var clone; if(!helper) helper = $controllers[0].helper; @@ -459,8 +460,10 @@ if(targetLeft + helperRect.width > containmentRect.left + body.scrollLeft + containmentRect.width) // right boundary targetLeft = containmentRect.left + body.scrollLeft + containmentRect.width - helperRect.width; - targetLeft -= containmentRect.left; - targetTop -= containmentRect.top; + if (parentRect) { + targetLeft -= parentRect.left; + targetTop -= parentRect.top; + } } this.style.left = targetLeft - body.scrollLeft + 'px'; this.style.top = targetTop - body.scrollTop + 'px'; @@ -499,6 +502,16 @@ }, clone, $element, placeholder, $controllers[0].getPart(), $scope.$index); } } + + function getPositionedParentRect(el) { + while (el !== document.documentElement) { + el = el.parentNode; + var cssPos = el.style.position; + if (cssPos != '' && cssPos != 'static') { + return el.getBoundingClientRect(); + } + } + } } }; }]);