Chromium Code Reviews| 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 wallpaper manager and is used to show the |
| 14 * progress and other messages. | 14 * downloading progress and other messages. |
| 15 * @constructor | 15 * @constructor |
| 16 * @param {HTMLElement} dialogDom FileManager top-level div. | 16 * @param {HTMLElement} dialogDom Wallpaper manager body tag. |
| 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; |
| 25 } | |
| 27 | 26 |
| 28 this.copyManager_.addEventListener('copy-progress', | 27 // Functions may be reused for general butter bar : ---------------------------- |
| 29 this.onCopyProgress_.bind(this)); | 28 |
| 30 } | 29 // These functions are copied from butter_bar.js in file manager. We will |
| 30 // revisit it to see if we can share some code after butter bar is integrated | |
| 31 // with Photo Editor. | |
| 32 // See http://codereview.chromium.org/10916149/ for details. | |
| 33 // TODO(bshe): Remove these functions if we can share code with file manager. | |
| 31 | 34 |
| 32 /** | 35 /** |
| 33 * @return {boolean} True if visible. | 36 * @return {boolean} True if visible. |
| 34 * @private | 37 * @private |
| 35 */ | 38 */ |
| 36 ButterBar.prototype.isVisible_ = function() { | 39 ButterBar.prototype.isVisible_ = function() { |
| 37 return this.butter_.classList.contains('visible'); | 40 return this.butter_.classList.contains('visible'); |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 /** | 43 /** |
| 41 * @return {boolean} True if displaying an error. | 44 * @return {boolean} True if displaying an error. |
| 42 * @private | 45 * @private |
| 43 */ | 46 */ |
| 44 ButterBar.prototype.isError_ = function() { | 47 ButterBar.prototype.isError_ = function() { |
| 45 return this.butter_.classList.contains('error'); | 48 return this.butter_.classList.contains('error'); |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 /** | 51 /** |
| 49 * Show butter bar. | 52 * Show butter bar. |
| 50 * @param {string} message The message to be shown. | 53 * @param {string} message The message to be shown. |
| 51 * @param {object} opt_options Options: 'actions', 'progress', 'timeout'. | 54 * @param {object} opt_options Options: 'actions', 'progress', 'timeout'. |
| 52 */ | 55 */ |
| 53 ButterBar.prototype.show = function(message, opt_options) { | 56 ButterBar.prototype.show = function(message, opt_options) { |
| 54 this.clearShowTimeout_(); | 57 this.clearShowTimeout_(); |
| 55 this.clearHideTimeout_(); | 58 this.clearHideTimeout_(); |
| 56 | 59 |
| 57 var actions = this.butter_.querySelector('.actions'); | 60 var actions = this.butter_.querySelector('.actions'); |
| 58 actions.textContent = ''; | 61 actions.textContent = ''; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 * Clear the hide timeout if it is set. | 178 * Clear the hide timeout if it is set. |
| 176 * @private | 179 * @private |
| 177 */ | 180 */ |
| 178 ButterBar.prototype.clearHideTimeout_ = function() { | 181 ButterBar.prototype.clearHideTimeout_ = function() { |
| 179 if (this.hideTimeout_) { | 182 if (this.hideTimeout_) { |
| 180 clearTimeout(this.hideTimeout_); | 183 clearTimeout(this.hideTimeout_); |
| 181 this.hideTimeout_ = null; | 184 this.hideTimeout_ = null; |
| 182 } | 185 } |
| 183 }; | 186 }; |
| 184 | 187 |
| 188 // Functions specific to WallpaperManager butter bar : ------------------------- | |
| 189 | |
| 185 /** | 190 /** |
| 186 * @private | 191 * Sets the XMLHttpRequest object that we want to show progress. |
| 187 * @return {string?} The type of operation. | 192 * @param {XMLHttpRequest} xhr The XMLHttpRequest. |
| 188 */ | 193 */ |
| 189 ButterBar.prototype.transferType_ = function() { | 194 ButterBar.prototype.setRequest = function(xhr) { |
| 190 var progress = this.progress_; | 195 if (this.xhr_) |
|
Vladislav Kaznacheev
2012/09/12 13:45:12
Formatting: extra space after this.xhr everywhere
bshe
2012/09/12 18:48:58
you mean this.xhr_? I dont think I have an extra s
| |
| 191 if (!progress || | 196 this.xhr_.abort(); |
| 192 progress.pendingMoves === 0 && progress.pendingCopies === 0) | 197 this.xhr_ = xhr; |
| 193 return 'TRANSFER'; | 198 this.xhr_.addEventListener('loadstart', |
| 194 | 199 this.onDownloadStart_.bind(this)); |
| 195 if (progress.pendingMoves > 0) { | 200 this.xhr_.addEventListener('progress', |
| 196 if (progress.pendingCopies > 0) | 201 this.onDownloadProgress_.bind(this)); |
| 197 return 'TRANSFER'; | 202 this.xhr_.addEventListener('abort', |
| 198 return 'MOVE'; | 203 this.onDownloadAbort_.bind(this)); |
| 199 } | 204 this.xhr_.addEventListener('error', |
| 200 | 205 this.onDownloadError_.bind(this)); |
| 201 return 'COPY'; | 206 this.xhr_.addEventListener('load', |
| 207 this.onDownloadComplete_.bind(this)); | |
| 202 }; | 208 }; |
| 203 | 209 |
| 204 /** | 210 /** |
| 205 * Set up butter bar for showing copy progress. | 211 * Sets the options and strings and shows progress on butter bar. |
| 206 * @private | 212 * @private |
| 207 */ | 213 */ |
| 208 ButterBar.prototype.showProgress_ = function() { | 214 ButterBar.prototype.showProgress_ = function() { |
| 209 this.progress_ = this.copyManager_.getStatus(); | 215 var options = {progress: this.percentComplete_, actions: {}, |
| 210 var options = {progress: this.progress_.percentage, actions: {}, timeout: 0}; | 216 timeout: 0}; |
| 211 | 217 |
| 212 var type = this.transferType_(); | 218 var progressString = 'Downloading'; |
| 213 var progressString = (this.progress_.pendingItems === 1) ? | |
| 214 strf(type + '_FILE_NAME', this.progress_.filename) : | |
| 215 strf(type + '_ITEMS_REMAINING', this.progress_.pendingItems); | |
| 216 | 219 |
| 217 if (this.isVisible_()) { | 220 if (this.isVisible_()) { |
| 218 this.update_(progressString, options); | 221 this.update_(progressString, options); |
| 219 } else { | 222 } else { |
| 220 options.actions[str('CANCEL_LABEL')] = | 223 options.actions['Cancel'] = function() { |
| 221 this.copyManager_.requestCancel.bind(this.copyManager_); | 224 this.xhr_.abort(); |
| 225 }; | |
| 222 this.show(progressString, options); | 226 this.show(progressString, options); |
| 223 } | 227 } |
| 224 }; | 228 }; |
| 225 | 229 |
| 226 /** | 230 /** |
| 227 * 'copy-progress' event handler. Show progress or an appropriate message. | 231 * Sets a timeout to show a butter bar when wallpaper downloading starts. |
| 228 * @private | 232 * @private |
| 229 * @param {cr.Event} event A 'copy-progress' event from FileCopyManager. | 233 * @param {ProgressEvent} e A loadstart ProgressEvent from XMLHttpRequest. |
| 230 */ | 234 */ |
| 231 ButterBar.prototype.onCopyProgress_ = function(event) { | 235 ButterBar.prototype.onDownloadStart_ = function(e) { |
| 232 if (event.reason != 'PROGRESS') | 236 this.percentComplete_ = 0; |
| 233 this.clearShowTimeout_(); | 237 this.showTimeout_ = setTimeout(function() { |
| 238 this.showTimeout_ = null; | |
| 239 this.showProgress_(); | |
| 240 }.bind(this), 500); | |
| 241 }; | |
| 234 | 242 |
| 235 switch (event.reason) { | 243 /** |
| 236 case 'BEGIN': | 244 * Shows abort message in butter bar for 1 second if wallpaper downloading |
| 237 this.showTimeout_ = setTimeout(function() { | 245 * aborted. |
| 238 this.showTimeout_ = null; | 246 * @private |
| 239 this.showProgress_(); | 247 * @param {ProgressEvent} e An abort ProgressEvent from XMLHttpRequest. |
| 240 }.bind(this), 500); | 248 */ |
| 241 break; | 249 ButterBar.prototype. onDownloadAbort_ = function(e) { |
| 250 this.show('Downloading aborted', {timeout: 1000}); | |
| 251 }; | |
| 242 | 252 |
| 243 case 'PROGRESS': | 253 /** |
| 244 this.showProgress_(); | 254 * Hides butter bar when download complete. |
| 245 break; | 255 * @private |
| 256 * @param {ProgressEvent} e A load ProgressEvent from XMLHttpRequest. | |
| 257 */ | |
| 258 ButterBar.prototype. onDownloadComplete_ = function(e) { | |
| 259 this.hide_(); | |
| 260 }; | |
| 246 | 261 |
| 247 case 'SUCCESS': | 262 /** |
| 248 this.hide_(); | 263 * Shows error message when receiving an error event. |
| 249 break; | 264 * @private |
| 265 * @param {ProgressEvent} e An error ProgressEvent from XMLHttpRequest. | |
| 266 */ | |
| 267 ButterBar.prototype. onDownloadError_ = function(e) { | |
| 268 this.showError_('An error occured while downloading wallpaper'); | |
| 269 }; | |
| 250 | 270 |
| 251 case 'CANCELLED': | 271 /** |
| 252 this.show(str(this.transferType_() + '_CANCELLED'), {timeout: 1000}); | 272 * Calculates downloading percentage and shows downloading progress. |
| 253 break; | 273 * @private |
| 254 | 274 * @param {ProgressEvent} e A progress ProgressEvent from XMLHttpRequest. |
|
Vladislav Kaznacheev
2012/09/12 13:45:12
The description does not parse.
bshe
2012/09/12 18:48:58
Done.
| |
| 255 case 'ERROR': | 275 */ |
| 256 if (event.error.reason === 'TARGET_EXISTS') { | 276 ButterBar.prototype.onDownloadProgress_ = function(e) { |
| 257 var name = event.error.data.name; | 277 if (e.lengthComputable) { |
| 258 if (event.error.data.isDirectory) | 278 this.percentComplete_ = e.loaded / e.total; |
| 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 } | 279 } |
| 280 this.showProgress_(); | |
| 280 }; | 281 }; |
| OLD | NEW |