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