Add priority system to the panels

This commit is contained in:
Ben Ogle 2014-10-17 13:44:40 -07:00
parent b94485eafd
commit de78e53b35
3 changed files with 76 additions and 3 deletions

View File

@ -40,3 +40,57 @@ describe "PanelContainer", ->
panel1.destroy() panel1.destroy()
expect(removePanelSpy).toHaveBeenCalledWith({panel: panel1, index: 0}) expect(removePanelSpy).toHaveBeenCalledWith({panel: panel1, index: 0})
describe "panel priority", ->
describe 'left panel container', ->
[initialPanel] = []
beforeEach ->
container = new PanelContainer({viewRegistry, location: 'left'})
initialPanel = new Panel(item: new TestPanelItem())
container.addPanel(initialPanel)
describe 'when a panel with low piority is added', ->
it 'is inserted at the beginning of the list', ->
container.onDidAddPanel addPanelSpy = jasmine.createSpy()
panel = new Panel(item: new TestPanelItem(), priority: 0)
container.addPanel(panel)
expect(addPanelSpy).toHaveBeenCalledWith({panel, index: 0})
expect(container.getPanels()[0]).toBe panel
describe 'when a panel with piority between two other panels is added', ->
it 'is inserted at the between the two panels', ->
panel = new Panel(item: new TestPanelItem(), priority: 1000)
container.addPanel(panel)
container.onDidAddPanel addPanelSpy = jasmine.createSpy()
panel = new Panel(item: new TestPanelItem(), priority: 101)
container.addPanel(panel)
expect(addPanelSpy).toHaveBeenCalledWith({panel, index: 1})
expect(container.getPanels()[1]).toBe panel
describe 'right panel container', ->
[initialPanel] = []
beforeEach ->
container = new PanelContainer({viewRegistry, location: 'right'})
initialPanel = new Panel(item: new TestPanelItem())
container.addPanel(initialPanel)
describe 'when a panel with high piority is added', ->
it 'is inserted at the beginning of the list', ->
container.onDidAddPanel addPanelSpy = jasmine.createSpy()
panel = new Panel(item: new TestPanelItem(), priority: 1000)
container.addPanel(panel)
expect(addPanelSpy).toHaveBeenCalledWith({panel, index: 0})
expect(container.getPanels()[0]).toBe panel
describe 'when a panel with low piority is added', ->
it 'is inserted at the end of the list', ->
container.onDidAddPanel addPanelSpy = jasmine.createSpy()
panel = new Panel(item: new TestPanelItem(), priority: 0)
container.addPanel(panel)
expect(addPanelSpy).toHaveBeenCalledWith({panel, index: 1})
expect(container.getPanels()[1]).toBe panel

View File

@ -38,8 +38,13 @@ class PanelContainer
addPanel: (panel) -> addPanel: (panel) ->
@subscriptions.add panel.onDidDestroy(@panelDestoryed.bind(this)) @subscriptions.add panel.onDidDestroy(@panelDestoryed.bind(this))
index = @panels.length
@panels.push(panel) index = @getPanelIndex(panel)
if index is @panels.length
@panels.push(panel)
else
@panels.splice(index, 0, panel)
@emitter.emit 'did-add-panel', {panel, index} @emitter.emit 'did-add-panel', {panel, index}
panel panel
@ -48,3 +53,14 @@ class PanelContainer
if index > -1 if index > -1
@panels.splice(index, 1) @panels.splice(index, 1)
@emitter.emit 'did-remove-panel', {panel, index} @emitter.emit 'did-remove-panel', {panel, index}
getPanelIndex: (panel) ->
priority = panel.getPriority()
if @location in ['bottom', 'right']
for p, i in @panels by -1
return i + 1 if priority < p.getPriority()
0
else
for p, i in @panels
return i if priority < p.getPriority()
@panels.length

View File

@ -3,9 +3,10 @@
# Public: # Public:
module.exports = module.exports =
class Panel class Panel
constructor: ({@viewRegistry, @item, @visible}) -> constructor: ({@viewRegistry, @item, @visible, @priority}) ->
@emitter = new Emitter @emitter = new Emitter
@visible ?= true @visible ?= true
@priority ?= 100
destroy: -> destroy: ->
@emitter.emit 'did-destroy', this @emitter.emit 'did-destroy', this
@ -33,6 +34,8 @@ class Panel
getItemView: -> @viewRegistry.getView(@item) getItemView: -> @viewRegistry.getView(@item)
getPriority: -> @priority
isVisible: -> @visible isVisible: -> @visible
hide: -> hide: ->