WIP: Split panes can be closed with meta-w

Still some issues… view gets screwed up after closing panes in certain layout arrangements.
This commit is contained in:
Nathan Sobo
2012-03-20 12:29:47 -06:00
parent e74f0e66ac
commit 285c04ea62
4 changed files with 56 additions and 6 deletions

View File

@@ -198,14 +198,39 @@ describe "RootView", ->
expect(editor5.position().top).toBe editor4.outerHeight()
expect(editor5.outerHeight()).toBe Math.floor(1/3 * rootView.height())
describe "when close is triggered on an editor pane", ->
it "adjusts the layout, focuses the next most-recently active editor, and closes the window when there are no remaining editors", ->
spyOn(window, 'close')
editor = rootView.find('.editor').view()
editor.trigger 'split-right'
editor.trigger 'split-right'
editor.trigger 'split-right'
[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.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)
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()
describe ".addPane(view)", ->
it "adds the given view to the rootView (at the bottom by default)", ->

View File

@@ -99,6 +99,7 @@ class Editor extends View
@on 'split-right', => @splitRight()
@on 'split-up', => @splitUp()
@on 'split-down', => @splitDown()
@on 'close', => @remove(); false
buildCursorAndSelection: ->
@cursor = new Cursor(this)
@@ -113,6 +114,7 @@ class Editor extends View
false
@hiddenInput.on 'focus', =>
@rootView()?.editorFocused(this)
@isFocused = true
@addClass 'focused'
@@ -157,6 +159,9 @@ class Editor extends View
@setMaxLineLength() if @softWrap
@focus()
rootView: ->
@parents('#root-view').view()
selectTextOnMouseMovement: ->
moveHandler = (e) => @selectToScreenPosition(@screenPositionFromMouseEvent(e))
@on 'mousemove', moveHandler
@@ -440,8 +445,11 @@ class Editor extends View
@parents('#root-view').view().adjustSplitPanes()
remove: (selector, keepData) ->
@unsubscribeFromBuffer() unless keepData
return super if keepData
@unsubscribeFromBuffer()
rootView = @rootView()
super
rootView?.editorRemoved(this)
unsubscribeFromBuffer: ->
@buffer.off ".editor#{@id}"

View File

@@ -15,7 +15,10 @@ class RootView extends View
@div id: 'root-view', =>
@subview 'editor', new Editor
editors: null
initialize: ({url}) ->
@editors = []
@editor.keyEventHandler = window.keymap
@createProject(url)
@@ -36,10 +39,24 @@ class RootView extends View
addPane: (view) ->
@append(view)
editorFocused: (editor) ->
_.remove(@editors, editor)
@editors.push(editor)
editorRemoved: (editor) ->
_.remove(@editors, editor)
@adjustSplitPanes()
if @editors.length
@focusLastActiveEditor()
else
window.close()
focusLastActiveEditor: ->
_.last(@editors).focus()
adjustSplitPanes: (element = @children(':first'))->
if element.hasClass('row')
totalUnits = @horizontalGridUnits(element)
console.log totalUnits
unitsSoFar = 0
for child in element.children()
child = $(child)
@@ -54,7 +71,6 @@ class RootView extends View
else if element.hasClass('column')
totalUnits = @verticalGridUnits(element)
console.log "total vertical", totalUnits
unitsSoFar = 0
for child in element.children()
child = $(child)

View File

@@ -2,7 +2,8 @@ _ = require 'underscore'
_.mixin
remove: (array, element) ->
array.splice(array.indexOf(element), 1)
index = array.indexOf(element)
array.splice(index, 1) if index >= 0
sum: (array) ->
sum = 0