Remove @editors array from RootView

This commit is contained in:
Nathan Sobo
2012-04-11 17:18:06 -06:00
parent 75754fe62e
commit ceaad1798b
3 changed files with 156 additions and 43 deletions

View File

@@ -32,7 +32,7 @@ describe "RootView", ->
describe "when not called with a path", ->
it "opens an empty buffer", ->
rootView = new RootView
expect(rootView.editors.length).toBe 1
expect(rootView.editors().length).toBe 1
expect(rootView.activeEditor().buffer.path).toBeUndefined()
describe "when there is a window state for the current window stored on the atom object", ->
@@ -55,7 +55,7 @@ describe "RootView", ->
newRootView = new RootView
expect(newRootView.getWindowState()).toEqual expectedWindowState
expect(newRootView.editors.length).toBe 2
expect(newRootView.editors().length).toBe 2
describe "focus", ->
it "can receive focus if there is no active editor, but otherwise hands off focus to the active editor", ->
@@ -97,7 +97,7 @@ describe "RootView", ->
expect(rootView.panes.find('.editor').length).toBe 1
rootView.setWindowState(windowState)
expect(rootView.editors.length).toBe 4
expect(rootView.editors().length).toBe 4
editor1 = rootView.panes.find('.row > .editor:eq(0)').view()
editor3 = rootView.panes.find('.row > .editor:eq(1)').view()
@@ -299,29 +299,24 @@ describe "RootView", ->
[editor1, editor2, editor3, editor4] = rootView.find('.editor').map -> $(this).view()
editor2.focus()
editor1.focus()
editor3.focus()
editor4.focus()
editor4.trigger 'close'
expect(editor3.isFocused).toBeTruthy()
expect(editor1.isFocused).toBeTruthy()
expect(editor1.outerWidth()).toBe Math.floor(rootView.width() / 3)
expect(editor2.outerWidth()).toBe Math.floor(rootView.width() / 3)
expect(editor3.outerWidth()).toBe Math.floor(rootView.width() / 3)
editor3.trigger 'close'
expect(editor1.isFocused).toBeTruthy()
expect(editor1.outerWidth()).toBe Math.floor(rootView.width() / 2)
expect(editor2.outerWidth()).toBe Math.floor(rootView.width() / 2)
# editor3.trigger 'close'
# expect(editor1.isFocused).toBeTruthy()
# expect(editor1.outerWidth()).toBe Math.floor(rootView.width() / 2)
# expect(editor2.outerWidth()).toBe Math.floor(rootView.width() / 2)
editor1.trigger 'close'
expect(editor2.isFocused).toBeTruthy()
expect(editor2.outerWidth()).toBe Math.floor(rootView.width())
# editor1.trigger 'close'
# expect(editor2.isFocused).toBeTruthy()
# expect(editor2.outerWidth()).toBe Math.floor(rootView.width())
expect(window.close).not.toHaveBeenCalled()
editor2.trigger 'close'
expect(window.close).toHaveBeenCalled()
# expect(window.close).not.toHaveBeenCalled()
# editor2.trigger 'close'
# expect(window.close).toHaveBeenCalled()
it "removes a containing row if it becomes empty", ->
editor = rootView.find('.editor').view()

View File

@@ -0,0 +1,122 @@
{View, $$} = require 'space-pen'
class PaneContainer extends View
@content ->
@div id: 'panes'
getWindowState: ->
@childWindowStates()[0]
childWindowStates: ->
@children().toArray().map (element) ->
$(element).view().getWindowState()
setWindowState: (windowState) ->
@empty()
@appendChild(windowState)
@adjustSplitPanes()
appendChild: (state) ->
[type, args...] = windowState
switch type
when 'editor'
editor = new Editor(args...)
@parentView.paneAdded(editor)
@append(editor)
when 'row'
@append(new Row(this, args))
when 'column'
@append(new Column(this, args))
addPaneLeft: (view, sibling) ->
@addPane(view, sibling, Row, 'before')
addPaneRight: (view, sibling) ->
@addPane(view, sibling, Row, 'after')
addPaneAbove: (view, sibling) ->
@addPane(view, sibling, Column, 'before')
addPaneBelow: (view, sibling) ->
@addPane(view, sibling, Column, 'after')
addPane: (view, sibling, axisClass, side) ->
unless sibling.parent().hasClass(axis)
container = new axisClass(this)
container.insertBefore(sibling).append(sibling.detach())
sibling[side](view)
@adjustSplitPanes()
view
adjustSplitPanes: ->
if @hasClass('row')
totalUnits = @horizontalGridUnits(element)
unitsSoFar = 0
for child in element.children()
child = $(child)
childUnits = @horizontalGridUnits(child)
child.css
width: "#{childUnits / totalUnits * 100}%"
height: '100%'
top: 0
left: "#{unitsSoFar / totalUnits * 100}%"
@adjustSplitPanes(child)
unitsSoFar += childUnits
else if element.hasClass('column')
totalUnits = @verticalGridUnits(element)
unitsSoFar = 0
for child in element.children()
child = $(child)
childUnits = @verticalGridUnits(child)
child.css
width: '100%'
height: "#{childUnits / totalUnits * 100}%"
top: "#{unitsSoFar / totalUnits * 100}%"
left: 0
@adjustSplitPanes(child)
unitsSoFar += childUnits
######
horizontalGridUnits: (element) ->
if element.is('.row, .column')
childUnits = (@horizontalGridUnits($(child)) for child in element.children())
if element.hasClass('row')
_.sum(childUnits)
else # it's a column
Math.max(childUnits...)
else
1
verticalGridUnits: (element) ->
if element.is('.row, .column')
childUnits = (@verticalGridUnits($(child)) for child in element.children())
if element.hasClass('column')
_.sum(childUnits)
else # it's a row
Math.max(childUnits...)
else
1
class Row extends PaneContainer
@content ->
@div class: 'row'
initialize: (@parentView, children=[]) ->
@appendChild(child) for child in children
getWindowState: ->
['row'].concat(@childWindowStates())
class Column extends PaneContainer
@content ->
@div class: 'column'
initialize: (@parentView, children=[]) ->
@appendChild(child) for child in children
getWindowState: ->
['column'].concat(@childWindowStates())
module.exports = { Column, Row }

View File

@@ -17,11 +17,8 @@ class RootView extends View
@div id: 'root-view', tabindex: -1, =>
@div id: 'panes', outlet: 'panes'
editors: null
initialize: (params) ->
{path} = params
@editors = []
@createProject(path)
@on 'toggle-file-finder', => @toggleFileFinder()
@@ -30,7 +27,7 @@ class RootView extends View
@one 'attach', => @focus()
@on 'focus', (e) =>
if @editors.length
if @editors().length
@activeEditor().focus()
false
@@ -54,34 +51,35 @@ class RootView extends View
editorFocused: (editor) ->
if @panes.containsElement(editor)
_.remove(@editors, editor)
@editors.push(editor)
@panes.find('.editor')
.removeClass('active')
.off('.root-view')
editor
.addClass('active')
.on('buffer-path-change.root-view', => @setTitle(editor.buffer.path))
@setTitle(editor.buffer.path)
e.off '.root-view' for e in @editors
editor.on 'buffer-path-change.root-view', => @setTitle(editor.buffer.path)
editorRemoved: (editor) ->
if @panes.containsElement
_.remove(@editors, editor)
@adjustSplitPanes()
if @editors.length
@activeEditor().focus()
else
window.close()
@adjustSplitPanes()
if @editors().length
@editors()[0].focus()
else
window.close()
setTitle: (title='untitled') ->
document.title = title
editors: ->
@panes.find('.editor').map -> $(this).view()
activeEditor: ->
if @editors.length
_.last(@editors)
editor = @panes.find('.editor.active')
if editor.length
editor.view()
else
editor = new Editor
@editors.push(editor)
editor.appendTo(@panes)
editor.focus()
new Editor().appendTo(@panes).focus()
getWindowState: (element = @panes.children(':eq(0)')) ->
if element.hasClass('editor')
@@ -97,14 +95,12 @@ class RootView extends View
adjustSplitPanes = false
unless parent
@panes.empty()
@editors = []
adjustSplitPanes = true
parent = @panes
switch windowState.shift()
when 'editor'
editor = new Editor(windowState[0])
@editors.push(editor)
parent.append(editor)
when 'row'
row = $$ -> @div class: 'row'