OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * Called from the main frame when unloading. |
| 9 * @param {boolean=} opt_exiting True if the app is exiting. |
| 10 */ |
| 11 function unload(opt_exiting) { Gallery.instance.onUnload(opt_exiting); } |
| 12 |
| 13 /** |
| 14 * Overrided metadata worker's path. |
| 15 * @type {string} |
| 16 * @const |
| 17 */ |
| 18 ContentProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; |
| 19 |
| 20 /** |
| 21 * Gallery for viewing and editing image files. |
| 22 * |
| 23 * @param {!VolumeManager} volumeManager The VolumeManager instance of the |
| 24 * system. |
| 25 * @class |
| 26 * @constructor |
| 27 */ |
| 28 function Gallery(volumeManager) { |
| 29 this.context_ = { |
| 30 appWindow: chrome.app.window.current(), |
| 31 onBack: function() {}, |
| 32 onClose: function() {}, |
| 33 onMaximize: function() {}, |
| 34 onMinimize: function() {}, |
| 35 onAppRegionChanged: function() {}, |
| 36 metadataCache: MetadataCache.createFull(volumeManager), |
| 37 shareActions: [], |
| 38 readonlyDirName: '', |
| 39 saveDirEntry: null, |
| 40 displayStringFunction: function() { return ''; }, |
| 41 loadTimeData: {} |
| 42 }; |
| 43 this.container_ = document.querySelector('.gallery'); |
| 44 this.document_ = document; |
| 45 this.metadataCache_ = this.context_.metadataCache; |
| 46 this.volumeManager_ = volumeManager; |
| 47 this.selectedEntry_ = null; |
| 48 this.metadataCacheObserverId_ = null; |
| 49 this.onExternallyUnmountedBound_ = this.onExternallyUnmounted_.bind(this); |
| 50 |
| 51 this.dataModel_ = new cr.ui.ArrayDataModel([]); |
| 52 this.selectionModel_ = new cr.ui.ListSelectionModel(); |
| 53 |
| 54 this.initDom_(); |
| 55 this.initListeners_(); |
| 56 } |
| 57 |
| 58 /** |
| 59 * Gallery extends cr.EventTarget. |
| 60 */ |
| 61 Gallery.prototype.__proto__ = cr.EventTarget.prototype; |
| 62 |
| 63 /** |
| 64 * Tools fade-out timeout im milliseconds. |
| 65 * @const |
| 66 * @type {number} |
| 67 */ |
| 68 Gallery.FADE_TIMEOUT = 3000; |
| 69 |
| 70 /** |
| 71 * First time tools fade-out timeout im milliseconds. |
| 72 * @const |
| 73 * @type {number} |
| 74 */ |
| 75 Gallery.FIRST_FADE_TIMEOUT = 1000; |
| 76 |
| 77 /** |
| 78 * Time until mosaic is initialized in the background. Used to make gallery |
| 79 * in the slide mode load faster. In miiliseconds. |
| 80 * @const |
| 81 * @type {number} |
| 82 */ |
| 83 Gallery.MOSAIC_BACKGROUND_INIT_DELAY = 1000; |
| 84 |
| 85 /** |
| 86 * Types of metadata Gallery uses (to query the metadata cache). |
| 87 * @const |
| 88 * @type {string} |
| 89 */ |
| 90 Gallery.METADATA_TYPE = 'thumbnail|filesystem|media|streaming|drive'; |
| 91 |
| 92 /** |
| 93 * Initializes listeners. |
| 94 * @private |
| 95 */ |
| 96 Gallery.prototype.initListeners_ = function() { |
| 97 this.keyDownBound_ = this.onKeyDown_.bind(this); |
| 98 this.document_.body.addEventListener('keydown', this.keyDownBound_); |
| 99 |
| 100 this.inactivityWatcher_ = new MouseInactivityWatcher( |
| 101 this.container_, Gallery.FADE_TIMEOUT, this.hasActiveTool.bind(this)); |
| 102 |
| 103 // Search results may contain files from different subdirectories so |
| 104 // the observer is not going to work. |
| 105 if (!this.context_.searchResults && this.context_.curDirEntry) { |
| 106 this.metadataCacheObserverId_ = this.metadataCache_.addObserver( |
| 107 this.context_.curDirEntry, |
| 108 MetadataCache.CHILDREN, |
| 109 'thumbnail', |
| 110 this.updateThumbnails_.bind(this)); |
| 111 } |
| 112 this.volumeManager_.addEventListener( |
| 113 'externally-unmounted', this.onExternallyUnmountedBound_); |
| 114 }; |
| 115 |
| 116 /** |
| 117 * Closes gallery when a volume containing the selected item is unmounted. |
| 118 * @param {!Event} event The unmount event. |
| 119 * @private |
| 120 */ |
| 121 Gallery.prototype.onExternallyUnmounted_ = function(event) { |
| 122 if (!this.selectedEntry_) |
| 123 return; |
| 124 |
| 125 if (this.volumeManager_.getVolumeInfo(this.selectedEntry_) === |
| 126 event.volumeInfo) { |
| 127 this.onBack_(); |
| 128 } |
| 129 }; |
| 130 |
| 131 /** |
| 132 * Unloads the Gallery. |
| 133 * @param {boolean} exiting True if the app is exiting. |
| 134 */ |
| 135 Gallery.prototype.onUnload = function(exiting) { |
| 136 if (this.metadataCacheObserverId_ !== null) |
| 137 this.metadataCache_.removeObserver(this.metadataCacheObserverId_); |
| 138 this.volumeManager_.removeEventListener( |
| 139 'externally-unmounted', this.onExternallyUnmountedBound_); |
| 140 this.slideMode_.onUnload(exiting); |
| 141 }; |
| 142 |
| 143 /** |
| 144 * Initializes DOM UI |
| 145 * @private |
| 146 */ |
| 147 Gallery.prototype.initDom_ = function() { |
| 148 // Initialize the dialog label. |
| 149 cr.ui.dialogs.BaseDialog.OK_LABEL = str('GALLERY_OK_LABEL'); |
| 150 cr.ui.dialogs.BaseDialog.CANCEL_LABEL = str('GALLERY_CANCEL_LABEL'); |
| 151 |
| 152 var content = util.createChild(this.container_, 'content'); |
| 153 content.addEventListener('click', this.onContentClick_.bind(this)); |
| 154 |
| 155 this.header_ = util.createChild(this.container_, 'header tool dimmable'); |
| 156 this.toolbar_ = util.createChild(this.container_, 'toolbar tool dimmable'); |
| 157 |
| 158 var backButton = util.createChild(this.container_, |
| 159 'back-button tool dimmable'); |
| 160 util.createChild(backButton); |
| 161 backButton.addEventListener('click', this.onBack_.bind(this)); |
| 162 |
| 163 var preventDefault = function(event) { event.preventDefault(); }; |
| 164 |
| 165 var minimizeButton = util.createChild(this.header_, |
| 166 'minimize-button tool dimmable', |
| 167 'button'); |
| 168 minimizeButton.tabIndex = -1; |
| 169 minimizeButton.addEventListener('click', this.onMinimize_.bind(this)); |
| 170 minimizeButton.addEventListener('mousedown', preventDefault); |
| 171 |
| 172 var maximizeButton = util.createChild(this.header_, |
| 173 'maximize-button tool dimmable', |
| 174 'button'); |
| 175 maximizeButton.tabIndex = -1; |
| 176 maximizeButton.addEventListener('click', this.onMaximize_.bind(this)); |
| 177 maximizeButton.addEventListener('mousedown', preventDefault); |
| 178 |
| 179 var closeButton = util.createChild(this.header_, |
| 180 'close-button tool dimmable', |
| 181 'button'); |
| 182 closeButton.tabIndex = -1; |
| 183 closeButton.addEventListener('click', this.onClose_.bind(this)); |
| 184 closeButton.addEventListener('mousedown', preventDefault); |
| 185 |
| 186 this.filenameSpacer_ = util.createChild(this.toolbar_, 'filename-spacer'); |
| 187 this.filenameEdit_ = util.createChild(this.filenameSpacer_, |
| 188 'namebox', 'input'); |
| 189 |
| 190 this.filenameEdit_.setAttribute('type', 'text'); |
| 191 this.filenameEdit_.addEventListener('blur', |
| 192 this.onFilenameEditBlur_.bind(this)); |
| 193 |
| 194 this.filenameEdit_.addEventListener('focus', |
| 195 this.onFilenameFocus_.bind(this)); |
| 196 |
| 197 this.filenameEdit_.addEventListener('keydown', |
| 198 this.onFilenameEditKeydown_.bind(this)); |
| 199 |
| 200 util.createChild(this.toolbar_, 'button-spacer'); |
| 201 |
| 202 this.prompt_ = new ImageEditor.Prompt(this.container_, str); |
| 203 |
| 204 this.modeButton_ = util.createChild(this.toolbar_, 'button mode', 'button'); |
| 205 this.modeButton_.addEventListener('click', |
| 206 this.toggleMode_.bind(this, null)); |
| 207 |
| 208 this.mosaicMode_ = new MosaicMode(content, |
| 209 this.dataModel_, |
| 210 this.selectionModel_, |
| 211 this.metadataCache_, |
| 212 this.volumeManager_, |
| 213 this.toggleMode_.bind(this, null)); |
| 214 |
| 215 this.slideMode_ = new SlideMode(this.container_, |
| 216 content, |
| 217 this.toolbar_, |
| 218 this.prompt_, |
| 219 this.dataModel_, |
| 220 this.selectionModel_, |
| 221 this.context_, |
| 222 this.toggleMode_.bind(this), |
| 223 str); |
| 224 |
| 225 this.slideMode_.addEventListener('image-displayed', function() { |
| 226 cr.dispatchSimpleEvent(this, 'image-displayed'); |
| 227 }.bind(this)); |
| 228 this.slideMode_.addEventListener('image-saved', function() { |
| 229 cr.dispatchSimpleEvent(this, 'image-saved'); |
| 230 }.bind(this)); |
| 231 |
| 232 var deleteButton = this.createToolbarButton_('delete', 'GALLERY_DELETE'); |
| 233 deleteButton.addEventListener('click', this.delete_.bind(this)); |
| 234 |
| 235 this.shareButton_ = this.createToolbarButton_('share', 'GALLERY_SHARE'); |
| 236 this.shareButton_.setAttribute('disabled', ''); |
| 237 this.shareButton_.addEventListener('click', this.toggleShare_.bind(this)); |
| 238 |
| 239 this.shareMenu_ = util.createChild(this.container_, 'share-menu'); |
| 240 this.shareMenu_.hidden = true; |
| 241 util.createChild(this.shareMenu_, 'bubble-point'); |
| 242 |
| 243 this.dataModel_.addEventListener('splice', this.onSplice_.bind(this)); |
| 244 this.dataModel_.addEventListener('content', this.onContentChange_.bind(this)); |
| 245 |
| 246 this.selectionModel_.addEventListener('change', this.onSelection_.bind(this)); |
| 247 this.slideMode_.addEventListener('useraction', this.onUserAction_.bind(this)); |
| 248 }; |
| 249 |
| 250 /** |
| 251 * Creates toolbar button. |
| 252 * |
| 253 * @param {string} className Class to add. |
| 254 * @param {string} title Button title. |
| 255 * @return {!HTMLElement} Newly created button. |
| 256 * @private |
| 257 */ |
| 258 Gallery.prototype.createToolbarButton_ = function(className, title) { |
| 259 var button = util.createChild(this.toolbar_, className, 'button'); |
| 260 button.title = str(title); |
| 261 return button; |
| 262 }; |
| 263 |
| 264 /** |
| 265 * Loads the content. |
| 266 * |
| 267 * @param {!Array.<Entry>} entries Array of entries. |
| 268 * @param {!Array.<Entry>} selectedEntries Array of selected entries. |
| 269 */ |
| 270 Gallery.prototype.load = function(entries, selectedEntries) { |
| 271 var items = []; |
| 272 for (var index = 0; index < entries.length; ++index) { |
| 273 items.push(new Gallery.Item(entries[index])); |
| 274 } |
| 275 this.dataModel_.push.apply(this.dataModel_, items); |
| 276 |
| 277 this.selectionModel_.adjustLength(this.dataModel_.length); |
| 278 |
| 279 // Comparing Entries by reference is not safe. Therefore we have to use URLs. |
| 280 var entryIndexesByURLs = {}; |
| 281 for (var index = 0; index < entries.length; index++) { |
| 282 entryIndexesByURLs[entries[index].toURL()] = index; |
| 283 } |
| 284 |
| 285 for (var i = 0; i !== selectedEntries.length; i++) { |
| 286 var selectedIndex = entryIndexesByURLs[selectedEntries[i].toURL()]; |
| 287 if (selectedIndex !== undefined) |
| 288 this.selectionModel_.setIndexSelected(selectedIndex, true); |
| 289 else |
| 290 console.error('Cannot select ' + selectedEntries[i]); |
| 291 } |
| 292 |
| 293 if (this.selectionModel_.selectedIndexes.length === 0) |
| 294 this.onSelection_(); |
| 295 |
| 296 var mosaic = this.mosaicMode_ && this.mosaicMode_.getMosaic(); |
| 297 |
| 298 // Mosaic view should show up if most of the selected files are images. |
| 299 var imagesCount = 0; |
| 300 for (var i = 0; i !== selectedEntries.length; i++) { |
| 301 if (FileType.getMediaType(selectedEntries[i]) === 'image') |
| 302 imagesCount++; |
| 303 } |
| 304 var mostlyImages = imagesCount > (selectedEntries.length / 2.0); |
| 305 |
| 306 var forcedMosaic = (this.context_.pageState && |
| 307 this.context_.pageState.gallery === 'mosaic'); |
| 308 |
| 309 var showMosaic = (mostlyImages && selectedEntries.length > 1) || forcedMosaic; |
| 310 if (mosaic && showMosaic) { |
| 311 this.setCurrentMode_(this.mosaicMode_); |
| 312 mosaic.init(); |
| 313 mosaic.show(); |
| 314 this.inactivityWatcher_.check(); // Show the toolbar. |
| 315 cr.dispatchSimpleEvent(this, 'loaded'); |
| 316 } else { |
| 317 this.setCurrentMode_(this.slideMode_); |
| 318 var maybeLoadMosaic = function() { |
| 319 if (mosaic) |
| 320 mosaic.init(); |
| 321 cr.dispatchSimpleEvent(this, 'loaded'); |
| 322 }.bind(this); |
| 323 /* TODO: consider nice blow-up animation for the first image */ |
| 324 this.slideMode_.enter(null, function() { |
| 325 // Flash the toolbar briefly to show it is there. |
| 326 this.inactivityWatcher_.kick(Gallery.FIRST_FADE_TIMEOUT); |
| 327 }.bind(this), |
| 328 maybeLoadMosaic); |
| 329 } |
| 330 }; |
| 331 |
| 332 /** |
| 333 * Closes the Gallery and go to Files.app. |
| 334 * @private |
| 335 */ |
| 336 Gallery.prototype.back_ = function() { |
| 337 if (util.isFullScreen(this.context_.appWindow)) { |
| 338 util.toggleFullScreen(this.context_.appWindow, |
| 339 false); // Leave the full screen mode. |
| 340 } |
| 341 this.context_.onBack(this.getSelectedEntries()); |
| 342 }; |
| 343 |
| 344 /** |
| 345 * Handles user's 'Back' action (Escape or a click on the X icon). |
| 346 * @private |
| 347 */ |
| 348 Gallery.prototype.onBack_ = function() { |
| 349 this.executeWhenReady(this.back_.bind(this)); |
| 350 }; |
| 351 |
| 352 /** |
| 353 * Handles user's 'Close' action. |
| 354 * @private |
| 355 */ |
| 356 Gallery.prototype.onClose_ = function() { |
| 357 this.executeWhenReady(this.context_.onClose); |
| 358 }; |
| 359 |
| 360 /** |
| 361 * Handles user's 'Maximize' action (Escape or a click on the X icon). |
| 362 * @private |
| 363 */ |
| 364 Gallery.prototype.onMaximize_ = function() { |
| 365 this.executeWhenReady(this.context_.onMaximize); |
| 366 }; |
| 367 |
| 368 /** |
| 369 * Handles user's 'Maximize' action (Escape or a click on the X icon). |
| 370 * @private |
| 371 */ |
| 372 Gallery.prototype.onMinimize_ = function() { |
| 373 this.executeWhenReady(this.context_.onMinimize); |
| 374 }; |
| 375 |
| 376 /** |
| 377 * Executes a function when the editor is done with the modifications. |
| 378 * @param {function} callback Function to execute. |
| 379 */ |
| 380 Gallery.prototype.executeWhenReady = function(callback) { |
| 381 this.currentMode_.executeWhenReady(callback); |
| 382 }; |
| 383 |
| 384 /** |
| 385 * @return {Object} File browser private API. |
| 386 */ |
| 387 Gallery.getFileBrowserPrivate = function() { |
| 388 return chrome.fileBrowserPrivate || window.top.chrome.fileBrowserPrivate; |
| 389 }; |
| 390 |
| 391 /** |
| 392 * @return {boolean} True if some tool is currently active. |
| 393 */ |
| 394 Gallery.prototype.hasActiveTool = function() { |
| 395 return this.currentMode_.hasActiveTool() || |
| 396 this.isSharing_() || this.isRenaming_(); |
| 397 }; |
| 398 |
| 399 /** |
| 400 * External user action event handler. |
| 401 * @private |
| 402 */ |
| 403 Gallery.prototype.onUserAction_ = function() { |
| 404 this.closeShareMenu_(); |
| 405 // Show the toolbar and hide it after the default timeout. |
| 406 this.inactivityWatcher_.kick(); |
| 407 }; |
| 408 |
| 409 /** |
| 410 * Sets the current mode, update the UI. |
| 411 * @param {Object} mode Current mode. |
| 412 * @private |
| 413 */ |
| 414 Gallery.prototype.setCurrentMode_ = function(mode) { |
| 415 if (mode !== this.slideMode_ && mode !== this.mosaicMode_) |
| 416 console.error('Invalid Gallery mode'); |
| 417 |
| 418 this.currentMode_ = mode; |
| 419 this.container_.setAttribute('mode', this.currentMode_.getName()); |
| 420 this.updateSelectionAndState_(); |
| 421 this.updateButtons_(); |
| 422 }; |
| 423 |
| 424 /** |
| 425 * Mode toggle event handler. |
| 426 * @param {function=} opt_callback Callback. |
| 427 * @param {Event=} opt_event Event that caused this call. |
| 428 * @private |
| 429 */ |
| 430 Gallery.prototype.toggleMode_ = function(opt_callback, opt_event) { |
| 431 if (!this.modeButton_) |
| 432 return; |
| 433 |
| 434 if (this.changingMode_) // Do not re-enter while changing the mode. |
| 435 return; |
| 436 |
| 437 if (opt_event) |
| 438 this.onUserAction_(); |
| 439 |
| 440 this.changingMode_ = true; |
| 441 |
| 442 var onModeChanged = function() { |
| 443 this.changingMode_ = false; |
| 444 if (opt_callback) opt_callback(); |
| 445 }.bind(this); |
| 446 |
| 447 var tileIndex = Math.max(0, this.selectionModel_.selectedIndex); |
| 448 |
| 449 var mosaic = this.mosaicMode_.getMosaic(); |
| 450 var tileRect = mosaic.getTileRect(tileIndex); |
| 451 |
| 452 if (this.currentMode_ === this.slideMode_) { |
| 453 this.setCurrentMode_(this.mosaicMode_); |
| 454 mosaic.transform( |
| 455 tileRect, this.slideMode_.getSelectedImageRect(), true /* instant */); |
| 456 this.slideMode_.leave( |
| 457 tileRect, |
| 458 function() { |
| 459 // Animate back to normal position. |
| 460 mosaic.transform(); |
| 461 mosaic.show(); |
| 462 onModeChanged(); |
| 463 }.bind(this)); |
| 464 } else { |
| 465 this.setCurrentMode_(this.slideMode_); |
| 466 this.slideMode_.enter( |
| 467 tileRect, |
| 468 function() { |
| 469 // Animate to zoomed position. |
| 470 mosaic.transform(tileRect, this.slideMode_.getSelectedImageRect()); |
| 471 mosaic.hide(); |
| 472 }.bind(this), |
| 473 onModeChanged); |
| 474 } |
| 475 }; |
| 476 |
| 477 /** |
| 478 * Deletes the selected items. |
| 479 * @private |
| 480 */ |
| 481 Gallery.prototype.delete_ = function() { |
| 482 this.onUserAction_(); |
| 483 |
| 484 // Clone the sorted selected indexes array. |
| 485 var indexesToRemove = this.selectionModel_.selectedIndexes.slice(); |
| 486 if (!indexesToRemove.length) |
| 487 return; |
| 488 |
| 489 /* TODO(dgozman): Implement Undo delete, Remove the confirmation dialog. */ |
| 490 |
| 491 var itemsToRemove = this.getSelectedItems(); |
| 492 var plural = itemsToRemove.length > 1; |
| 493 var param = plural ? itemsToRemove.length : itemsToRemove[0].getFileName(); |
| 494 |
| 495 function deleteNext() { |
| 496 if (!itemsToRemove.length) |
| 497 return; // All deleted. |
| 498 |
| 499 // TODO(hirono): Use fileOperationManager. |
| 500 var entry = itemsToRemove.pop().getEntry(); |
| 501 entry.remove(deleteNext, function() { |
| 502 util.flog('Error deleting: ' + entry.name, deleteNext); |
| 503 }); |
| 504 } |
| 505 |
| 506 // Prevent the Gallery from handling Esc and Enter. |
| 507 this.document_.body.removeEventListener('keydown', this.keyDownBound_); |
| 508 var restoreListener = function() { |
| 509 this.document_.body.addEventListener('keydown', this.keyDownBound_); |
| 510 }.bind(this); |
| 511 |
| 512 |
| 513 var confirm = new cr.ui.dialogs.ConfirmDialog(this.container_); |
| 514 confirm.show(strf(plural ? |
| 515 'GALLERY_CONFIRM_DELETE_SOME' : 'GALLERY_CONFIRM_DELETE_ONE', param), |
| 516 function() { |
| 517 restoreListener(); |
| 518 this.selectionModel_.unselectAll(); |
| 519 this.selectionModel_.leadIndex = -1; |
| 520 // Remove items from the data model, starting from the highest index. |
| 521 while (indexesToRemove.length) |
| 522 this.dataModel_.splice(indexesToRemove.pop(), 1); |
| 523 // Delete actual files. |
| 524 deleteNext(); |
| 525 }.bind(this), |
| 526 function() { |
| 527 // Restore the listener after a timeout so that ESC is processed. |
| 528 setTimeout(restoreListener, 0); |
| 529 }); |
| 530 }; |
| 531 |
| 532 /** |
| 533 * @return {Array.<Gallery.Item>} Current selection. |
| 534 */ |
| 535 Gallery.prototype.getSelectedItems = function() { |
| 536 return this.selectionModel_.selectedIndexes.map( |
| 537 this.dataModel_.item.bind(this.dataModel_)); |
| 538 }; |
| 539 |
| 540 /** |
| 541 * @return {Array.<Entry>} Array of currently selected entries. |
| 542 */ |
| 543 Gallery.prototype.getSelectedEntries = function() { |
| 544 return this.selectionModel_.selectedIndexes.map(function(index) { |
| 545 return this.dataModel_.item(index).getEntry(); |
| 546 }.bind(this)); |
| 547 }; |
| 548 |
| 549 /** |
| 550 * @return {?Gallery.Item} Current single selection. |
| 551 */ |
| 552 Gallery.prototype.getSingleSelectedItem = function() { |
| 553 var items = this.getSelectedItems(); |
| 554 if (items.length > 1) { |
| 555 console.error('Unexpected multiple selection'); |
| 556 return null; |
| 557 } |
| 558 return items[0]; |
| 559 }; |
| 560 |
| 561 /** |
| 562 * Selection change event handler. |
| 563 * @private |
| 564 */ |
| 565 Gallery.prototype.onSelection_ = function() { |
| 566 this.updateSelectionAndState_(); |
| 567 this.updateShareMenu_(); |
| 568 }; |
| 569 |
| 570 /** |
| 571 * Data model splice event handler. |
| 572 * @private |
| 573 */ |
| 574 Gallery.prototype.onSplice_ = function() { |
| 575 this.selectionModel_.adjustLength(this.dataModel_.length); |
| 576 }; |
| 577 |
| 578 /** |
| 579 * Content change event handler. |
| 580 * @param {Event} event Event. |
| 581 * @private |
| 582 */ |
| 583 Gallery.prototype.onContentChange_ = function(event) { |
| 584 var index = this.dataModel_.indexOf(event.item); |
| 585 if (index !== this.selectionModel_.selectedIndex) |
| 586 console.error('Content changed for unselected item'); |
| 587 this.updateSelectionAndState_(); |
| 588 }; |
| 589 |
| 590 /** |
| 591 * Keydown handler. |
| 592 * |
| 593 * @param {Event} event Event. |
| 594 * @private |
| 595 */ |
| 596 Gallery.prototype.onKeyDown_ = function(event) { |
| 597 var wasSharing = this.isSharing_(); |
| 598 this.closeShareMenu_(); |
| 599 |
| 600 if (this.currentMode_.onKeyDown(event)) |
| 601 return; |
| 602 |
| 603 switch (util.getKeyModifiers(event) + event.keyIdentifier) { |
| 604 case 'U+0008': // Backspace. |
| 605 // The default handler would call history.back and close the Gallery. |
| 606 event.preventDefault(); |
| 607 break; |
| 608 |
| 609 case 'U+001B': // Escape |
| 610 // Swallow Esc if it closed the Share menu, otherwise close the Gallery. |
| 611 if (!wasSharing) |
| 612 this.onBack_(); |
| 613 break; |
| 614 |
| 615 case 'U+004D': // 'm' switches between Slide and Mosaic mode. |
| 616 this.toggleMode_(null, event); |
| 617 break; |
| 618 |
| 619 case 'U+0056': // 'v' |
| 620 this.slideMode_.startSlideshow(SlideMode.SLIDESHOW_INTERVAL_FIRST, event); |
| 621 break; |
| 622 |
| 623 case 'U+007F': // Delete |
| 624 case 'Shift-U+0033': // Shift+'3' (Delete key might be missing). |
| 625 this.delete_(); |
| 626 break; |
| 627 } |
| 628 }; |
| 629 |
| 630 // Name box and rename support. |
| 631 |
| 632 /** |
| 633 * Updates the UI related to the selected item and the persistent state. |
| 634 * |
| 635 * @private |
| 636 */ |
| 637 Gallery.prototype.updateSelectionAndState_ = function() { |
| 638 var numSelectedItems = this.selectionModel_.selectedIndexes.length; |
| 639 var displayName = ''; |
| 640 var selectedEntryURL = null; |
| 641 |
| 642 // If it's selecting something, update the variable values. |
| 643 if (numSelectedItems) { |
| 644 var selectedItem = |
| 645 this.dataModel_.item(this.selectionModel_.selectedIndex); |
| 646 this.selectedEntry_ = selectedItem.getEntry(); |
| 647 selectedEntryURL = this.selectedEntry_.toURL(); |
| 648 |
| 649 if (numSelectedItems === 1) { |
| 650 window.top.document.title = this.selectedEntry_.name; |
| 651 displayName = ImageUtil.getDisplayNameFromName(this.selectedEntry_.name); |
| 652 } else if (this.context_.curDirEntry) { |
| 653 // If the Gallery was opened on search results the search query will not |
| 654 // be recorded in the app state and the relaunch will just open the |
| 655 // gallery in the curDirEntry directory. |
| 656 window.top.document.title = this.context_.curDirEntry.name; |
| 657 displayName = strf('GALLERY_ITEMS_SELECTED', numSelectedItems); |
| 658 } |
| 659 } |
| 660 |
| 661 window.top.util.updateAppState( |
| 662 null, // Keep the current directory. |
| 663 selectedEntryURL, // Update the selection. |
| 664 {gallery: (this.currentMode_ === this.mosaicMode_ ? 'mosaic' : 'slide')}); |
| 665 |
| 666 // We can't rename files in readonly directory. |
| 667 // We can only rename a single file. |
| 668 this.filenameEdit_.disabled = numSelectedItems !== 1 || |
| 669 this.context_.readonlyDirName; |
| 670 this.filenameEdit_.value = displayName; |
| 671 }; |
| 672 |
| 673 /** |
| 674 * Click event handler on filename edit box |
| 675 * @private |
| 676 */ |
| 677 Gallery.prototype.onFilenameFocus_ = function() { |
| 678 ImageUtil.setAttribute(this.filenameSpacer_, 'renaming', true); |
| 679 this.filenameEdit_.originalValue = this.filenameEdit_.value; |
| 680 setTimeout(this.filenameEdit_.select.bind(this.filenameEdit_), 0); |
| 681 this.onUserAction_(); |
| 682 }; |
| 683 |
| 684 /** |
| 685 * Blur event handler on filename edit box. |
| 686 * |
| 687 * @param {Event} event Blur event. |
| 688 * @return {boolean} if default action should be prevented. |
| 689 * @private |
| 690 */ |
| 691 Gallery.prototype.onFilenameEditBlur_ = function(event) { |
| 692 if (this.filenameEdit_.value && this.filenameEdit_.value[0] === '.') { |
| 693 this.prompt_.show('GALLERY_FILE_HIDDEN_NAME', 5000); |
| 694 this.filenameEdit_.focus(); |
| 695 event.stopPropagation(); |
| 696 event.preventDefault(); |
| 697 return false; |
| 698 } |
| 699 |
| 700 var item = this.getSingleSelectedItem(); |
| 701 if (item) { |
| 702 var oldEntry = item.getEntry(); |
| 703 |
| 704 var onFileExists = function() { |
| 705 this.prompt_.show('GALLERY_FILE_EXISTS', 3000); |
| 706 this.filenameEdit_.value = name; |
| 707 this.filenameEdit_.focus(); |
| 708 }.bind(this); |
| 709 |
| 710 var onSuccess = function() { |
| 711 var event = new Event('content'); |
| 712 event.item = item; |
| 713 event.oldEntry = oldEntry; |
| 714 event.metadata = null; // Metadata unchanged. |
| 715 this.dataModel_.dispatchEvent(event); |
| 716 }.bind(this); |
| 717 |
| 718 if (this.filenameEdit_.value) { |
| 719 item.rename( |
| 720 this.filenameEdit_.value, onSuccess, onFileExists); |
| 721 } |
| 722 } |
| 723 |
| 724 ImageUtil.setAttribute(this.filenameSpacer_, 'renaming', false); |
| 725 this.onUserAction_(); |
| 726 }; |
| 727 |
| 728 /** |
| 729 * Keydown event handler on filename edit box |
| 730 * @private |
| 731 */ |
| 732 Gallery.prototype.onFilenameEditKeydown_ = function() { |
| 733 switch (event.keyCode) { |
| 734 case 27: // Escape |
| 735 this.filenameEdit_.value = this.filenameEdit_.originalValue; |
| 736 this.filenameEdit_.blur(); |
| 737 break; |
| 738 |
| 739 case 13: // Enter |
| 740 this.filenameEdit_.blur(); |
| 741 break; |
| 742 } |
| 743 event.stopPropagation(); |
| 744 }; |
| 745 |
| 746 /** |
| 747 * @return {boolean} True if file renaming is currently in progress. |
| 748 * @private |
| 749 */ |
| 750 Gallery.prototype.isRenaming_ = function() { |
| 751 return this.filenameSpacer_.hasAttribute('renaming'); |
| 752 }; |
| 753 |
| 754 /** |
| 755 * Content area click handler. |
| 756 * @private |
| 757 */ |
| 758 Gallery.prototype.onContentClick_ = function() { |
| 759 this.closeShareMenu_(); |
| 760 this.filenameEdit_.blur(); |
| 761 }; |
| 762 |
| 763 // Share button support. |
| 764 |
| 765 /** |
| 766 * @return {boolean} True if the Share menu is active. |
| 767 * @private |
| 768 */ |
| 769 Gallery.prototype.isSharing_ = function() { |
| 770 return !this.shareMenu_.hidden; |
| 771 }; |
| 772 |
| 773 /** |
| 774 * Close Share menu if it is open. |
| 775 * @private |
| 776 */ |
| 777 Gallery.prototype.closeShareMenu_ = function() { |
| 778 if (this.isSharing_()) |
| 779 this.toggleShare_(); |
| 780 }; |
| 781 |
| 782 /** |
| 783 * Share button handler. |
| 784 * @private |
| 785 */ |
| 786 Gallery.prototype.toggleShare_ = function() { |
| 787 if (!this.shareButton_.hasAttribute('disabled')) |
| 788 this.shareMenu_.hidden = !this.shareMenu_.hidden; |
| 789 this.inactivityWatcher_.check(); |
| 790 }; |
| 791 |
| 792 /** |
| 793 * Updates available actions list based on the currently selected urls. |
| 794 * @private. |
| 795 */ |
| 796 Gallery.prototype.updateShareMenu_ = function() { |
| 797 var entries = this.getSelectedEntries(); |
| 798 |
| 799 function isShareAction(task) { |
| 800 var taskParts = task.taskId.split('|'); |
| 801 return taskParts[0] !== chrome.runtime.id; |
| 802 } |
| 803 |
| 804 var api = Gallery.getFileBrowserPrivate(); |
| 805 var mimeTypes = []; // TODO(kaznacheev) Collect mime types properly. |
| 806 |
| 807 var createShareMenu = function(tasks) { |
| 808 var wasHidden = this.shareMenu_.hidden; |
| 809 this.shareMenu_.hidden = true; |
| 810 var items = this.shareMenu_.querySelectorAll('.item'); |
| 811 for (var i = 0; i !== items.length; i++) { |
| 812 items[i].parentNode.removeChild(items[i]); |
| 813 } |
| 814 |
| 815 for (var t = 0; t !== tasks.length; t++) { |
| 816 var task = tasks[t]; |
| 817 if (!isShareAction(task)) continue; |
| 818 |
| 819 var item = util.createChild(this.shareMenu_, 'item'); |
| 820 item.textContent = task.title; |
| 821 item.style.backgroundImage = 'url(' + task.iconUrl + ')'; |
| 822 item.addEventListener('click', function(taskId) { |
| 823 this.toggleShare_(); // Hide the menu. |
| 824 // TODO(hirono): Use entries instead of URLs. |
| 825 this.executeWhenReady( |
| 826 api.executeTask.bind( |
| 827 api, |
| 828 taskId, |
| 829 util.entriesToURLs(entries), |
| 830 function(result) { |
| 831 var alertDialog = |
| 832 new cr.ui.dialogs.AlertDialog(this.container_); |
| 833 util.isTeleported(window).then(function(teleported) { |
| 834 if (teleported) |
| 835 util.showOpenInOtherDesktopAlert(alertDialog, entries); |
| 836 }.bind(this)); |
| 837 }.bind(this))); |
| 838 }.bind(this, task.taskId)); |
| 839 } |
| 840 |
| 841 var empty = this.shareMenu_.querySelector('.item') === null; |
| 842 ImageUtil.setAttribute(this.shareButton_, 'disabled', empty); |
| 843 this.shareMenu_.hidden = wasHidden || empty; |
| 844 }.bind(this); |
| 845 |
| 846 // Create or update the share menu with a list of sharing tasks and show |
| 847 // or hide the share button. |
| 848 // TODO(mtomasz): Pass Entries directly, instead of URLs. |
| 849 if (!entries.length) |
| 850 createShareMenu([]); // Empty list of tasks, since there is no selection. |
| 851 else |
| 852 api.getFileTasks(util.entriesToURLs(entries), mimeTypes, createShareMenu); |
| 853 }; |
| 854 |
| 855 /** |
| 856 * Updates thumbnails. |
| 857 * @private |
| 858 */ |
| 859 Gallery.prototype.updateThumbnails_ = function() { |
| 860 if (this.currentMode_ === this.slideMode_) |
| 861 this.slideMode_.updateThumbnails(); |
| 862 |
| 863 if (this.mosaicMode_) { |
| 864 var mosaic = this.mosaicMode_.getMosaic(); |
| 865 if (mosaic.isInitialized()) |
| 866 mosaic.reload(); |
| 867 } |
| 868 }; |
| 869 |
| 870 /** |
| 871 * Updates buttons. |
| 872 * @private |
| 873 */ |
| 874 Gallery.prototype.updateButtons_ = function() { |
| 875 if (this.modeButton_) { |
| 876 var oppositeMode = |
| 877 this.currentMode_ === this.slideMode_ ? this.mosaicMode_ : |
| 878 this.slideMode_; |
| 879 this.modeButton_.title = str(oppositeMode.getTitle()); |
| 880 } |
| 881 }; |
| 882 |
| 883 window.addEventListener('load', function() { |
| 884 var entries = window.launchData.items.map( |
| 885 function(item) { return item.entry; }); |
| 886 window.backgroundComponent. |
| 887 then(function(inBackgroundComponent) { |
| 888 window.loadTimeData.data = inBackgroundComponent.stringData; |
| 889 var gallery = new Gallery(inBackgroundComponent.volumeManager); |
| 890 gallery.load(entries, entries); |
| 891 }). |
| 892 catch(function(error) { |
| 893 console.error(error.stack || error); |
| 894 }); |
| 895 }); |
OLD | NEW |