Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

When flipping views, if the doc is already open on the other view, show it without closing the original pane #12248

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/view/Pane.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ define(function (require, exports, module) {
return {indexRequested: requestIndex, index: index};
}

/**
* Ensures that the given pane is focused after other focus related events occur
* @params {string} paneId - paneId of the pane to focus
* @private
*/
function _ensurePaneIsFocused(paneId) {
var pane = MainViewManager._getPane(paneId);

// Defer the focusing until other focus events have occurred.
setTimeout(function () {
// Focus has most likely changed: give it back to the given pane.
pane.focus();
this._lastFocusedElement = pane.$el[0];
MainViewManager.setActivePaneId(paneId);
}, 1);
}

/**
* @typedef {!$el: jQuery, getFile:function():!File, updateLayout:function(forceRefresh:boolean), destroy:function(), getScrollPos:function():?, adjustScrollPos:function(state:Object=, heightDelta:number)=, getViewState:function():?*=, restoreViewState:function(viewState:!*)=, notifyContainerChange:function()=, notifyVisibilityChange:function(boolean)=} View
*/
Expand Down Expand Up @@ -245,40 +262,29 @@ define(function (require, exports, module) {
var currentFile = self.getCurrentlyViewedFile();
var otherPaneId = self.id === FIRST_PANE ? SECOND_PANE : FIRST_PANE;
var otherPane = MainViewManager._getPane(otherPaneId);
var sameDocInOtherView = otherPane.getViewForPath(currentFile.fullPath);

// If the same doc view is present in the destination pane prevent flip
if (otherPane.getViewForPath(currentFile.fullPath)) {
// If the same doc view is present in the destination, show the file instead of flipping it
if (sameDocInOtherView) {
CommandManager.execute(Commands.FILE_OPEN, {fullPath: currentFile.fullPath,
paneId: otherPaneId}).always(function () {
_ensurePaneIsFocused(otherPaneId);
});
return;
}

// Currently active pane is not necessarily self.id as just clicking the button does not
// give focus to the pane. This way it is possible to flip multiple panes to the active one
// without losing focus.
var activePaneIdBeforeFlip = MainViewManager.getActivePaneId();
var currentFileOnOtherPaneIndex = otherPane.findInViewList(currentFile.fullPath);

// if the currentFile is already on other pane just close the current pane
if (currentFileOnOtherPaneIndex !== -1) {
CommandManager.execute(Commands.FILE_CLOSE, {File: currentFile, paneId: self.id});
}

MainViewManager._moveView(self.id, otherPaneId, currentFile).always(function () {
CommandManager.execute(Commands.FILE_OPEN, {fullPath: currentFile.fullPath,
paneId: otherPaneId}).always(function () {

var activePaneBeforeFlip = MainViewManager._getPane(activePaneIdBeforeFlip);

// Trigger view list changes for both panes
self.trigger("viewListChange");
otherPane.trigger("viewListChange");

// Defer the focusing until other focus events have occurred.
setTimeout(function () {
// Focus has most likely changed: give it back to the original pane.
activePaneBeforeFlip.focus();
self._lastFocusedElement = activePaneBeforeFlip.$el[0];
MainViewManager.setActivePaneId(activePaneIdBeforeFlip);
}, 1);
_ensurePaneIsFocused(activePaneIdBeforeFlip);
});
});
});
Expand Down
31 changes: 31 additions & 0 deletions test/spec/MainViewManager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,37 @@ define(function (require, exports, module) {
expect(MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE)).toEqual(null);
});
});
it("should show the file instead of flipping if file is already open", function () {
runs(function () {
MainViewManager.setLayoutScheme(1, 2);
});
runs(function () {
promise = CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN, { fullPath: testPath + "/test.js",
paneId: "first-pane" });
waitsForDone(promise, Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN);
});
runs(function () {
promise = CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN, { fullPath: testPath + "/test.js",
paneId: "second-pane" });
waitsForDone(promise, Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN);
});
runs(function () {
promise = CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN, { fullPath: testPath + "/test.css",
paneId: "second-pane" });
waitsForDone(promise, Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN);
});
runs(function () {
MainViewManager._getPane("first-pane").$headerFlipViewBtn.trigger("click");
});
runs(function () {
MainViewManager.setActivePaneId("first-pane");
expect(MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE).name).toEqual("test.js");
expect(EditorManager.getCurrentFullEditor().document.file.name).toEqual("test.js");
MainViewManager.setActivePaneId("second-pane");
expect(MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE).name).toEqual("test.js");
expect(EditorManager.getCurrentFullEditor().document.file.name).toEqual("test.js");
});
});
it("should merge two panes to the right", function () {
runs(function () {
MainViewManager.setLayoutScheme(1, 2);
Expand Down