| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @param {DialogType} dialogType | 6 * @param {DialogType} dialogType |
| 7 * @param {!VolumeManagerWrapper} volumeManager | 7 * @param {!VolumeManagerWrapper} volumeManager |
| 8 * @param {!FileManagerUI} ui | 8 * @param {!FileManagerUI} ui |
| 9 * @param {!MetadataModel} metadataModel | 9 * @param {!MetadataModel} metadataModel |
| 10 * @param {!DirectoryModel} directoryModel | 10 * @param {!DirectoryModel} directoryModel |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 document.querySelector('#default-task'), cr.ui.Command); | 77 document.querySelector('#default-task'), cr.ui.Command); |
| 78 | 78 |
| 79 /** | 79 /** |
| 80 * @private {!cr.ui.Command} | 80 * @private {!cr.ui.Command} |
| 81 * @const | 81 * @const |
| 82 */ | 82 */ |
| 83 this.openWithCommand_ = | 83 this.openWithCommand_ = |
| 84 assertInstanceof(document.querySelector('#open-with'), cr.ui.Command); | 84 assertInstanceof(document.querySelector('#open-with'), cr.ui.Command); |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * @public {!FileTasks} | 87 * @private {Promise<!FileTasks>} |
| 88 */ | 88 */ |
| 89 this.tasks = new FileTasks( | 89 this.tasks_ = null; |
| 90 this.volumeManager_, this.metadataModel_, this.directoryModel_, this.ui_); | |
| 91 | 90 |
| 92 ui.taskMenuButton.addEventListener( | 91 ui.taskMenuButton.addEventListener( |
| 93 'select', this.onTaskItemClicked_.bind(this)); | 92 'select', this.onTaskItemClicked_.bind(this)); |
| 94 this.selectionHandler_.addEventListener( | 93 this.selectionHandler_.addEventListener( |
| 95 FileSelectionHandler.EventType.CHANGE, | 94 FileSelectionHandler.EventType.CHANGE, |
| 96 this.onSelectionChanged_.bind(this)); | 95 this.onSelectionChanged_.bind(this)); |
| 97 this.selectionHandler_.addEventListener( | 96 this.selectionHandler_.addEventListener( |
| 98 FileSelectionHandler.EventType.CHANGE_THROTTLED, | 97 FileSelectionHandler.EventType.CHANGE_THROTTLED, |
| 99 this.onSelectionChangeThrottled_.bind(this)); | 98 this.onSelectionChangeThrottled_.bind(this)); |
| 100 } | 99 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 118 title: str('TASK_OPEN'), | 117 title: str('TASK_OPEN'), |
| 119 disabled: true, | 118 disabled: true, |
| 120 taskId: null | 119 taskId: null |
| 121 }; | 120 }; |
| 122 } | 121 } |
| 123 | 122 |
| 124 return TaskController.cachedDisabledTaskItem_; | 123 return TaskController.cachedDisabledTaskItem_; |
| 125 }; | 124 }; |
| 126 | 125 |
| 127 /** | 126 /** |
| 128 * Execute task depending on the selection and the dialog type. | |
| 129 */ | |
| 130 TaskController.prototype.executeSelectionTask = function() { | |
| 131 if (this.dialogType_ == DialogType.FULL_PAGE) { | |
| 132 if (this.tasks) | |
| 133 this.tasks.executeDefault(); | |
| 134 return true; | |
| 135 } | |
| 136 if (!this.ui_.dialogFooter.okButton.disabled) { | |
| 137 this.ui_.dialogFooter.okButton.click(); | |
| 138 return true; | |
| 139 } | |
| 140 return false; | |
| 141 }; | |
| 142 | |
| 143 /** | |
| 144 * Task combobox handler. | 127 * Task combobox handler. |
| 145 * | 128 * |
| 146 * @param {Object} event Event containing task which was clicked. | 129 * @param {Object} event Event containing task which was clicked. |
| 147 * @private | 130 * @private |
| 148 */ | 131 */ |
| 149 TaskController.prototype.onTaskItemClicked_ = function(event) { | 132 TaskController.prototype.onTaskItemClicked_ = function(event) { |
| 150 if (!this.tasks) | 133 this.getFileTasks() |
| 151 return; | 134 .then(function(tasks) { |
| 135 switch (event.item.type) { |
| 136 case FileTasks.TaskMenuButtonItemType.ShowMenu: |
| 137 this.ui_.taskMenuButton.showMenu(false); |
| 138 break; |
| 139 case FileTasks.TaskMenuButtonItemType.RunTask: |
| 140 tasks.execute(event.item.task.taskId); |
| 141 break; |
| 142 case FileTasks.TaskMenuButtonItemType.ChangeDefaultTask: |
| 143 var selection = this.selectionHandler_.selection; |
| 144 var extensions = []; |
| 152 | 145 |
| 153 switch (event.item.type) { | 146 for (var i = 0; i < selection.entries.length; i++) { |
| 154 case FileTasks.TaskMenuButtonItemType.ShowMenu: | 147 var match = /\.(\w+)$/g.exec(selection.entries[i].toURL()); |
| 155 this.ui_.taskMenuButton.showMenu(false); | 148 if (match) { |
| 156 break; | 149 var ext = match[1].toUpperCase(); |
| 157 case FileTasks.TaskMenuButtonItemType.RunTask: | 150 if (extensions.indexOf(ext) == -1) { |
| 158 this.tasks.execute(event.item.task.taskId); | 151 extensions.push(ext); |
| 159 break; | 152 } |
| 160 case FileTasks.TaskMenuButtonItemType.ChangeDefaultTask: | 153 } |
| 161 var selection = this.selectionHandler_.selection; | 154 } |
| 162 var extensions = []; | |
| 163 | 155 |
| 164 for (var i = 0; i < selection.entries.length; i++) { | 156 var format = ''; |
| 165 var match = /\.(\w+)$/g.exec(selection.entries[i].toURL()); | 157 |
| 166 if (match) { | 158 if (extensions.length == 1) { |
| 167 var ext = match[1].toUpperCase(); | 159 format = extensions[0]; |
| 168 if (extensions.indexOf(ext) == -1) { | |
| 169 extensions.push(ext); | |
| 170 } | 160 } |
| 171 } | 161 |
| 162 // Change default was clicked. We should open "change default" dialog. |
| 163 tasks.showTaskPicker( |
| 164 this.ui_.defaultTaskPicker, |
| 165 loadTimeData.getString('CHANGE_DEFAULT_MENU_ITEM'), |
| 166 strf('CHANGE_DEFAULT_CAPTION', format), |
| 167 this.changeDefaultTask_.bind(this, selection), |
| 168 true); |
| 169 break; |
| 170 default: |
| 171 assertNotReached('Unknown task.'); |
| 172 } | 172 } |
| 173 | 173 }.bind(this)) |
| 174 var format = ''; | 174 .catch(function(error) { |
| 175 | 175 if (error) |
| 176 if (extensions.length == 1) { | 176 console.error(error.stack || error); |
| 177 format = extensions[0]; | 177 }); |
| 178 } | |
| 179 | |
| 180 // Change default was clicked. We should open "change default" dialog. | |
| 181 this.tasks.showTaskPicker( | |
| 182 this.ui_.defaultTaskPicker, | |
| 183 loadTimeData.getString('CHANGE_DEFAULT_MENU_ITEM'), | |
| 184 strf('CHANGE_DEFAULT_CAPTION', format), | |
| 185 this.changeDefaultTask_.bind(this, selection), | |
| 186 true); | |
| 187 break; | |
| 188 default: | |
| 189 assertNotReached('Unknown task.'); | |
| 190 } | |
| 191 }; | 178 }; |
| 192 | 179 |
| 193 /** | 180 /** |
| 194 * Sets the given task as default, when this task is applicable. | 181 * Sets the given task as default, when this task is applicable. |
| 195 * | 182 * |
| 196 * @param {!FileSelection} selection File selection. | 183 * @param {!FileSelection} selection File selection. |
| 197 * @param {Object} task Task to set as default. | 184 * @param {Object} task Task to set as default. |
| 198 * @private | 185 * @private |
| 199 */ | 186 */ |
| 200 TaskController.prototype.changeDefaultTask_ = function(selection, task) { | 187 TaskController.prototype.changeDefaultTask_ = function(selection, task) { |
| 201 chrome.fileManagerPrivate.setDefaultTask( | 188 chrome.fileManagerPrivate.setDefaultTask( |
| 202 task.taskId, | 189 task.taskId, |
| 203 selection.entries, | 190 selection.entries, |
| 204 assert(selection.mimeTypes), | 191 assert(selection.mimeTypes), |
| 205 util.checkAPIError); | 192 util.checkAPIError); |
| 206 this.metadataUpdateController_.refreshCurrentDirectoryMetadata(); | 193 this.metadataUpdateController_.refreshCurrentDirectoryMetadata(); |
| 207 | 194 |
| 208 // Update task menu button unless the task button was updated other selection. | 195 // Update task menu button unless the task button was updated other selection. |
| 209 if (this.selectionHandler_.selection === selection) { | 196 if (this.selectionHandler_.selection === selection) { |
| 210 this.tasks = new FileTasks( | 197 this.tasks_ = null; |
| 211 this.volumeManager_, this.metadataModel_, this.directoryModel_, | 198 this.getFileTasks() |
| 212 this.ui_); | 199 .then(function(tasks) { |
| 213 this.tasks.init(selection.entries, selection.mimeTypes); | 200 tasks.display(this.ui_.taskMenuButton); |
| 214 this.tasks.display(this.ui_.taskMenuButton); | 201 }.bind(this)) |
| 202 .catch(function(error) { |
| 203 if (error) |
| 204 console.error(error.stack || error); |
| 205 }); |
| 215 } | 206 } |
| 216 this.selectionHandler_.onFileSelectionChanged(); | 207 this.selectionHandler_.onFileSelectionChanged(); |
| 217 }; | 208 }; |
| 218 | 209 |
| 219 /** | 210 /** |
| 220 * Executes default task. | 211 * Executes default task. |
| 221 */ | 212 */ |
| 222 TaskController.prototype.executeDefaultTask = function() { | 213 TaskController.prototype.executeDefaultTask = function() { |
| 223 if (this.tasks) | 214 this.getFileTasks() |
| 224 this.tasks.execute(this.ui_.fileContextMenu.defaultTaskMenuItem.taskId); | 215 .then(function(tasks) { |
| 216 tasks.execute(this.ui_.fileContextMenu.defaultTaskMenuItem.taskId); |
| 217 }.bind(this)) |
| 218 .catch(function(error) { |
| 219 if (error) |
| 220 console.error(error.stack || error); |
| 221 }); |
| 225 }; | 222 }; |
| 226 | 223 |
| 227 /** | 224 /** |
| 228 * Get MIME type for an entry. This method first tries to obtain the MIME type | 225 * Get MIME type for an entry. This method first tries to obtain the MIME type |
| 229 * from metadata. If it fails, this falls back to obtain the MIME type from its | 226 * from metadata. If it fails, this falls back to obtain the MIME type from its |
| 230 * content or name. | 227 * content or name. |
| 231 * | 228 * |
| 232 * @param {!Entry} entry An entry to obtain its mime type. | 229 * @param {!Entry} entry An entry to obtain its mime type. |
| 233 * @return {!Promise} | 230 * @return {!Promise} |
| 234 * @private | 231 * @private |
| (...skipping 13 matching lines...) Expand all Loading... |
| 248 }); | 245 }); |
| 249 }); | 246 }); |
| 250 }); | 247 }); |
| 251 }; | 248 }; |
| 252 | 249 |
| 253 /** | 250 /** |
| 254 * Handles change of selection and clears context menu. | 251 * Handles change of selection and clears context menu. |
| 255 * @private | 252 * @private |
| 256 */ | 253 */ |
| 257 TaskController.prototype.onSelectionChanged_ = function() { | 254 TaskController.prototype.onSelectionChanged_ = function() { |
| 255 this.tasks_ = null; |
| 258 var selection = this.selectionHandler_.selection; | 256 var selection = this.selectionHandler_.selection; |
| 259 this.tasks = new FileTasks( | |
| 260 this.volumeManager_, this.metadataModel_, this.directoryModel_, | |
| 261 this.ui_); | |
| 262 // Caller of update context menu task items. | 257 // Caller of update context menu task items. |
| 263 // FileSelectionHandler.EventType.CHANGE | 258 // FileSelectionHandler.EventType.CHANGE |
| 264 if (this.dialogType_ === DialogType.FULL_PAGE && | 259 if (this.dialogType_ === DialogType.FULL_PAGE && |
| 265 selection.directoryCount === 0 && selection.fileCount > 0) { | 260 selection.directoryCount === 0 && selection.fileCount > 0) { |
| 266 // Show disabled items for position calculation of the menu. They will be | 261 // Show disabled items for position calculation of the menu. They will be |
| 267 // overridden in this.updateFileSelectionAsync(). | 262 // overridden in this.updateFileSelectionAsync(). |
| 268 this.updateContextMenuTaskItems_( | 263 this.updateContextMenuTaskItems_( |
| 269 [TaskController.createTemporaryDisabledTaskItem_()]); | 264 [TaskController.createTemporaryDisabledTaskItem_()]); |
| 270 } else { | 265 } else { |
| 271 // Update context menu. | 266 // Update context menu. |
| 272 this.updateContextMenuTaskItems_([]); | 267 this.updateContextMenuTaskItems_([]); |
| 273 } | 268 } |
| 274 }; | 269 }; |
| 275 | 270 |
| 276 /** | 271 /** |
| 277 * Handles change of selection asynchronously and updates context menu. | 272 * Handles change of selection asynchronously and updates context menu. |
| 278 * @private | 273 * @private |
| 279 */ | 274 */ |
| 280 TaskController.prototype.onSelectionChangeThrottled_ = function() { | 275 TaskController.prototype.onSelectionChangeThrottled_ = function() { |
| 281 // FileSelectionHandler.EventType.CHANGE_THROTTLED | |
| 282 // Update the file tasks. | |
| 283 var selection = this.selectionHandler_.selection; | 276 var selection = this.selectionHandler_.selection; |
| 284 if (this.dialogType_ === DialogType.FULL_PAGE && | 277 if (this.dialogType_ === DialogType.FULL_PAGE && |
| 285 selection.directoryCount === 0 && selection.fileCount > 0) { | 278 selection.directoryCount === 0 && selection.fileCount > 0) { |
| 286 selection.computeAdditional(this.metadataModel_).then(function() { | 279 this.getFileTasks() |
| 287 if (this.selectionHandler_.selection !== selection) | 280 .then(function(tasks) { |
| 288 return; | 281 tasks.display(this.ui_.taskMenuButton); |
| 289 this.tasks.init(selection.entries, assert(selection.mimeTypes)).then( | 282 this.updateContextMenuTaskItems_(tasks.getTaskItems()); |
| 290 function() { | 283 }.bind(this)) |
| 291 if (this.selectionHandler_.selection !== selection) | 284 .catch(function(error) { |
| 292 return; | 285 if (error) |
| 293 this.tasks.display(this.ui_.taskMenuButton); | 286 console.error(error.stack || error); |
| 294 this.updateContextMenuTaskItems_( | 287 }); |
| 295 assert(this.tasks.getTaskItems())); | |
| 296 }.bind(this)); | |
| 297 }.bind(this)); | |
| 298 } else { | 288 } else { |
| 299 this.ui_.taskMenuButton.hidden = true; | 289 this.ui_.taskMenuButton.hidden = true; |
| 300 } | 290 } |
| 291 } |
| 292 |
| 293 /** |
| 294 * @return {!Promise<!FileTasks>} |
| 295 * @public |
| 296 */ |
| 297 TaskController.prototype.getFileTasks = function() { |
| 298 if (this.tasks_) |
| 299 return this.tasks_; |
| 300 |
| 301 var selection = this.selectionHandler_.selection; |
| 302 return selection.computeAdditional(this.metadataModel_).then( |
| 303 function() { |
| 304 if (this.selectionHandler_.selection !== selection) |
| 305 return Promise.reject(); |
| 306 return FileTasks.create( |
| 307 this.volumeManager_, this.metadataModel_, this.directoryModel_, |
| 308 this.ui_, selection.entries, assert(selection.mimeTypes)). |
| 309 then(function(tasks) { |
| 310 if (this.selectionHandler_.selection !== selection) |
| 311 return Promise.reject(); |
| 312 return tasks; |
| 313 }.bind(this)); |
| 314 }.bind(this)); |
| 301 }; | 315 }; |
| 302 | 316 |
| 303 /** | 317 /** |
| 304 * Returns whether default task command can be executed or not. | 318 * Returns whether default task command can be executed or not. |
| 305 * @return {boolean} True if default task command is executable. | 319 * @return {boolean} True if default task command is executable. |
| 306 */ | 320 */ |
| 307 TaskController.prototype.canExecuteDefaultTask = function() { | 321 TaskController.prototype.canExecuteDefaultTask = function() { |
| 308 return this.canExecuteDefaultTask_; | 322 return this.canExecuteDefaultTask_; |
| 309 }; | 323 }; |
| 310 | 324 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 this.canExecuteOpenWith_ = items.length > 1; | 366 this.canExecuteOpenWith_ = items.length > 1; |
| 353 this.openWithCommand_.canExecuteChange(this.ui_.listContainer.element); | 367 this.openWithCommand_.canExecuteChange(this.ui_.listContainer.element); |
| 354 | 368 |
| 355 this.ui_.fileContextMenu.tasksSeparator.hidden = items.length === 0; | 369 this.ui_.fileContextMenu.tasksSeparator.hidden = items.length === 0; |
| 356 }; | 370 }; |
| 357 | 371 |
| 358 /** | 372 /** |
| 359 * @param {FileEntry} entry | 373 * @param {FileEntry} entry |
| 360 */ | 374 */ |
| 361 TaskController.prototype.executeEntryTask = function(entry) { | 375 TaskController.prototype.executeEntryTask = function(entry) { |
| 362 if (this.dialogType_ == DialogType.FULL_PAGE) { | 376 this.metadataModel_.get([entry], ['contentMimeType']).then( |
| 363 this.metadataModel_.get([entry], ['contentMimeType']).then( | 377 function(props) { |
| 364 function(props) { | 378 FileTasks.create( |
| 365 var tasks = new FileTasks( | 379 this.volumeManager_, this.metadataModel_, this.directoryModel_, |
| 366 this.volumeManager_, this.metadataModel_, this.directoryModel_, | 380 this.ui_, [entry], [props[0].contentMimeType || null]) |
| 367 this.ui_); | 381 .then(function(tasks) { |
| 368 tasks.init([entry], [props[0].contentMimeType || '']); | 382 tasks.executeDefault(); |
| 369 tasks.executeDefault(); | 383 }); |
| 370 }.bind(this)); | 384 }.bind(this)); |
| 371 } else { | |
| 372 var selection = this.selectionHandler_.selection; | |
| 373 if (selection.entries.length === 1 && | |
| 374 util.isSameEntry(selection.entries[0], entry)) { | |
| 375 this.ui_.dialogFooter.okButton.click(); | |
| 376 } | |
| 377 } | |
| 378 }; | 385 }; |
| OLD | NEW |