Skip to content
Permalink
Browse files

Merge pull request #437 from koenbok/scroll-fix

LayerDraggable, fix zero velocity moves; fix leak in @_eventBuffer

LGTM πŸš€
  • Loading branch information
nvh committed Nov 3, 2016
2 parents a4e959e + faa019f commit 84779848fbff9d3ffaf61bb169185c7431e6df86
Showing with 43 additions and 5 deletions.
  1. +12 βˆ’4 framer/LayerDraggable.coffee
  2. +31 βˆ’1 test/tests/ScrollComponentTest.coffee
@@ -85,7 +85,6 @@ class exports.LayerDraggable extends BaseClass
y: @layer.y - @_correctedLayerStartPoint.y

constructor: (@layer) ->

options = Defaults.getDefaults("LayerDraggable", {})

super options
@@ -196,12 +195,11 @@ class exports.LayerDraggable extends BaseClass
# If we started dragging from another event we need to capture some initial values
@touchStart(event) if not @_point

@_lastEvent = event

event.preventDefault()
event.stopPropagation() if @propagateEvents is false

touchEvent = Events.touchEvent(event)
@_lastEvent = touchEvent

@_eventBuffer.push
x: touchEvent.clientX
@@ -280,8 +278,9 @@ class exports.LayerDraggable extends BaseClass
# (which would return a stale value before the simulation had finished one tick)
# and because @_start currently calls calculateVelocity().
@_isDragging = false

@_ignoreUpdateLayerPosition = true
@_lastEvent = null
@_eventBuffer.reset()


##############################################################
@@ -378,6 +377,15 @@ class exports.LayerDraggable extends BaseClass
get: ->
# return null if not @isDragging
velocity = @velocity
if velocity.x == 0 and velocity.y == 0
delta = @_lastEvent?.delta
return null if not delta
if Math.abs(delta.x) > Math.abs(delta.y)
return "right" if delta.x > 0
return "left"
else
return "down" if delta.y > 0
return "up"
if Math.abs(velocity.x) > Math.abs(velocity.y)
return "right" if velocity.x > 0
return "left"
@@ -35,7 +35,7 @@ describe "ScrollComponent", ->
copy = instance.copy()
copy.scrollHorizontal.should.be.false
describe "Events", ->
describe "scolling with mousEvents", ->
describe "scrolling with mousEvents", ->
it "should work", ->
scroll = new ScrollComponent size: 200
new Layer
@@ -84,6 +84,36 @@ describe "ScrollComponent", ->
done()
scroll.emit(Events.MouseWheel, {wheelDeltaX: -75, wheelDeltaY: -150})

describe "scrolling with touch events", ->
it "should have direction", (done) ->
scroll = new ScrollComponent size: 200
draggable = scroll.content.draggable
new Layer
width: 400
height: 400
parent: scroll.content

# collect the move events as they happen
moves = []
scroll.on Events.Move, (event) -> moves.push scroll.direction

# verify the timeline of move events looks good
draggable.on Events.DragAnimationEnd, (event) ->
ups = _.lastIndexOf(moves, "up")
downs = moves.indexOf("down")
nulls = moves.indexOf(null)
if ups < downs < nulls or (nulls == -1 and ups < downs) then return done()
done("expected up+, down+, null+, but got: #{moves}")

# TODO instead of actually taking time, trick time
draggable._touchStart({clientX:100, clientY:100, preventDefault:(()->), stopPropagation:(()->)})
Utils.delay 0.01, ->
draggable._touchMove({clientX:100, clientY:111, preventDefault:(()->), stopPropagation:(()->), delta:{x:0,y:11}})
Utils.delay 0.20, -> # enough time to set velocity to zero
draggable._touchMove({clientX:100, clientY:112, preventDefault:(()->), stopPropagation:(()->), delta:{x:0,y:1}})
Utils.delay 0.21, ->
draggable._touchEnd({clientX:100, clientY:112, preventDefault:(()->), stopPropagation:(()->), delta:{x:0,y:0}})

describe "PageComponent", ->
it "should fire scroll events", (done) ->
page = new PageComponent

0 comments on commit 8477984

Please sign in to comment.
You can’t perform that action at this time.