Add a PaneContainer subview for RootView

PaneContainer is responsible for all pane-related logic. Laying them
out, switching focus between them, etc. This should help make RootView
simpler and keep pane-layout related tests in their own focused area.
This commit is contained in:
Nathan Sobo
2013-02-20 22:28:43 -07:00
committed by probablycorey
parent 9ecb03e470
commit fee835f899
8 changed files with 192 additions and 226 deletions

View File

@@ -0,0 +1,41 @@
{View} = require 'space-pen'
$ = require 'jquery'
module.exports =
class PaneContainer extends View
registerDeserializer(this)
@deserialize: ({root}) ->
container = new PaneContainer
container.append(deserialize(root)) if root
container
@content: ->
@div id: 'panes'
serialize: ->
deserializer: 'PaneContainer'
root: @getRoot()?.serialize()
focusNextPane: ->
panes = @getPanes()
currentIndex = panes.indexOf(@getFocusedPane())
nextIndex = (currentIndex + 1) % panes.length
panes[nextIndex].focus()
getRoot: ->
@children().first().view()
getPanes: ->
@find('.pane').toArray().map (node)-> $(node).view()
getFocusedPane: ->
@find('.pane:has(:focus)').view()
adjustPaneDimensions: ->
if root = @getRoot()
root.css(width: '100%', height: '100%', top: 0, left: 0)
root.adjustDimensions()
afterAttach: ->
@adjustPaneDimensions()

View File

@@ -125,27 +125,31 @@ class Pane extends View
items = [@copyCurrentItem()] unless items.length
pane = new Pane(items...)
this[side](pane)
rootView?.adjustPaneDimensions()
@getContainer().adjustPaneDimensions()
pane.focus()
pane
buildPaneAxis: (axis) ->
switch axis
when 'row' then new PaneRow
when 'column' then new PaneColumn
getContainer: ->
@closest('#panes').view()
copyCurrentItem: ->
deserialize(@currentItem.serialize())
remove: (selector, keepData) ->
return super if keepData
# find parent elements before removing from dom
container = @getContainer()
parentAxis = @parent('.row, .column')
super
if parentAxis.children().length == 1
sibling = parentAxis.children().detach()
parentAxis.replaceWith(sibling)
rootView?.adjustPaneDimensions()
container.adjustPaneDimensions()
afterRemove: ->
item.destroy?() for item in @getItems()
buildPaneAxis: (axis) ->
switch axis
when 'row' then new PaneRow
when 'column' then new PaneColumn

View File

@@ -10,6 +10,7 @@ Project = require 'project'
Pane = require 'pane'
PaneColumn = require 'pane-column'
PaneRow = require 'pane-row'
PaneContainer = require 'pane-container'
module.exports =
class RootView extends View
@@ -19,17 +20,16 @@ class RootView extends View
ignoredNames: [".git", ".svn", ".DS_Store"]
disabledPackages: []
@content: ->
@content: ({panes}) ->
@div id: 'root-view', =>
@div id: 'horizontal', outlet: 'horizontal', =>
@div id: 'vertical', outlet: 'vertical', =>
@div id: 'panes', outlet: 'panes'
@subview 'panes', panes ? new PaneContainer
@deserialize: ({ panesViewState, packageStates, projectPath }) ->
atom.atomPackageStates = packageStates ? {}
rootView = new RootView
rootView.setRootPane(deserialize(panesViewState)) if panesViewState
rootView
panes = deserialize(panesViewState) if panesViewState?.deserializer is 'PaneContainer'
new RootView({panes})
title: null
@@ -67,7 +67,7 @@ class RootView extends View
serialize: ->
deserializer: 'RootView'
panesViewState: @panes.children().view()?.serialize()
panesViewState: @panes.serialize()
packageStates: atom.serializeAtomPackages()
handleFocus: (e) ->
@@ -170,24 +170,8 @@ class RootView extends View
getActiveEditSession: ->
@getActiveEditor()?.activeEditSession
focusNextPane: ->
panes = @panes.find('.pane')
currentIndex = panes.toArray().indexOf(@getFocusedPane()[0])
nextIndex = (currentIndex + 1) % panes.length
panes.eq(nextIndex).view().focus()
getFocusedPane: ->
@panes.find('.pane:has(:focus)')
setRootPane: (pane) ->
@panes.empty()
@panes.append(pane)
@adjustPaneDimensions()
adjustPaneDimensions: ->
rootPane = @panes.children().first().view()
rootPane?.css(width: '100%', height: '100%', top: 0, left: 0)
rootPane?.adjustDimensions()
focusNextPane: -> @panes.focusNextPane()
getFocusedPane: -> @panes.getFocusedPane()
remove: ->
editor.remove() for editor in @getEditors()

View File

@@ -151,6 +151,9 @@ window.registerDeserializers = (args...) ->
window.registerDeserializer = (klass) ->
deserializers[klass.name] = klass
window.unregisterDeserializer = (klass) ->
delete deserializers[klass.name]
window.deserialize = (state) ->
deserializers[state?.deserializer]?.deserialize(state)