Serialize the items and activeItem via PaneModel

This necessitates setting the ::activeView based on the model's
::activeItem instead of doing it in ::showItem.
This commit is contained in:
Corey Johnson & Nathan Sobo 2014-01-07 17:23:29 -07:00
parent 14175d80ef
commit 25c099f3a2
4 changed files with 38 additions and 19 deletions

View File

@ -16,7 +16,7 @@ describe "PaneContainer", ->
serialize: -> { deserializer: 'TestView', @name }
getUri: -> path.join(temp.dir, @name)
save: -> @saved = true
isEqual: (other) -> @name is other.name
isEqual: (other) -> @name is other?.name
container = new PaneContainer
pane1 = new Pane(new TestView('1'))

View File

@ -13,7 +13,7 @@ describe "Pane", ->
initialize: ({@id, @text}) ->
serialize: -> { deserializer: 'TestView', @id, @text }
getUri: -> @id
isEqual: (other) -> @id == other.id and @text == other.text
isEqual: (other) -> other? and @id == other.id and @text == other.text
beforeEach ->
atom.deserializers.add(TestView)
@ -558,12 +558,12 @@ describe "Pane", ->
pane2 = pane1.splitRight()
expect(container.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0]]
expect(pane2.items).toEqual []
expect(pane2.activeItem).toBe null
expect(pane2.activeItem).toBeUndefined()
pane3 = pane2.splitRight()
expect(container.find('.row .pane').toArray()).toEqual [pane1[0], pane2[0], pane3[0]]
expect(pane3.items).toEqual []
expect(pane3.activeItem).toBe null
expect(pane3.activeItem).toBeUndefined()
describe "splitLeft(items...)", ->
it "builds a row if needed, then appends a new pane before itself", ->

View File

@ -1,7 +1,24 @@
{find, compact} = require 'underscore-plus'
{Model} = require 'theorist'
Serializable = require 'serializable'
module.exports =
class PaneModel extends Model
activeItem: null
Serializable.includeInto(this)
constructor: ({@items}) ->
@properties
activeItem: null
constructor: ({@items, @activeItem}) ->
@items ?= []
@activeItem ?= @items[0]
serializeParams: ->
items: compact(@items.map((item) -> item.serialize?()))
activeItemUri: @activeItem?.getUri?()
deserializeParams: (params) ->
{items, activeItemUri} = params
params.items = items.map (itemState) -> atom.deserializers.deserialize(itemState)
params.activeItem = find params.items, (item) -> item.getUri?() is activeItemUri
params

View File

@ -27,11 +27,12 @@ class Pane extends View
@delegatesProperties 'items', 'activeItem', toProperty: 'model'
previousActiveItem: null
# Private:
initialize: (args...) ->
if args[0]?.items # deserializing
{items, activeItemUri, @focusOnAttach} = args[0]
@model = new PaneModel({items})
if args[0]?.model?
{@model, @focusOnAttach} = args[0]
else
@model = new PaneModel(items: args)
@ -39,8 +40,7 @@ class Pane extends View
@viewsByItem = new WeakMap()
unless activeItemUri? and @showItemForUri(activeItemUri)
@showItem(@items[0]) if @items.length > 0
@subscribe @model.$activeItem, 'value', @onActiveItemChanged
@command 'pane:save-items', @saveItems
@command 'pane:show-next-item', @showNextItem
@ -66,13 +66,12 @@ class Pane extends View
@on 'focusin', => @makeActive()
deserializeParams: (params) ->
params.items = _.compact(params.items.map (itemState) -> atom.deserializers.deserialize(itemState))
params.model = PaneModel.deserialize(params.model)
params
serializeParams: ->
items: _.compact(@items.map (item) -> item.serialize?())
model: @model.serialize()
focusOnAttach: @is(':has(:focus)')
activeItemUri: @getActivePaneItem()?.getUri?()
# Private:
afterAttach: (onDom) ->
@ -146,20 +145,23 @@ class Pane extends View
# Public: Focuses the given item.
showItem: (item) ->
return if !item? or item is @activeItem
if item?
@addItem(item)
@model.activeItem = item
if @activeItem
@activeItem.off? 'title-changed', @activeItemTitleChanged
onActiveItemChanged: (item) =>
@previousActiveItem?.off? 'title-changed', @activeItemTitleChanged
@previousActiveItem = item
return unless item?
isFocused = @is(':has(:focus)')
@addItem(item)
item.on? 'title-changed', @activeItemTitleChanged
view = @viewForItem(item)
@itemViews.children().not(view).hide()
@itemViews.append(view) unless view.parent().is(@itemViews)
view.show() if @attached
view.focus() if isFocused
@activeItem = item
@activeView = view
@trigger 'pane:active-item-changed', [item]