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

"close others" extension added to default extensions directory. #4590

Merged
merged 13 commits into from
Oct 9, 2013
65 changes: 39 additions & 26 deletions src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,20 +715,12 @@ define(function (require, exports, module) {
return $.Deferred().reject().promise();
}

/**
* Saves all unsaved documents. Returns a Promise that will be resolved once ALL the save
* operations have been completed. If ANY save operation fails, an error dialog is immediately
* shown and the other files wait to save until it is dismissed; after all files have been
* processed, the Promise is rejected if any ONE save operation failed.
*
* @return {$.Promise}
*/
function saveAll() {
function _saveFileList(fileList) {
// Do in serial because doSave shows error UI for each file, and we don't want to stack
// multiple dialogs on top of each other
var userCanceled = false;
return Async.doSequentially(
DocumentManager.getWorkingSet(),
fileList,
function (file) {
// Abort remaining saves if user canceled any Save dialog
if (userCanceled) {
Expand All @@ -753,6 +745,18 @@ define(function (require, exports, module) {
);
}

/**
* Saves all unsaved documents. Returns a Promise that will be resolved once ALL the save
* operations have been completed. If ANY save operation fails, an error dialog is immediately
* shown and the other files wait to save until it is dismissed; after all files have been
* processed, the Promise is rejected if any ONE save operation failed.
*
* @return {$.Promise}
*/
function saveAll() {
return _saveFileList(DocumentManager.getWorkingSet());
}

/**
* Prompts user with save as dialog and saves document.
* @return {$.Promise} a promise that is resolved once the save has been completed
Expand Down Expand Up @@ -903,21 +907,12 @@ define(function (require, exports, module) {
}
return promise;
}

/**
* Closes all open documents; equivalent to calling handleFileClose() for each document, except
* that unsaved changes are confirmed once, in bulk.
* @param {?{promptOnly: boolean}} If true, only displays the relevant confirmation UI and does NOT
* actually close any documents. This is useful when chaining close-all together with
* other user prompts that may be cancelable.
* @return {$.Promise} a promise that is resolved when all files are closed
*/
function handleFileCloseAll(commandData) {
var result = new $.Deferred(),
promptOnly = commandData && commandData.promptOnly;

var unsavedDocs = [];
DocumentManager.getWorkingSet().forEach(function (file) {
function _doCloseDocumentList(list, promptOnly) {
var result = new $.Deferred(),
unsavedDocs = [];

list.forEach(function (file) {
var doc = DocumentManager.getOpenDocumentForPath(file.fullPath);
if (doc && doc.isDirty) {
unsavedDocs.push(doc);
Expand Down Expand Up @@ -980,7 +975,7 @@ define(function (require, exports, module) {
result.reject();
} else if (id === Dialogs.DIALOG_BTN_OK) {
// Save all unsaved files, then if that succeeds, close all
saveAll().done(function () {
_saveFileList(list).done(function () {
result.resolve();
}).fail(function () {
result.reject();
Expand All @@ -997,13 +992,29 @@ define(function (require, exports, module) {
// guarantees that handlers run in the order they are added.
result.done(function () {
if (!promptOnly) {
DocumentManager.closeAll();
DocumentManager.removeFilesFromWorkingSet(list);
}
});

return result.promise();
}

/**
* Closes all open documents; equivalent to calling handleFileClose() for each document, except
* that unsaved changes are confirmed once, in bulk.
* @param {?{promptOnly: boolean}} If true, only displays the relevant confirmation UI and does NOT
* actually close any documents. This is useful when chaining close-all together with
* other user prompts that may be cancelable.
* @return {$.Promise} a promise that is resolved when all files are closed
*/
function handleFileCloseAll(commandData) {
return _doCloseDocumentList(DocumentManager.getWorkingSet(), (commandData && commandData.promptOnly));
}

function handleFileCloseList(documentList) {
return _doCloseDocumentList(documentList);
}

/**
* @private - tracks our closing state if we get called again
*/
Expand Down Expand Up @@ -1182,6 +1193,8 @@ define(function (require, exports, module) {

// Exported for unit testing only
exports._parseDecoratedPath = _parseDecoratedPath;

exports.handleFileCloseList = handleFileCloseList;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other API in this file is exposed as a command instead of directly. For consistency it seems like this one should be the same way.


// Register global commands
CommandManager.register(Strings.CMD_FILE_OPEN, Commands.FILE_OPEN, handleFileOpen);
Expand Down
85 changes: 43 additions & 42 deletions src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,41 @@ define(function (require, exports, module) {
// Dispatch event
$(exports).triggerHandler("workingSetAddList", [uniqueFileList]);
}

/**
* Warning: low level API - use FILE_CLOSE command in most cases.
* 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. Does not prompt for unsaved changes.
* @param {!FileEntry} file
* @param {boolean=} true to suppress redraw after removal
*/
function removeFromWorkingSet(file, suppressRedraw) {

function _internalRemoveFromWorkingSet(file) {
// If doc isn't in working set, do nothing
var index = findInWorkingSet(file.fullPath);
if (index === -1) {
return;
return false;
}

// Remove
_workingSet.splice(index, 1);
_workingSetMRUOrder.splice(findInWorkingSet(file.fullPath, _workingSetMRUOrder), 1);
_workingSetAddedOrder.splice(findInWorkingSet(file.fullPath, _workingSetAddedOrder), 1);

return true;
}

function removeFromWorkingSet(file, suppressRedraw) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: this should be named removeFileFromWorkingSet()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually what would be better is if you could pass a string or an array to removeFromWorkingSet(). you can use $.isArray() to determine if it's an array then you don't need two different functions.

I think you could re-work the handlers for workingSetRemove so there isn't 2 different events (workingSetRemove and workingSetRemoveList) using the same technique.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use the new EcmaScript 5 Array.isArray() function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In project/WorkingSetView.js see _handleFileRemoved function. What is the need of below code.

// 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);
}

In comments it stated that it is used to show icon on the next file. But i removed this block and still i'm able to see icon on the next file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to remove this block. the icon shows up on mouse over so this is just to make sure that when you click the 'x' that it shows up when the item below moves up.

if (_internalRemoveFromWorkingSet(file)) {
$(exports).triggerHandler("workingSetRemove", [file, suppressRedraw]);
}
}

function removeFilesFromWorkingSet(fileList) {
if (!fileList) {
return;
}

fileList.forEach(function (file) {
_internalRemoveFromWorkingSet(file);
});

// Dispatch event
$(exports).triggerHandler("workingSetRemove", [file, suppressRedraw]);
$(exports).triggerHandler("workingSetRemoveList", [fileList]);
}


/**
* Removes all files from the working set list.
Expand Down Expand Up @@ -569,7 +581,6 @@ define(function (require, exports, module) {
_removeAllFromWorkingSet();
}


/**
* Cleans up any loose Documents whose only ref is its own master Editor, and that Editor is not
* rooted in the UI anywhere. This can happen if the Editor is auto-created via Document APIs that
Expand Down Expand Up @@ -817,43 +828,32 @@ define(function (require, exports, module) {
* @param {boolean} isFolder True if path is a folder; False if it is a file.
*/
function notifyPathNameChanged(oldName, newName, isFolder) {
var i, path;

// Update open documents. This will update _currentDocument too, since
// the current document is always open.
var keysToDelete = [];
for (path in _openDocuments) {
if (_openDocuments.hasOwnProperty(path)) {
if (FileUtils.isAffectedWhenRenaming(path, oldName, newName, isFolder)) {
var doc = _openDocuments[path];

// Copy value to new key
var newKey = path.replace(oldName, newName);
_openDocuments[newKey] = doc;

keysToDelete.push(path);

// Update document file
FileUtils.updateFileEntryPath(doc.file, oldName, newName, isFolder);
doc._notifyFilePathChanged();

if (!isFolder) {
// If the path name is a file, there can only be one matched entry in the open document
// list, which we just updated. Break out of the for .. in loop.
break;
}
}
CollectionUtils.forEach(_openDocuments, function (doc, path) {
if (FileUtils.isAffectedWhenRenaming(path, oldName, newName, isFolder)) {
// Copy value to new key
var newKey = path.replace(oldName, newName);
_openDocuments[newKey] = doc;

keysToDelete.push(path);

// Update document file
FileUtils.updateFileEntryPath(doc.file, oldName, newName, isFolder);
doc._notifyFilePathChanged();
}
}
});

// Delete the old keys
for (i = 0; i < keysToDelete.length; i++) {
delete _openDocuments[keysToDelete[i]];
}
keysToDelete.forEach(function (fullPath) {
delete _openDocuments[fullPath];
});

// Update working set
for (i = 0; i < _workingSet.length; i++) {
FileUtils.updateFileEntryPath(_workingSet[i], oldName, newName, isFolder);
}
_workingSet.forEach(function (fileEntry) {
FileUtils.updateFileEntryPath(fileEntry, oldName, newName, isFolder);
});

// Send a "fileNameChanged" event. This will trigger the views to update.
$(exports).triggerHandler("fileNameChange", [oldName, newName]);
Expand Down Expand Up @@ -951,6 +951,7 @@ define(function (require, exports, module) {
exports.addToWorkingSet = addToWorkingSet;
exports.addListToWorkingSet = addListToWorkingSet;
exports.removeFromWorkingSet = removeFromWorkingSet;
exports.removeFilesFromWorkingSet = removeFilesFromWorkingSet;
exports.getNextPrevFile = getNextPrevFile;
exports.swapWorkingSetIndexes = swapWorkingSetIndexes;
exports.sortWorkingSet = sortWorkingSet;
Expand Down
83 changes: 83 additions & 0 deletions src/extensions/default/CloseOthers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window, document */

define(function (require, exports, module) {
"use strict";

var Menus = brackets.getModule("command/Menus"),
CommandManager = brackets.getModule("command/CommandManager"),
Commands = brackets.getModule("command/Commands"),
dm = brackets.getModule("document/DocumentManager"),
docCH = brackets.getModule("document/DocumentCommandHandlers"),
strings = brackets.getModule("i18n!nls/strings"),
settings = JSON.parse(require("text!settings.json")),
working_set_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_MENU),
close_others = "file.close_others",
close_above = "file.close_above",
close_below = "file.close_below";

function handleClose(mode) {

var targetIndex = dm.findInWorkingSet(dm.getCurrentDocument().file.fullPath),
workingSet = dm.getWorkingSet().slice(0),
start = (mode === close_below) ? (targetIndex + 1) : 0,
end = (mode === close_above) ? (targetIndex) : (workingSet.length),
docList = [],
i;

if (mode === close_others) {
end--;
workingSet.splice(targetIndex, 1);
}

for (i = start; i < end; i++) {
docList.push(workingSet[i]);
}

docCH.handleFileCloseList(docList);
}

if (settings.close_below) {
CommandManager.register(strings.CMD_FILE_CLOSE_BELOW, close_below, function () {
handleClose(close_below);
});
working_set_cmenu.addMenuItem(close_below, "", Menus.AFTER, Commands.FILE_CLOSE);
}

if (settings.close_others) {
CommandManager.register(strings.CMD_FILE_CLOSE_OTHERS, close_others, function () {
handleClose(close_others);
});
working_set_cmenu.addMenuItem(close_others, "", Menus.AFTER, Commands.FILE_CLOSE);
}

if (settings.close_above) {
CommandManager.register(strings.CMD_FILE_CLOSE_ABOVE, close_above, function () {
handleClose(close_above);
});
working_set_cmenu.addMenuItem(close_above, "", Menus.AFTER, Commands.FILE_CLOSE);
}
});
5 changes: 5 additions & 0 deletions src/extensions/default/CloseOthers/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"close_others": true,
"close_above": true,
"close_below": true
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JeffryBooher Shall we enable only "close others"? Because, it would annoy users by showing other two (close_above and close_below) all the time. If i once know how to add and remove menus dynamically we can add them. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I say show them. If it becomes a problem we can easily turn it off

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. consider if there is only one file in the working set, it will still show "Close Others", "Close Others Above" and "Close Others Below". So i should work next to add and remove menus intelligently based user click. So if you wish, i can down those two flags and give pull request for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it and the convention is to disable the menu items which we have a story for and we will be working soon. So leave them enabled for now and then we'll have to fix it so they are disabled when the first/last item are selected later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have many menu items that are enabled when they shouldn't be so we are aware of the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. that's cool. just making sure you know this nit. then, no problem. let me do those small change and give PR.

3 changes: 3 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ define({
"CMD_OPEN_FOLDER" : "Open Folder\u2026",
"CMD_FILE_CLOSE" : "Close",
"CMD_FILE_CLOSE_ALL" : "Close All",
"CMD_FILE_CLOSE_OTHERS" : "Close Others",
"CMD_FILE_CLOSE_ABOVE" : "Close Others Above",
"CMD_FILE_CLOSE_BELOW" : "Close Others Below",
"CMD_FILE_SAVE" : "Save",
"CMD_FILE_SAVE_ALL" : "Save All",
"CMD_FILE_SAVE_AS" : "Save As\u2026",
Expand Down