diff --git a/framer/Layer.coffee b/framer/Layer.coffee index 0201c05ea..ad91fc338 100644 --- a/framer/Layer.coffee +++ b/framer/Layer.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", @) diff --git a/test/tests/AlignTest.coffee b/test/tests/AlignTest.coffee index ff94746b4..97fb85f1c 100644 --- a/test/tests/AlignTest.coffee +++ b/test/tests/AlignTest.coffee @@ -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 \ No newline at end of file