Move item removal events to PaneModel

This commit is contained in:
Corey Johnson & Nathan Sobo 2014-01-07 17:55:56 -07:00
parent 2938a8e650
commit 1a487db29f
2 changed files with 19 additions and 15 deletions

View File

@ -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]

View File

@ -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)