diff --git a/src/document/DocumentCommandHandlers.js b/src/document/DocumentCommandHandlers.js index 60d72a2cde7..592ff85ff96 100644 --- a/src/document/DocumentCommandHandlers.js +++ b/src/document/DocumentCommandHandlers.js @@ -329,12 +329,12 @@ define(function (require, exports, module) { /** * Opens the given file, makes it the current document, AND adds it to the working set. - * @param {!{fullPath:string}} Params for FILE_OPEN command + * @param {!{fullPath:string, index:number=}} Params for FILE_OPEN command */ function handleFileAddToWorkingSet(commandData) { return handleFileOpen(commandData).done(function (doc) { // addToWorkingSet is synchronous - DocumentManager.addToWorkingSet(doc.file); + DocumentManager.addToWorkingSet(doc.file, commandData.index); }); } @@ -602,15 +602,13 @@ define(function (require, exports, module) { }); } else { // Working set has file selection focus // replace original file in working set with new file + var index = DocumentManager.findInWorkingSet(doc.file.fullPath); // remove old file from working set. - DocumentManager.removeFromWorkingSet(doc.file); + DocumentManager.removeFromWorkingSet(doc.file, true); //add new file to working set FileViewController - .addToWorkingSetAndSelect(path, - FileViewController.WORKING_SET_VIEW) - .always(function () { - _configureEditorAndResolve(file); - }); + .addToWorkingSetAndSelect(path, FileViewController.WORKING_SET_VIEW, index) + .always(_configureEditorAndResolve(file)); } } diff --git a/src/document/DocumentManager.js b/src/document/DocumentManager.js index 67848b4d677..acb699d8dbe 100644 --- a/src/document/DocumentManager.js +++ b/src/document/DocumentManager.js @@ -227,8 +227,9 @@ define(function (require, exports, module) { * Adds the given file to the end of the working set list, if it is not already in the list. * Does not change which document is currently open in the editor. Completes synchronously. * @param {!FileEntry} file + * @param {number=} index - insert into the working set list at this 0-based index */ - function addToWorkingSet(file) { + function addToWorkingSet(file, index) { // If doc is already in working set, don't add it again if (findInWorkingSet(file.fullPath) !== -1) { return; @@ -241,7 +242,13 @@ define(function (require, exports, module) { } else { file = new NativeFileSystem.FileEntry(file.fullPath); } - _workingSet.push(file); + if ((index === undefined) || (index === null) || (index === -1)) { + // If no index is specified, just add the file to the end of the working set. + _workingSet.push(file); + } else { + // If specified, insert into the working set list at this 0-based index + _workingSet.splice(index, 0, file); + } // Add to MRU order: either first or last, depending on whether it's already the current doc or not if (_currentDocument && _currentDocument.file.fullPath === file.fullPath) { @@ -254,9 +261,13 @@ define(function (require, exports, module) { _workingSetAddedOrder.unshift(file); // Dispatch event - $(exports).triggerHandler("workingSetAdd", file); + if ((index === undefined) || (index === null) || (index === -1)) { + $(exports).triggerHandler("workingSetAdd", file); + } else { + $(exports).triggerHandler("workingSetSort"); + } } - + /** * Adds the given file list to the end of the working set list. * Does not change which document is currently open in the editor. @@ -297,8 +308,9 @@ define(function (require, exports, module) { * Removes the given file from the working set list, if it was in the list. Does not change * the current editor even if it's for this file. * @param {!FileEntry} file + * @param {boolean=} true to suppress redraw after removal */ - function removeFromWorkingSet(file) { + function removeFromWorkingSet(file, suppressRedraw) { // If doc isn't in working set, do nothing var index = findInWorkingSet(file.fullPath); if (index === -1) { @@ -311,7 +323,7 @@ define(function (require, exports, module) { _workingSetAddedOrder.splice(findInWorkingSet(file.fullPath, _workingSetAddedOrder), 1); // Dispatch event - $(exports).triggerHandler("workingSetRemove", file); + $(exports).triggerHandler("workingSetRemove", [file, suppressRedraw]); } /** diff --git a/src/project/FileViewController.js b/src/project/FileViewController.js index 684794ef1ef..c6a04ccf258 100644 --- a/src/project/FileViewController.js +++ b/src/project/FileViewController.js @@ -178,11 +178,12 @@ define(function (require, exports, module) { * @param {!fullPath} * @param {?String} selectIn - specify either WORING_SET_VIEW or PROJECT_MANAGER. * Default is WORING_SET_VIEW. + * @param {number=} index - insert into the working set list at this 0-based index * @return {!$.Promise} */ - function addToWorkingSetAndSelect(fullPath, selectIn) { + function addToWorkingSetAndSelect(fullPath, selectIn, index) { var result = new $.Deferred(), - promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: fullPath}); + promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: fullPath, index: index}); // This properly handles sending the right nofications in cases where the document // is already the current one. In that case we will want to notify with diff --git a/src/project/WorkingSetView.js b/src/project/WorkingSetView.js index 92f65949d6c..2b6f90b49d4 100644 --- a/src/project/WorkingSetView.js +++ b/src/project/WorkingSetView.js @@ -511,23 +511,26 @@ define(function (require, exports, module) { /** * @private - * @param {FileEntry} file + * @param {FileEntry} file + * @param {boolean=} suppressRedraw If true, suppress redraw */ - function _handleFileRemoved(file) { - var $listItem = _findListItemFromFile(file); - if ($listItem) { - // Make the next file in the list show the close icon, - // without having to move the mouse, if there is a next file. - var $nextListItem = $listItem.next(); - if ($nextListItem && $nextListItem.length > 0) { - var canClose = ($listItem.find(".can-close").length === 1); - var isDirty = isOpenAndDirty($nextListItem.data(_FILE_KEY)); - _updateFileStatusIcon($nextListItem, isDirty, canClose); + function _handleFileRemoved(file, suppressRedraw) { + if (!suppressRedraw) { + var $listItem = _findListItemFromFile(file); + if ($listItem) { + // Make the next file in the list show the close icon, + // without having to move the mouse, if there is a next file. + var $nextListItem = $listItem.next(); + if ($nextListItem && $nextListItem.length > 0) { + var canClose = ($listItem.find(".can-close").length === 1); + var isDirty = isOpenAndDirty($nextListItem.data(_FILE_KEY)); + _updateFileStatusIcon($nextListItem, isDirty, canClose); + } + $listItem.remove(); } - $listItem.remove(); + + _redraw(); } - - _redraw(); } function _handleRemoveList(removedFiles) { @@ -592,8 +595,8 @@ define(function (require, exports, module) { _handleFileListAdded(addedFiles); }); - $(DocumentManager).on("workingSetRemove", function (event, removedFile) { - _handleFileRemoved(removedFile); + $(DocumentManager).on("workingSetRemove", function (event, removedFile, suppressRedraw) { + _handleFileRemoved(removedFile, suppressRedraw); }); $(DocumentManager).on("workingSetRemoveList", function (event, removedFiles) {