Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3299d9

Browse files
committed
Automatically scroll page when widget is moving beyond viewport (gridstack#827).
1 parent e4ff669 commit c3299d9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Change log
2929
- widget x and y are now ints (thanks [@DonnchaC](https://github.com/donnchac))
3030
- allow all droppable options (thanks [@vigor-vlad](https://github.com/vigor-vlad))
3131
- properly track mouse position in `getCellFromPixel` (thanks [@aletorrado](https://github.com/aletorrado))
32+
- scroll when moving widget up or down out of viewport ([#827](https://github.com/troolee/gridstack.js/issues/827))
3233

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

src/gridstack.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,52 @@
125125
if (style.height) {
126126
style.removeProperty('height');
127127
}
128+
},
129+
getScrollParent: function(el) {
130+
var returnEl;
131+
if (el == null) {
132+
returnEl = null;
133+
} else if (el.scrollHeight > el.clientHeight) {
134+
returnEl = el;
135+
} else {
136+
returnEl = Utils.getScrollParent(el.parentNode);
137+
}
138+
return returnEl;
139+
},
140+
updateScrollPosition: function(el, ui, distance) {
141+
// is widget in view?
142+
var rect = el.getBoundingClientRect();
143+
var innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight);
144+
if (rect.top < 0 ||
145+
rect.bottom > innerHeightOrClientHeight
146+
) {
147+
// set scrollTop of first parent that scrolls
148+
// if parent is larger than el, set as low as possible
149+
// to get entire widget on screen
150+
var offsetDiffDown = rect.bottom - innerHeightOrClientHeight;
151+
var offsetDiffUp = rect.top;
152+
var scrollEl = Utils.getScrollParent(el);
153+
var prevScroll = scrollEl.scrollTop;
154+
if (scrollEl != null) {
155+
if (rect.top < 0 && distance < 0) {
156+
// moving up
157+
if (el.offsetHeight > innerHeightOrClientHeight) {
158+
scrollEl.scrollTop += distance;
159+
} else {
160+
scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp;
161+
}
162+
} else if (distance > 0) {
163+
// moving down
164+
if (el.offsetHeight > innerHeightOrClientHeight) {
165+
scrollEl.scrollTop += distance;
166+
} else {
167+
scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown;
168+
}
169+
}
170+
// move widget y by amount scrolled
171+
ui.position.top += scrollEl.scrollTop - prevScroll;
172+
}
173+
}
128174
}
129175
};
130176

@@ -1106,6 +1152,9 @@
11061152
}
11071153

11081154
if (event.type == 'drag') {
1155+
var distance = ui.position.top - node._prevYPix;
1156+
node._prevYPix = ui.position.top;
1157+
Utils.updateScrollPosition(el[0], ui, distance);
11091158
if (el.data('inTrashZone') || x < 0 || x >= self.grid.width || y < 0 ||
11101159
(!self.grid.float && y > self.grid.getGridHeight())) {
11111160
if (!node._temporaryRemoved) {
@@ -1177,6 +1226,7 @@
11771226
node.el = self.placeholder;
11781227
node._beforeDragX = node.x;
11791228
node._beforeDragY = node.y;
1229+
node._prevYPix = ui.position.top;
11801230

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

0 commit comments

Comments
 (0)