| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 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 |
| 8 * duration. |
| 9 */ |
| 10 var MINIMUM_BUTTER_DISPLAY_TIME_MS = 1300; |
| 11 |
| 12 /** |
| 13 * Butter bar is shown on top of the wallpaper manager and is used to show the |
| 14 * downloading progress and other messages. |
| 15 * @constructor |
| 16 * @param {HTMLElement} dialogDom Wallpaper manager body tag. |
| 17 */ |
| 18 function ButterBar(dialogDom) { |
| 19 this.dialogDom_ = dialogDom; |
| 20 this.butter_ = this.dialogDom_.querySelector('#butter-bar'); |
| 21 this.document_ = this.butter_.ownerDocument; |
| 22 this.hideTimeout_ = null; |
| 23 this.showTimeout_ = null; |
| 24 this.xhr_ = null; |
| 25 this.lastShowTime_ = 0; |
| 26 } |
| 27 |
| 28 // Functions may be reused for general butter bar : ---------------------------- |
| 29 |
| 30 // These functions are copied from butter_bar.js in file manager. We will |
| 31 // revisit it to see if we can share some code after butter bar is integrated |
| 32 // with Photo Editor. |
| 33 // See http://codereview.chromium.org/10916149/ for details. |
| 34 // TODO(bshe): Remove these functions if we can share code with file manager. |
| 35 |
| 36 /** |
| 37 * @return {boolean} True if visible. |
| 38 * @private |
| 39 */ |
| 40 ButterBar.prototype.isVisible_ = function() { |
| 41 return this.butter_.classList.contains('visible'); |
| 42 }; |
| 43 |
| 44 /** |
| 45 * @return {boolean} True if displaying an error. |
| 46 * @private |
| 47 */ |
| 48 ButterBar.prototype.isError_ = function() { |
| 49 return this.butter_.classList.contains('error'); |
| 50 }; |
| 51 |
| 52 /** |
| 53 * Show butter bar. |
| 54 * @param {string} message The message to be shown. |
| 55 * @param {object} opt_options Options: 'actions', 'progress', 'timeout'. |
| 56 */ |
| 57 ButterBar.prototype.show = function(message, opt_options) { |
| 58 this.clearShowTimeout_(); |
| 59 this.clearHideTimeout_(); |
| 60 |
| 61 var actions = this.butter_.querySelector('.actions'); |
| 62 actions.textContent = ''; |
| 63 if (opt_options && 'actions' in opt_options) { |
| 64 for (var label in opt_options.actions) { |
| 65 var link = this.document_.createElement('a'); |
| 66 link.addEventListener('click', function(callback) { |
| 67 callback(); |
| 68 return false; |
| 69 }.bind(null, opt_options.actions[label])); |
| 70 actions.appendChild(link); |
| 71 } |
| 72 actions.hidden = false; |
| 73 } else { |
| 74 actions.hidden = true; |
| 75 } |
| 76 |
| 77 this.butter_.querySelector('.progress-bar').hidden = |
| 78 !(opt_options && 'progress' in opt_options); |
| 79 |
| 80 this.butter_.classList.remove('error'); |
| 81 this.butter_.classList.remove('visible'); // Will be shown in update_ |
| 82 this.update_(message, opt_options); |
| 83 }; |
| 84 |
| 85 /** |
| 86 * Show error message in butter bar. |
| 87 * @private |
| 88 * @param {string} message Message. |
| 89 * @param {object} opt_options Same as in show(). |
| 90 */ |
| 91 ButterBar.prototype.showError_ = function(message, opt_options) { |
| 92 this.show(message, opt_options); |
| 93 this.butter_.classList.add('error'); |
| 94 }; |
| 95 |
| 96 /** |
| 97 * Set message and/or progress. |
| 98 * @private |
| 99 * @param {string} message Message. |
| 100 * @param {object} opt_options Same as in show(). |
| 101 */ |
| 102 ButterBar.prototype.update_ = function(message, opt_options) { |
| 103 if (!opt_options) |
| 104 opt_options = {}; |
| 105 |
| 106 this.clearHideTimeout_(); |
| 107 |
| 108 var timeout = ('timeout' in opt_options) ? opt_options.timeout : 10 * 1000; |
| 109 if (timeout) { |
| 110 this.hideTimeout_ = setTimeout(function() { |
| 111 this.hideTimeout_ = null; |
| 112 this.hide_(); |
| 113 }.bind(this), timeout); |
| 114 } |
| 115 |
| 116 this.butter_.querySelector('.butter-message').textContent = message; |
| 117 if (message && !this.isVisible_()) { |
| 118 // The butter bar is made visible on the first non-empty message. |
| 119 this.butter_.classList.add('visible'); |
| 120 this.lastShowTime_ = Date.now(); |
| 121 } |
| 122 if (opt_options && 'progress' in opt_options) { |
| 123 this.butter_.querySelector('.progress-track').style.width = |
| 124 (opt_options.progress * 100) + '%'; |
| 125 } |
| 126 }; |
| 127 |
| 128 /** |
| 129 * Hide butter bar. There might be some delay before hiding so that butter bar |
| 130 * would be shown for no less than the minimal time. |
| 131 * @param {boolean} opt_force If true hide immediately. |
| 132 * @private |
| 133 */ |
| 134 ButterBar.prototype.hide_ = function(opt_force) { |
| 135 this.clearHideTimeout_(); |
| 136 |
| 137 if (!this.isVisible_()) |
| 138 return; |
| 139 |
| 140 var delay = |
| 141 MINIMUM_BUTTER_DISPLAY_TIME_MS - (Date.now() - this.lastShowTime_); |
| 142 |
| 143 if (opt_force || delay <= 0) { |
| 144 this.butter_.classList.remove('visible'); |
| 145 } else { |
| 146 // Reschedule hide to comply with the minimal display time. |
| 147 this.hideTimeout_ = setTimeout(function() { |
| 148 this.hideTimeout_ = null; |
| 149 this.hide_(true); |
| 150 }.bind(this), delay); |
| 151 } |
| 152 }; |
| 153 |
| 154 /** |
| 155 * If butter bar shows an error message, close it. |
| 156 * @return {boolean} True if butter bar was closed. |
| 157 */ |
| 158 ButterBar.prototype.hideError = function() { |
| 159 if (this.isVisible_() && this.isError_()) { |
| 160 this.hide_(true /* force */); |
| 161 return true; |
| 162 } else { |
| 163 return false; |
| 164 } |
| 165 }; |
| 166 |
| 167 /** |
| 168 * Clear the show timeout if it is set. |
| 169 * @private |
| 170 */ |
| 171 ButterBar.prototype.clearShowTimeout_ = function() { |
| 172 if (this.showTimeout_) { |
| 173 clearTimeout(this.showTimeout_); |
| 174 this.showTimeout_ = null; |
| 175 } |
| 176 }; |
| 177 |
| 178 /** |
| 179 * Clear the hide timeout if it is set. |
| 180 * @private |
| 181 */ |
| 182 ButterBar.prototype.clearHideTimeout_ = function() { |
| 183 if (this.hideTimeout_) { |
| 184 clearTimeout(this.hideTimeout_); |
| 185 this.hideTimeout_ = null; |
| 186 } |
| 187 }; |
| 188 |
| 189 // Functions specific to WallpaperManager butter bar : ------------------------- |
| 190 |
| 191 /** |
| 192 * Sets the XMLHttpRequest object that we want to show progress. |
| 193 * @param {XMLHttpRequest} xhr The XMLHttpRequest. |
| 194 */ |
| 195 ButterBar.prototype.setRequest = function(xhr) { |
| 196 if (this.xhr_) |
| 197 this.xhr_.abort(); |
| 198 this.xhr_ = xhr; |
| 199 this.xhr_.addEventListener('loadstart', |
| 200 this.onDownloadStart_.bind(this)); |
| 201 this.xhr_.addEventListener('progress', |
| 202 this.onDownloadProgress_.bind(this)); |
| 203 this.xhr_.addEventListener('abort', |
| 204 this.onDownloadAbort_.bind(this)); |
| 205 this.xhr_.addEventListener('error', |
| 206 this.onDownloadError_.bind(this)); |
| 207 this.xhr_.addEventListener('load', |
| 208 this.onDownloadComplete_.bind(this)); |
| 209 }; |
| 210 |
| 211 /** |
| 212 * Sets the options and strings and shows progress on butter bar. |
| 213 * @private |
| 214 */ |
| 215 ButterBar.prototype.showProgress_ = function() { |
| 216 var options = {progress: this.percentComplete_, actions: {}, |
| 217 timeout: 0}; |
| 218 |
| 219 var progressString = 'Downloading'; |
| 220 |
| 221 if (this.isVisible_()) { |
| 222 this.update_(progressString, options); |
| 223 } else { |
| 224 options.actions['Cancel'] = function() { |
| 225 this.xhr_.abort(); |
| 226 }; |
| 227 this.show(progressString, options); |
| 228 } |
| 229 }; |
| 230 |
| 231 /** |
| 232 * Sets a timeout to show a butter bar when wallpaper downloading starts. |
| 233 * @private |
| 234 * @param {Event} e A loadstart ProgressEvent from XMLHttpRequest. |
| 235 */ |
| 236 ButterBar.prototype.onDownloadStart_ = function(e) { |
| 237 this.percentComplete_ = 0; |
| 238 this.showTimeout_ = setTimeout(function() { |
| 239 this.showTimeout_ = null; |
| 240 this.showProgress_(); |
| 241 }.bind(this), 500); |
| 242 }; |
| 243 |
| 244 /** |
| 245 * Shows abort message in butter bar for 1 second if wallpaper downloading |
| 246 * aborted. |
| 247 * @private |
| 248 * @param {Event} e An abort ProgressEvent from XMLHttpRequest. |
| 249 */ |
| 250 ButterBar.prototype.onDownloadAbort_ = function(e) { |
| 251 this.show('Downloading aborted', {timeout: 1000}); |
| 252 this.xhr_ = null; |
| 253 }; |
| 254 |
| 255 /** |
| 256 * Hides butter bar when download complete. |
| 257 * @private |
| 258 * @param {Event} e A load ProgressEvent from XMLHttpRequest. |
| 259 */ |
| 260 ButterBar.prototype.onDownloadComplete_ = function(e) { |
| 261 this.hide_(); |
| 262 this.xhr_ = null; |
| 263 }; |
| 264 |
| 265 /** |
| 266 * Shows error message when receiving an error event. |
| 267 * @private |
| 268 * @param {Event} e An error ProgressEvent from XMLHttpRequest. |
| 269 */ |
| 270 ButterBar.prototype.onDownloadError_ = function(e) { |
| 271 this.showError_('An error occured while downloading wallpaper'); |
| 272 this.xhr_ = null; |
| 273 }; |
| 274 |
| 275 /** |
| 276 * Calculates downloading percentage and shows downloading progress. |
| 277 * @private |
| 278 * @param {Event} e A progress ProgressEvent from XMLHttpRequest. |
| 279 */ |
| 280 ButterBar.prototype.onDownloadProgress_ = function(e) { |
| 281 if (e.lengthComputable) { |
| 282 this.percentComplete_ = e.loaded / e.total; |
| 283 } |
| 284 this.showProgress_(); |
| 285 }; |
| OLD | NEW |