Project drawer state is restored upon refresh

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-05-02 16:08:02 -07:00
parent 5864fa5145
commit bb689574d0
3 changed files with 52 additions and 8 deletions

View File

@@ -9,7 +9,9 @@ describe "TreeView", ->
beforeEach ->
rootView = new RootView(pathToOpen: require.resolve('fixtures/'))
project = rootView.project
treeView = new TreeView(rootView)
rootView.registerExtension(TreeView)
treeView = rootView.find(".tree-view").view()
treeView.root = treeView.find('> li:first').view()
sampleJs = treeView.find('.file:contains(sample.js)')
sampleTxt = treeView.find('.file:contains(sample.txt)')
@@ -38,6 +40,24 @@ describe "TreeView", ->
expect(rootEntries.find('> .file:contains(sample.js)')).toExist()
expect(rootEntries.find('> .file:contains(sample.txt)')).toExist()
describe "serialization", ->
newTreeView = null
afterEach ->
newTreeView.deactivate()
it "restores expanded directories and selected file when deserialized", ->
treeView.find('.directory:contains(zed)').click()
sampleJs.click()
newRootView = RootView.deserialize(rootView.serialize())
newRootView.registerExtension(TreeView)
newTreeView = newRootView.find(".tree-view").view()
expect(newTreeView).toExist()
expect(newTreeView.selectedEntry()).toMatchSelector(".file:contains(sample.js)")
expect(newTreeView.find(".directory:contains(zed)")).toHaveClass("expanded")
describe "when a directory's disclosure arrow is clicked", ->
it "expands / collapses the associated directory", ->
subdir = treeView.root.find('.entries > li:contains(dir/)').view()

View File

@@ -19,7 +19,6 @@ afterEach ->
document.title = defaultTitle
ensureNoDirectorySubscriptions()
window.keymap.bindKeys '*', 'meta-w': 'close'
$(document).on 'close', -> window.close()

View File

@@ -9,14 +9,31 @@ _ = require 'underscore'
module.exports =
class TreeView extends View
@activate: (rootView) ->
@activate: (rootView, state) ->
requireStylesheet 'tree-view.css'
rootView.horizontal.prepend(new TreeView(rootView))
if state
@treeView = TreeView.deserialize(state, rootView)
else
@treeView = new TreeView(rootView)
rootView.horizontal.prepend(@treeView)
@content: (rootView) ->
@div class: 'tree-view', tabindex: -1, =>
@subview 'root', new DirectoryView(directory: rootView.project.getRootDirectory(), isExpanded: true)
@deserialize: (state, rootView) ->
treeView = new TreeView(rootView)
treeView.root.deserializeEntryExpansionStates(state.directoryExpansionStates)
treeView.selectEntryForPath(state.selectedPath)
treeView
@serialize: ->
@treeView.serialize()
root: null
initialize: (@rootView) ->
@on 'click', '.entry', (e) =>
entry = $(e.currentTarget).view()
@@ -37,15 +54,23 @@ class TreeView extends View
@on 'tree-view:unfocus', => @rootView.activeEditor()?.focus()
@rootView.on 'tree-view:focus', => this.focus()
serialize: ->
directoryExpansionStates: @root.serializeEntryExpansionStates()
selectedPath: @selectedEntry()?.getPath()
deactivate: ->
@root.unwatchEntries()
selectActiveFile: ->
activeFilePath = @rootView.activeEditor()?.buffer.path
for element in @find(".file")
fileView = $(element).view()
if fileView.getPath() == activeFilePath
@selectEntry(fileView)
@selectEntryForPath(activeFilePath)
selectEntryForPath: (path) ->
for element in @find(".entry")
view = $(element).view()
if view.getPath() == path
@selectEntry(view)
return
moveDown: ->
selectedEntry = @selectedEntry()