1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * TODO(dzvorygin): Here we use this hack, since 'hidden' is standard
9 * attribute and we can't use it's setter as usual.
10 * @param {boolean} value New value of hidden property.
12 cr.ui.Command.prototype.setHidden = function(value) {
13 this.__lookupSetter__('hidden').call(this, value);
20 var Command = function() {};
23 * Handles the execute event.
24 * @param {Event} event Command event.
25 * @param {FileManager} fileManager FileManager.
27 Command.prototype.execute = function(event, fileManager) {};
30 * Handles the can execute event.
31 * @param {Event} event Can execute event.
32 * @param {FileManager} fileManager FileManager.
34 Command.prototype.canExecute = function(event, fileManager) {};
37 * Utility for commands.
42 * Extracts entry on which command event was dispatched.
44 * @param {DirectoryTree|DirectoryItem|NavigationList|HTMLLIElement|cr.ui.List}
45 * element Directory to extract a path from.
46 * @return {Entry} Entry of the found node.
48 CommandUtil.getCommandEntry = function(element) {
49 if (element instanceof NavigationList) {
50 // element is a NavigationList.
52 /** @type {NavigationModelItem} */
53 var selectedItem = element.selectedItem;
54 return selectedItem && selectedItem.getCachedEntry();
55 } else if (element instanceof NavigationListItem) {
56 // element is a subitem of NavigationList.
57 /** @type {NavigationList} */
58 var navigationList = element.parentElement;
59 var index = navigationList.getIndexOfListItem(element);
60 /** @type {NavigationModelItem} */
61 var item = (index != -1) ? navigationList.dataModel.item(index) : null;
62 return item && item.getCachedEntry();
63 } else if (element instanceof DirectoryTree) {
64 // element is a DirectoryTree.
65 return element.selectedItem;
66 } else if (element instanceof DirectoryItem) {
67 // element is a sub item in DirectoryTree.
69 // DirectoryItem.fullPath is set on initialization, but entry is lazily.
70 // We may use fullPath just in case that the entry has not been set yet.
72 } else if (element instanceof cr.ui.List) {
73 // element is a normal List (eg. the file list on the right panel).
74 var entry = element.selectedItem;
75 // Check if it is Entry or not by referring the fullPath member variable.
76 return entry && entry.fullPath ? entry : null;
78 console.warn('Unsupported element');
84 * @param {NavigationList} navigationList navigation list to extract root node.
85 * @return {?RootType} Type of the found root.
87 CommandUtil.getCommandRootType = function(navigationList) {
88 var root = CommandUtil.getCommandEntry(navigationList);
90 PathUtil.isRootPath(root.fullPath) &&
91 PathUtil.getRootType(root.fullPath);
95 * Checks if command can be executed on drive.
96 * @param {Event} event Command event to mark.
97 * @param {FileManager} fileManager FileManager to use.
99 CommandUtil.canExecuteEnabledOnDriveOnly = function(event, fileManager) {
100 event.canExecute = fileManager.isOnDrive();
104 * Checks if command should be visible on drive.
105 * @param {Event} event Command event to mark.
106 * @param {FileManager} fileManager FileManager to use.
108 CommandUtil.canExecuteVisibleOnDriveOnly = function(event, fileManager) {
109 event.canExecute = fileManager.isOnDrive();
110 event.command.setHidden(!fileManager.isOnDrive());
114 * Sets as the command as always enabled.
115 * @param {Event} event Command event to mark.
117 CommandUtil.canExecuteAlways = function(event) {
118 event.canExecute = true;
122 * Returns a single selected/passed entry or null.
123 * @param {Event} event Command event.
124 * @param {FileManager} fileManager FileManager to use.
125 * @return {FileEntry} The entry or null.
127 CommandUtil.getSingleEntry = function(event, fileManager) {
128 if (event.target.entry) {
129 return event.target.entry;
131 var selection = fileManager.getSelection();
132 if (selection.totalCount == 1) {
133 return selection.entries[0];
139 * Obtains target entries that can be pinned from the selection.
140 * If directories are included in the selection, it just returns an empty
141 * array to avoid confusing because pinning directory is not supported
144 * @return {Array.<Entry>} Target entries.
146 CommandUtil.getPinTargetEntries = function() {
147 var hasDirectory = false;
148 var results = fileManager.getSelection().entries.filter(function(entry) {
149 hasDirectory = hasDirectory || entry.isDirectory;
150 if (!entry || hasDirectory)
152 var metadata = fileManager.metadataCache_.getCached(entry, 'drive');
153 if (!metadata || metadata.hosted)
155 entry.pinned = metadata.pinned;
158 return hasDirectory ? [] : results;
162 * Sets the default handler for the commandId and prevents handling
163 * the keydown events for this command. Not doing that breaks relationship
164 * of original keyboard event and the command. WebKit would handle it
165 * differently in some cases.
166 * @param {Node} node to register command handler on.
167 * @param {string} commandId Command id to respond to.
169 CommandUtil.forceDefaultHandler = function(node, commandId) {
170 var doc = node.ownerDocument;
171 var command = doc.querySelector('command[id="' + commandId + '"]');
172 node.addEventListener('keydown', function(e) {
173 if (command.matchesEvent(e)) {
174 // Prevent cr.ui.CommandManager of handling it and leave it
175 // for the default handler.
179 node.addEventListener('command', function(event) {
180 if (event.command.id !== commandId)
182 document.execCommand(event.command.id);
183 event.cancelBubble = true;
185 node.addEventListener('canExecute', function(event) {
186 if (event.command.id === commandId)
187 event.canExecute = document.queryCommandEnabled(event.command.id);
195 CommandUtil.defaultCommand = {
196 execute: function(event, fileManager) {
197 fileManager.document.execCommand(event.command.id);
199 canExecute: function(event, fileManager) {
200 event.canExecute = fileManager.document.queryCommandEnabled(
206 * Creates the volume switch command with index.
207 * @param {number} index Volume index from 1 to 9.
208 * @return {Command} Volume switch command.
210 CommandUtil.createVolumeSwitchCommand = function(index) {
212 execute: function(event, fileManager) {
213 fileManager.navigationList.selectByIndex(index - 1);
215 canExecute: function(event, fileManager) {
216 event.canExecute = index > 0 &&
217 index <= fileManager.navigationList.dataModel.length;
223 * Handle of the command events.
224 * @param {FileManager} fileManager FileManager.
227 var CommandHandler = function(fileManager) {
230 * @type {FileManager}
233 this.fileManager_ = fileManager;
237 * @type {Object.<string, cr.ui.Command>}
243 * Whether the ctrl key is pressed or not.
247 this.ctrlKeyPressed_ = false;
251 // Decorate command tags in the document.
252 var commands = fileManager.document.querySelectorAll('command');
253 for (var i = 0; i < commands.length; i++) {
254 cr.ui.Command.decorate(commands[i]);
255 this.commands_[commands[i].id] = commands[i];
259 fileManager.document.addEventListener('command', this.onCommand_.bind(this));
260 fileManager.document.addEventListener('canExecute',
261 this.onCanExecute_.bind(this));
262 fileManager.document.addEventListener('keydown', this.onKeyDown_.bind(this));
263 fileManager.document.addEventListener('keyup', this.onKeyUp_.bind(this));
267 * Updates the availability of all commands.
269 CommandHandler.prototype.updateAvailability = function() {
270 for (var id in this.commands_) {
271 this.commands_[id].canExecuteChange();
276 * Checks if the handler should ignore the current event, eg. since there is
277 * a popup dialog currently opened.
279 * @return {boolean} True if the event should be ignored, false otherwise.
282 CommandHandler.prototype.shouldIgnoreEvents_ = function() {
283 // Do not handle commands, when a dialog is shown.
284 if (this.fileManager_.document.querySelector('.cr-dialog-container.shown'))
287 return false; // Do not ignore.
291 * Handles command events.
292 * @param {Event} event Command event.
295 CommandHandler.prototype.onCommand_ = function(event) {
296 if (this.shouldIgnoreEvents_())
298 var handler = CommandHandler.COMMANDS_[event.command.id];
299 handler.execute.call(this, event, this.fileManager_);
303 * Handles canExecute events.
304 * @param {Event} event Can execute event.
307 CommandHandler.prototype.onCanExecute_ = function(event) {
308 if (this.shouldIgnoreEvents_())
310 var handler = CommandHandler.COMMANDS_[event.command.id];
311 handler.canExecute.call(this, event, this.fileManager_);
315 * Handle key down event.
316 * @param {Event} event Key down event.
319 CommandHandler.prototype.onKeyDown_ = function(event) {
320 // 17 is the keycode of Ctrl key and it means the event is not for other keys
321 // with Ctrl modifier but for ctrl key itself.
322 if (util.getKeyModifiers(event) + event.keyCode == 'Ctrl-17') {
323 this.ctrlKeyPressed_ = true;
324 this.updateAvailability();
329 * Handle key up event.
330 * @param {Event} event Key up event.
333 CommandHandler.prototype.onKeyUp_ = function(event) {
334 // 17 is the keycode of Ctrl key and it means the event is not for other keys
335 // with Ctrl modifier but for ctrl key itself.
336 if (util.getKeyModifiers(event) + event.keyCode == '17') {
337 this.ctrlKeyPressed_ = false;
338 this.updateAvailability();
344 * @type {Object.<string, Command>}
348 CommandHandler.COMMANDS_ = {};
351 * Unmounts external drive.
354 CommandHandler.COMMANDS_['unmount'] = {
356 * @param {Event} event Command event.
357 * @param {FileManager} fileManager The file manager instance.
359 execute: function(event, fileManager) {
360 var root = CommandUtil.getCommandEntry(event.target);
362 fileManager.unmountVolume(PathUtil.getRootPath(root.fullPath));
365 * @param {Event} event Command event.
367 canExecute: function(event, fileManager) {
368 var rootType = CommandUtil.getCommandRootType(event.target);
370 event.canExecute = (rootType == RootType.ARCHIVE ||
371 rootType == RootType.REMOVABLE);
372 event.command.setHidden(!event.canExecute);
373 event.command.label = rootType == RootType.ARCHIVE ?
374 str('CLOSE_ARCHIVE_BUTTON_LABEL') :
375 str('UNMOUNT_DEVICE_BUTTON_LABEL');
380 * Formats external drive.
383 CommandHandler.COMMANDS_['format'] = {
385 * @param {Event} event Command event.
386 * @param {FileManager} fileManager The file manager instance.
388 execute: function(event, fileManager) {
389 var directoryModel = fileManager.directoryModel;
390 var root = CommandUtil.getCommandEntry(event.target);
391 // If an entry is not found from the event target, use the current
392 // directory. This can happen for the format button for unsupported and
393 // unrecognized volumes.
395 root = directoryModel.getCurrentDirEntry();
397 // TODO(satorux): Stop assuming fullPath to be unique. crbug.com/320967
398 var mountPath = root.fullPath;
399 var volumeInfo = fileManager.volumeManager.getVolumeInfo(mountPath);
401 fileManager.confirm.show(
402 loadTimeData.getString('FORMATTING_WARNING'),
403 chrome.fileBrowserPrivate.formatVolume.bind(null,
404 volumeInfo.volumeId));
408 * @param {Event} event Command event.
409 * @param {FileManager} fileManager The file manager instance.
411 canExecute: function(event, fileManager) {
412 var directoryModel = fileManager.directoryModel;
413 var root = CommandUtil.getCommandEntry(event.target);
414 // See the comment in execute() for why doing this.
416 root = directoryModel.getCurrentDirEntry();
417 var removable = root &&
418 PathUtil.getRootType(root.fullPath) == RootType.REMOVABLE;
419 // Don't check if the volume is read-only. Unformatted volume is
420 // considered read-only per directoryModel.isPathReadOnly(), but can be
421 // formatted. An error will be raised if formatting failed anyway.
422 event.canExecute = removable;
423 event.command.setHidden(!removable);
428 * Initiates new folder creation.
431 CommandHandler.COMMANDS_['new-folder'] = {
432 execute: function(event, fileManager) {
433 fileManager.createNewFolder();
435 canExecute: function(event, fileManager) {
436 var directoryModel = fileManager.directoryModel;
437 event.canExecute = !fileManager.isOnReadonlyDirectory() &&
438 !fileManager.isRenamingInProgress() &&
439 !directoryModel.isSearching() &&
440 !directoryModel.isScanning();
445 * Initiates new window creation.
448 CommandHandler.COMMANDS_['new-window'] = {
449 execute: function(event, fileManager) {
450 fileManager.backgroundPage.launchFileManager({
451 defaultPath: fileManager.getCurrentDirectory()
454 canExecute: function(event, fileManager) {
456 fileManager.getCurrentDirectoryEntry() &&
457 (fileManager.dialogType === DialogType.FULL_PAGE);
462 * Deletes selected files.
465 CommandHandler.COMMANDS_['delete'] = {
466 execute: function(event, fileManager) {
467 fileManager.deleteSelection();
469 canExecute: function(event, fileManager) {
470 var allowDeletingWhileOffline =
471 fileManager.directoryModel.getCurrentRootType() === RootType.DRIVE;
472 var selection = fileManager.getSelection();
473 event.canExecute = (!fileManager.isOnReadonlyDirectory() ||
474 allowDeletingWhileOffline) &&
476 selection.totalCount > 0;
481 * Pastes files from clipboard.
484 CommandHandler.COMMANDS_['paste'] = {
485 execute: function() {
486 document.execCommand(event.command.id);
488 canExecute: function(event, fileManager) {
489 var document = fileManager.document;
490 var fileTransferController = fileManager.fileTransferController;
491 event.canExecute = (fileTransferController &&
492 fileTransferController.queryPasteCommandEnabled());
496 CommandHandler.COMMANDS_['cut'] = CommandUtil.defaultCommand;
497 CommandHandler.COMMANDS_['copy'] = CommandUtil.defaultCommand;
500 * Initiates file renaming.
503 CommandHandler.COMMANDS_['rename'] = {
504 execute: function(event, fileManager) {
505 fileManager.initiateRename();
507 canExecute: function(event, fileManager) {
508 var allowRenamingWhileOffline =
509 fileManager.directoryModel.getCurrentRootType() === RootType.DRIVE;
510 var selection = fileManager.getSelection();
512 !fileManager.isRenamingInProgress() &&
513 (!fileManager.isOnReadonlyDirectory() || allowRenamingWhileOffline) &&
515 selection.totalCount == 1;
523 CommandHandler.COMMANDS_['volume-help'] = {
524 execute: function(event, fileManager) {
525 if (fileManager.isOnDrive())
526 util.visitURL(str('GOOGLE_DRIVE_HELP_URL'));
528 util.visitURL(str('FILES_APP_HELP_URL'));
530 canExecute: CommandUtil.canExecuteAlways
534 * Opens drive buy-more-space url.
537 CommandHandler.COMMANDS_['drive-buy-more-space'] = {
538 execute: function(event, fileManager) {
539 util.visitURL(str('GOOGLE_DRIVE_BUY_STORAGE_URL'));
541 canExecute: CommandUtil.canExecuteVisibleOnDriveOnly
545 * Opens drive.google.com.
548 CommandHandler.COMMANDS_['drive-go-to-drive'] = {
549 execute: function(event, fileManager) {
550 util.visitURL(str('GOOGLE_DRIVE_ROOT_URL'));
552 canExecute: CommandUtil.canExecuteVisibleOnDriveOnly
556 * Displays open with dialog for current selection.
559 CommandHandler.COMMANDS_['open-with'] = {
560 execute: function(event, fileManager) {
561 var tasks = fileManager.getSelection().tasks;
563 tasks.showTaskPicker(fileManager.defaultTaskPicker,
564 str('OPEN_WITH_BUTTON_LABEL'),
567 tasks.execute(task.taskId);
571 canExecute: function(event, fileManager) {
572 var tasks = fileManager.getSelection().tasks;
573 event.canExecute = tasks && tasks.size() > 1;
578 * Focuses search input box.
581 CommandHandler.COMMANDS_['search'] = {
582 execute: function(event, fileManager) {
583 var element = fileManager.document.querySelector('#search-box input');
587 canExecute: function(event, fileManager) {
588 event.canExecute = !fileManager.isRenamingInProgress();
593 * Activates the n-th volume.
596 CommandHandler.COMMANDS_['volume-switch-1'] =
597 CommandUtil.createVolumeSwitchCommand(1);
598 CommandHandler.COMMANDS_['volume-switch-2'] =
599 CommandUtil.createVolumeSwitchCommand(2);
600 CommandHandler.COMMANDS_['volume-switch-3'] =
601 CommandUtil.createVolumeSwitchCommand(3);
602 CommandHandler.COMMANDS_['volume-switch-4'] =
603 CommandUtil.createVolumeSwitchCommand(4);
604 CommandHandler.COMMANDS_['volume-switch-5'] =
605 CommandUtil.createVolumeSwitchCommand(5);
606 CommandHandler.COMMANDS_['volume-switch-6'] =
607 CommandUtil.createVolumeSwitchCommand(6);
608 CommandHandler.COMMANDS_['volume-switch-7'] =
609 CommandUtil.createVolumeSwitchCommand(7);
610 CommandHandler.COMMANDS_['volume-switch-8'] =
611 CommandUtil.createVolumeSwitchCommand(8);
612 CommandHandler.COMMANDS_['volume-switch-9'] =
613 CommandUtil.createVolumeSwitchCommand(9);
616 * Flips 'available offline' flag on the file.
619 CommandHandler.COMMANDS_['toggle-pinned'] = {
620 execute: function(event, fileManager) {
621 var pin = !event.command.checked;
622 event.command.checked = pin;
623 var entries = CommandUtil.getPinTargetEntries();
627 // Pick an entry and pin it.
629 // Check if all the entries are pinned or not.
630 if (entries.length == 0)
632 currentEntry = entries.shift();
633 chrome.fileBrowserPrivate.pinDriveFile(
634 currentEntry.toURL(),
639 // Check the result of pinning
640 entryPinned: function() {
641 // Convert to boolean.
642 error = !!chrome.runtime.lastError;
644 fileManager.metadataCache_.get(
645 currentEntry, 'filesystem', steps.showError);
647 fileManager.metadataCache_.clear(currentEntry, 'drive');
648 fileManager.metadataCache_.get(
649 currentEntry, 'drive', steps.updateUI.bind(this));
652 // Update the user interface accoding to the cache state.
653 updateUI: function(drive) {
654 fileManager.updateMetadataInUI_(
655 'drive', [currentEntry.toURL()], [drive]);
661 showError: function(filesystem) {
662 fileManager.alert.showHtml(str('DRIVE_OUT_OF_SPACE_HEADER'),
663 strf('DRIVE_OUT_OF_SPACE_MESSAGE',
664 unescape(currentEntry.name),
665 util.bytesToString(filesystem.size)));
671 canExecute: function(event, fileManager) {
672 var entries = CommandUtil.getPinTargetEntries();
674 for (var i = 0; i < entries.length; i++) {
675 checked = checked && entries[i].pinned;
677 if (entries.length > 0) {
678 event.canExecute = true;
679 event.command.setHidden(false);
680 event.command.checked = checked;
682 event.canExecute = false;
683 event.command.setHidden(true);
689 * Creates zip file for current selection.
692 CommandHandler.COMMANDS_['zip-selection'] = {
693 execute: function(event, fileManager) {
694 var dirEntry = fileManager.getCurrentDirectoryEntry();
695 var selectionEntries = fileManager.getSelection().entries;
696 fileManager.fileOperationManager_.zipSelection(dirEntry, selectionEntries);
698 canExecute: function(event, fileManager) {
699 var dirEntry = fileManager.getCurrentDirectoryEntry();
700 var selection = fileManager.getSelection();
703 !fileManager.isOnReadonlyDirectory() &&
704 !fileManager.isOnDrive() &&
705 selection && selection.totalCount > 0;
710 * Shows the share dialog for the current selection (single only).
713 CommandHandler.COMMANDS_['share'] = {
714 execute: function(event, fileManager) {
715 fileManager.shareSelection();
717 canExecute: function(event, fileManager) {
718 var selection = fileManager.getSelection();
719 event.canExecute = fileManager.isOnDrive() &&
720 !fileManager.isDriveOffline() &&
721 selection && selection.totalCount == 1;
722 event.command.setHidden(!fileManager.isOnDrive());
727 * Creates a shortcut of the selected folder (single only).
730 CommandHandler.COMMANDS_['create-folder-shortcut'] = {
732 * @param {Event} event Command event.
733 * @param {FileManager} fileManager The file manager instance.
735 execute: function(event, fileManager) {
736 var entry = CommandUtil.getCommandEntry(event.target);
738 fileManager.createFolderShortcut(entry.fullPath);
742 * @param {Event} event Command event.
743 * @param {FileManager} fileManager The file manager instance.
745 canExecute: function(event, fileManager) {
746 var entry = CommandUtil.getCommandEntry(event.target);
747 var folderShortcutExists = entry &&
748 fileManager.folderShortcutExists(entry.fullPath);
750 var onlyOneFolderSelected = true;
751 // Only on list, user can select multiple files. The command is enabled only
752 // when a single file is selected.
753 if (event.target instanceof cr.ui.List &&
754 !(event.target instanceof NavigationList)) {
755 var items = event.target.selectedItems;
756 onlyOneFolderSelected = (items.length == 1 && items[0].isDirectory);
759 var eligible = entry &&
760 PathUtil.isEligibleForFolderShortcut(entry.fullPath);
762 eligible && onlyOneFolderSelected && !folderShortcutExists;
763 event.command.setHidden(!eligible || !onlyOneFolderSelected);
768 * Removes the folder shortcut.
771 CommandHandler.COMMANDS_['remove-folder-shortcut'] = {
773 * @param {Event} event Command event.
774 * @param {FileManager} fileManager The file manager instance.
776 execute: function(event, fileManager) {
777 var entry = CommandUtil.getCommandEntry(event.target);
778 if (entry && entry.fullPath)
779 fileManager.removeFolderShortcut(entry.fullPath);
783 * @param {Event} event Command event.
784 * @param {FileManager} fileManager The file manager instance.
786 canExecute: function(event, fileManager) {
787 var entry = CommandUtil.getCommandEntry(event.target);
788 var path = entry && entry.fullPath;
790 var eligible = path && PathUtil.isEligibleForFolderShortcut(path);
791 var isShortcut = path && fileManager.folderShortcutExists(path);
792 event.canExecute = isShortcut && eligible;
793 event.command.setHidden(!event.canExecute);
798 * Zoom in to the Files.app.
801 CommandHandler.COMMANDS_['zoom-in'] = {
802 execute: function(event, fileManager) {
803 chrome.fileBrowserPrivate.zoom('in');
805 canExecute: CommandUtil.canExecuteAlways
809 * Zoom out from the Files.app.
812 CommandHandler.COMMANDS_['zoom-out'] = {
813 execute: function(event, fileManager) {
814 chrome.fileBrowserPrivate.zoom('out');
816 canExecute: CommandUtil.canExecuteAlways
820 * Reset the zoom factor.
823 CommandHandler.COMMANDS_['zoom-reset'] = {
824 execute: function(event, fileManager) {
825 chrome.fileBrowserPrivate.zoom('reset');
827 canExecute: CommandUtil.canExecuteAlways