Skip to content

Commit

Permalink
Automatically scroll page when widget is moving beyond viewport (grid…
Browse files Browse the repository at this point in the history
  • Loading branch information
radiolips committed May 9, 2018
1 parent e4ff669 commit c3299d9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Change log
- widget x and y are now ints (thanks [@DonnchaC](https://github.com/donnchac))
- allow all droppable options (thanks [@vigor-vlad](https://github.com/vigor-vlad))
- properly track mouse position in `getCellFromPixel` (thanks [@aletorrado](https://github.com/aletorrado))
- scroll when moving widget up or down out of viewport ([#827](https://github.com/troolee/gridstack.js/issues/827))

## v0.3.0 (2017-04-21)

Expand Down
50 changes: 50 additions & 0 deletions src/gridstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,52 @@
if (style.height) {
style.removeProperty('height');
}
},
getScrollParent: function(el) {
var returnEl;
if (el == null) {
returnEl = null;
} else if (el.scrollHeight > el.clientHeight) {
returnEl = el;
} else {
returnEl = Utils.getScrollParent(el.parentNode);
}
return returnEl;
},
updateScrollPosition: function(el, ui, distance) {
// is widget in view?
var rect = el.getBoundingClientRect();
var innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight);
if (rect.top < 0 ||
rect.bottom > innerHeightOrClientHeight
) {
// set scrollTop of first parent that scrolls
// if parent is larger than el, set as low as possible
// to get entire widget on screen
var offsetDiffDown = rect.bottom - innerHeightOrClientHeight;
var offsetDiffUp = rect.top;
var scrollEl = Utils.getScrollParent(el);
var prevScroll = scrollEl.scrollTop;
if (scrollEl != null) {
if (rect.top < 0 && distance < 0) {
// moving up
if (el.offsetHeight > innerHeightOrClientHeight) {
scrollEl.scrollTop += distance;
} else {
scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp;
}
} else if (distance > 0) {
// moving down
if (el.offsetHeight > innerHeightOrClientHeight) {
scrollEl.scrollTop += distance;
} else {
scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown;
}
}
// move widget y by amount scrolled
ui.position.top += scrollEl.scrollTop - prevScroll;
}
}
}
};

Expand Down Expand Up @@ -1106,6 +1152,9 @@
}

if (event.type == 'drag') {
var distance = ui.position.top - node._prevYPix;
node._prevYPix = ui.position.top;
Utils.updateScrollPosition(el[0], ui, distance);
if (el.data('inTrashZone') || x < 0 || x >= self.grid.width || y < 0 ||
(!self.grid.float && y > self.grid.getGridHeight())) {
if (!node._temporaryRemoved) {
Expand Down Expand Up @@ -1177,6 +1226,7 @@
node.el = self.placeholder;
node._beforeDragX = node.x;
node._beforeDragY = node.y;
node._prevYPix = ui.position.top;

self.dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1));
self.dd.resizable(el, 'option', 'minHeight', strictCellHeight * (node.minHeight || 1));
Expand Down

0 comments on commit c3299d9

Please sign in to comment.