Add Pane::onDidMoveItem()

This commit is contained in:
Nathan Sobo 2014-08-27 10:08:58 -06:00
parent b8fcbe9451
commit 9bd2eec4bc
2 changed files with 19 additions and 7 deletions

View File

@ -323,24 +323,32 @@ describe "Pane", ->
expect(pane.itemForUri("bogus")).toBeUndefined()
describe "::moveItem(item, index)", ->
it "moves the item to the given index and emits an 'item-moved' event with the item and its new index", ->
[pane, item1, item2, item3, item4] = []
beforeEach ->
pane = new Pane(items: [new Item("A"), new Item("B"), new Item("C"), new Item("D")])
[item1, item2, item3, item4] = pane.getItems()
pane.on 'item-moved', itemMovedHandler = jasmine.createSpy("itemMovedHandler")
it "moves the item to the given index and invokes ::onDidMoveItem observers", ->
pane.moveItem(item1, 2)
expect(pane.getItems()).toEqual [item2, item3, item1, item4]
expect(itemMovedHandler).toHaveBeenCalledWith(item1, 2)
itemMovedHandler.reset()
pane.moveItem(item2, 3)
expect(pane.getItems()).toEqual [item3, item1, item4, item2]
expect(itemMovedHandler).toHaveBeenCalledWith(item2, 3)
itemMovedHandler.reset()
pane.moveItem(item2, 1)
expect(pane.getItems()).toEqual [item3, item2, item1, item4]
expect(itemMovedHandler).toHaveBeenCalledWith(item2, 1)
it "invokes ::onDidMoveItem() observers", ->
events = []
pane.onDidMoveItem (event) -> events.push(event)
pane.moveItem(item1, 2)
pane.moveItem(item2, 3)
expect(events).toEqual [
{item: item1, oldIndex: 0, newIndex: 2}
{item: item2, oldIndex: 0, newIndex: 3}
]
describe "::moveItemToPane(item, pane, index)", ->
[container, pane1, pane2] = []

View File

@ -100,6 +100,9 @@ class Pane extends Model
onDidRemoveItem: (fn) ->
@emitter.on 'did-remove-item', fn
onDidMoveItem: (fn) ->
@emitter.on 'did-move-item', fn
onDidChangeActiveItem: (fn) ->
@emitter.on 'did-change-active-item', fn
@ -230,6 +233,7 @@ class Pane extends Model
@items.splice(oldIndex, 1)
@items.splice(newIndex, 0, item)
@emit 'item-moved', item, newIndex
@emitter.emit 'did-move-item', {item, oldIndex, newIndex}
# Public: Moves the given item to the given index at another pane.
moveItemToPane: (item, pane, index) ->