Add openSingletonSync() to rootView

This commit is contained in:
Ben Ogle
2013-10-29 18:39:44 -07:00
parent a212a7c259
commit 1be8d5e618
3 changed files with 99 additions and 18 deletions

View File

@@ -210,7 +210,7 @@ describe "RootView", ->
rootView.trigger 'window:decrease-font-size'
expect(config.get('editor.fontSize')).toBe 1
describe ".open(filePath, options)", ->
describe ".openSync(filePath, options)", ->
describe "when there is no active pane", ->
beforeEach ->
spyOn(Pane.prototype, 'focus')
@@ -242,6 +242,12 @@ describe "RootView", ->
editSession = rootView.openSync('b', changeFocus: false)
expect(rootView.getActivePane().focus).not.toHaveBeenCalled()
describe "when the split option is 'right'", ->
it "creates a new pane and opens the file in said pane", ->
editSession = rootView.openSync('b', split: 'right')
expect(rootView.getActivePane().activeItem).toBe editSession
expect(editSession.getPath()).toBe require.resolve('./fixtures/dir/b')
describe "when there is an active pane", ->
[activePane, initialItemCount] = []
beforeEach ->
@@ -284,7 +290,55 @@ describe "RootView", ->
editSession = rootView.openSync('b', changeFocus: false)
expect(activePane.focus).not.toHaveBeenCalled()
describe ".openAsync(filePath)", ->
describe "when the split option is 'right'", ->
it "creates a new pane and opens the file in said pane", ->
pane1 = rootView.getActivePane()
editSession = rootView.openSync('b', split: 'right')
pane2 = rootView.getActivePane()
expect(pane2[0]).not.toBe pane1[0]
expect(editSession.getPath()).toBe require.resolve('./fixtures/dir/b')
expect(rootView.panes.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0]]
editSession = rootView.openSync('file1', split: 'right')
pane3 = rootView.getActivePane()
expect(pane3[0]).toBe pane2[0]
expect(editSession.getPath()).toBe require.resolve('./fixtures/dir/file1')
expect(rootView.panes.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0]]
describe ".openSingletonSync(filePath, options)", ->
describe "when there is an active pane", ->
[pane1] = []
beforeEach ->
spyOn(Pane.prototype, 'focus').andCallFake -> @makeActive()
pane1 = rootView.getActivePane()
it "creates a new pane and reuses the file when already open", ->
rootView.openSingletonSync('b', split: 'right')
pane2 = rootView.getActivePane()
expect(pane2[0]).not.toBe pane1[0]
expect(pane1.itemForUri('b')).toBeFalsy()
expect(pane2.itemForUri('b')).not.toBeFalsy()
expect(rootView.panes.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0]]
pane1.focus()
expect(rootView.getActivePane()[0]).toBe pane1[0]
rootView.openSingletonSync('b', split: 'right')
pane3 = rootView.getActivePane()
expect(pane3[0]).toBe pane2[0]
expect(pane1.itemForUri('b')).toBeFalsy()
expect(pane2.itemForUri('b')).not.toBeFalsy()
expect(rootView.panes.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0]]
it "reuses the file when already open", ->
rootView.openSync('b')
rootView.openSingletonSync('b', split: 'right')
expect(rootView.panes.find('.pane').toArray()).toEqual [pane1[0]]
describe ".open(filePath)", ->
beforeEach ->
spyOn(Pane.prototype, 'focus')

View File

@@ -161,6 +161,12 @@ class PaneContainer extends View
getActiveView: ->
@getActivePane()?.activeView
paneForUri: (uri) ->
for pane in @getPanes()
view = pane.itemForUri(uri)
return pane if view?
null
adjustPaneDimensions: ->
if root = @getRoot()
root.css(width: '100%', height: '100%', top: 0, left: 0)

View File

@@ -194,23 +194,44 @@ class RootView extends View
editSession
# Private: Only used in specs
openSync: (filePath, options = {}) ->
changeFocus = options.changeFocus ? true
initialLine = options.initialLine
filePath = project.relativize(filePath)
if activePane = @getActivePane()
if filePath
editSession = activePane.itemForUri(filePath) ? project.openSync(filePath, {initialLine})
else
editSession = project.openSync()
activePane.showItem(editSession)
else
editSession = project.openSync(filePath, {initialLine})
activePane = new Pane(editSession)
@panes.setRoot(activePane)
openSync: (uri, {changeFocus, initialLine, pane, split}={}) ->
changeFocus ?= true
pane ?= @getActivePane()
uri = project.relativize(uri)
activePane.focus() if changeFocus
editSession
if pane
if uri
paneItem = pane.itemForUri(uri) ? project.openSync(uri, {initialLine})
else
paneItem = project.openSync()
if split
panes = @getPanes()
if panes.length == 1
pane = panes[0].splitRight()
else
pane = _.last(panes)
pane.showItem(paneItem)
else
paneItem = project.openSync(uri, {initialLine})
pane = new Pane(paneItem)
@panes.setRoot(pane)
pane.focus() if changeFocus
paneItem
openSingletonSync: (uri, {split}={}) ->
uri = project.relativize(uri)
pane = @panes.paneForUri(uri)
if pane
paneItem = pane.itemForUri(uri)
pane.showItem(paneItem)
pane.focus()
paneItem
else
@openSync(uri, {split})
# Public: Updates the application's title, based on whichever file is open.
updateTitle: ->