Permalink
Browse files

Improving performance of sendToBack and bringToFront

  • Loading branch information...
nvh committed Sep 5, 2017
1 parent 7994d36 commit ac2d0b24199368fd937bfb5e2bff8f9b6f2a8a8e
Showing with 78 additions and 21 deletions.
  1. +21 −3 framer/Layer.coffee
  2. +57 −18 test/tests/LayerTest.coffee
View
@@ -1053,7 +1053,7 @@ class exports.Layer extends BaseClass
# Remove from previous parent children
if @_parent
@_parent._children = _.without @_parent._children, @
@_parent._children = _.pull @_parent._children, @
@_parent._element.removeChild @_element
@_parent.emit "change:children", {added: [], removed: [@]}
@_parent.emit "change:subLayers", {added: [], removed: [@]}
@@ -1275,10 +1275,28 @@ class exports.Layer extends BaseClass
## INDEX ORDERING
bringToFront: ->
@index = _.max(_.union([0], @siblingLayers.map (layer) -> layer.index)) + 1
maxIndex = null
siblings = @parent?.children ? @context.layers
return if siblings.count <= 1
for layer in siblings
continue if layer is @
maxIndex ?= layer.index
if layer.index > maxIndex
maxIndex = layer.index
if maxIndex?
@index = maxIndex + 1
sendToBack: ->
@index = _.min(_.union([0], @siblingLayers.map (layer) -> layer.index)) - 1
minIndex = null
siblings = @parent?.children ? @context.layers
return if siblings.count <= 1
for layer in siblings
continue if layer is @
minIndex ?= layer.index
if layer.index < minIndex
minIndex = layer.index
if minIndex?
@index = minIndex - 1
placeBefore: (layer) ->
return if layer not in @siblingLayers
@@ -1057,8 +1057,8 @@ describe "Layer", ->
layerB = new Layer superLayer: layerA
layerC = new Layer superLayer: layerA
assert.equal layerB.index, 1
assert.equal layerC.index, 2
assert.equal layerB.index, 0
assert.equal layerC.index, 1
it "should send back and front", ->
@@ -1068,41 +1068,80 @@ describe "Layer", ->
layerC.sendToBack()
assert.equal layerB.index, 1
assert.equal layerC.index, -1
assert.equal layerB.index, 0
assert.equal layerC.index, -1
layerC.bringToFront()
assert.equal layerB.index, 1
assert.equal layerC.index, 2
assert.equal layerB.index, 0
assert.equal layerC.index, 1
it "should take the layer index below the mininum layer index", ->
layerA = new Layer
layerB = new Layer
parent: layerA
index: 10
layerC = new Layer
parent: layerA
index: 11
layerC.sendToBack()
assert.equal layerB.index, 10
assert.equal layerC.index, 9
it "should handle negative values correctly when sending to back", ->
layerA = new Layer
layerB = new Layer
parent: layerA
index: -3
layerC = new Layer
parent: layerA
index: -2
layerC.sendToBack()
assert.equal layerB.index, -3
assert.equal layerC.index, -4
it "should leave the index alone if it is the only layer", ->
layerA = new Layer
layerB = new Layer
parent: layerA
index: 10
layerB.sendToBack()
layerB.bringToFront()
assert.equal layerB.index, 10
it "should place in front", ->
layerA = new Layer
layerB = new Layer superLayer: layerA # 1
layerC = new Layer superLayer: layerA # 2
layerD = new Layer superLayer: layerA # 3
layerB = new Layer superLayer: layerA # 0
layerC = new Layer superLayer: layerA # 1
layerD = new Layer superLayer: layerA # 2
layerB.placeBefore layerC
assert.equal layerB.index, 2
assert.equal layerC.index, 1
assert.equal layerD.index, 3
assert.equal layerB.index, 1
assert.equal layerC.index, 0
assert.equal layerD.index, 2
it "should place behind", ->
layerA = new Layer
layerB = new Layer superLayer: layerA # 1
layerC = new Layer superLayer: layerA # 2
layerD = new Layer superLayer: layerA # 3
layerB = new Layer superLayer: layerA # 0
layerC = new Layer superLayer: layerA # 1
layerD = new Layer superLayer: layerA # 2
layerC.placeBehind layerB
# TODO: Still something fishy here, but it works
assert.equal layerB.index, 2
assert.equal layerC.index, 1
assert.equal layerD.index, 4
assert.equal layerB.index, 1
assert.equal layerC.index, 0
assert.equal layerD.index, 3
it "should get a children by name", ->

0 comments on commit ac2d0b2

Please sign in to comment.