Permalink
Browse files

Merge pull request #285 from koenbok/child-parent

Change hierarchy API: superLayer/subLayers → parent/children
  • Loading branch information...
koenbok committed Jan 11, 2016
2 parents 83b7c3d + feef674 commit 7296de7d34b49b2aa18da7669b8f5acf1c14f0bc
@@ -17,7 +17,7 @@ class exports.BackgroundLayer extends Layer
@_context.domEventManager.wrap(window).addEventListener("resize", @layout)
layout: =>
if @superLayer
@frame = @superLayer.frame
if @parent
@frame = @parent.frame
else
@frame = @_context.frame
View
@@ -17,16 +17,16 @@ class CompatLayer extends Layer
constructor: (options={}) ->
if options.hasOwnProperty "superView"
options.superLayer = options.superView
options.parent = options.superView
super options
@define "superView", compatProperty "superLayer", "superView"
@define "subViews", compatProperty "subLayers", "subViews"
@define "superView", compatProperty "parent", "superView"
@define "subViews", compatProperty "children", "subViews"
@define "siblingViews", compatProperty "siblingLayers", "siblingViews"
addSubView = (layer) -> @addSubLayer layer
removeSubView = (layer) -> @removeSubLayer layer
addSubView = (layer) -> @addChild layer
removeSubView = (layer) -> @removeChild layer
class CompatView extends CompatLayer
@@ -80,11 +80,11 @@ class exports.DeviceComponent extends BaseClass
@background.backgroundColor = "transparent"
@background.classList.add("DeviceBackground")
# @phone = new Layer superLayer:@background
# @phone = new Layer parent:@background
@phone = new Layer
@screen = new Layer superLayer:@phone
@viewport = new Layer superLayer:@screen
@content = new Layer superLayer:@viewport
@screen = new Layer parent:@phone
@viewport = new Layer parent:@screen
@content = new Layer parent:@viewport
@phone.backgroundColor = "transparent"
@phone.classList.add("DevicePhone")
@@ -101,7 +101,7 @@ class exports.DeviceComponent extends BaseClass
@content.originX = 0
@content.originY = 0
@keyboardLayer = new Layer superLayer:@viewport
@keyboardLayer = new Layer parent:@viewport
@keyboardLayer.on "click", => @toggleKeyboard()
@keyboardLayer.classList.add("DeviceKeyboard")
@keyboardLayer.backgroundColor = "transparent"
@@ -34,7 +34,7 @@ class exports.PageComponent extends ScrollComponent
@on(Events.ScrollEnd, @_scrollEnd)
@content.on("change:frame", _.debounce(@_scrollMove, 16))
@content.on("change:subLayers", @_resetHistory)
@content.on("change:children", @_resetHistory)
@_resetHistory()
@@ -52,10 +52,10 @@ class exports.PageComponent extends ScrollComponent
if !withoutCurrentPage
point = {x:@scrollX + (@originX * @width), y:@scrollY + (@originY * @height)}
layers = @content.subLayersAbove(point, @originX, @originY) if direction in ["up", "top", "north"]
layers = @content.subLayersBelow(point, @originX, @originY) if direction in ["down", "bottom", "south"]
layers = @content.subLayersLeft(point, @originX, @originY) if direction in ["left", "west"]
layers = @content.subLayersRight(point, @originX, @originY) if direction in ["right", "east"]
layers = @content.childrenAbove(point, @originX, @originY) if direction in ["up", "top", "north"]
layers = @content.childrenBelow(point, @originX, @originY) if direction in ["down", "bottom", "south"]
layers = @content.childrenLeft(point, @originX, @originY) if direction in ["left", "west"]
layers = @content.childrenRight(point, @originX, @originY) if direction in ["right", "east"]
# See if there is one close by that we should go to
if withoutCurrentPage
@@ -99,18 +99,18 @@ class exports.PageComponent extends ScrollComponent
throw new Error("#{direction} should be in #{directions}")
# For allowing pages added with 'addPage' to behave consistently with pages added
# to the PageComponent using 'superLayer', keep the original page point
# to the PageComponent using 'parent', keep the original page point
# so one of the two coordinates is left untouched after the page is added
point = page.point
if @content.subLayers.length
if @content.children.length
point.x = Utils.frameGetMaxX(@content.contentFrame()) if direction in ["right", "east"]
point.y = Utils.frameGetMaxY(@content.contentFrame()) if direction in ["down", "bottom", "south"]
page.point = point
if page.superLayer isnt @content
page.superLayer = @content
if page.parent isnt @content
page.parent = @content
else
@updateContent()
@@ -124,10 +124,10 @@ class exports.PageComponent extends ScrollComponent
@content.on(Events.AnimationStop, @_onAminationEnd)
horizontalPageIndex: (page) ->
(_.sortBy(@content.subLayers, (l) -> l.x)).indexOf(page)
(_.sortBy(@content.children, (l) -> l.x)).indexOf(page)
verticalPageIndex: (page) ->
(_.sortBy(@content.subLayers, (l) -> l.y)).indexOf(page)
(_.sortBy(@content.children, (l) -> l.y)).indexOf(page)
_scrollStart: =>
@_currentPage = @currentPage
@@ -114,12 +114,12 @@ class exports.ScrollComponent extends Layer
@_content.destroy() if @content
@_content = layer
@_content.superLayer = @
@_content.parent = @
@_content.name = "content"
@_content.clip = false
@_content.draggable.enabled = true
@_content.draggable.momentum = true
@_content.on("change:subLayers", @updateContent)
@_content.on("change:children", @updateContent)
# Update the content view size on resizing the ScrollComponent
@on("change:width", @updateContent)
@@ -158,10 +158,10 @@ class exports.ScrollComponent extends Layer
@content.draggable.constraints = constraintsFrame
# Change the default background color if we added subLayers. We keep the default
# Change the default background color if we added children. We keep the default
# color around until you set a content layer so you can see the ScrollComponent
# on your screen after creation.
if @content.subLayers.length
if @content.children.length
if @content.backgroundColor?.isEqual(Framer.Defaults.Layer.backgroundColor)
@content.backgroundColor = null
@@ -267,10 +267,10 @@ class exports.ScrollComponent extends Layer
scrollToLayer: (contentLayer, originX=0, originY=0, animate=true, animationOptions={curve:"spring(500,50,0)"}) ->
if contentLayer and contentLayer.superLayer isnt @content
if contentLayer and contentLayer.parent isnt @content
throw Error("This layer is not in the scroll component content")
if not contentLayer or @content.subLayers.length == 0
if not contentLayer or @content.children.length == 0
scrollPoint = {x:0, y:0}
else
scrollPoint = @_scrollPointForLayer(contentLayer, originX, originY)
@@ -301,7 +301,7 @@ class exports.ScrollComponent extends Layer
return Utils.framePointForOrigin(layer, originX, originY)
_contentLayersSortedByDistanceForScrollPoint: (scrollPoint, originX=0, originY=0) ->
return Utils.frameSortByAbsoluteDistance(scrollPoint, @content.subLayers, originX, originY)
return Utils.frameSortByAbsoluteDistance(scrollPoint, @content.children, originX, originY)
_pointInConstraints: (point) ->
@@ -376,7 +376,7 @@ class exports.ScrollComponent extends Layer
copy: ->
copy = super
contentLayer = _.first(_.without(copy.subLayers, copy.content))
contentLayer = _.first(_.without(copy.children, copy.content))
copy.setContentLayer(contentLayer)
copy.props = @props
return copy
@@ -404,18 +404,18 @@ wrapComponent = (instance, layer, options = {correct:true}) ->
# correct that here.
if options.correct is true
if layer.subLayers.length is 0
if layer.children.length is 0
wrapper = new Layer
wrapper.name = "ScrollComponent"
wrapper.frame = layer.frame
layer.superLayer = wrapper
layer.parent = wrapper
layer.x = layer.y = 0
layer = wrapper
# console.info "Corrected the scroll component without sub layers"
scroll.frame = layer.frame
scroll.superLayer = layer.superLayer
scroll.parent = layer.parent
scroll.index = layer.index
# Copy over the name, if we don't have it try to use the variable
@@ -57,7 +57,7 @@ class exports.SliderComponent extends Layer
super options
@knobSize = options.knobSize or 30
@knob.superLayer = @fill.superLayer = @knobOverlay.superLayer = @sliderOverlay.superLayer = @
@knob.parent = @fill.parent = @knobOverlay.parent = @sliderOverlay.parent = @
# Set fill initially
if @width > @height
View
@@ -69,8 +69,8 @@ class exports.Importer
# Pass three, insert the layers into the dom
# (they were not inserted yet because of the shadow keyword)
for layer in @_createdLayers
if not layer.superLayer
layer.superLayer = null
if not layer.parent
layer.parent = null
return @_createdLayersByName
@@ -87,7 +87,7 @@ class exports.Importer
return Framer.Utils.domLoadJSONSync @paths.layerInfo
_createLayer: (info, superLayer) ->
_createLayer: (info, parent) ->
# Resize the layer frames
info.layerFrame = resizeFrame(@scale, info.layerFrame) if info.layerFrame
@@ -125,10 +125,10 @@ class exports.Importer
# Figure out what the super layer should be. If this layer has a contentLayer
# (like a scroll view) we attach it to that instead.
if superLayer?.contentLayer
layerInfo.superLayer = superLayer.contentLayer
else if superLayer
layerInfo.superLayer = superLayer
if parent?.contentLayer
layerInfo.parent = parent.contentLayer
else if parent
layerInfo.parent = parent
# Layer names cannot start with a number
if startsWithNumber(layerInfo.name)
@@ -149,7 +149,7 @@ class exports.Importer
if layerInfo.name.toLowerCase().indexOf("draggable") != -1
layer.draggable.enabled = true
# A layer without an image, mask or sublayers should be zero
# A layer without an image, mask or children should be zero
if not layer.image and not info.children.length and not info.maskFrame
layer.frame = Utils.frameZero()
@@ -198,13 +198,13 @@ class exports.Importer
traverse = (layer) ->
if layer.superLayer
layer.frame = Utils.convertPoint(layer.frame, null, layer.superLayer)
if layer.parent
layer.frame = Utils.convertPoint(layer.frame, null, layer.parent)
for subLayer in layer.subLayers
traverse(subLayer)
for child in layer.children
traverse(child)
if not layer.superLayer
if not layer.parent
traverse(layer)
exports.Importer.load = (path, scale) ->
Oops, something went wrong.

0 comments on commit 7296de7

Please sign in to comment.