diff --git a/spec/app/pane-spec.coffee b/spec/app/pane-spec.coffee index f0f0d99aa..331e414e4 100644 --- a/spec/app/pane-spec.coffee +++ b/spec/app/pane-spec.coffee @@ -27,6 +27,27 @@ describe "Pane", -> expect(view2.css('display')).toBe '' expect(pane.currentItem).toBe view2 + it "triggers 'pane:active-item-changed' if the pane is active and the item isn't already the currentItem", -> + pane.makeActive() + itemChangedHandler = jasmine.createSpy("itemChangedHandler") + container.on 'pane:active-item-changed', itemChangedHandler + + expect(pane.currentItem).toBe view1 + pane.showItem(view2) + pane.showItem(view2) + expect(itemChangedHandler.callCount).toBe 1 + expect(itemChangedHandler.argsForCall[0][1]).toBe view2 + itemChangedHandler.reset() + + pane.showItem(editSession1) + expect(itemChangedHandler).toHaveBeenCalled() + expect(itemChangedHandler.argsForCall[0][1]).toBe editSession1 + itemChangedHandler.reset() + + pane.makeInactive() + pane.showItem(editSession2) + expect(itemChangedHandler).not.toHaveBeenCalled() + describe "when the given item isn't yet in the items list on the pane", -> it "adds it to the items list after the current item", -> view3 = $$ -> @div id: 'view-3', "View 3" @@ -130,6 +151,18 @@ describe "Pane", -> pane.focus() expect(focusHandler).toHaveBeenCalled() + it "triggers 'pane:active-item-changed' if it was not previously active", -> + itemChangedHandler = jasmine.createSpy("itemChangedHandler") + container.on 'pane:active-item-changed', itemChangedHandler + + expect(pane.isActive()).toBeFalsy() + pane.focusin() + expect(pane.isActive()).toBeTruthy() + pane.focusin() + + expect(itemChangedHandler.callCount).toBe 1 + expect(itemChangedHandler.argsForCall[0][1]).toBe pane.currentItem + describe "split methods", -> [pane1, view3, view4] = [] beforeEach -> diff --git a/src/app/pane-container.coffee b/src/app/pane-container.coffee index 4e91b3b35..ca4c2f254 100644 --- a/src/app/pane-container.coffee +++ b/src/app/pane-container.coffee @@ -13,11 +13,6 @@ class PaneContainer extends View @content: -> @div id: 'panes' - initialize: -> - @on 'focusin', (e) => - focusedPane = $(e.target).closest('.pane').view() - @setActivePane(focusedPane) - serialize: -> deserializer: 'PaneContainer' root: @getRoot()?.serialize() @@ -40,9 +35,6 @@ class PaneContainer extends View getActivePane: -> @find('.pane.active').view() ? @find('.pane:first').view() - setActivePane: (pane) -> - @find('.pane').removeClass('active') - pane.addClass('active') adjustPaneDimensions: -> if root = @getRoot() diff --git a/src/app/pane.coffee b/src/app/pane.coffee index 69ce2af62..cb5e3636d 100644 --- a/src/app/pane.coffee +++ b/src/app/pane.coffee @@ -27,9 +27,21 @@ class Pane extends View @command 'pane:split-right', => @splitRight() @command 'pane:split-up', => @splitUp() @command 'pane:split-down', => @splitDown() - @on 'focus', => - @viewForCurrentItem().focus() - false + @on 'focus', => @currentView.focus(); false + @on 'focusin', => @makeActive() + + makeActive: -> + for pane in @getContainer().getPanes() when pane isnt this + pane.makeInactive() + wasActive = @isActive() + @addClass('active') + @trigger 'pane:active-item-changed', [@currentItem] unless wasActive + + makeInactive: -> + @removeClass('active') + + isActive: -> + @hasClass('active') getItems: -> new Array(@items...) @@ -55,6 +67,7 @@ class Pane extends View @showItem(@items[index]) showItem: (item) -> + return if item is @currentItem @addItem(item) @itemViews.children().hide() view = @viewForItem(item) @@ -62,6 +75,7 @@ class Pane extends View @currentItem = item @currentView = view @currentView.show() + @trigger 'pane:active-item-changed', [item] if @isActive() addItem: (item) -> return if _.include(@items, item)