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; | |
bshe
2013/01/25 04:35:05
Instead of creating a progressBar for each thumbna
| |
12 this.progressBar_ = cr.doc.createElement('div'); | |
13 this.progressBar_.classList.add('progress-bar'); | |
14 var progressTrack = cr.doc.createElement('div'); | |
15 progressTrack.classList.add('progress-track'); | |
16 this.progressBar_.appendChild(progressTrack); | |
17 this.selectedGridItem_ = null; | |
18 } | |
19 | |
20 /** | |
21 * Sets the XMLHttpRequest |xhr| to monitor and the wallpaper thumbnail grid | |
22 * item |selectedGridItem| to show progress bar. Cancels previous xhr and | |
23 * hides/removes previous progress bar if any. | |
24 * Note: this must be called before xhr.send() function. Otherwise, we wont get | |
25 * loadstart event. | |
26 * @param {XMLHttpRequest} xhr The XMLHttpRequest. | |
27 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper thumbnail | |
28 * grid item. It extends from cr.ui.ListItem. | |
29 */ | |
30 ProgressManager.prototype.reset = function(xhr, selectedGridItem) { | |
31 if (this.xhr_) | |
32 this.xhr_.abort(); | |
33 this.hide(this.selectedGridItem_); | |
34 this.xhr_ = xhr; | |
35 this.selectedGridItem_ = selectedGridItem; | |
36 this.xhr_.addEventListener('loadstart', | |
37 this.onDownloadStart_.bind(this)); | |
38 this.xhr_.addEventListener('progress', | |
39 this.onDownloadProgress_.bind(this)); | |
40 this.xhr_.addEventListener('abort', | |
41 this.onDownloadAbort_.bind(this)); | |
42 this.xhr_.addEventListener('error', | |
43 this.onDownloadError_.bind(this)); | |
44 this.xhr_.addEventListener('load', | |
45 this.onDownloadComplete_.bind(this)); | |
46 }; | |
47 | |
48 /** | |
49 * Removes the progress bar in |selectedGridItem| if any. May called | |
50 * asynchronously. | |
51 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper thumbnail | |
52 grid item. It extends from cr.ui.ListItem. | |
53 */ | |
54 ProgressManager.prototype.hide = function(selectedGridItem) { | |
55 if (selectedGridItem && selectedGridItem.querySelector('.progress-bar')) | |
56 selectedGridItem.removeChild(this.progressBar_); | |
57 }; | |
58 | |
59 /** | |
60 * Calculates and updates the width of progress track. | |
61 * @private | |
62 */ | |
63 ProgressManager.prototype.showProgress_ = function() { | |
64 this.progressBar_.querySelector('.progress-track').style.width = | |
65 (this.percentComplete_ * 100) + '%'; | |
66 }; | |
67 | |
68 /** | |
69 * Shows a 0% progress bar to indicate downloading starts. | |
70 * @private | |
71 * @param {Event} e A loadstart ProgressEvent from XMLHttpRequest. | |
72 */ | |
73 ProgressManager.prototype.onDownloadStart_ = function(e) { | |
74 this.percentComplete_ = 0; | |
75 this.showProgress_(); | |
76 this.selectedGridItem_.appendChild(this.progressBar_); | |
77 }; | |
78 | |
79 /** | |
80 * Hides progress bar when progression is terminated. | |
81 * @private | |
82 * @param {Event} e An abort ProgressEvent from XMLHttpRequest. | |
83 */ | |
84 ProgressManager.prototype.onDownloadAbort_ = function(e) { | |
85 this.xhr_ = null; | |
86 this.hide(this.selectedGridItem_); | |
87 }; | |
88 | |
89 /** | |
90 * Download completed successfully. Shows a 100% progress bar and clears |xhr_|. | |
91 * @private | |
92 * @param {Event} e A load ProgressEvent from XMLHttpRequest. | |
93 */ | |
94 ProgressManager.prototype.onDownloadComplete_ = function(e) { | |
95 this.progressBar_.querySelector('.progress-track').style.width = '100%'; | |
96 this.xhr_ = null; | |
97 }; | |
98 | |
99 /** | |
100 * Shows error message when progression failed. | |
101 * @private | |
102 * @param {Event} e An error ProgressEvent from XMLHttpRequest. | |
103 */ | |
104 ProgressManager.prototype.onDownloadError_ = function(e) { | |
105 // TODO(bshe): Add error message back once we decide how to show error | |
106 // message in the new UI. http://crbug.com/162563 | |
107 this.xhr_ = null; | |
108 this.hide(this.selectedGridItem_); | |
109 }; | |
110 | |
111 /** | |
112 * Calculates downloading percentage and shows downloading progress. | |
113 * @private | |
114 * @param {Event} e A progress ProgressEvent from XMLHttpRequest. | |
115 */ | |
116 ProgressManager.prototype.onDownloadProgress_ = function(e) { | |
117 if (e.lengthComputable) { | |
118 this.percentComplete_ = e.loaded / e.total; | |
119 this.showProgress_(); | |
120 } | |
121 }; | |
OLD | NEW |