Add Pane::onWillDestroyItem()

This commit is contained in:
Nathan Sobo
2014-08-27 10:34:57 -06:00
parent 2d58d9c8b5
commit 8225f759bf
2 changed files with 25 additions and 8 deletions

View File

@@ -144,14 +144,32 @@ describe "Pane", ->
pane = new Pane(items: [new Item("A"), new Item("B"), new Item("C")])
[item1, item2, item3] = pane.getItems()
it "removes the item from the items list", ->
it "removes the item from the items list and destroyes it", ->
expect(pane.getActiveItem()).toBe item1
pane.destroyItem(item2)
expect(item2 in pane.getItems()).toBe false
expect(item2.isDestroyed()).toBe true
expect(pane.getActiveItem()).toBe item1
pane.destroyItem(item1)
expect(item1 in pane.getItems()).toBe false
expect(item1.isDestroyed()).toBe true
it "invokes ::onWillDestroyItem() observers before destroying the item", ->
events = []
pane.onWillDestroyItem (event) ->
expect(item2.isDestroyed()).toBe false
events.push(event)
pane.destroyItem(item2)
expect(item2.isDestroyed()).toBe true
expect(events).toEqual [{item: item2, index: 1}]
it "invokes ::onDidRemoveItem() observers", ->
events = []
pane.onDidRemoveItem (event) -> events.push(event)
pane.destroyItem(item2)
expect(events).toEqual [{item: item2, index: 1, destroyed: true}]
describe "when the destroyed item is the active item and is the first item", ->
it "activates the next item", ->
@@ -168,12 +186,6 @@ describe "Pane", ->
pane.destroyItem(item2)
expect(pane.getActiveItem()).toBe item1
it "invokes ::onDidRemoveItem() observers", ->
events = []
pane.onDidRemoveItem (event) -> events.push(event)
pane.destroyItem(item2)
expect(events).toEqual [{item: item2, index: 1, destroyed: true}]
describe "if the item is modified", ->
itemUri = null

View File

@@ -109,6 +109,9 @@ class Pane extends Model
onDidChangeActiveItem: (fn) ->
@emitter.on 'did-change-active-item', fn
onWillDestroyItem: (fn) ->
@emitter.on 'will-destroy-item', fn
isActive: -> @active
# Called by the view layer to indicate that the pane has gained focus.
@@ -252,8 +255,10 @@ class Pane extends Model
# Public: Destroys the given item. If it is the active item, activate the next
# one. If this is the last item, also destroys the pane.
destroyItem: (item) ->
if item?
index = @items.indexOf(item)
if index isnt -1
@emit 'before-item-destroyed', item
@emitter.emit 'will-destroy-item', {item, index}
if @promptToSaveItem(item)
@removeItem(item, true)
item.destroy?()