Permalink
Browse files

Make sure direct properties always win

For example x and y win over point, etc.
  • Loading branch information...
koenbok committed May 10, 2016
1 parent 528a1cc commit 907090360198ec756d8d52575978c1ed3a0205e4
Showing with 76 additions and 4 deletions.
  1. +18 −4 framer/Layer.coffee
  2. +58 −0 test/tests/AlignTest.coffee
@@ -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={}) ->
@@ -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
@@ -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", @)
@@ -1,4 +1,5 @@
describe "Align", ->
createAlignedLayers = (property,value,properties={}) ->
properties.width ?= 500
properties.height ?= 300
@@ -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
@@ -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
@@ -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.