| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 * The minimum about of time to display the butter bar for, in ms. | 6 * The minimum about of time to display the butter bar for, in ms. |
| 7 * Justification is 1000ms for minimum display time plus 300ms for transition | 7 * Justification is 1000ms for minimum display time plus 300ms for transition |
| 8 * duration. | 8 * duration. |
| 9 */ | 9 */ |
| 10 var MINIMUM_BUTTER_DISPLAY_TIME_MS = 1300; | 10 var MINIMUM_BUTTER_DISPLAY_TIME_MS = 1300; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Butter bar is shown on top of the file list and is used to show the copy | 13 * Butter bar is shown on top of the document and is used to show the progress |
| 14 * progress and other messages. | 14 * and other messages. |
| 15 * @constructor | 15 * @constructor |
| 16 * @param {HTMLElement} dialogDom FileManager top-level div. | 16 * @param {HTMLElement} dialogDom Document top-level div. |
| 17 * @param {FileCopyManagerWrapper} copyManager The copy manager. | |
| 18 */ | 17 */ |
| 19 function ButterBar(dialogDom, copyManager) { | 18 function ButterBar(dialogDom) { |
| 20 this.dialogDom_ = dialogDom; | 19 this.dialogDom_ = dialogDom; |
| 21 this.butter_ = this.dialogDom_.querySelector('#butter-bar'); | 20 this.butter_ = this.dialogDom_.querySelector('#butter-bar'); |
| 22 this.document_ = this.butter_.ownerDocument; | 21 this.document_ = this.butter_.ownerDocument; |
| 23 this.copyManager_ = copyManager; | |
| 24 this.hideTimeout_ = null; | 22 this.hideTimeout_ = null; |
| 25 this.showTimeout_ = null; | 23 this.showTimeout_ = null; |
| 26 this.lastShowTime_ = 0; | 24 this.lastShowTime_ = 0; |
| 27 | |
| 28 this.copyManager_.addEventListener('copy-progress', | |
| 29 this.onCopyProgress_.bind(this)); | |
| 30 } | 25 } |
| 31 | 26 |
| 32 /** | 27 /** |
| 33 * @return {boolean} True if visible. | 28 * @return {boolean} True if visible. |
| 34 * @private | 29 * @private |
| 35 */ | 30 */ |
| 36 ButterBar.prototype.isVisible_ = function() { | 31 ButterBar.prototype.isVisible_ = function() { |
| 37 return this.butter_.classList.contains('visible'); | 32 return this.butter_.classList.contains('visible'); |
| 38 }; | 33 }; |
| 39 | 34 |
| 40 /** | 35 /** |
| 41 * @return {boolean} True if displaying an error. | 36 * @return {boolean} True if displaying an error. |
| 42 * @private | 37 * @private |
| 43 */ | 38 */ |
| 44 ButterBar.prototype.isError_ = function() { | 39 ButterBar.prototype.isError_ = function() { |
| 45 return this.butter_.classList.contains('error'); | 40 return this.butter_.classList.contains('error'); |
| 46 }; | 41 }; |
| 47 | 42 |
| 48 /** | 43 /** |
| 49 * Show butter bar. | 44 * Show butter bar. |
| 50 * @param {string} message The message to be shown. | 45 * @param {string} message The message to be shown. |
| 51 * @param {object} opt_options Options: 'actions', 'progress', 'timeout'. | 46 * @param {object} opt_options Options: 'actions', 'progress', 'timeout'. |
| 52 */ | 47 */ |
| 53 ButterBar.prototype.show = function(message, opt_options) { | 48 ButterBar.prototype.show = function(message, opt_options) { |
| 54 this.clearShowTimeout_(); | 49 this.clearShowTimeout_(); |
| 55 this.clearHideTimeout_(); | 50 this.clearHideTimeout_(); |
| 56 | 51 |
| 57 var actions = this.butter_.querySelector('.actions'); | 52 var actions = this.butter_.querySelector('.actions'); |
| 58 actions.textContent = ''; | 53 actions.textContent = ''; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 /** | 169 /** |
| 175 * Clear the hide timeout if it is set. | 170 * Clear the hide timeout if it is set. |
| 176 * @private | 171 * @private |
| 177 */ | 172 */ |
| 178 ButterBar.prototype.clearHideTimeout_ = function() { | 173 ButterBar.prototype.clearHideTimeout_ = function() { |
| 179 if (this.hideTimeout_) { | 174 if (this.hideTimeout_) { |
| 180 clearTimeout(this.hideTimeout_); | 175 clearTimeout(this.hideTimeout_); |
| 181 this.hideTimeout_ = null; | 176 this.hideTimeout_ = null; |
| 182 } | 177 } |
| 183 }; | 178 }; |
| 184 | |
| 185 /** | |
| 186 * @private | |
| 187 * @return {string?} The type of operation. | |
| 188 */ | |
| 189 ButterBar.prototype.transferType_ = function() { | |
| 190 var progress = this.progress_; | |
| 191 if (!progress || | |
| 192 progress.pendingMoves === 0 && progress.pendingCopies === 0) | |
| 193 return 'TRANSFER'; | |
| 194 | |
| 195 if (progress.pendingMoves > 0) { | |
| 196 if (progress.pendingCopies > 0) | |
| 197 return 'TRANSFER'; | |
| 198 return 'MOVE'; | |
| 199 } | |
| 200 | |
| 201 return 'COPY'; | |
| 202 }; | |
| 203 | |
| 204 /** | |
| 205 * Set up butter bar for showing copy progress. | |
| 206 * @private | |
| 207 */ | |
| 208 ButterBar.prototype.showProgress_ = function() { | |
| 209 this.progress_ = this.copyManager_.getStatus(); | |
| 210 var options = {progress: this.progress_.percentage, actions: {}, timeout: 0}; | |
| 211 | |
| 212 var type = this.transferType_(); | |
| 213 var progressString = (this.progress_.pendingItems === 1) ? | |
| 214 strf(type + '_FILE_NAME', this.progress_.filename) : | |
| 215 strf(type + '_ITEMS_REMAINING', this.progress_.pendingItems); | |
| 216 | |
| 217 if (this.isVisible_()) { | |
| 218 this.update_(progressString, options); | |
| 219 } else { | |
| 220 options.actions[str('CANCEL_LABEL')] = | |
| 221 this.copyManager_.requestCancel.bind(this.copyManager_); | |
| 222 this.show(progressString, options); | |
| 223 } | |
| 224 }; | |
| 225 | |
| 226 /** | |
| 227 * 'copy-progress' event handler. Show progress or an appropriate message. | |
| 228 * @private | |
| 229 * @param {cr.Event} event A 'copy-progress' event from FileCopyManager. | |
| 230 */ | |
| 231 ButterBar.prototype.onCopyProgress_ = function(event) { | |
| 232 if (event.reason != 'PROGRESS') | |
| 233 this.clearShowTimeout_(); | |
| 234 | |
| 235 switch (event.reason) { | |
| 236 case 'BEGIN': | |
| 237 this.showTimeout_ = setTimeout(function() { | |
| 238 this.showTimeout_ = null; | |
| 239 this.showProgress_(); | |
| 240 }.bind(this), 500); | |
| 241 break; | |
| 242 | |
| 243 case 'PROGRESS': | |
| 244 this.showProgress_(); | |
| 245 break; | |
| 246 | |
| 247 case 'SUCCESS': | |
| 248 this.hide_(); | |
| 249 break; | |
| 250 | |
| 251 case 'CANCELLED': | |
| 252 this.show(str(this.transferType_() + '_CANCELLED'), {timeout: 1000}); | |
| 253 break; | |
| 254 | |
| 255 case 'ERROR': | |
| 256 if (event.error.reason === 'TARGET_EXISTS') { | |
| 257 var name = event.error.data.name; | |
| 258 if (event.error.data.isDirectory) | |
| 259 name += '/'; | |
| 260 this.showError_(strf(this.transferType_() + | |
| 261 '_TARGET_EXISTS_ERROR', name)); | |
| 262 } else if (event.error.reason === 'FILESYSTEM_ERROR') { | |
| 263 if (event.error.data.toGDrive && | |
| 264 event.error.data.code === FileError.QUOTA_EXCEEDED_ERR) { | |
| 265 // The alert will be shown in FileManager.onCopyProgress_. | |
| 266 this.hide_(); | |
| 267 } else { | |
| 268 this.showError_(strf(this.transferType_() + '_FILESYSTEM_ERROR', | |
| 269 util.getFileErrorString(event.error.data.code))); | |
| 270 } | |
| 271 } else { | |
| 272 this.showError_(strf(this.transferType_() + '_UNEXPECTED_ERROR', | |
| 273 event.error)); | |
| 274 } | |
| 275 break; | |
| 276 | |
| 277 default: | |
| 278 console.log('Unknown "copy-progress" event reason: ' + event.reason); | |
| 279 } | |
| 280 }; | |
| OLD | NEW |