Add PaneContainer::observePanes and ::onDidAddPane

This commit is contained in:
Nathan Sobo
2014-08-28 17:14:15 -06:00
parent 5471e9bccc
commit cee7539e35
4 changed files with 52 additions and 6 deletions

View File

@@ -88,3 +88,17 @@ describe "PaneContainer", ->
pane1.activate()
pane2.activate()
expect(observed).toEqual [pane1.itemAtIndex(0), pane2.itemAtIndex(0)]
describe "::observePanes()", ->
it "invokes with subscribers with all current and future panes", ->
container = new PaneContainer
container.getRoot().splitRight()
[pane1, pane2] = container.getPanes()
observed = []
container.observePanes (pane) -> observed.push(pane)
pane3 = pane2.splitDown()
pane4 = pane2.splitRight()
expect(observed).toEqual [pane1, pane2, pane3, pane4]

View File

@@ -11,6 +11,10 @@ class PaneAxis extends Model
atom.deserializers.add(this)
Serializable.includeInto(this)
parent: null
container: null
orientation: null
constructor: ({@container, @orientation, children}) ->
@emitter = new Emitter
@subscriptionsByChild = new WeakMap
@@ -28,6 +32,14 @@ class PaneAxis extends Model
children: @children.map (child) -> child.serialize()
orientation: @orientation
getParent: -> @parent
setParent: (@parent) -> @parent
getContainer: -> @container
setContainer: (@container) -> @container
getViewClass: ->
if @orientation is 'vertical'
PaneColumnView ?= require './pane-column-view'
@@ -52,8 +64,8 @@ class PaneAxis extends Model
@emitter.on 'did-destroy', fn
addChild: (child, index=@children.length) ->
child.parent = this
child.container = @container
child.setParent(this)
child.setContainer(@container)
@subscribeToChild(child)
@@ -74,8 +86,8 @@ class PaneAxis extends Model
@unsubscribeFromChild(oldChild)
@subscribeToChild(newChild)
newChild.parent = this
newChild.container = @container
newChild.setParent(this)
newChild.setContainer(@container)
index = @children.indexOf(oldChild)
@children.splice(index, 1, newChild)

View File

@@ -49,6 +49,13 @@ class PaneContainer extends Model
fn(@getRoot())
@onDidChangeRoot(fn)
onDidAddPane: (fn) ->
@emitter.on 'did-add-pane', fn
observePanes: (fn) ->
fn(pane) for pane in @getPanes()
@onDidAddPane ({pane}) -> fn(pane)
onDidChangeActivePane: (fn) ->
@emitter.on 'did-change-active-pane', fn
@@ -69,8 +76,8 @@ class PaneContainer extends Model
getRoot: -> @root
setRoot: (@root) ->
@root.parent = this
@root.container = this
@root.setParent(this)
@root.setContainer(this)
@emitter.emit 'did-change-root', @root
if not @getActivePane()? and @root instanceof Pane
@setActivePane(@root)
@@ -127,6 +134,9 @@ class PaneContainer extends Model
paneItemDestroyed: (item) ->
@emitter.emit 'did-destroy-pane-item', item
didAddPane: (pane) ->
@emitter.emit 'did-add-pane', pane
# Called by Model superclass when destroyed
destroyed: ->
pane.destroy() for pane in @getPanes()

View File

@@ -55,6 +55,16 @@ class Pane extends Model
# Called by the view layer to construct a view for this model.
getViewClass: -> PaneView ?= require './pane-view'
getParent: -> @parent
setParent: (@parent) -> @parent
getContainer: -> @container
setContainer: (container) ->
container.didAddPane({pane: this}) unless container is @container
@container = container
###
Section: Event Subscription
###