Skip to content

Commit

Permalink
Merge branch 'master' of github.com:koenbok/framer
Browse files Browse the repository at this point in the history
  • Loading branch information
koenbok committed Sep 21, 2016
2 parents 5b3372d + 56f69e7 commit 8d9f4ea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 46 deletions.
23 changes: 4 additions & 19 deletions framer/BaseClass.coffee
Expand Up @@ -9,9 +9,6 @@ DefinedPropertiesKey = "_DefinedPropertiesKey"
DefinedPropertiesValuesKey = "_DefinedPropertiesValuesKey"
DefinedPropertiesOrderKey = "_DefinedPropertiesOrderKey"

capitalizeFirstLetter = (string) ->
string.charAt(0).toUpperCase() + string.slice(1)

class exports.BaseClass extends EventEmitter

#################################################################
Expand All @@ -23,23 +20,11 @@ class exports.BaseClass extends EventEmitter
if @ isnt BaseClass
@_addDescriptor(propertyName, descriptor)

# Set the getter/setter as setProperty on this object so we can access and override it easily
getName = "get#{capitalizeFirstLetter(propertyName)}"
@::[getName] = descriptor.get
descriptor.get = @::[getName]

if descriptor.set
setName = "set#{capitalizeFirstLetter(propertyName)}"
@::[setName] = descriptor.set
descriptor.set = @::[setName]

# Better readonly errors for debugging

# else
# descriptor.set = (value) ->
# throw Error("#{@constructor.name}.#{propertyName} is readonly")
if not descriptor.set?
descriptor.set = (value) ->
throw Error("#{@constructor.name}.#{propertyName} is readonly")

# Define the property
# Define the property on the prototype
Object.defineProperty(@prototype, propertyName, descriptor)

@_addDescriptor: (propertyName, descriptor) ->
Expand Down
14 changes: 11 additions & 3 deletions framer/Layer.coffee
Expand Up @@ -50,6 +50,11 @@ layerProperty = (obj, name, cssProperty, fallback, validator, transformer, optio
@_element.style[cssProperty] = LayerStyle[cssProperty](@)

set?(@, value)

# We try to not send any events while we run the constructor, it just
# doesn't make sense, because no one can listen to use yet.
return if @__constructor

@emit("change:#{name}", value)
@emit("change:point", value) if name in ["x", "y"]
@emit("change:size", value) if name in ["width", "height"]
Expand Down Expand Up @@ -79,8 +84,9 @@ class exports.Layer extends BaseClass
constructor: (options={}) ->

# Make sure we never call the constructor twice
throw Error("Layer.constructor #{@toInspect()} called twice") if @__constructed
@__constructed = true
throw Error("Layer.constructor #{@toInspect()} called twice") if @__constructorCalled
@__constructorCalled = true
@__constructor = true

# Set needed private variables
@_properties = {}
Expand Down Expand Up @@ -136,6 +142,8 @@ class exports.Layer extends BaseClass
@_stateMachine = new LayerStateMachine(@)
@_context.emit("layer:create", @)

delete @__constructor

##############################################################
# Properties

Expand Down Expand Up @@ -777,7 +785,7 @@ class exports.Layer extends BaseClass

# If there is no parent we need to walk through the root
if @parent is null
return _.filter @_context.getLayers(), (layer) =>
return _.filter @_context.layers, (layer) =>
layer isnt @ and layer.parent is null

return _.without @parent.children, @
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -24,7 +24,7 @@
"coverage-capture": "./test/coverage-capture",
"eventemitter3": "^1.1.1",
"gulp": "^3.8.11",
"gulp-mocha-phantomjs": "^0.10.1",
"gulp-mocha-phantomjs": "^0.11.0",
"gulp-plumber": "^1.0.1",
"gulp-rename": "^1.2.2",
"gulp-template": "^4.0.0",
Expand Down
36 changes: 18 additions & 18 deletions test/tests/BaseClassTest.coffee
Expand Up @@ -107,29 +107,29 @@ describe "BaseClass", ->
testClass = new TestClass3()
testClass.keys().should.eql ["testA", "testB"]

it "should create getters/setters", ->
# it "should create getters/setters", ->

class TestClass4 extends Framer.BaseClass
@define "testA", @simpleProperty "testA", 100
# class TestClass4 extends Framer.BaseClass
# @define "testA", @simpleProperty "testA", 100

testClass = new TestClass4()
testClass.setTestA(500)
testClass.getTestA().should.equal 500
testClass.testA.should.equal 500
# testClass = new TestClass4()
# testClass.setTestA(500)
# testClass.getTestA().should.equal 500
# testClass.testA.should.equal 500

it "should override getters/setters", ->
# it "should override getters/setters", ->

class TestClass5 extends Framer.BaseClass
@define "testA", @simpleProperty "testA", 100
# class TestClass5 extends Framer.BaseClass
# @define "testA", @simpleProperty "testA", 100

class TestClass6 extends TestClass5
setTestA: (value) ->
super value * 10
# class TestClass6 extends TestClass5
# setTestA: (value) ->
# super value * 10

testClass = new TestClass6()
testClass.setTestA(500)
testClass.getTestA().should.equal 5000
testClass.testA.should.equal 5000
# testClass = new TestClass6()
# testClass.setTestA(500)
# testClass.getTestA().should.equal 5000
# testClass.testA.should.equal 5000

it "should work with proxyProperties", ->

Expand Down Expand Up @@ -186,7 +186,7 @@ describe "BaseClass", ->
get: () -> "value"

instance = new TestClass()
(-> instance.testProp = "foo").should.throw "setting a property that has only a getter"
(-> instance.testProp = "foo").should.throw "TestClass.testProp is readonly"

it "should not set read-only prop via props setter", ->

Expand Down
6 changes: 3 additions & 3 deletions test/tests/ContextTest.coffee
Expand Up @@ -219,7 +219,7 @@ describe "Context", ->

context = new Framer.Context(name:"Test")
context.on "layer:create", ->
context.getLayers().length.should.equal 1
context.layers.length.should.equal 1
callback()

context.run ->
Expand All @@ -231,10 +231,10 @@ describe "Context", ->
context = new Framer.Context(name:"Test")

context.on "layer:create", ->
context.getLayers().length.should.equal 1
context.layers.length.should.equal 1

context.on "layer:destroy", ->
context.getLayers().length.should.equal 0
context.layers.length.should.equal 0
callback()

context.run ->
Expand Down
4 changes: 2 additions & 2 deletions test/tests/LayerTest.coffee
Expand Up @@ -525,7 +525,7 @@ describe "Layer", ->
layer.shadowBlur.should.equal 10
layer.shadowSpread.should.equal 10

layer.style.boxShadow.should.equal "rgba(123, 123, 123, 0.496094) 10px 10px 10px 10px"
layer.style.boxShadow.should.equal "rgba(123, 123, 123, 0.498039) 10px 10px 10px 10px"

# Only after we set a color a shadow should be drawn
layer.shadowColor = "red"
Expand Down Expand Up @@ -982,7 +982,7 @@ describe "Layer", ->
layer = new Layer
layer.destroy()

(layer in Framer.CurrentContext.getLayers()).should.be.false
(layer in Framer.CurrentContext.layers).should.be.false
assert.equal layer._element.parentNode, null

it "should set text", ->
Expand Down

0 comments on commit 8d9f4ea

Please sign in to comment.