Allow panes to have model objects as items in addition to views

The problem I've been struggling with is that we need to potentially 
assign tabs both to EditSessions and also to other views added by
extensions, like a markdown preview view. EditSessions are however not
actually views… instead they plug into editors. The solution is to
have the pane ask a model object what view should be used to render
it. When asked to show a non-view item, the pane constructs and
appends a view for that item or recycles an appropriate view that it
has already appended.
This commit is contained in:
Nathan Sobo
2013-02-18 11:15:33 -07:00
committed by probablycorey
parent 2bdc077d2a
commit 372393d9ca
4 changed files with 59 additions and 6 deletions

View File

@@ -52,6 +52,9 @@ class EditSession
@subscribe @displayBuffer, "changed", (e) =>
@trigger 'screen-lines-changed', e
getViewClass: ->
require 'editor'
destroy: ->
throw new Error("Edit session already destroyed") if @destroyed
@destroyed = true

View File

@@ -66,7 +66,12 @@ class Editor extends View
editor.isFocused = state.isFocused
editor
initialize: ({editSession, @mini, deserializing} = {}) ->
initialize: (editSessionOrOptions) ->
if editSessionOrOptions instanceof EditSession
editSession = editSessionOrOptions
else
{editSession, @mini, deserializing} = (options ? {})
requireStylesheet 'editor.css'
@id = Editor.nextEditorId++
@@ -485,6 +490,9 @@ class Editor extends View
index = @pushEditSession(editSession) if index == -1
@setActiveEditSessionIndex(index)
setModel: (editSession) ->
@edit(editSession)
pushEditSession: (editSession) ->
index = @editSessions.length
@editSessions.push(editSession)

View File

@@ -1,4 +1,5 @@
{View} = require 'space-pen'
$ = require 'jquery'
PaneRow = require 'pane-row'
PaneColumn = require 'pane-column'
@@ -12,13 +13,27 @@ class Pane extends View
new Pane(deserialize(wrappedView))
initialize: (@items...) ->
@viewsByItem = new WeakMap
@viewsByClassName = {}
@showItem(@items[0])
showItem: (item) ->
@itemViews.children().hide()
@itemViews.append(item) unless @itemViews.children(item).length
item.show()
view = @viewForItem(item)
unless view.parent().is(@itemViews)
@itemViews.append(view)
view.show()
viewForItem: (item) ->
if item instanceof $
item
else
viewClass = item.getViewClass()
if view = @viewsByClassName[viewClass.name]
view.setModel(item)
view
else
@viewsByClassName[viewClass.name] = new viewClass(item)
serialize: ->
deserializer: "Pane"