Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 5 | 'use strict'; |
| 6 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 7 | var CommandUtil = {}; |
| 8 | |
| 9 | /** |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 10 | * Extracts path on which command event was dispatched. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | * |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 12 | * @param {DirectoryTree|DirectoryItem|NavigationList|HTMLLIElement|cr.ui.List} |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 13 | * element Directory to extract a path from. |
| 14 | * @return {?string} Path of the found node. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | */ |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 16 | CommandUtil.getCommandPath = function(element) { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 17 | if (element instanceof NavigationList) { |
| 18 | // element is a NavigationList. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 19 | return element.selectedItem; |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 20 | } else if (element instanceof NavigationListItem) { |
| 21 | // element is a subitem of NavigationList. |
| 22 | var navigationList = element.parentElement; |
| 23 | var index = navigationList.getIndexOfListItem(element); |
| 24 | return (index != -1) ? navigationList.dataModel.item(index) : null; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 25 | } else if (element instanceof DirectoryTree) { |
| 26 | // element is a DirectoryTree. |
| 27 | var item = element.selectedItem; |
| 28 | return item && item.fullPath; |
| 29 | } else if (element instanceof DirectoryItem) { |
| 30 | // element is a sub item in DirectoryTree. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 32 | // DirectoryItem.fullPath is set on initialization, but entry is lazily. |
| 33 | // We may use fullPath just in case that the entry has not been set yet. |
| 34 | return element.entry && element.entry.fullPath || |
| 35 | element.fullPath; |
| 36 | } else if (cr.ui.List) { |
| 37 | // element is a normal List (eg. the file list on the right panel). |
| 38 | var entry = element.selectedItem; |
| 39 | return entry && entry.fullPath; |
| 40 | } else { |
| 41 | console.warn('Unsupported element'); |
| 42 | return null; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 43 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /** |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 47 | * @param {NavigationList} navigationList navigation list to extract root node. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 48 | * @return {?RootType} Type of the found root. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 49 | */ |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 50 | CommandUtil.getCommandRootType = function(navigationList) { |
| 51 | var root = CommandUtil.getCommandPath(navigationList); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 52 | return root && PathUtil.isRootPath(root) && PathUtil.getRootType(root); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 56 | * Checks if command can be executed on drive. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | * @param {Event} event Command event to mark. |
| 58 | * @param {FileManager} fileManager FileManager to use. |
| 59 | */ |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 60 | CommandUtil.canExecuteEnabledOnDriveOnly = function(event, fileManager) { |
| 61 | event.canExecute = fileManager.isOnDrive(); |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Checks if command should be visible on drive. |
| 66 | * @param {Event} event Command event to mark. |
| 67 | * @param {FileManager} fileManager FileManager to use. |
| 68 | */ |
| 69 | CommandUtil.canExecuteVisibleOnDriveOnly = function(event, fileManager) { |
| 70 | event.canExecute = fileManager.isOnDrive(); |
| 71 | event.command.setHidden(!fileManager.isOnDrive()); |
| 72 | }; |
| 73 | |
| 74 | /** |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 75 | * Checks if command should be visible on drive with pressing ctrl key. |
| 76 | * @param {Event} event Command event to mark. |
| 77 | * @param {FileManager} fileManager FileManager to use. |
| 78 | */ |
| 79 | CommandUtil.canExecuteVisibleOnDriveWithCtrlKeyOnly = |
| 80 | function(event, fileManager) { |
| 81 | event.canExecute = fileManager.isOnDrive() && fileManager.isCtrlKeyPressed(); |
| 82 | event.command.setHidden(!event.canExecute); |
| 83 | }; |
| 84 | |
| 85 | /** |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 86 | * Sets as the command as always enabled. |
| 87 | * @param {Event} event Command event to mark. |
| 88 | */ |
| 89 | CommandUtil.canExecuteAlways = function(event) { |
| 90 | event.canExecute = true; |
| 91 | }; |
| 92 | |
| 93 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 94 | * Returns a single selected/passed entry or null. |
| 95 | * @param {Event} event Command event. |
| 96 | * @param {FileManager} fileManager FileManager to use. |
| 97 | * @return {FileEntry} The entry or null. |
| 98 | */ |
| 99 | CommandUtil.getSingleEntry = function(event, fileManager) { |
| 100 | if (event.target.entry) { |
| 101 | return event.target.entry; |
| 102 | } |
| 103 | var selection = fileManager.getSelection(); |
| 104 | if (selection.totalCount == 1) { |
| 105 | return selection.entries[0]; |
| 106 | } |
| 107 | return null; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * Registers handler on specific command on specific node. |
| 112 | * @param {Node} node Node to register command handler on. |
| 113 | * @param {string} commandId Command id to respond to. |
| 114 | * @param {{execute:function, canExecute:function}} handler Handler to use. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 115 | * @param {...Object} var_args Additional arguments to pass to handler. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 116 | */ |
| 117 | CommandUtil.registerCommand = function(node, commandId, handler, var_args) { |
| 118 | var args = Array.prototype.slice.call(arguments, 3); |
| 119 | |
| 120 | node.addEventListener('command', function(event) { |
| 121 | if (event.command.id == commandId) { |
| 122 | handler.execute.apply(handler, [event].concat(args)); |
| 123 | event.cancelBubble = true; |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | node.addEventListener('canExecute', function(event) { |
| 128 | if (event.command.id == commandId) |
| 129 | handler.canExecute.apply(handler, [event].concat(args)); |
| 130 | }); |
| 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * Sets Commands.defaultCommand for the commandId and prevents handling |
| 135 | * the keydown events for this command. Not doing that breaks relationship |
| 136 | * of original keyboard event and the command. WebKit would handle it |
| 137 | * differently in some cases. |
| 138 | * @param {Node} node to register command handler on. |
| 139 | * @param {string} commandId Command id to respond to. |
| 140 | */ |
| 141 | CommandUtil.forceDefaultHandler = function(node, commandId) { |
| 142 | var doc = node.ownerDocument; |
| 143 | var command = doc.querySelector('command[id="' + commandId + '"]'); |
| 144 | node.addEventListener('keydown', function(e) { |
| 145 | if (command.matchesEvent(e)) { |
| 146 | // Prevent cr.ui.CommandManager of handling it and leave it |
| 147 | // for the default handler. |
| 148 | e.stopPropagation(); |
| 149 | } |
| 150 | }); |
| 151 | CommandUtil.registerCommand(node, commandId, Commands.defaultCommand, doc); |
| 152 | }; |
| 153 | |
| 154 | var Commands = {}; |
| 155 | |
| 156 | /** |
| 157 | * Forwards all command events to standard document handlers. |
| 158 | */ |
| 159 | Commands.defaultCommand = { |
| 160 | execute: function(event, document) { |
| 161 | document.execCommand(event.command.id); |
| 162 | }, |
| 163 | canExecute: function(event, document) { |
| 164 | event.canExecute = document.queryCommandEnabled(event.command.id); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | /** |
| 169 | * Unmounts external drive. |
| 170 | */ |
| 171 | Commands.unmountCommand = { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 172 | /** |
| 173 | * @param {Event} event Command event. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 174 | * @param {FileManager} fileManager The file manager instance. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 175 | */ |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 176 | execute: function(event, fileManager) { |
| 177 | var root = CommandUtil.getCommandPath(event.target); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 178 | if (root) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 179 | fileManager.unmountVolume(PathUtil.getRootPath(root)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 180 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 181 | /** |
| 182 | * @param {Event} event Command event. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 183 | */ |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 184 | canExecute: function(event) { |
| 185 | var rootType = CommandUtil.getCommandRootType(event.target); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 186 | |
| 187 | event.canExecute = (rootType == RootType.ARCHIVE || |
| 188 | rootType == RootType.REMOVABLE); |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 189 | event.command.setHidden(!event.canExecute); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 190 | event.command.label = rootType == RootType.ARCHIVE ? |
| 191 | str('CLOSE_ARCHIVE_BUTTON_LABEL') : |
| 192 | str('UNMOUNT_DEVICE_BUTTON_LABEL'); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 193 | } |
| 194 | }; |
| 195 | |
| 196 | /** |
| 197 | * Formats external drive. |
| 198 | */ |
| 199 | Commands.formatCommand = { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 200 | /** |
| 201 | * @param {Event} event Command event. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 202 | * @param {FileManager} fileManager The file manager instance. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 203 | */ |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 204 | execute: function(event, fileManager) { |
| 205 | var root = CommandUtil.getCommandPath(event.target); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 206 | |
| 207 | if (root) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 208 | var url = util.makeFilesystemUrl(PathUtil.getRootPath(root)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 209 | fileManager.confirm.show( |
| 210 | loadTimeData.getString('FORMATTING_WARNING'), |
| 211 | chrome.fileBrowserPrivate.formatDevice.bind(null, url)); |
| 212 | } |
| 213 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 214 | /** |
| 215 | * @param {Event} event Command event. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 216 | * @param {FileManager} fileManager The file manager instance. |
| 217 | * @param {DirectoryModel} directoryModel The directory model instance. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 218 | */ |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 219 | canExecute: function(event, fileManager, directoryModel) { |
| 220 | var root = CommandUtil.getCommandPath(event.target); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 221 | var removable = root && |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 222 | PathUtil.getRootType(root) == RootType.REMOVABLE; |
| 223 | var isReadOnly = root && directoryModel.isPathReadOnly(root); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 224 | event.canExecute = removable && !isReadOnly; |
| 225 | event.command.setHidden(!removable); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 226 | } |
| 227 | }; |
| 228 | |
| 229 | /** |
| 230 | * Imports photos from external drive |
| 231 | */ |
| 232 | Commands.importCommand = { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 233 | /** |
| 234 | * @param {Event} event Command event. |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 235 | * @param {NavigationList} navigationList Target navigation list. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 236 | */ |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 237 | execute: function(event, navigationList) { |
| 238 | var root = CommandUtil.getCommandPath(navigationList); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 239 | if (!root) |
| 240 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 241 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 242 | // TODO(mtomasz): Implement launching Photo Importer. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 243 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 244 | /** |
| 245 | * @param {Event} event Command event. |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 246 | * @param {NavigationList} navigationList Target navigation list. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 247 | */ |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 248 | canExecute: function(event, navigationList) { |
| 249 | var rootType = CommandUtil.getCommandRootType(navigationList); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 250 | event.canExecute = (rootType != RootType.DRIVE); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 251 | } |
| 252 | }; |
| 253 | |
| 254 | /** |
| 255 | * Initiates new folder creation. |
| 256 | */ |
| 257 | Commands.newFolderCommand = { |
| 258 | execute: function(event, fileManager) { |
| 259 | fileManager.createNewFolder(); |
| 260 | }, |
| 261 | canExecute: function(event, fileManager, directoryModel) { |
| 262 | event.canExecute = !fileManager.isOnReadonlyDirectory() && |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 263 | !fileManager.isRenamingInProgress() && |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 264 | !directoryModel.isSearching() && |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 265 | !directoryModel.isScanning(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 266 | } |
| 267 | }; |
| 268 | |
| 269 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 270 | * Initiates new window creation. |
| 271 | */ |
| 272 | Commands.newWindowCommand = { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 273 | execute: function(event, fileManager, directoryModel) { |
| 274 | chrome.runtime.getBackgroundPage(function(background) { |
| 275 | var appState = { |
| 276 | defaultPath: directoryModel.getCurrentDirPath() |
| 277 | }; |
| 278 | background.launchFileManager(appState); |
| 279 | }); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 280 | }, |
| 281 | canExecute: function(event, fileManager) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 282 | event.canExecute = (fileManager.dialogType == DialogType.FULL_PAGE); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * Changed the default app handling inserted media. |
| 288 | */ |
| 289 | Commands.changeDefaultAppCommand = { |
| 290 | execute: function(event, fileManager) { |
| 291 | fileManager.showChangeDefaultAppPicker(); |
| 292 | }, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 293 | canExecute: CommandUtil.canExecuteAlways |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | /** |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 297 | * Deletes selected files. |
| 298 | */ |
| 299 | Commands.deleteFileCommand = { |
| 300 | execute: function(event, fileManager) { |
| 301 | fileManager.deleteSelection(); |
| 302 | }, |
| 303 | canExecute: function(event, fileManager) { |
| 304 | var selection = fileManager.getSelection(); |
| 305 | event.canExecute = !fileManager.isOnReadonlyDirectory() && |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 306 | selection && |
| 307 | selection.totalCount > 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 308 | } |
| 309 | }; |
| 310 | |
| 311 | /** |
| 312 | * Pastes files from clipboard. |
| 313 | */ |
| 314 | Commands.pasteFileCommand = { |
| 315 | execute: Commands.defaultCommand.execute, |
| 316 | canExecute: function(event, document, fileTransferController) { |
| 317 | event.canExecute = (fileTransferController && |
| 318 | fileTransferController.queryPasteCommandEnabled()); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | /** |
| 323 | * Initiates file renaming. |
| 324 | */ |
| 325 | Commands.renameFileCommand = { |
| 326 | execute: function(event, fileManager) { |
| 327 | fileManager.initiateRename(); |
| 328 | }, |
| 329 | canExecute: function(event, fileManager) { |
| 330 | var selection = fileManager.getSelection(); |
| 331 | event.canExecute = |
| 332 | !fileManager.isRenamingInProgress() && |
| 333 | !fileManager.isOnReadonlyDirectory() && |
| 334 | selection && |
| 335 | selection.totalCount == 1; |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 340 | * Opens drive help. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 341 | */ |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 342 | Commands.volumeHelpCommand = { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 343 | execute: function() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 344 | if (fileManager.isOnDrive()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 345 | chrome.windows.create({url: FileManager.GOOGLE_DRIVE_HELP}); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 346 | else |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 347 | chrome.windows.create({url: FileManager.FILES_APP_HELP}); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 348 | }, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 349 | canExecute: CommandUtil.canExecuteAlways |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 353 | * Opens drive buy-more-space url. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 354 | */ |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 355 | Commands.driveBuySpaceCommand = { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 356 | execute: function() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 357 | chrome.windows.create({url: FileManager.GOOGLE_DRIVE_BUY_STORAGE}); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 358 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 359 | canExecute: CommandUtil.canExecuteVisibleOnDriveOnly |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 363 | * Clears drive cache. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 364 | */ |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 365 | Commands.driveClearCacheCommand = { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 366 | execute: function() { |
| 367 | chrome.fileBrowserPrivate.clearDriveCache(); |
| 368 | }, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 369 | canExecute: CommandUtil.canExecuteVisibleOnDriveWithCtrlKeyOnly |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | /** |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 373 | * Opens drive.google.com. |
| 374 | */ |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 375 | Commands.driveGoToDriveCommand = { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 376 | execute: function() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 377 | chrome.windows.create({url: FileManager.GOOGLE_DRIVE_ROOT}); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 378 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 379 | canExecute: CommandUtil.canExecuteVisibleOnDriveOnly |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | /** |
| 383 | * Displays open with dialog for current selection. |
| 384 | */ |
| 385 | Commands.openWithCommand = { |
| 386 | execute: function(event, fileManager) { |
| 387 | var tasks = fileManager.getSelection().tasks; |
| 388 | if (tasks) { |
| 389 | tasks.showTaskPicker(fileManager.defaultTaskPicker, |
| 390 | str('OPEN_WITH_BUTTON_LABEL'), |
| 391 | null, |
| 392 | function(task) { |
| 393 | tasks.execute(task.taskId); |
| 394 | }); |
| 395 | } |
| 396 | }, |
| 397 | canExecute: function(event, fileManager) { |
| 398 | var tasks = fileManager.getSelection().tasks; |
| 399 | event.canExecute = tasks && tasks.size() > 1; |
| 400 | } |
| 401 | }; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * Focuses search input box. |
| 405 | */ |
| 406 | Commands.searchCommand = { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 407 | execute: function(event, fileManager, element) { |
| 408 | element.focus(); |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 409 | element.select(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 410 | }, |
| 411 | canExecute: function(event, fileManager) { |
| 412 | event.canExecute = !fileManager.isRenamingInProgress(); |
| 413 | } |
| 414 | }; |
| 415 | |
| 416 | /** |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 417 | * Activates the n-th volume. |
| 418 | */ |
| 419 | Commands.volumeSwitchCommand = { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 420 | execute: function(event, navigationList, index) { |
| 421 | navigationList.selectByIndex(index - 1); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 422 | }, |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 423 | canExecute: function(event, navigationList, index) { |
| 424 | event.canExecute = index > 0 && index <= navigationList.dataModel.length; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 425 | } |
| 426 | }; |
| 427 | |
| 428 | /** |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 429 | * Flips 'available offline' flag on the file. |
| 430 | */ |
| 431 | Commands.togglePinnedCommand = { |
| 432 | execute: function(event, fileManager) { |
| 433 | var pin = !event.command.checked; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 434 | event.command.checked = pin; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 435 | var entries = Commands.togglePinnedCommand.getTargetEntries_(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 436 | var currentEntry; |
| 437 | var error = false; |
| 438 | var steps = { |
| 439 | // Pick an entry and pin it. |
| 440 | start: function() { |
| 441 | // Check if all the entries are pinned or not. |
| 442 | if (entries.length == 0) |
| 443 | return; |
| 444 | currentEntry = entries.shift(); |
| 445 | chrome.fileBrowserPrivate.pinDriveFile( |
| 446 | currentEntry.toURL(), |
| 447 | pin, |
| 448 | steps.entryPinned); |
| 449 | }, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 450 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 451 | // Check the result of pinning |
| 452 | entryPinned: function() { |
| 453 | // Convert to boolean. |
| 454 | error = !!chrome.runtime.lastError; |
| 455 | if (error && pin) { |
| 456 | fileManager.metadataCache_.get( |
| 457 | currentEntry, 'filesystem', steps.showError); |
| 458 | } |
| 459 | fileManager.metadataCache_.clear(currentEntry, 'drive'); |
| 460 | fileManager.metadataCache_.get( |
| 461 | currentEntry, 'drive', steps.updateUI.bind(this)); |
| 462 | }, |
| 463 | |
| 464 | // Update the user interface accoding to the cache state. |
| 465 | updateUI: function(drive) { |
| 466 | fileManager.updateMetadataInUI_( |
| 467 | 'drive', [currentEntry.toURL()], [drive]); |
| 468 | if (!error) |
| 469 | steps.start(); |
| 470 | }, |
| 471 | |
| 472 | // Show the error |
| 473 | showError: function(filesystem) { |
| 474 | fileManager.alert.showHtml(str('DRIVE_OUT_OF_SPACE_HEADER'), |
| 475 | strf('DRIVE_OUT_OF_SPACE_MESSAGE', |
| 476 | unescape(currentEntry.name), |
| 477 | util.bytesToString(filesystem.size))); |
| 478 | } |
| 479 | }; |
| 480 | steps.start(); |
| 481 | }, |
| 482 | |
| 483 | canExecute: function(event, fileManager) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 484 | var entries = Commands.togglePinnedCommand.getTargetEntries_(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 485 | var checked = true; |
| 486 | for (var i = 0; i < entries.length; i++) { |
| 487 | checked = checked && entries[i].pinned; |
| 488 | } |
| 489 | if (entries.length > 0) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 490 | event.canExecute = true; |
| 491 | event.command.setHidden(false); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 492 | event.command.checked = checked; |
| 493 | } else { |
| 494 | event.canExecute = false; |
| 495 | event.command.setHidden(true); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 496 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 497 | }, |
| 498 | |
| 499 | /** |
| 500 | * Obtains target entries from the selection. |
| 501 | * If directories are included in the selection, it just returns an empty |
| 502 | * array to avoid confusing because pinning directory is not supported |
| 503 | * currently. |
| 504 | * |
| 505 | * @return {Array.<Entry>} Target entries. |
| 506 | * @private |
| 507 | */ |
| 508 | getTargetEntries_: function() { |
| 509 | var hasDirectory = false; |
| 510 | var results = fileManager.getSelection().entries.filter(function(entry) { |
| 511 | hasDirectory = hasDirectory || entry.isDirectory; |
| 512 | if (!entry || hasDirectory) |
| 513 | return false; |
| 514 | var metadata = fileManager.metadataCache_.getCached(entry, 'drive'); |
| 515 | if (!metadata || metadata.hosted) |
| 516 | return false; |
| 517 | entry.pinned = metadata.pinned; |
| 518 | return true; |
| 519 | }); |
| 520 | return hasDirectory ? [] : results; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 521 | } |
| 522 | }; |
| 523 | |
| 524 | /** |
| 525 | * Creates zip file for current selection. |
| 526 | */ |
| 527 | Commands.zipSelectionCommand = { |
| 528 | execute: function(event, fileManager, directoryModel) { |
| 529 | var dirEntry = directoryModel.getCurrentDirEntry(); |
| 530 | var selectionEntries = fileManager.getSelection().entries; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 531 | fileManager.copyManager_.zipSelection(dirEntry, selectionEntries); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 532 | }, |
| 533 | canExecute: function(event, fileManager) { |
| 534 | var selection = fileManager.getSelection(); |
| 535 | event.canExecute = !fileManager.isOnReadonlyDirectory() && |
| 536 | !fileManager.isOnDrive() && |
| 537 | selection && selection.totalCount > 0; |
| 538 | } |
| 539 | }; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 540 | |
| 541 | /** |
| 542 | * Shows the share dialog for the current selection (single only). |
| 543 | */ |
| 544 | Commands.shareCommand = { |
| 545 | execute: function(event, fileManager) { |
| 546 | fileManager.shareSelection(); |
| 547 | }, |
| 548 | canExecute: function(event, fileManager) { |
| 549 | var selection = fileManager.getSelection(); |
| 550 | event.canExecute = fileManager.isOnDrive() && |
| 551 | !fileManager.isDriveOffline() && |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 552 | selection && selection.totalCount == 1; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 553 | event.command.setHidden(!fileManager.isOnDrive()); |
| 554 | } |
| 555 | }; |
| 556 | |
| 557 | /** |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 558 | * Creates a shortcut of the selected folder (single only). |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 559 | */ |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 560 | Commands.createFolderShortcutCommand = { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 561 | /** |
| 562 | * @param {Event} event Command event. |
| 563 | * @param {FileManager} fileManager The file manager instance. |
| 564 | */ |
| 565 | execute: function(event, fileManager) { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 566 | var path = CommandUtil.getCommandPath(event.target); |
| 567 | if (path) |
| 568 | fileManager.createFolderShortcut(path); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 569 | }, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 570 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 571 | /** |
| 572 | * @param {Event} event Command event. |
| 573 | * @param {FileManager} fileManager The file manager instance. |
| 574 | */ |
| 575 | canExecute: function(event, fileManager) { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 576 | var target = event.target; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 577 | // TODO(yoshiki): remove this after launching folder shortcuts feature. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 578 | if (!fileManager.isFolderShortcutsEnabled() || |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 579 | (!target instanceof NavigationListItem && |
| 580 | !target instanceof DirectoryItem)) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 581 | event.command.setHidden(true); |
| 582 | return; |
| 583 | } |
| 584 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 585 | var path = CommandUtil.getCommandPath(event.target); |
| 586 | var folderShortcutExists = path && fileManager.folderShortcutExists(path); |
| 587 | |
| 588 | var onlyOneFolderSelected = true; |
| 589 | // Only on list, user can select multiple files. The command is enabled only |
| 590 | // when a single file is selected. |
| 591 | if (event.target instanceof cr.ui.List) { |
| 592 | var items = event.target.selectedItems; |
| 593 | onlyOneFolderSelected = (items.length == 1 && items[0].isDirectory); |
| 594 | } |
| 595 | |
| 596 | var eligible = path && PathUtil.isEligibleForFolderShortcut(path); |
| 597 | event.canExecute = |
| 598 | eligible && onlyOneFolderSelected && !folderShortcutExists; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 599 | event.command.setHidden(!eligible || !onlyOneFolderSelected); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 600 | } |
| 601 | }; |
| 602 | |
| 603 | /** |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 604 | * Removes the folder shortcut. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 605 | */ |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 606 | Commands.removeFolderShortcutCommand = { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 607 | /** |
| 608 | * @param {Event} event Command event. |
| 609 | * @param {FileManager} fileManager The file manager instance. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 610 | */ |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 611 | execute: function(event, fileManager) { |
| 612 | var path = CommandUtil.getCommandPath(event.target); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 613 | if (path) |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 614 | fileManager.removeFolderShortcut(path); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 615 | }, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 616 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 617 | /** |
| 618 | * @param {Event} event Command event. |
| 619 | * @param {FileManager} fileManager The file manager instance. |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 620 | */ |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 621 | canExecute: function(event, fileManager) { |
| 622 | var target = event.target; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 623 | // TODO(yoshiki): remove this after launching folder shortcut feature. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 624 | if (!fileManager.isFolderShortcutsEnabled() || |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 625 | (!target instanceof NavigationListItem && |
| 626 | !target instanceof DirectoryItem)) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 627 | event.command.setHidden(true); |
| 628 | return; |
| 629 | } |
| 630 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 631 | var path = CommandUtil.getCommandPath(target); |
| 632 | var eligible = path && PathUtil.isEligibleForFolderShortcut(path); |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 633 | var isShortcut = path && fileManager.folderShortcutExists(path); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 634 | event.canExecute = isShortcut && eligible; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 635 | event.command.setHidden(!event.canExecute); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 636 | } |
| 637 | }; |
| 638 | |
| 639 | /** |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 640 | * Zoom in to the Files.app. |
| 641 | */ |
| 642 | Commands.zoomInCommand = { |
| 643 | execute: function(event) { |
| 644 | chrome.fileBrowserPrivate.zoom('in'); |
| 645 | }, |
| 646 | canExecute: CommandUtil.canExecuteAlways |
| 647 | }; |
| 648 | |
| 649 | /** |
| 650 | * Zoom out from the Files.app. |
| 651 | */ |
| 652 | Commands.zoomOutCommand = { |
| 653 | execute: function(event) { |
| 654 | chrome.fileBrowserPrivate.zoom('out'); |
| 655 | }, |
| 656 | canExecute: CommandUtil.canExecuteAlways |
| 657 | }; |
| 658 | |
| 659 | /** |
| 660 | * Reset the zoom factor. |
| 661 | */ |
| 662 | Commands.zoomResetCommand = { |
| 663 | execute: function(event) { |
| 664 | chrome.fileBrowserPrivate.zoom('reset'); |
| 665 | }, |
| 666 | canExecute: CommandUtil.canExecuteAlways |
| 667 | }; |