Skip to content

Commit

Permalink
Make sure direct properties always win
Browse files Browse the repository at this point in the history
For example x and y win over point, etc.
  • Loading branch information
koenbok committed May 10, 2016
1 parent 528a1cc commit 9070903
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
22 changes: 18 additions & 4 deletions framer/Layer.coffee
Expand Up @@ -64,6 +64,16 @@ layerPropertyPointTransformer = (value, layer, property) ->

return value

layerPropertyIgnore = (options, propertyName, properties) ->
return options unless options.hasOwnProperty(propertyName)

for p in properties
if options.hasOwnProperty(p)
delete options[propertyName]
return options

return options

class exports.Layer extends BaseClass

constructor: (options={}) ->
Expand All @@ -89,6 +99,11 @@ class exports.Layer extends BaseClass
# We have to create the element before we set the defaults
@_createElement()

# Sanitize calculated property setters so direct properties always win
layerPropertyIgnore(options, "point", ["x", "y"])
layerPropertyIgnore(options, "size", ["width", "height"])
layerPropertyIgnore(options, "frame", ["x", "y", "width", "height"])

super Defaults.getDefaults("Layer", options)

# Add this layer to the current context
Expand All @@ -106,10 +121,9 @@ class exports.Layer extends BaseClass
else
@parent = options.parent

# Set some calculated properties, the order is important
for p in ["index", "width", "height", "x", "y"]
if options.hasOwnProperty(p)
@[p] = options[p]
# Make sure we set the right index
if options.hasOwnProperty("index")
@index = options.index

@_context.emit("layer:create", @)

Expand Down
58 changes: 58 additions & 0 deletions test/tests/AlignTest.coffee
@@ -1,4 +1,5 @@
describe "Align", ->

createAlignedLayers = (property,value,properties={}) ->
properties.width ?= 500
properties.height ?= 300
Expand All @@ -15,11 +16,13 @@ describe "Align", ->
layer

describe "center", ->

it "should center the layer", ->
{child} = createAlignedLayers('x',Align.center)
child.x.should.equal 200
{child} = createAlignedLayers('y',Align.center)
child.y.should.equal 50

it "should work when the layer has no parent", ->
layer = new Layer
width: 100
Expand All @@ -28,6 +31,7 @@ describe "Align", ->
y: Align.center
layer.x.should.equal 150
layer.y.should.equal 75

it "should take borderWidth into account", ->
{child} = createAlignedLayers('x',Align.center,{borderWidth:30})
child.x.should.equal 170
Expand All @@ -36,53 +40,107 @@ describe "Align", ->


describe "left", ->

it "should left align the layer", ->
{child} = createAlignedLayers('x',Align.left)
child.x.should.equal 0

it "should work when the layer has no parent", ->
layer = new Layer
width: 100
x: Align.left
layer.x.should.equal 0

it "should take borderWidth into account", ->
{child} = createAlignedLayers('x',Align.left,{borderWidth:30})
child.x.should.equal 0

describe "right", ->

it "should right align the layer", ->
{child} = createAlignedLayers('x',Align.right)
child.x.should.equal 400

it "should work when the layer has no parent", ->
layer = new Layer
width: 100
x: Align.right
layer.x.should.equal 300

it "should take borderWidth into account", ->
{child} = createAlignedLayers('x',Align.right,{borderWidth:30})
child.x.should.equal 340

describe "top", ->

it "should top align the layer", ->
{child} = createAlignedLayers('y',Align.top)
child.y.should.equal 0

it "should work when the layer has no parent", ->
layer = new Layer
height: 100
y: Align.top
layer.y.should.equal 0

it "should take borderWidth into account", ->
{child} = createAlignedLayers('y',Align.top,{borderWidth:30})
child.y.should.equal 0

describe "bottom", ->

it "should bottom align the layer", ->
{child} = createAlignedLayers('y',Align.bottom)
child.y.should.equal 100

it "should work when the layer has no parent", ->
layer = new Layer
height: 100
y: Align.bottom
layer.y.should.equal 200

it "should take borderWidth into account", ->
{child} = createAlignedLayers('y',Align.bottom,{borderWidth:30})
child.y.should.equal 40

describe "constructors", ->

it "should work with size", ->
test = new Layer
parent: new Layer size: 200
x: Align.center
y: Align.center
size: 100

test.x.should.equal 50
test.y.should.equal 50

it "should work with point and size", ->
test = new Layer
parent: new Layer size: 200
size: 100
point: Align.center

test.x.should.equal 50
test.y.should.equal 50

it "should work with point", ->
test = new Layer
parent: new Layer size: 200
width: 100
height: 100
point: Align.center

test.x.should.equal 50
test.y.should.equal 50

it "should work with both size and width height", ->
test = new Layer
parent: new Layer size: 200
width: 100
height: 100
point: Align.center
size: 200

test.x.should.equal 50
test.y.should.equal 50

0 comments on commit 9070903

Please sign in to comment.