Move Workspace and PaneContainer serialization to the model layer

The views are now stateless. You just construct them with a model to
deserialize.
This commit is contained in:
Nathan Sobo 2014-01-14 11:36:51 -07:00
parent 4a7b43f609
commit c350285044
8 changed files with 20 additions and 46 deletions

View File

@ -121,7 +121,7 @@ describe "PaneContainerView", ->
describe "serialization", -> describe "serialization", ->
it "can be serialized and deserialized, and correctly adjusts dimensions of deserialized panes after attach", -> it "can be serialized and deserialized, and correctly adjusts dimensions of deserialized panes after attach", ->
newContainer = atom.deserializers.deserialize(container.serialize()) newContainer = new PaneContainerView(container.model.testSerialization())
expect(newContainer.find('.pane-row > :contains(1)')).toExist() expect(newContainer.find('.pane-row > :contains(1)')).toExist()
expect(newContainer.find('.pane-row > .pane-column > :contains(2)')).toExist() expect(newContainer.find('.pane-row > .pane-column > :contains(2)')).toExist()
expect(newContainer.find('.pane-row > .pane-column > :contains(3)')).toExist() expect(newContainer.find('.pane-row > .pane-column > :contains(3)')).toExist()
@ -133,7 +133,7 @@ describe "PaneContainerView", ->
it "removes empty panes on deserialization", -> it "removes empty panes on deserialization", ->
# only deserialize pane 1's view successfully # only deserialize pane 1's view successfully
TestView.deserialize = ({name}) -> new TestView(name) if name is '1' TestView.deserialize = ({name}) -> new TestView(name) if name is '1'
newContainer = atom.deserializers.deserialize(container.serialize()) newContainer = new PaneContainerView(container.model.testSerialization())
expect(newContainer.find('.pane-row, .pane-column')).not.toExist() expect(newContainer.find('.pane-row, .pane-column')).not.toExist()
expect(newContainer.find('> :contains(1)')).toExist() expect(newContainer.find('> :contains(1)')).toExist()

View File

@ -602,12 +602,12 @@ describe "PaneView", ->
describe "serialization", -> describe "serialization", ->
it "can serialize and deserialize the pane and all its items", -> it "can serialize and deserialize the pane and all its items", ->
newPane = pane.testSerialization() newPane = new PaneView(pane.model.testSerialization())
expect(newPane.getItems()).toEqual [view1, editor1, view2, editor2] expect(newPane.getItems()).toEqual [view1, editor1, view2, editor2]
it "restores the active item on deserialization", -> it "restores the active item on deserialization", ->
pane.activateItem(editor2) pane.activateItem(editor2)
newPane = pane.testSerialization() newPane = new PaneView(pane.model.testSerialization())
expect(newPane.activeItem).toEqual editor2 expect(newPane.activeItem).toEqual editor2
it "does not show items that cannot be deserialized", -> it "does not show items that cannot be deserialized", ->
@ -618,7 +618,7 @@ describe "PaneView", ->
pane.activateItem(new Unserializable) pane.activateItem(new Unserializable)
newPane = pane.testSerialization() newPane = new PaneView(pane.model.testSerialization())
expect(newPane.activeItem).toEqual pane.items[0] expect(newPane.activeItem).toEqual pane.items[0]
expect(newPane.items.length).toBe pane.items.length - 1 expect(newPane.items.length).toBe pane.items.length - 1
@ -626,13 +626,13 @@ describe "PaneView", ->
container.attachToDom() container.attachToDom()
pane.focus() pane.focus()
container2 = container.testSerialization() container2 = new PaneContainerView(container.model.testSerialization())
pane2 = container2.getRoot() pane2 = container2.getRoot()
container2.attachToDom() container2.attachToDom()
expect(pane2).toMatchSelector(':has(:focus)') expect(pane2).toMatchSelector(':has(:focus)')
$(document.activeElement).blur() $(document.activeElement).blur()
container3 = container.testSerialization() container3 = new PaneContainerView(container.model.testSerialization())
pane3 = container3.getRoot() pane3 = container3.getRoot()
container3.attachToDom() container3.attachToDom()
expect(pane3).not.toMatchSelector(':has(:focus)') expect(pane3).not.toMatchSelector(':has(:focus)')

View File

@ -88,12 +88,12 @@ describe "Window", ->
describe ".unloadEditorWindow()", -> describe ".unloadEditorWindow()", ->
it "saves the serialized state of the window so it can be deserialized after reload", -> it "saves the serialized state of the window so it can be deserialized after reload", ->
workspaceViewState = atom.workspaceView.serialize() workspaceState = atom.workspace.serialize()
syntaxState = atom.syntax.serialize() syntaxState = atom.syntax.serialize()
atom.unloadEditorWindow() atom.unloadEditorWindow()
expect(atom.state.workspaceView).toEqual workspaceViewState expect(atom.state.workspace).toEqual workspaceState
expect(atom.state.syntax).toEqual syntaxState expect(atom.state.syntax).toEqual syntaxState
expect(atom.saveSync).toHaveBeenCalled() expect(atom.saveSync).toHaveBeenCalled()

View File

@ -3,6 +3,7 @@ Q = require 'q'
path = require 'path' path = require 'path'
temp = require 'temp' temp = require 'temp'
PaneView = require '../src/pane-view' PaneView = require '../src/pane-view'
Workspace = require '../src/workspace'
describe "WorkspaceView", -> describe "WorkspaceView", ->
pathToOpen = null pathToOpen = null
@ -10,7 +11,8 @@ describe "WorkspaceView", ->
beforeEach -> beforeEach ->
atom.project.setPath(atom.project.resolve('dir')) atom.project.setPath(atom.project.resolve('dir'))
pathToOpen = atom.project.resolve('a') pathToOpen = atom.project.resolve('a')
atom.workspaceView = new WorkspaceView atom.workspace = new Workspace
atom.workspaceView = new WorkspaceView(atom.workspace)
atom.workspaceView.enableKeymap() atom.workspaceView.enableKeymap()
atom.workspaceView.openSync(pathToOpen) atom.workspaceView.openSync(pathToOpen)
atom.workspaceView.focus() atom.workspaceView.focus()
@ -19,11 +21,12 @@ describe "WorkspaceView", ->
viewState = null viewState = null
simulateReload = -> simulateReload = ->
workspaceState = atom.workspaceView.serialize() workspaceState = atom.workspace.serialize()
projectState = atom.project.serialize() projectState = atom.project.serialize()
atom.workspaceView.remove() atom.workspaceView.remove()
atom.project = atom.deserializers.deserialize(projectState) atom.project = atom.deserializers.deserialize(projectState)
atom.workspaceView = WorkspaceView.deserialize(workspaceState) atom.workspace = Workspace.deserialize(workspaceState)
atom.workspaceView = new WorkspaceView(atom.workspace)
atom.workspaceView.attachToDom() atom.workspaceView.attachToDom()
describe "when the serialized WorkspaceView has an unsaved buffer", -> describe "when the serialized WorkspaceView has an unsaved buffer", ->
@ -187,7 +190,7 @@ describe "WorkspaceView", ->
describe "when the root view is deserialized", -> describe "when the root view is deserialized", ->
it "updates the title to contain the project's path", -> it "updates the title to contain the project's path", ->
workspaceView2 = atom.deserializers.deserialize(atom.workspaceView.serialize()) workspaceView2 = new WorkspaceView(atom.workspace.testSerialization())
item = atom.workspaceView.getActivePaneItem() item = atom.workspaceView.getActivePaneItem()
expect(workspaceView2.title).toBe "#{item.getTitle()} - #{atom.project.getPath()}" expect(workspaceView2.title).toBe "#{item.getTitle()} - #{atom.project.getPath()}"
workspaceView2.remove() workspaceView2.remove()

View File

@ -230,8 +230,10 @@ class Atom extends Model
# Private: # Private:
deserializeWorkspaceView: -> deserializeWorkspaceView: ->
Workspace = require './workspace'
WorkspaceView = require './workspace-view' WorkspaceView = require './workspace-view'
@workspaceView = @deserializers.deserialize(@state.workspaceView) ? new WorkspaceView @workspace = Workspace.deserialize(@state.workspace) ? new Workspace
@workspaceView = new WorkspaceView(@workspace)
$(@workspaceViewParentSelector).append(@workspaceView) $(@workspaceViewParentSelector).append(@workspaceView)
# Private: # Private:
@ -277,7 +279,7 @@ class Atom extends Model
return if not @project and not @workspaceView return if not @project and not @workspaceView
@state.syntax = @syntax.serialize() @state.syntax = @syntax.serialize()
@state.workspaceView = @workspaceView.serialize() @state.workspace = @workspace.serialize()
@packages.deactivatePackages() @packages.deactivatePackages()
@state.packageStates = @packages.packageStates @state.packageStates = @packages.packageStates
@saveSync() @saveSync()

View File

@ -1,4 +1,3 @@
Serializable = require 'serializable'
Delegator = require 'delegato' Delegator = require 'delegato'
{$, View} = require './space-pen-extensions' {$, View} = require './space-pen-extensions'
PaneView = require './pane-view' PaneView = require './pane-view'
@ -7,15 +6,10 @@ PaneContainer = require './pane-container'
# Private: Manages the list of panes within a {WorkspaceView} # Private: Manages the list of panes within a {WorkspaceView}
module.exports = module.exports =
class PaneContainerView extends View class PaneContainerView extends View
atom.deserializers.add(this)
Serializable.includeInto(this)
Delegator.includeInto(this) Delegator.includeInto(this)
@delegatesMethod 'saveAll', toProperty: 'model' @delegatesMethod 'saveAll', toProperty: 'model'
@deserialize: (state) ->
new this(PaneContainer.deserialize(state.model))
@content: -> @content: ->
@div class: 'panes' @div class: 'panes'
@ -33,9 +27,6 @@ class PaneContainerView extends View
viewClass = model.getViewClass() viewClass = model.getViewClass()
model._view ?= new viewClass(model) model._view ?= new viewClass(model)
serializeParams: ->
model: @model.serialize()
### Public ### ### Public ###
getRoot: -> getRoot: ->

View File

@ -1,5 +1,4 @@
{$, View} = require './space-pen-extensions' {$, View} = require './space-pen-extensions'
Serializable = require 'serializable'
Delegator = require 'delegato' Delegator = require 'delegato'
PropertyAccessors = require 'property-accessors' PropertyAccessors = require 'property-accessors'
@ -13,15 +12,11 @@ Pane = require './pane'
# building a package that deals with switching between panes or tiems. # building a package that deals with switching between panes or tiems.
module.exports = module.exports =
class PaneView extends View class PaneView extends View
Serializable.includeInto(this)
Delegator.includeInto(this) Delegator.includeInto(this)
PropertyAccessors.includeInto(this) PropertyAccessors.includeInto(this)
@version: 1 @version: 1
@deserialize: (state) ->
new this(Pane.deserialize(state.model))
@content: (wrappedView) -> @content: (wrappedView) ->
@div class: 'pane', tabindex: -1, => @div class: 'pane', tabindex: -1, =>
@div class: 'item-views', outlet: 'itemViews' @div class: 'item-views', outlet: 'itemViews'
@ -86,13 +81,6 @@ class PaneView extends View
@command 'pane:close', => @destroyItems() @command 'pane:close', => @destroyItems()
@command 'pane:close-other-items', => @destroyInactiveItems() @command 'pane:close-other-items', => @destroyInactiveItems()
deserializeParams: (params) ->
params.model = Pane.deserialize(params.model)
params
serializeParams: ->
model: @model.serialize()
# Deprecated: Use ::destroyItem # Deprecated: Use ::destroyItem
removeItem: (item) -> @destroyItem(item) removeItem: (item) -> @destroyItem(item)

View File

@ -5,7 +5,6 @@ _ = require 'underscore-plus'
Delegator = require 'delegato' Delegator = require 'delegato'
{$, $$, View} = require './space-pen-extensions' {$, $$, View} = require './space-pen-extensions'
fs = require 'fs-plus' fs = require 'fs-plus'
Serializable = require 'serializable'
Workspace = require './workspace' Workspace = require './workspace'
EditorView = require './editor-view' EditorView = require './editor-view'
PaneView = require './pane-view' PaneView = require './pane-view'
@ -40,8 +39,6 @@ Editor = require './editor'
# #
module.exports = module.exports =
class WorkspaceView extends View class WorkspaceView extends View
atom.deserializers.add(this, PaneView, PaneRowView, PaneColumnView, EditorView)
Serializable.includeInto(this)
Delegator.includeInto(this) Delegator.includeInto(this)
@delegatesProperty 'fullScreen', 'destroyedItemUris', toProperty: 'model' @delegatesProperty 'fullScreen', 'destroyedItemUris', toProperty: 'model'
@ -51,9 +48,6 @@ class WorkspaceView extends View
@version: 4 @version: 4
@deserialize: (state) ->
new this(Workspace.deserialize(state.model))
@configDefaults: @configDefaults:
ignoredNames: [".git", ".svn", ".DS_Store"] ignoredNames: [".git", ".svn", ".DS_Store"]
excludeVcsIgnoredPaths: true excludeVcsIgnoredPaths: true
@ -128,10 +122,6 @@ class WorkspaceView extends View
@command 'core:save', => @saveActivePaneItem() @command 'core:save', => @saveActivePaneItem()
@command 'core:save-as', => @saveActivePaneItemAs() @command 'core:save-as', => @saveActivePaneItemAs()
# Private:
serializeParams: ->
model: @model.serialize()
# Private: # Private:
handleFocus: (e) -> handleFocus: (e) ->
if @getActivePane() if @getActivePane()