Permalink
Browse files

Merge pull request #456 from koenbok/feature/layer-selector

Feature/layer selector
  • Loading branch information...
nvh committed Jun 9, 2017
2 parents d511624 + cd057d4 commit c9450ede9ed7690dabb70038d9666cb0d43b4e6a
Showing with 128 additions and 0 deletions.
  1. +6 −0 framer/Context.coffee
  2. +12 −0 framer/Layer.coffee
  3. +26 −0 framer/Utils.coffee
  4. +12 −0 test/tests/ContextTest.coffee
  5. +35 −0 test/tests/LayerTest.coffee
  6. +37 −0 test/tests/UtilsTest.coffee
View
@@ -150,6 +150,12 @@ class exports.Context extends BaseClass
return layer if layer
return @layerForElement(element.parentNode)
selectLayer: (selector) ->
Utils.findLayer(@_layers, selector)
selectAllLayers: (selector) ->
Utils.filterLayers(@_layers, selector)
layout: =>
@rootLayers.map (l) -> l.layout()
View
@@ -823,6 +823,18 @@ class exports.Layer extends BaseClass
querySelector: (query) -> @_element.querySelector(query)
querySelectorAll: (query) -> @_element.querySelectorAll(query)
selectChild: (selector) ->
Utils.findLayer(@descendants, selector)
selectAllChildren: (selector) ->
Utils.filterLayers(@descendants, selector)
@select: (selector) ->
Framer.CurrentContext.selectLayer(selector)
@selectAll: (selector) ->
Framer.CurrentContext.selectAllLayers(selector)
destroy: ->
# Todo: check this
View
@@ -284,6 +284,32 @@ Utils.uuid = ->
output.join ""
Utils.findLayer = (layers, selector) ->
_.find layers, (layer) -> Utils.layerMatchesSelector(layer, selector)
Utils.filterLayers = (layers, selector) ->
_.filter layers, (layer) -> Utils.layerMatchesSelector(layer, selector)
Utils.layerMatchesSelector = (layer, selector) ->
getHierarchyString = (l) ->
# create a string of the hierarchy so we can run regex on it
nameArr = _.pluck(l.ancestors().reverse(), 'name')
return nameArr.join('>') + ">#{layer.name}"
hierarchyMatch = (hierarchy, string) ->
string = string.replace(/\s*>\s*/g, '>') # clean spaces around >
string = string.split('*').join('[^>]*') # anything but >
string = string.split(' ').join('(?:.*)>') # anything but ends with >
string = string.split(',').join('$|') # or
regexString = "(^|>)"+string+"$"
regExp = new RegExp(regexString)
return regExp.test(hierarchy)
if selector
hierarchy = getHierarchyString(layer, selector)
return hierarchyMatch(hierarchy, selector)
Utils.arrayFromArguments = (args) ->
# Convert an arguments object to an array
return args[0] if _.isArray(args[0])
@@ -205,7 +205,19 @@ describe "Context", ->
layerA = new Layer
context.layerForElement(layerA._element).should.equal layerA
it "should implement selectLayer", ->
context = new Framer.Context(name: "Test")
context.run ->
layerA = new Layer name: "layerA"
context.selectLayer("layerA").should.eql layerA
it "should implement selectAllLayers", ->
context = new Framer.Context(name: "Test")
context.run ->
layerA = new Layer name: "layerA"
context.selectAllLayers("layerA").should.eql [layerA]
describe "Events", ->
@@ -878,6 +878,41 @@ describe "Layer", ->
layerC = new Layer superLayer: layerB
layerC.ancestors().should.eql [layerB, layerA]
describe "Select Layer", ->
beforeEach ->
Framer.CurrentContext.reset()
it "should select the layer named B", ->
layerA = new Layer name: 'A'
layerB = new Layer name: 'B', parent: layerA
layerA.selectChild('B').should.equal layerB
it "should select the layer named C", ->
layerA = new Layer name: 'A'
layerB = new Layer name: 'B', parent: layerA
layerC = new Layer name: 'C', parent: layerB
layerA.selectChild('B > *').should.equal layerC
it "should have a static method `select`", ->
layerA = new Layer name: 'A'
layerB = new Layer name: 'B', parent: layerA
layerC = new Layer name: 'C', parent: layerB
Layer.select('B > *').should.equal layerC
it "should have a method `selectAllChildren`", ->
layerA = new Layer name: 'A'
layerB = new Layer name: 'B', parent: layerA
layerC = new Layer name: 'C', parent: layerB
layerD = new Layer name: 'D', parent: layerB
layerA.selectAllChildren('B > *').should.eql [layerC, layerD]
it "should have a static method `selectAll`", ->
layerA = new Layer name: 'A'
layerB = new Layer name: 'B', parent: layerA # asdas
layerC = new Layer name: 'C', parent: layerB
layerD = new Layer name: 'D', parent: layerB
Layer.selectAll('A *').should.eql [layerB, layerC, layerD]
describe "Frame", ->
@@ -193,6 +193,43 @@ describe "Utils", ->
# test = -> Utils.domLoadDataSync("static/nonexisting.txt")
# test.should.throw()
describe "layerMatchesSelector", ->
it "should match exact", ->
layerA = new Layer name: "layerA"
Utils.layerMatchesSelector(layerA, 'layerA').should.equal true
it "should match anything below", ->
layerA = new Layer name: "layerA"
layerB = new Layer name: "layerB", parent: layerA
layerC = new Layer name: "layerC", parent: layerB
Utils.layerMatchesSelector(layerB, 'layerA > *').should.equal true
it "should match descendant with wildcard", ->
layerA = new Layer name: "layerA"
layerB = new Layer name: "layerB", parent: layerA
layerC = new Layer name: "layerC", parent: layerB
Utils.layerMatchesSelector(layerC, 'layerA layer*').should.equal true
it "should match containing", ->
layerA = new Layer name: "layerA1"
layerB = new Layer name: "layerB1", parent: layerA
layerC = new Layer name: "layerC1", parent: layerB
Utils.layerMatchesSelector(layerB, '*rB*').should.equal true
it "should match multiple direct children", ->
layerA = new Layer name: "layerA"
layerB = new Layer name: "layerB", parent: layerA
layerC = new Layer name: "layerC", parent: layerB
Utils.layerMatchesSelector(layerC, 'layerA>layerB>layerC').should.equal true
it "should match multiple direct children", ->
layerA = new Layer name: "layerA"
layerB = new Layer name: "layerB", parent: layerA
layerC = new Layer name: "layerC", parent: layerB
Utils.layerMatchesSelector(layerA, 'layerB,layerC').should.equal false
Utils.layerMatchesSelector(layerB, 'layerB,layerC').should.equal true
Utils.layerMatchesSelector(layerC, 'layerB,layerC').should.equal true
describe "modulate", ->
it "should have the right results", ->

0 comments on commit c9450ed

Please sign in to comment.