mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Add Pane::onWillDestroyItem()
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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?()
|
||||
|
||||
Reference in New Issue
Block a user