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