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

Commit

Permalink
Merge pull request #1919 from adobe/pflynn/ws-context-menu
Browse files Browse the repository at this point in the history
Add 'Rename' and 'Show in Tree' to working set context menu
  • Loading branch information
gruehle committed Oct 23, 2012
2 parents 0b04088 + 18c89a5 commit 6a262f4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/command/Menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ define(function (require, exports, module) {
var working_set_cmenu = registerContextMenu(ContextMenuIds.WORKING_SET_MENU);
working_set_cmenu.addMenuItem(Commands.FILE_CLOSE);
working_set_cmenu.addMenuItem(Commands.FILE_SAVE);
working_set_cmenu.addMenuItem(Commands.FILE_RENAME);
working_set_cmenu.addMenuItem(Commands.NAVIGATE_SHOW_IN_FILE_TREE);

var editor_cmenu = registerContextMenu(ContextMenuIds.EDITOR_MENU);
editor_cmenu.addMenuItem(Commands.TOGGLE_QUICK_EDIT);
Expand Down
9 changes: 8 additions & 1 deletion src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,15 @@ define(function (require, exports, module) {
);
}

/** Show a textfield to rename whatever is currently selected in the sidebar (working set OR tree) */
function handleFileRename() {
ProjectManager.renameSelectedItem();
// Prefer selected tree item (which could be a folder); else use current file
var entry = ProjectManager.getSelectedItem();
if (!entry) {
var doc = DocumentManager.getCurrentDocument();
entry = doc && doc.file;
}
ProjectManager.renameItemInline(entry);
}

/** Closes the window, then quits the app */
Expand Down
116 changes: 64 additions & 52 deletions src/project/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,10 @@ define(function (require, exports, module) {


/**
* Returns the tree node corresponding to the given file/folder. Returns null if the path lies
* outside the project, or if it doesn't exist.
* Finds the tree node corresponding to the given file/folder (rejected if the path lies
* outside the project, or if it doesn't exist).
*
* @param {!Entry} entry FileEntry of DirectoryEntry to show
* @param {!Entry} entry FileEntry or DirectoryEntry to find
* @return {$.Promise} Resolved with jQ obj for the jsTree tree node; or rejected if not found
*/
function _findTreeNode(entry) {
Expand All @@ -786,6 +786,9 @@ define(function (require, exports, module) {

// We're going to traverse from root of tree, one segment at a time
var pathSegments = projRelativePath.split("/");
if (entry.isDirectory) {
pathSegments.pop(); // DirectoryEntry always has a trailing "/"
}

function findInSubtree($nodes, segmentI) {
var seg = pathSegments[segmentI];
Expand Down Expand Up @@ -825,8 +828,15 @@ define(function (require, exports, module) {
return result.promise();
}

function showInTree(fileEntry) {
_findTreeNode(fileEntry)
/**
* Expands tree nodes to show the given file or folder and selects it. Silently no-ops if the
* path lies outside the project, or if it doesn't exist.
*
* @param {!Entry} entry FileEntry or DirectoryEntry to show
* @return {$.Promise} Resolved when done; or rejected if not found
*/
function showInTree(entry) {
return _findTreeNode(entry)
.done(function ($node) {
// jsTree will automatically expand parent nodes to ensure visible
_projectTree.jstree("select_node", $node, false);
Expand Down Expand Up @@ -1166,56 +1176,58 @@ define(function (require, exports, module) {
}

/**
* Rename the selected item in the project tree
* Initiates a rename of the selected item in the project tree, showing an inline editor
* for input. Silently no-ops if the entry lies outside the tree or doesn't exist.
* @param {!Entry} entry FileEntry or DirectoryEntry to rename
*/
function renameSelectedItem() {
var selected = _projectTree.jstree("get_selected"),
isFolder = selected.hasClass("jstree-open") || selected.hasClass("jstree-closed");
function renameItemInline(entry) {
// First make sure the item in the tree is visible - jsTree's rename API doesn't do anything to ensure inline input is visible
showInTree(entry)
.done(function (selected) {
var isFolder = selected.hasClass("jstree-open") || selected.hasClass("jstree-closed");

if (selected) {
_projectTree.on("rename.jstree", function (event, data) {
$(event.target).off("rename.jstree");

// Make sure the file was actually renamed
if (data.rslt.old_name === data.rslt.new_name) {
return;
}

var _resetOldFilename = function () {
_projectTree.jstree("set_text", selected, data.rslt.old_name);
_projectTree.jstree("sort", selected.parent());
};

if (!_checkForValidFilename(data.rslt.new_name)) {
// Invalid filename. Reset the old name and bail.
_resetOldFilename();
return;
}

var oldName = selected.data("entry").fullPath;
var newName = oldName.replace(data.rslt.old_name, data.rslt.new_name);

renameItem(oldName, newName, isFolder)
.done(function () {

// If a folder was renamed, re-select it here, since openAndSelectDocument()
// changed the selection.
if (isFolder) {
var oldSuppressToggleOpen = suppressToggleOpen;

// Supress the open/close toggle
suppressToggleOpen = true;
_projectTree.jstree("select_node", selected, true);
suppressToggleOpen = oldSuppressToggleOpen;
}
})
.fail(function (err) {
// Error during rename. Reset to the old name and alert the user.
_projectTree.one("rename.jstree", function (event, data) {
// Make sure the file was actually renamed
if (data.rslt.old_name === data.rslt.new_name) {
return;
}

var _resetOldFilename = function () {
_projectTree.jstree("set_text", selected, data.rslt.old_name);
_projectTree.jstree("sort", selected.parent());
};

if (!_checkForValidFilename(data.rslt.new_name)) {
// Invalid filename. Reset the old name and bail.
_resetOldFilename();
});
return;
}

var oldName = selected.data("entry").fullPath;
var newName = oldName.replace(data.rslt.old_name, data.rslt.new_name);

renameItem(oldName, newName, isFolder)
.done(function () {

// If a folder was renamed, re-select it here, since openAndSelectDocument()
// changed the selection.
if (isFolder) {
var oldSuppressToggleOpen = suppressToggleOpen;

// Supress the open/close toggle
suppressToggleOpen = true;
_projectTree.jstree("select_node", selected, true);
suppressToggleOpen = oldSuppressToggleOpen;
}
})
.fail(function (err) {
// Error during rename. Reset to the old name and alert the user.
_resetOldFilename();
});
});
_projectTree.jstree("rename");
});
_projectTree.jstree("rename");
}
// No fail handler: silently no-op if file doesn't exist in tree
}

/**
Expand Down Expand Up @@ -1260,7 +1272,7 @@ define(function (require, exports, module) {
exports.isWelcomeProjectPath = isWelcomeProjectPath;
exports.updateWelcomeProjectPath = updateWelcomeProjectPath;
exports.createNewItem = createNewItem;
exports.renameSelectedItem = renameSelectedItem;
exports.renameItemInline = renameItemInline;
exports.forceFinishRename = forceFinishRename;
exports.showInTree = showInTree;
});

0 comments on commit 6a262f4

Please sign in to comment.