| OLD | NEW |
| (Empty) |
| 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 | |
| 5 'use strict'; | |
| 6 | |
| 7 /** | |
| 8 * ImportingDialog manages the import process (which is really a copying). | |
| 9 * @param {HTMLElement} parentNode Node to be parent for this dialog. | |
| 10 * @param {FileOperationManager} fileOperationManager File operation manager | |
| 11 * instance. | |
| 12 * @param {MetadataCache} metadataCache Metadata cache. | |
| 13 * @param {number=} opt_parentWindowId Id of the parent window. | |
| 14 * @constructor | |
| 15 */ | |
| 16 function ImportingDialog( | |
| 17 parentNode, fileOperationManager, metadataCache, opt_parentWindowId) { | |
| 18 cr.ui.dialogs.BaseDialog.call(this, parentNode); | |
| 19 this.parentWindowId_ = opt_parentWindowId; | |
| 20 this.fileOperationManager_ = fileOperationManager; | |
| 21 this.metadataCache_ = metadataCache; | |
| 22 this.onCopyProgressBound_ = this.onCopyProgress_.bind(this); | |
| 23 } | |
| 24 | |
| 25 ImportingDialog.prototype = { | |
| 26 __proto__: cr.ui.dialogs.BaseDialog.prototype | |
| 27 }; | |
| 28 | |
| 29 /** | |
| 30 * One-time initialization of DOM. | |
| 31 * @private | |
| 32 */ | |
| 33 ImportingDialog.prototype.initDom_ = function() { | |
| 34 cr.ui.dialogs.BaseDialog.prototype.initDom_.call(this); | |
| 35 | |
| 36 this.container_.classList.add('importing-dialog'); | |
| 37 | |
| 38 this.content_ = util.createChild(this.frame_, 'content'); | |
| 39 this.frame_.insertBefore(this.content_, this.frame_.firstChild); | |
| 40 this.content_.appendChild(this.text_); | |
| 41 | |
| 42 this.imageBox_ = util.createChild(this.frame_, 'img-container'); | |
| 43 this.imageBox_.setAttribute('state', 'progress'); | |
| 44 this.frame_.insertBefore(this.imageBox_, this.frame_.firstChild); | |
| 45 | |
| 46 var progressContainer = util.createChild(this.content_, 'progress-container'); | |
| 47 this.progress_ = util.createChild(progressContainer, 'progress-bar'); | |
| 48 util.createChild(this.progress_, 'progress-track smoothed'); | |
| 49 | |
| 50 this.buttons_ = this.frame_.querySelector('.cr-dialog-buttons'); | |
| 51 this.content_.appendChild(this.buttons_); | |
| 52 | |
| 53 this.viewButton_ = util.createChild( | |
| 54 this.buttons_, 'cr-dialog-view', 'button'); | |
| 55 this.viewButton_.addEventListener('click', | |
| 56 this.onViewClick_.bind(this)); | |
| 57 this.buttons_.insertBefore(this.viewButton_, this.buttons_.firstChild); | |
| 58 | |
| 59 this.viewButton_.textContent = | |
| 60 loadTimeData.getString('VIEW_LABEL'); | |
| 61 this.viewButton_.hidden = true; | |
| 62 this.cancelButton_.textContent = | |
| 63 loadTimeData.getString('PHOTO_IMPORT_CANCEL_BUTTON'); | |
| 64 this.okButton_.textContent = | |
| 65 loadTimeData.getString('OK_LABEL'); | |
| 66 this.okButton_.hidden = true; | |
| 67 }; | |
| 68 | |
| 69 /** | |
| 70 * Shows dialog. | |
| 71 * @param {Array.<FileEntry>} entries Entries to import. | |
| 72 * @param {boolean} move Whether to move files instead of copying them. | |
| 73 */ | |
| 74 ImportingDialog.prototype.show = function(entries, move) { | |
| 75 var message = loadTimeData.getString('PHOTO_IMPORT_IMPORTING'); | |
| 76 cr.ui.dialogs.BaseDialog.prototype.show.call(this, message, null, null, null); | |
| 77 | |
| 78 this.error_ = false; | |
| 79 this.entries_ = entries; | |
| 80 this.move_ = move; | |
| 81 | |
| 82 this.progress_.querySelector('.progress-track').style.width = '0'; | |
| 83 this.fileOperationManager_.addEventListener('copy-progress', | |
| 84 this.onCopyProgressBound_); | |
| 85 | |
| 86 this.previewEntry_(0); | |
| 87 }; | |
| 88 | |
| 89 /** | |
| 90 * Starts copying. | |
| 91 * @param {DirectoryEntry} destination Directory to import to. | |
| 92 */ | |
| 93 ImportingDialog.prototype.start = function(destination) { | |
| 94 this.destination_ = destination; | |
| 95 this.fileOperationManager_.paste( | |
| 96 this.entries_.map(function(e) { return e.fullPath }), | |
| 97 this.destination_.fullPath, | |
| 98 this.move_); | |
| 99 }; | |
| 100 | |
| 101 /** | |
| 102 * Shows entry preview. | |
| 103 * @param {number} index Entry index. | |
| 104 * @private | |
| 105 */ | |
| 106 ImportingDialog.prototype.previewEntry_ = function(index) { | |
| 107 var box = this.imageBox_; | |
| 108 var entry = this.entries_[index]; | |
| 109 this.metadataCache_.get(entry, 'thumbnail|filesystem', | |
| 110 function(metadata) { | |
| 111 new ThumbnailLoader(entry.toURL(), | |
| 112 ThumbnailLoader.LoaderType.IMAGE, | |
| 113 metadata). | |
| 114 load(box, ThumbnailLoader.FillMode.FILL); | |
| 115 }); | |
| 116 }; | |
| 117 | |
| 118 /** | |
| 119 * Closes dialog. | |
| 120 * @param {function()=} opt_onHide Completion callback. | |
| 121 */ | |
| 122 ImportingDialog.prototype.hide = function(opt_onHide) { | |
| 123 this.fileOperationManager_.removeEventListener('copy-progress', | |
| 124 this.onCopyProgressBound_); | |
| 125 cr.ui.dialogs.BaseDialog.prototype.hide.call(this, opt_onHide); | |
| 126 }; | |
| 127 | |
| 128 /** | |
| 129 * Cancel button click event handler. | |
| 130 * @private | |
| 131 */ | |
| 132 ImportingDialog.prototype.onCancelClick_ = function() { | |
| 133 this.fileOperationManager_.requestCancel(); | |
| 134 this.hide(); | |
| 135 }; | |
| 136 | |
| 137 /** | |
| 138 * OK button click event handler. | |
| 139 * @private | |
| 140 */ | |
| 141 ImportingDialog.prototype.onOkClick_ = function() { | |
| 142 this.hide(); | |
| 143 if (!this.error_) | |
| 144 window.close(); | |
| 145 }; | |
| 146 | |
| 147 /** | |
| 148 * View button click event handler. Invokes the mosaic view. | |
| 149 * @private | |
| 150 */ | |
| 151 ImportingDialog.prototype.onViewClick_ = function() { | |
| 152 var url = chrome.runtime.getURL('main.html') + | |
| 153 '?{%22gallery%22:%22mosaic%22}#' + this.destination_.fullPath; | |
| 154 if (!this.parentWindowId_ || | |
| 155 !util.redirectMainWindow(this.parentWindowId_, url)) { | |
| 156 // The parent window hasn't been found. Launch the url as a new window. | |
| 157 // TODO(mtomasz): Change it to chrome.fileBrowserPrivate.openNewWindow. | |
| 158 chrome.app.window.create(url); | |
| 159 } | |
| 160 this.hide(); | |
| 161 window.close(); | |
| 162 }; | |
| 163 | |
| 164 /** | |
| 165 * Event handler for keydown event. | |
| 166 * @param {Event} event The event. | |
| 167 * @private | |
| 168 */ | |
| 169 ImportingDialog.prototype.onContainerKeyDown_ = function(event) { | |
| 170 // Handle Escape. | |
| 171 if (event.keyCode == 27) { | |
| 172 this.onCancelClick_(event); | |
| 173 event.preventDefault(); | |
| 174 } | |
| 175 }; | |
| 176 | |
| 177 /** | |
| 178 * 'copy-progress' event handler. Show progress. | |
| 179 * @param {Event} event A 'copy-progress' event from FileOperationManager. | |
| 180 * @private | |
| 181 */ | |
| 182 ImportingDialog.prototype.onCopyProgress_ = function(event) { | |
| 183 switch (event.reason) { | |
| 184 case 'BEGIN': | |
| 185 case 'PROGRESS': | |
| 186 var progress = this.fileOperationManager_.getStatus().percentage; | |
| 187 this.progress_.querySelector('.progress-track').style.width = | |
| 188 (progress * 100) + '%'; | |
| 189 var index = Math.round(this.entries_.length * progress); | |
| 190 if (index == this.entries_.length) index--; | |
| 191 this.previewEntry_(index); | |
| 192 break; | |
| 193 | |
| 194 case 'SUCCESS': | |
| 195 this.text_.textContent = | |
| 196 loadTimeData.getString('PHOTO_IMPORT_IMPORT_COMPLETE'); | |
| 197 this.imageBox_.setAttribute('state', 'success'); | |
| 198 this.cancelButton_.hidden = true; | |
| 199 this.viewButton_.hidden = false; | |
| 200 this.okButton_.hidden = false; | |
| 201 break; | |
| 202 | |
| 203 case 'CANCELLED': | |
| 204 this.hide(); | |
| 205 break; | |
| 206 | |
| 207 case 'ERROR': | |
| 208 this.error_ = true; | |
| 209 this.text_.textContent = | |
| 210 loadTimeData.getString('PHOTO_IMPORT_IMPORTING_ERROR'); | |
| 211 this.imageBox_.setAttribute('state', 'error'); | |
| 212 this.cancelButton_.hidden = true; | |
| 213 this.viewButton_.hidden = true; | |
| 214 this.okButton_.hidden = false; | |
| 215 break; | |
| 216 | |
| 217 default: | |
| 218 console.error('Unknown "copy-progress" event reason: ' + event.reason); | |
| 219 } | |
| 220 }; | |
| OLD | NEW |