-
Notifications
You must be signed in to change notification settings - Fork 9
/
findModule.coffee
49 lines (40 loc) · 1.75 KB
/
findModule.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
_getHierarchy = (layer) ->
string = ''
for a in layer.ancestors()
string = a.name+'>'+string
return string = string+layer.name
_match = (hierarchy, string) ->
# prepare regex tokens
string = string.replace(/\s*>\s*/g,'>') # clean up spaces around arrows
string = string.split('*').join('[^>]*') # asteriks as layer name wildcard
string = string.split(' ').join('(?:.*)>') # space as structure wildcard
string = string.split(',').join('$|') # allow multiple searches using comma
regexString = "(^|>)"+string+"$" # always bottom layer, maybe part of hierarchy
regExp = new RegExp(regexString)
return hierarchy.match(regExp)
_findAll = (selector, fromLayer) ->
layers = Framer.CurrentContext._layers
if selector?
stringNeedsRegex = _.find ['*',' ','>',','], (c) -> _.includes selector,c
unless stringNeedsRegex or fromLayer
layers = _.filter layers, (layer) ->
if layer.name is selector then true
else
layers = _.filter layers, (layer) ->
hierarchy = _getHierarchy(layer)
if fromLayer?
_match(hierarchy, fromLayer.name+' '+selector)
else
_match(hierarchy, selector)
else
layers
# Global
exports.Find = (selector, fromLayer) -> _findAll(selector, fromLayer)[0]
exports.ƒ = (selector, fromLayer) -> _findAll(selector, fromLayer)[0]
exports.FindAll = (selector, fromLayer) -> _findAll(selector, fromLayer)
exports.ƒƒ = (selector, fromLayer) -> _findAll(selector, fromLayer)
# Methods
Layer::find = (selector, fromLayer) -> _findAll(selector, @)[0]
Layer::ƒ = (selector, fromLayer) -> _findAll(selector, @)[0]
Layer::findAll = (selector, fromLayer) -> _findAll(selector, @)
Layer::ƒƒ = (selector, fromLayer) -> _findAll(selector, @)