From de78e53b35938bd9364f289a900205380e245314 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Fri, 17 Oct 2014 13:44:40 -0700 Subject: [PATCH] Add priority system to the panels --- spec/panel-container-spec.coffee | 54 ++++++++++++++++++++++++++++++++ src/panel-container.coffee | 20 ++++++++++-- src/panel.coffee | 5 ++- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/spec/panel-container-spec.coffee b/spec/panel-container-spec.coffee index 7931d6b3c..7633a963b 100644 --- a/spec/panel-container-spec.coffee +++ b/spec/panel-container-spec.coffee @@ -40,3 +40,57 @@ describe "PanelContainer", -> panel1.destroy() 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 diff --git a/src/panel-container.coffee b/src/panel-container.coffee index 9bd6971ef..4bf7e30ba 100644 --- a/src/panel-container.coffee +++ b/src/panel-container.coffee @@ -38,8 +38,13 @@ class PanelContainer addPanel: (panel) -> @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} panel @@ -48,3 +53,14 @@ class PanelContainer if index > -1 @panels.splice(index, 1) @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 diff --git a/src/panel.coffee b/src/panel.coffee index c90b35165..87284eea8 100644 --- a/src/panel.coffee +++ b/src/panel.coffee @@ -3,9 +3,10 @@ # Public: module.exports = class Panel - constructor: ({@viewRegistry, @item, @visible}) -> + constructor: ({@viewRegistry, @item, @visible, @priority}) -> @emitter = new Emitter @visible ?= true + @priority ?= 100 destroy: -> @emitter.emit 'did-destroy', this @@ -33,6 +34,8 @@ class Panel getItemView: -> @viewRegistry.getView(@item) + getPriority: -> @priority + isVisible: -> @visible hide: ->