Add Pane.removeItem

This commit is contained in:
Nathan Sobo
2013-02-18 15:23:23 -07:00
committed by probablycorey
parent ef0c62f532
commit 41f18ee6a2
2 changed files with 46 additions and 2 deletions

View File

@@ -45,6 +45,31 @@ describe "Pane", ->
pane.showItem(view2)
expect(pane.itemViews.find('#view-2')).toExist()
describe ".removeItem(item)", ->
it "removes the item from the items list and shows the next item if it was showing", ->
pane.removeItem(view1)
expect(pane.getItems()).toEqual [editSession1, view2, editSession2]
expect(pane.currentItem).toBe editSession1
pane.showItem(editSession2)
pane.removeItem(editSession2)
expect(pane.getItems()).toEqual [editSession1, view2]
expect(pane.currentItem).toBe editSession1
describe "when the item is a view", ->
it "removes the item from the 'item-views' div", ->
expect(view1.parent()).toMatchSelector pane.itemViews
pane.removeItem(view1)
expect(view1.parent()).not.toMatchSelector pane.itemViews
describe "when the item is a model", ->
it "removes the associated view only when all items that require it have been removed", ->
pane.showItem(editSession2)
pane.removeItem(editSession2)
expect(pane.itemViews.find('.editor')).toExist()
pane.removeItem(editSession1)
expect(pane.itemViews.find('.editor')).not.toExist()
describe "pane:show-next-item and pane:show-preview-item", ->
it "advances forward/backward through the pane's items, looping around at either end", ->
expect(pane.currentItem).toBe view1

View File

@@ -1,5 +1,6 @@
{View} = require 'space-pen'
$ = require 'jquery'
_ = require 'underscore'
PaneRow = require 'pane-row'
PaneColumn = require 'pane-column'
@@ -22,6 +23,9 @@ class Pane extends View
@command 'pane:show-next-item', @showNextItem
@command 'pane:show-previous-item', @showPreviousItem
getItems: ->
new Array(@items...)
showNextItem: =>
index = @getCurrentItemIndex()
if index < @items.length - 1
@@ -50,6 +54,21 @@ class Pane extends View
@currentItem = item
view.show()
removeItem: (item) ->
@showNextItem() if item is @currentItem and @items.length > 1
_.remove(@items, item)
@cleanupItemView(item)
cleanupItemView: (item) ->
if item instanceof $
item.remove()
else
viewClass = item.getViewClass()
otherItemsForView = @items.filter (i) -> i.getViewClass?() is viewClass
unless otherItemsForView.length
@viewsByClassName[viewClass.name].remove()
delete @viewsByClassName[viewClass.name]
viewForItem: (item) ->
if item instanceof $
item
@@ -57,9 +76,9 @@ class Pane extends View
viewClass = item.getViewClass()
if view = @viewsByClassName[viewClass.name]
view.setModel(item)
view
else
@viewsByClassName[viewClass.name] = new viewClass(item)
view = @viewsByClassName[viewClass.name] = new viewClass(item)
view
serialize: ->
deserializer: "Pane"