-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scrolling support vertically and horizontally #449
Changes from 5 commits
4ac8b49
030323a
a4438f4
624e638
ec1ac5e
3660289
13875c0
f6cb5cd
ef3e640
f752e0e
1187cd4
30473db
22f4e03
ae28525
1e319d5
f6a7d55
dd20e3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ var crossvent = require('crossvent'); | |
var classes = require('./classes'); | ||
var doc = document; | ||
var documentElement = doc.documentElement; | ||
var _autoScrollingInterval; // reference to auto scrolling | ||
|
||
function dragula (initialContainers, options) { | ||
var len = arguments.length; | ||
|
@@ -218,6 +219,7 @@ function dragula (initialContainers, options) { | |
|
||
function end () { | ||
if (!drake.dragging) { | ||
clearInterval(_autoScrollingInterval); | ||
return; | ||
} | ||
var item = _copy || _item; | ||
|
@@ -303,6 +305,7 @@ function dragula (initialContainers, options) { | |
|
||
function cleanup () { | ||
var item = _copy || _item; | ||
clearInterval(_autoScrollingInterval); | ||
ungrab(); | ||
removeMirrorImage(); | ||
if (item) { | ||
|
@@ -355,9 +358,12 @@ function dragula (initialContainers, options) { | |
} | ||
|
||
function drag (e) { | ||
if (!_mirror) { | ||
return; | ||
} | ||
if (!_mirror) { return; } | ||
// For iframe. When dragging an item and mouse moves out of the iframe and | ||
// mouseup, then decides to move back, the event will be 0 so we should | ||
// just call cancel. | ||
if (whichMouseButton(e) === 0) { cancel(); } | ||
|
||
e.preventDefault(); | ||
|
||
var clientX = getCoord('clientX', e); | ||
|
@@ -409,6 +415,8 @@ function dragula (initialContainers, options) { | |
function moved (type) { drake.emit(type, item, _lastDropTarget, _source); } | ||
function over () { if (changed) { moved('over'); } } | ||
function out () { if (_lastDropTarget) { moved('out'); } } | ||
|
||
startScroll(_item, e); | ||
} | ||
|
||
function spillOver (el) { | ||
|
@@ -605,4 +613,66 @@ function getCoord (coord, e) { | |
return host[coord]; | ||
} | ||
|
||
function getScrollContainer(node) { | ||
if (node === null) { return null; } | ||
if (node.scrollHeight > node.clientHeight) { | ||
return node; | ||
} else { | ||
if (!/(body|html)/i.test(node.parentNode.tagName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract regexp to a variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why I need a variable for this. Why would you want to hold on to the reference if it's not going to be used unless we're scrolling? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a commit for it, e9e4e5a; but I'm not sure if I'm happy with this path. I'm open to suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nguyenj I just meant putting it in a variable like this: var rbodyhtml = /(body|html)/i
if (!rbodyhtml.test(node.parentNode.tagName)) {
// ... I avoid inline regular expressions in conditionals because they dampen readability. There's no need to hoist it to the top of the file, certainly. |
||
return getScrollContainer(node.parentNode); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
function startAutoScrolling(node, amount, direction) { | ||
_autoScrollingInterval = setInterval(function() { | ||
node[direction] += (amount * 0.25); | ||
}, 15); | ||
} | ||
|
||
function startScroll(item, event) { | ||
var scrollEdge = 20; | ||
var scrollSpeed = 20; | ||
var scrollContainer = getScrollContainer(item); | ||
|
||
clearInterval(_autoScrollingInterval); | ||
|
||
// If a container contains the list that is scrollable | ||
if (scrollContainer) { | ||
|
||
// Scrolling vertically | ||
if (event.pageY - getOffset(scrollContainer).top < scrollEdge) { | ||
startAutoScrolling(scrollContainer, -scrollSpeed, 'scrollTop'); | ||
} else if ((getOffset(scrollContainer).top + scrollContainer.getBoundingClientRect().height) - event.pageY < scrollEdge) { | ||
startAutoScrolling(scrollContainer, scrollSpeed, 'scrollTop'); | ||
} | ||
|
||
// Scrolling horizontally | ||
if (event.pageX - scrollContainer.getBoundingClientRect().left < scrollEdge) { | ||
startAutoScrolling(scrollContainer, -scrollSpeed, 'scrollLeft'); | ||
} else if ((getOffset(scrollContainer).left + scrollContainer.getBoundingClientRect().width) - event.pageX < scrollEdge) { | ||
startAutoScrolling(scrollContainer, scrollSpeed, 'scrollLeft'); | ||
} | ||
|
||
// If the window contains the list | ||
} else { | ||
|
||
// Scrolling vertically | ||
if ((event.pageY - window.scrollY) < scrollEdge) { | ||
startAutoScrolling(document.body, -scrollSpeed, 'scrollTop'); | ||
} else if ((window.innerHeight - (event.pageY - window.scrollY)) < scrollEdge) { | ||
startAutoScrolling(document.body, scrollSpeed, 'scrollTop'); | ||
} | ||
|
||
// Scrolling horizontally | ||
if ((event.pageX - window.scrollX) < scrollEdge) { | ||
startAutoScrolling(document.body, -scrollSpeed, 'scrollLeft'); | ||
} else if ((window.innerWidth - (event.pageX - window.scrollX)) < scrollEdge) { | ||
startAutoScrolling(document.body, scrollSpeed, 'scrollLeft'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = dragula; |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else
here is redundant, please remove