Move methods related to item movement to PaneModel

This commit is contained in:
Corey Johnson & Nathan Sobo 2014-01-07 18:39:39 -07:00
parent 1a487db29f
commit 5837b7cfda
2 changed files with 31 additions and 29 deletions

View File

@ -27,6 +27,10 @@ class PaneModel extends Model
getItems: ->
clone(@items)
# Public: Returns the item at the specified index.
itemAtIndex: (index) ->
@items[index]
# Public: Switches to the next contained item.
showNextItem: =>
index = @getActiveItemIndex()
@ -66,17 +70,25 @@ class PaneModel extends Model
item
# Public:
removeItem: (item) ->
removeItem: (item, detach) ->
index = @items.indexOf(item)
@removeItemAtIndex(index) if index >= 0
@removeItemAtIndex(index, detach) if index >= 0
# Public: Just remove the item at the given index.
removeItemAtIndex: (index) ->
removeItemAtIndex: (index, detach) ->
item = @items[index]
@showNextItem() if item is @activeItem and @items.length > 1
@items.splice(index, 1)
@emit 'item-removed', item, index
@emit 'item-removed', item, index, detach
# Public: Returns the item at the specified index.
itemAtIndex: (index) ->
@items[index]
# Public: Moves the given item to a the new index.
moveItem: (item, newIndex) ->
oldIndex = @items.indexOf(item)
@items.splice(oldIndex, 1)
@items.splice(newIndex, 0, item)
@emit 'item-moved', item, newIndex
# Public: Moves the given item to another pane.
moveItemToPane: (item, pane, index) ->
pane.addItem(item, index)
@removeItem(item, true)

View File

@ -28,7 +28,7 @@ class Pane extends View
@delegatesProperties 'items', 'activeItem', toProperty: 'model'
@delegatesMethods 'getItems', 'showNextItem', 'showPreviousItem', 'getActiveItemIndex',
'showItemAtIndex', 'showItem', 'addItem', 'itemAtIndex', 'removeItem', 'removeItemAtIndex',
toProperty: 'model'
'moveItem', 'moveItemToPane', toProperty: 'model'
previousActiveItem: null
@ -47,6 +47,7 @@ class Pane extends View
@subscribe @model.$activeItem, 'value', @onActiveItemChanged
@subscribe @model, 'item-added', @onItemAdded
@subscribe @model, 'item-removed', @onItemRemoved
@subscribe @model, 'item-moved', @onItemMoved
@subscribe this, 'focus', => @activeView?.focus(); false
@subscribe this, 'focusin', => @makeActive()
@ -139,10 +140,13 @@ class Pane extends View
@subscribe item, 'destroyed', => @destroyItem(item)
@trigger 'pane:item-added', [item, index]
onItemRemoved: (item, index) =>
@cleanupItemView(item)
onItemRemoved: (item, index, detach) =>
@cleanupItemView(item, detach)
@trigger 'pane:item-removed', [item, index]
onItemMoved: (item, newIndex) =>
@trigger 'pane:item-moved', [item, newIndex]
# Private:
activeItemTitleChanged: =>
@trigger 'pane:active-item-title-changed'
@ -220,20 +224,6 @@ class Pane extends View
saveItems: =>
@saveItem(item) for item in @getItems()
# Public: Moves the given item to a the new index.
moveItem: (item, newIndex) ->
oldIndex = @items.indexOf(item)
@items.splice(oldIndex, 1)
@items.splice(newIndex, 0, item)
@trigger 'pane:item-moved', [item, newIndex]
# Public: Moves the given item to another pane.
moveItemToPane: (item, pane, index) ->
@isMovingItem = true
pane.addItem(item, index)
@removeItem(item)
@isMovingItem = false
# Public: Finds the first item that matches the given uri.
itemForUri: (uri) ->
_.detect @items, (item) -> item.getUri?() is uri
@ -247,24 +237,24 @@ class Pane extends View
false
# Private:
cleanupItemView: (item) ->
cleanupItemView: (item, detach) ->
if item instanceof $
viewToRemove = item
else if viewToRemove = @viewsByItem.get(item)
@viewsByItem.delete(item)
if @items.length > 0
if @isMovingItem and item is viewToRemove
if detach and item is viewToRemove
viewToRemove?.detach()
else if @isMovingItem and viewToRemove?.setModel
else if detach and viewToRemove?.setModel
viewToRemove.setModel(null) # dont want to destroy the model, so set to null
viewToRemove.remove()
else
viewToRemove?.remove()
else
if @isMovingItem and item is viewToRemove
if detach and item is viewToRemove
viewToRemove?.detach()
else if @isMovingItem and viewToRemove?.setModel
else if detach and viewToRemove?.setModel
viewToRemove.setModel(null) # dont want to destroy the model, so set to null
@parent().view().removeChild(this)