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. | |
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.removeEventListeners_(); | |
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 all event listeners progress manager currently registered. | |
46 * @private | |
47 */ | |
48 ProgressManager.prototype.removeEventListeners_ = function() { | |
49 this.xhr_.removeEventListener('loadstart', | |
50 this.onDownloadStart_.bind(this)); | |
flackr
2013/02/07 21:00:16
Sorry, I'm pretty sure this won't work. When you c
bshe
2013/02/07 21:45:58
dooh. You are absolutely right, bind creates new f
| |
51 this.xhr_.removeEventListener('progress', | |
52 this.onDownloadProgress_.bind(this)); | |
53 this.xhr_.removeEventListener('abort', | |
54 this.onDownloadAbort_.bind(this)); | |
55 this.xhr_.removeEventListener('error', | |
56 this.onDownloadError_.bind(this)); | |
57 this.xhr_.removeEventListener('load', | |
58 this.onDownloadComplete_.bind(this)); | |
59 }; | |
60 | |
61 /** | |
62 * Removes the progress bar in |selectedGridItem| if any. May be called | |
63 * asynchronously. | |
64 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper thumbnail | |
65 grid item. It extends from cr.ui.ListItem. | |
66 */ | |
67 ProgressManager.prototype.hideProgressBar = function(selectedGridItem) { | |
68 if (selectedGridItem && selectedGridItem.querySelector('.progress-bar')) { | |
69 this.progressBar_.hidden = true; | |
70 selectedGridItem.removeChild(this.progressBar_); | |
71 } | |
72 }; | |
73 | |
74 /** | |
75 * Calculates and updates the width of progress track. | |
76 * @private | |
77 * @param {float} percentComplete The percent of loaded content. | |
78 */ | |
79 ProgressManager.prototype.setProgress_ = function(percentComplete) { | |
80 this.progressBar_.querySelector('.progress-track').style.width = | |
81 (percentComplete * 100) + '%'; | |
82 }; | |
83 | |
84 /** | |
85 * Shows a 0% progress bar to indicate downloading starts. | |
86 * @private | |
87 * @param {Event} e A loadstart ProgressEvent from XMLHttpRequest. | |
88 */ | |
89 ProgressManager.prototype.onDownloadStart_ = function(e) { | |
90 this.setProgress_(0); | |
91 this.selectedGridItem_.appendChild(this.progressBar_); | |
92 this.progressBar_.hidden = false; | |
93 }; | |
94 | |
95 /** | |
96 * Hides progress bar when progression is terminated. | |
97 * @private | |
98 * @param {Event} e An abort ProgressEvent from XMLHttpRequest. | |
99 */ | |
100 ProgressManager.prototype.onDownloadAbort_ = function(e) { | |
101 this.removeEventListeners_(); | |
102 this.xhr_ = null; | |
103 this.hideProgressBar(this.selectedGridItem_); | |
104 }; | |
105 | |
106 /** | |
107 * Download completed successfully. Shows a 100% progress bar and clears |xhr_|. | |
108 * @private | |
109 * @param {Event} e A load ProgressEvent from XMLHttpRequest. | |
110 */ | |
111 ProgressManager.prototype.onDownloadComplete_ = function(e) { | |
112 this.setProgress_(1); | |
113 this.removeEventListeners_(); | |
114 this.xhr_ = null; | |
115 }; | |
116 | |
117 /** | |
118 * Shows error message when progression failed. | |
119 * @private | |
120 * @param {Event} e An error ProgressEvent from XMLHttpRequest. | |
121 */ | |
122 ProgressManager.prototype.onDownloadError_ = function(e) { | |
123 this.removeEventListeners_(); | |
124 // TODO(bshe): Add error message back once we decide how to show error | |
125 // message in the new UI. http://crbug.com/162563 | |
126 this.xhr_ = null; | |
127 this.hideProgressBar(this.selectedGridItem_); | |
128 }; | |
129 | |
130 /** | |
131 * Calculates downloading percentage and shows downloading progress. | |
132 * @private | |
133 * @param {Event} e A progress ProgressEvent from XMLHttpRequest. | |
134 */ | |
135 ProgressManager.prototype.onDownloadProgress_ = function(e) { | |
136 if (e.lengthComputable) | |
137 this.setProgress_(e.loaded / e.total); | |
138 }; | |
OLD | NEW |