From cee7539e35a7e030756dbdf84b09ba3fd3f25a3f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 28 Aug 2014 17:14:15 -0600 Subject: [PATCH] Add PaneContainer::observePanes and ::onDidAddPane --- spec/pane-container-spec.coffee | 14 ++++++++++++++ src/pane-axis.coffee | 20 ++++++++++++++++---- src/pane-container.coffee | 14 ++++++++++++-- src/pane.coffee | 10 ++++++++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/spec/pane-container-spec.coffee b/spec/pane-container-spec.coffee index 2b5f24c36..279b543ce 100644 --- a/spec/pane-container-spec.coffee +++ b/spec/pane-container-spec.coffee @@ -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] diff --git a/src/pane-axis.coffee b/src/pane-axis.coffee index 95039e737..e9bc4993b 100644 --- a/src/pane-axis.coffee +++ b/src/pane-axis.coffee @@ -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) diff --git a/src/pane-container.coffee b/src/pane-container.coffee index 37210cb7a..73bcabc6a 100644 --- a/src/pane-container.coffee +++ b/src/pane-container.coffee @@ -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() diff --git a/src/pane.coffee b/src/pane.coffee index bbd1bc729..4e4bb7350 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -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 ###