From 590352b079b22f040e3a5a6c668b2cbba58b07db Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 24 Apr 2012 12:50:00 -0600 Subject: [PATCH] RootView emits an 'active-editor-path-change' event whenever the path of the active editor changes for any reason (buffer path change, new buffer opened, focus changes) --- spec/app/root-view-spec.coffee | 34 +++++++++++++++++++++++++++++++--- src/app/root-view.coffee | 8 ++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index dd52ee908..fbc58a58f 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -388,16 +388,21 @@ describe "RootView", -> expect(commandHandler).toHaveBeenCalled() describe "when the path of the focused editor's buffer changes", -> - it "changes the document.title", -> + it "changes the document.title and emits an active-editor-path-change event", -> + pathChangeHandler = jasmine.createSpy 'pathChangeHandler' + rootView.on 'active-editor-path-change', pathChangeHandler + editor1 = rootView.activeEditor() expect(document.title).toBe path editor2 = rootView.activeEditor().splitLeft() editor2.setBuffer(new Buffer("second.txt")) - editor2.focus() + expect(pathChangeHandler).toHaveBeenCalled() expect(document.title).toBe "second.txt" + pathChangeHandler.reset() editor1.buffer.setPath("should-not-be-title.txt") + expect(pathChangeHandler).not.toHaveBeenCalled() expect(document.title).toBe "second.txt" it "creates a project if there isn't one yet and the buffer was previously unsaved", -> @@ -406,7 +411,30 @@ describe "RootView", -> rootView.activeEditor().buffer.saveAs('/tmp/ignore-me') expect(rootView.project.path).toBe '/tmp/' + describe "when editors are focused", -> + it "triggers 'active-editor-path-change' events if the path of the active editor actually changes", -> + pathChangeHandler = jasmine.createSpy 'pathChangeHandler' + rootView.on 'active-editor-path-change', pathChangeHandler + + editor1 = rootView.activeEditor() + editor2 = rootView.activeEditor().splitLeft() + + rootView.open(require.resolve('fixtures/sample.txt')) + expect(pathChangeHandler).toHaveBeenCalled() + pathChangeHandler.reset() + + editor1.focus() + expect(pathChangeHandler).toHaveBeenCalled() + pathChangeHandler.reset() + + rootView.focus() + expect(pathChangeHandler).not.toHaveBeenCalled() + + editor2.setBuffer editor1.buffer + editor2.focus() + expect(pathChangeHandler).not.toHaveBeenCalled() + describe "when the last editor is removed", -> - it "updates the title to the project path", -> + it "updates the title to the project path", -> rootView.editors()[0].remove() expect(document.title).toBe rootView.project.path diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index c9e788a7a..b39a240cd 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -81,16 +81,16 @@ class RootView extends View editorFocused: (editor) -> if @panes.containsElement(editor) - @panes.find('.editor') - .removeClass('active') - .off('.root-view') + previousActiveEditor = @panes.find('.editor.active').view() + previousActiveEditor?.removeClass('active').off('.root-view') editor .addClass('active') .on 'editor-path-change.root-view', => @trigger 'active-editor-path-change', editor.buffer.path - @trigger 'active-editor-path-change', editor.buffer.path + if not previousActiveEditor or editor.buffer.path != previousActiveEditor.buffer.path + @trigger 'active-editor-path-change', editor.buffer.path setTitle: (title='untitled') -> document.title = title