From 9bd2eec4bc5dddd0a3a24a0ccd45907df2e71536 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 27 Aug 2014 10:08:58 -0600 Subject: [PATCH] Add Pane::onDidMoveItem() --- spec/pane-spec.coffee | 22 +++++++++++++++------- src/pane.coffee | 4 ++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/spec/pane-spec.coffee b/spec/pane-spec.coffee index b525c1c69..71c9fcc03 100644 --- a/spec/pane-spec.coffee +++ b/spec/pane-spec.coffee @@ -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] = [] diff --git a/src/pane.coffee b/src/pane.coffee index 61ad60cc1..23f188d25 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -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) ->