Skip to content

Commit

Permalink
Fix stateCycle() and states.next() when not providing options
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh committed Oct 5, 2016
1 parent 2a7ad6c commit 1f18a6c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
9 changes: 5 additions & 4 deletions framer/Layer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,12 @@ class exports.Layer extends BaseClass

return animation

stateCycle: (args..., options={}) ->
states = []
states = _.flatten(args) if args.length
@animate(@states.machine.next(states), options)
stateCycle: (args...) ->
states = _.flatten(args)
if _.isObject(_.last(states))
options = states.pop()

@animate(@states.machine.next(states), options)
stateSwitch: (stateName, options={}) ->
return @animate(stateName, options) if options.animate is true
return @animate(stateName, _.defaults({}, options, {instant:true}))
Expand Down
7 changes: 4 additions & 3 deletions framer/LayerStates.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class LayerStates
stateProperties = {}

for k, v of properties

if k in LayerStatesIgnoredKeys
continue

if Color.isValidColorProperty(k, v)
stateProperties[k] = new Color(v)
continue
Expand Down Expand Up @@ -124,8 +124,9 @@ class LayerStates
keys = _.union(keys, _.keys(state))
return keys

next: (options) ->
next: (options...) ->
deprecatedWarning("next", "layer.stateCycle()")
options = _.flatten(options)
@machine.layer.stateCycle(options)

last: (options) ->
Expand Down
26 changes: 26 additions & 0 deletions test/tests/LayerStatesBackwardsTest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe "LayerStates Backwards compatibility", ->
done()
layer.states.next()

it "should still support layer.states.next with string arguments", (done) ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "stateB"
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "stateA"
done()
layer.states.next("stateB", "stateA")
layer.states.next("stateB", "stateA")

it "should still support layer.states.next with list arguments", (done) ->
layer = new Layer
layer.states =
stateA: x: 200
stateB: scale: 0.5
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "stateB"
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "stateA"
done()
layer.states.next ["stateB", "stateA"]
layer.states.next ["stateB", "stateA"]

it "should still support layer.states.last", (done) ->
layer = new Layer
layer.states =
Expand Down
29 changes: 28 additions & 1 deletion test/tests/LayerStatesTest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe "LayerStates", ->
describe "Adding", ->

describe "when setting multiple states", ->

it "should override existing states", ->
layer = new Layer
layer.states.test = x: 100
Expand Down Expand Up @@ -514,6 +514,33 @@ describe "LayerStates", ->
layer.states.current.name.should.equal "testA"
done()

it "should cycle list without options", (done) ->
layer = new Layer
layer.animationOptions.time = 0.1
layer.states.testA = {x: 200}
layer.states.testB = {x: 400}
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "testB"
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "testA"
done()
layer.stateCycle ["testB", "testA"]
layer.stateCycle ["testB", "testA"]

it "should cycle multiple arguments without options", (done) ->
layer = new Layer
layer.animationOptions.time = 0.1
layer.states.testA = {x: 200}
layer.states.testB = {x: 400}
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "testB"
layer.once Events.StateSwitchEnd, ->
layer.states.current.name.should.equal "testA"
done()
layer.stateCycle "testB", "testA"
layer.stateCycle "testB", "testA"


it "should cycle two out of three in arguments", (done) ->
layer = new Layer
layer.animationOptions.time = 0.1
Expand Down

0 comments on commit 1f18a6c

Please sign in to comment.