Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 /** | |
| 6 * Monitor the downloading progress of a XMLHttpRequest |xhr_| and shows the | |
| 7 * progress on |progressBar_|. | |
| 8 * @constructor | |
| 9 */ | |
| 10 function ProgressManager() { | |
| 11 this.xhr_ = null; | |
| 12 this.progressBar_ = document.querySelector('.progress-bar'); | |
| 13 this.selectedGridItem_ = null; | |
| 14 } | |
| 15 | |
| 16 /** | |
| 17 * Sets the XMLHttpRequest |xhr| to monitor, and the wallpaper thumbnail grid | |
| 18 * item |selectedGridItem| to show a progress bar for. Cancels previous xhr and | |
| 19 * hides/removes previous progress bar if any. | |
| 20 * Note: this must be called before xhr.send() function. Otherwise, we wont get | |
| 21 * loadstart event. | |
|
flackr
2013/02/06 15:30:46
The semantics of passing the XHR in and controllin
bshe
2013/02/06 17:51:31
You are right. And it seems we already did the abo
| |
| 22 * @param {XMLHttpRequest} xhr The XMLHttpRequest. | |
| 23 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper thumbnail | |
| 24 * grid item. It extends from cr.ui.ListItem. | |
| 25 */ | |
| 26 ProgressManager.prototype.reset = function(xhr, selectedGridItem) { | |
| 27 if (this.xhr_) | |
| 28 this.xhr_.abort(); | |
| 29 this.hideProgressBar(this.selectedGridItem_); | |
| 30 this.xhr_ = xhr; | |
| 31 this.selectedGridItem_ = selectedGridItem; | |
| 32 this.xhr_.addEventListener('loadstart', | |
| 33 this.onDownloadStart_.bind(this)); | |
| 34 this.xhr_.addEventListener('progress', | |
| 35 this.onDownloadProgress_.bind(this)); | |
| 36 this.xhr_.addEventListener('abort', | |
| 37 this.onDownloadAbort_.bind(this)); | |
| 38 this.xhr_.addEventListener('error', | |
| 39 this.onDownloadError_.bind(this)); | |
| 40 this.xhr_.addEventListener('load', | |
| 41 this.onDownloadComplete_.bind(this)); | |
| 42 }; | |
| 43 | |
| 44 /** | |
| 45 * Removes the progress bar in |selectedGridItem| if any. Maybe called | |
| 46 * asynchronously. | |
| 47 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper thumbnail | |
| 48 grid item. It extends from cr.ui.ListItem. | |
| 49 */ | |
| 50 ProgressManager.prototype.hideProgressBar = function(selectedGridItem) { | |
| 51 if (selectedGridItem && selectedGridItem.querySelector('.progress-bar')) { | |
| 52 this.progressBar_.hidden = true; | |
| 53 selectedGridItem.removeChild(this.progressBar_); | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 /** | |
| 58 * Calculates and updates the width of progress track. | |
| 59 * @private | |
| 60 * @param {float} percentComplete The percent of loaded content. | |
| 61 */ | |
| 62 ProgressManager.prototype.setProgress_ = function(percentComplete) { | |
| 63 this.progressBar_.querySelector('.progress-track').style.width = | |
| 64 (percentComplete * 100) + '%'; | |
| 65 }; | |
| 66 | |
| 67 /** | |
| 68 * Shows a 0% progress bar to indicate downloading starts. | |
| 69 * @private | |
| 70 * @param {Event} e A loadstart ProgressEvent from XMLHttpRequest. | |
| 71 */ | |
| 72 ProgressManager.prototype.onDownloadStart_ = function(e) { | |
| 73 this.setProgress_(0); | |
| 74 this.selectedGridItem_.appendChild(this.progressBar_); | |
| 75 this.progressBar_.hidden = false; | |
| 76 }; | |
| 77 | |
| 78 /** | |
| 79 * Hides progress bar when progression is terminated. | |
| 80 * @private | |
| 81 * @param {Event} e An abort ProgressEvent from XMLHttpRequest. | |
| 82 */ | |
| 83 ProgressManager.prototype.onDownloadAbort_ = function(e) { | |
| 84 this.xhr_ = null; | |
| 85 this.hideProgressBar(this.selectedGridItem_); | |
| 86 }; | |
| 87 | |
| 88 /** | |
| 89 * Download completed successfully. Shows a 100% progress bar and clears |xhr_|. | |
| 90 * @private | |
| 91 * @param {Event} e A load ProgressEvent from XMLHttpRequest. | |
| 92 */ | |
| 93 ProgressManager.prototype.onDownloadComplete_ = function(e) { | |
| 94 this.setProgress_(1); | |
| 95 this.xhr_ = null; | |
| 96 }; | |
| 97 | |
| 98 /** | |
| 99 * Shows error message when progression failed. | |
| 100 * @private | |
| 101 * @param {Event} e An error ProgressEvent from XMLHttpRequest. | |
| 102 */ | |
| 103 ProgressManager.prototype.onDownloadError_ = function(e) { | |
| 104 // TODO(bshe): Add error message back once we decide how to show error | |
| 105 // message in the new UI. http://crbug.com/162563 | |
| 106 this.xhr_ = null; | |
| 107 this.hideProgressBar(this.selectedGridItem_); | |
| 108 }; | |
| 109 | |
| 110 /** | |
| 111 * Calculates downloading percentage and shows downloading progress. | |
| 112 * @private | |
| 113 * @param {Event} e A progress ProgressEvent from XMLHttpRequest. | |
| 114 */ | |
| 115 ProgressManager.prototype.onDownloadProgress_ = function(e) { | |
| 116 if (e.lengthComputable) | |
| 117 this.setProgress_(e.loaded / e.total); | |
| 118 }; | |
| OLD | NEW |