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 @@ -908,21 +912,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 @@ -985,7 +980,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 @@ -1002,13 +997,29 @@ define(function (require, exports, module) {
// guarantees that handlers run in the order they are added.
result.done(function () {
if (!promptOnly) {
DocumentManager.closeAll();
DocumentManager.removeFromWorkingSet(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 @@ -1215,6 +1226,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
32 changes: 19 additions & 13 deletions src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,28 +316,35 @@ 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);

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

function removeFromWorkingSet(content, suppressRedraw) {
Copy link
Member

Choose a reason for hiding this comment

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

All the docs for this API were lost -- please copy them from the diff above. (And update the type annotation -- I think @param {!FileEntry|Array.<FileEntry>} file ?)

if (content) {
if ($.isArray(content)) {
content.forEach(function (file) {
_internalRemoveFromWorkingSet(file);
});

$(exports).triggerHandler("workingSetRemoveList", [content]);

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

/**
Expand Down Expand Up @@ -569,7 +576,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
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.

1 change: 1 addition & 0 deletions src/extensions/default/CloseOthers/unittest-files/dummy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//This is dummy file. To commit unnittest-files folder git needs a file.
Loading