Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Unified Diff: ui/file_manager/file_manager/foreground/js/file_manager_commands.js

Issue 1373733002: Reland: Files.app: add create directory operation to context menu of directory tree. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix flaky test cases. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/file_manager/file_manager/foreground/js/file_manager_commands.js
diff --git a/ui/file_manager/file_manager/foreground/js/file_manager_commands.js b/ui/file_manager/file_manager/foreground/js/file_manager_commands.js
index e998bffc213ae5ea823ad03c5858bf64ecf67870..d85c6f7d3dd3f3e06d821232574c9d176d12c420 100644
--- a/ui/file_manager/file_manager/foreground/js/file_manager_commands.js
+++ b/ui/file_manager/file_manager/foreground/js/file_manager_commands.js
@@ -515,86 +515,125 @@ CommandHandler.COMMANDS_['format'] = /** @type {Command} */ ({
* Initiates new folder creation.
* @type {Command}
*/
-CommandHandler.COMMANDS_['new-folder'] = /** @type {Command} */ ({
+CommandHandler.COMMANDS_['new-folder'] = (function() {
+ /**
+ * @constructor
+ * @struct
+ */
+ var NewFolderCommand = function() {};
+
/**
* @param {!Event} event Command event.
* @param {!FileManager} fileManager FileManager to use.
*/
- execute: function(event, fileManager) {
- var defaultName = str('DEFAULT_NEW_FOLDER_NAME');
-
- // Find a name that doesn't exist in the data model.
- var files = fileManager.directoryModel.getFileList();
- var hash = {};
- for (var i = 0; i < files.length; i++) {
- var name = files.item(i).name;
- // Filtering names prevents from conflicts with prototype's names
- // and '__proto__'.
- if (name.substring(0, defaultName.length) == defaultName)
- hash[name] = 1;
- }
-
- var baseName = defaultName;
- var separator = '';
- var suffix = '';
- var index = '';
-
- var advance = function() {
- separator = ' (';
- suffix = ')';
- index++;
- };
-
- var current = function() {
- return baseName + separator + index + suffix;
- };
+ NewFolderCommand.prototype.execute = function(event, fileManager) {
+ var targetDirectory;
+ var executedFromDirectoryTree;
- // Accessing hasOwnProperty is safe since hash properties filtered.
- while (hash.hasOwnProperty(current())) {
- advance();
+ if (event.target instanceof DirectoryTree) {
+ targetDirectory = event.target.selectedItem.entry;
+ executedFromDirectoryTree = true;
+ } else if (event.target instanceof DirectoryItem) {
+ targetDirectory = event.target.entry;
+ executedFromDirectoryTree = true;
+ } else {
+ targetDirectory = fileManager.directoryModel.getCurrentDirEntry();
+ executedFromDirectoryTree = false;
}
- var list = fileManager.ui.listContainer.currentList;
-
- var onSuccess = function(entry) {
- metrics.recordUserAction('CreateNewFolder');
- list.selectedItem = entry;
-
- fileManager.ui.listContainer.endBatchUpdates();
-
- fileManager.namingController.initiateRename();
- };
-
- var onError = function(error) {
- fileManager.ui.listContainer.endBatchUpdates();
+ var directoryModel = fileManager.directoryModel;
+ var directoryTree = fileManager.ui.directoryTree;
+ var listContainer = fileManager.ui.listContainer;
+
+ this.generateNewDirectoryName_(targetDirectory).then(function(newName) {
+ if (!executedFromDirectoryTree)
+ listContainer.startBatchUpdates();
+
+ return new Promise(targetDirectory.getDirectory.bind(targetDirectory,
+ newName,
+ {create: true, exclusive: true})).then(function(newDirectory) {
+ metrics.recordUserAction('CreateNewFolder');
+
+ // Select new directory and start rename operation.
+ if (executedFromDirectoryTree) {
+ directoryTree.updateAndSelectNewDirectory(
+ targetDirectory, newDirectory);
+ fileManager.getDirectoryTreeNamingController().attachAndStart(
+ assert(fileManager.ui.directoryTree.selectedItem));
+ } else {
+ directoryModel.updateAndSelectNewDirectory(
+ newDirectory).then(function() {
+ listContainer.endBatchUpdates();
+ fileManager.namingController.initiateRename();
+ }, function() {
+ listContainer.endBatchUpdates();
+ });
+ }
+ }, function(error) {
+ if (!executedFromDirectoryTree)
+ listContainer.endBatchUpdates();
+
+ fileManager.ui.alertDialog.show(
+ strf('ERROR_CREATING_FOLDER',
+ newName,
+ util.getFileErrorString(error.name)),
+ null, null);
+ });
+ });
+ };
- fileManager.ui.alertDialog.show(
- strf('ERROR_CREATING_FOLDER',
- current(),
- util.getFileErrorString(error.name)),
- null, null);
- };
+ /**
+ * Generates new directory name.
+ * @param {!DirectoryEntry} parentDirectory
+ * @param {number=} opt_index
+ * @private
+ */
+ NewFolderCommand.prototype.generateNewDirectoryName_ = function(
+ parentDirectory, opt_index) {
+ var index = opt_index || 0;
- var onAbort = function() {
- fileManager.ui.listContainer.endBatchUpdates();
- };
+ var defaultName = str('DEFAULT_NEW_FOLDER_NAME');
+ var newName = index === 0 ? defaultName :
+ defaultName + ' (' + index + ')';
+
+ return new Promise(parentDirectory.getDirectory.bind(
+ parentDirectory, newName, {create: false})).then(function(newEntry) {
+ return this.generateNewDirectoryName_(parentDirectory, index + 1);
+ }.bind(this)).catch(function() {
+ return newName;
+ });
+ };
- fileManager.ui.listContainer.startBatchUpdates();
- fileManager.directoryModel.createDirectory(
- current(), onSuccess, onError, onAbort);
- },
/**
* @param {!Event} event Command event.
* @param {!FileManager} fileManager FileManager to use.
*/
- canExecute: function(event, fileManager) {
- var directoryModel = fileManager.directoryModel;
- event.canExecute = !fileManager.isOnReadonlyDirectory() &&
- !fileManager.namingController.isRenamingInProgress() &&
- !directoryModel.isSearching() &&
- !directoryModel.isScanning();
- }
-});
+ NewFolderCommand.prototype.canExecute = function(event, fileManager) {
+ if (event.target instanceof DirectoryItem ||
+ event.target instanceof DirectoryTree) {
+ var entry = CommandUtil.getCommandEntry(event.target);
+ if (!entry || !CommandUtil.shouldShowMenuItemForEntry(
+ fileManager.volumeManager, entry)) {
+ event.canExecute = false;
+ event.command.setHidden(true);
+ return;
+ }
+
+ var locationInfo = fileManager.volumeManager.getLocationInfo(entry);
+ event.canExecute = locationInfo && !locationInfo.isReadOnly;
+ event.command.setHidden(false);
+ } else {
+ var directoryModel = fileManager.directoryModel;
+ event.canExecute = !fileManager.isOnReadonlyDirectory() &&
+ !fileManager.namingController.isRenamingInProgress() &&
+ !directoryModel.isSearching() &&
+ !directoryModel.isScanning();
+ event.command.setHidden(false);
+ }
+ };
+
+ return new NewFolderCommand();
+})();
/**
* Initiates new window creation.

Powered by Google App Engine
This is Rietveld 408576698