From 1a487db29f2c3e0eed915136c80ef59259de9bc7 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 7 Jan 2014 17:55:56 -0700 Subject: [PATCH] Move item removal events to PaneModel --- src/pane-model.coffee | 12 ++++++++++++ src/pane.coffee | 22 +++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/pane-model.coffee b/src/pane-model.coffee index 8b0be5d62..8d3f13d40 100644 --- a/src/pane-model.coffee +++ b/src/pane-model.coffee @@ -65,6 +65,18 @@ class PaneModel extends Model @emit 'item-added', item, index item + # Public: + removeItem: (item) -> + index = @items.indexOf(item) + @removeItemAtIndex(index) if index >= 0 + + # Public: Just remove the item at the given index. + removeItemAtIndex: (index) -> + item = @items[index] + @showNextItem() if item is @activeItem and @items.length > 1 + @items.splice(index, 1) + @emit 'item-removed', item, index + # Public: Returns the item at the specified index. itemAtIndex: (index) -> @items[index] diff --git a/src/pane.coffee b/src/pane.coffee index 0e3a7496d..7f1f6056c 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -27,7 +27,8 @@ class Pane extends View @delegatesProperties 'items', 'activeItem', toProperty: 'model' @delegatesMethods 'getItems', 'showNextItem', 'showPreviousItem', 'getActiveItemIndex', - 'showItemAtIndex', 'showItem', 'addItem', 'itemAtIndex', toProperty: 'model' + 'showItemAtIndex', 'showItem', 'addItem', 'itemAtIndex', 'removeItem', 'removeItemAtIndex', + toProperty: 'model' previousActiveItem: null @@ -45,6 +46,7 @@ class Pane extends View handleEvents: -> @subscribe @model.$activeItem, 'value', @onActiveItemChanged @subscribe @model, 'item-added', @onItemAdded + @subscribe @model, 'item-removed', @onItemRemoved @subscribe this, 'focus', => @activeView?.focus(); false @subscribe this, 'focusin', => @makeActive() @@ -137,6 +139,10 @@ class Pane extends View @subscribe item, 'destroyed', => @destroyItem(item) @trigger 'pane:item-added', [item, index] + onItemRemoved: (item, index) => + @cleanupItemView(item) + @trigger 'pane:item-removed', [item, index] + # Private: activeItemTitleChanged: => @trigger 'pane:active-item-title-changed' @@ -214,20 +220,6 @@ class Pane extends View saveItems: => @saveItem(item) for item in @getItems() - # Public: - removeItem: (item) -> - index = @items.indexOf(item) - @removeItemAtIndex(index) if index >= 0 - - # Public: Just remove the item at the given index. - removeItemAtIndex: (index) -> - item = @items[index] - @activeItem.off? 'title-changed', @activeItemTitleChanged if item is @activeItem - @showNextItem() if item is @activeItem and @items.length > 1 - _.remove(@items, item) - @cleanupItemView(item) - @trigger 'pane:item-removed', [item, index] - # Public: Moves the given item to a the new index. moveItem: (item, newIndex) -> oldIndex = @items.indexOf(item)