Index: chrome/browser/resources/file_manager/js/butter_bar.js |
diff --git a/chrome/browser/resources/file_manager/js/butter_bar.js b/chrome/browser/resources/file_manager/js/butter_bar.js |
index f19e48386dd733cad31542eb8b45b176272a2683..aed582e460609e936a8cf0720f20e6caea10eb01 100644 |
--- a/chrome/browser/resources/file_manager/js/butter_bar.js |
+++ b/chrome/browser/resources/file_manager/js/butter_bar.js |
@@ -23,45 +23,59 @@ function ButterBar(dialogDom, copyManager) { |
this.copyManager_ = copyManager; |
this.hideTimeout_ = null; |
this.showTimeout_ = null; |
- this.visible_ = false; |
this.lastShowTime_ = 0; |
- this.isError_ = false; |
this.copyManager_.addEventListener('copy-progress', |
this.onCopyProgress_.bind(this)); |
} |
/** |
+ * @return {boolean} True if visible. |
+ * @private |
+ */ |
+ButterBar.prototype.isVisible_ = function() { |
+ return this.butter_.classList.contains('visible'); |
+}; |
+ |
+/** |
+ * @return {boolean} True if displaying an error. |
+ * @private |
+ */ |
+ButterBar.prototype.isError_ = function() { |
+ return this.butter_.classList.contains('error'); |
+}; |
+ |
+ /** |
* Show butter bar. |
* @param {string} message The message to be shown. |
* @param {object} opt_options Options: 'actions', 'progress', 'timeout'. |
*/ |
ButterBar.prototype.show = function(message, opt_options) { |
- if (opt_options) { |
- if ('actions' in opt_options) { |
- var actions = this.butter_.querySelector('.actions'); |
- while (actions.childNodes.length) |
- actions.removeChild(actions.firstChild); |
- for (var label in opt_options.actions) { |
- var link = this.document_.createElement('a'); |
- link.addEventListener('click', function() { |
- opt_options.actions[label](); |
- return false; |
- }); |
- actions.appendChild(link); |
- } |
- actions.classList.remove('hide-in-butter'); |
- } |
- if ('progress' in opt_options) { |
- this.butter_.querySelector('.progress-bar').classList.remove( |
- 'hide-in-butter'); |
+ this.clearShowTimeout_(); |
+ this.clearHideTimeout_(); |
+ |
+ var actions = this.butter_.querySelector('.actions'); |
+ actions.textContent = ''; |
+ if (opt_options && 'actions' in opt_options) { |
+ for (var label in opt_options.actions) { |
+ var link = this.document_.createElement('a'); |
+ link.addEventListener('click', function(callback) { |
+ callback(); |
+ return false; |
+ }.bind(null, opt_options.actions[label])); |
+ actions.appendChild(link); |
} |
+ actions.hidden = false; |
+ } else { |
+ actions.hidden = true; |
} |
- this.visible_ = true; |
- this.isError_ = false; |
+ this.butter_.querySelector('.progress-bar').hidden = |
+ !(opt_options && 'progress' in opt_options); |
+ |
+ this.butter_.classList.remove('error'); |
+ this.butter_.classList.remove('visible'); // Will be shown in update_ |
this.update_(message, opt_options); |
- this.lastShowTime_ = Date.now(); |
}; |
/** |
@@ -72,7 +86,6 @@ ButterBar.prototype.show = function(message, opt_options) { |
*/ |
ButterBar.prototype.showError_ = function(message, opt_options) { |
this.show(message, opt_options); |
- this.isError_ = true; |
this.butter_.classList.add('error'); |
}; |
@@ -86,22 +99,21 @@ ButterBar.prototype.update_ = function(message, opt_options) { |
if (!opt_options) |
opt_options = {}; |
- var timeout = ('timeout' in opt_options) ? opt_options.timeout : 10 * 1000; |
- |
- if (this.hideTimeout_) |
- clearTimeout(this.hideTimeout_); |
+ this.clearHideTimeout_(); |
+ var timeout = ('timeout' in opt_options) ? opt_options.timeout : 10 * 1000; |
if (timeout) { |
this.hideTimeout_ = setTimeout(function() { |
- this.hideButter(); |
- this.hideTimeout_ = null; |
+ this.hideTimeout_ = null; |
+ this.hide_(); |
}.bind(this), timeout); |
} |
this.butter_.querySelector('.butter-message').textContent = message; |
- if (message) { |
+ if (message && !this.isVisible_()) { |
// The butter bar is made visible on the first non-empty message. |
- this.butter_.classList.remove('before-show'); |
+ this.butter_.classList.add('visible'); |
+ this.lastShowTime_ = Date.now(); |
} |
if (opt_options && 'progress' in opt_options) { |
this.butter_.querySelector('.progress-track').style.width = |
@@ -115,26 +127,26 @@ ButterBar.prototype.update_ = function(message, opt_options) { |
/** |
* Hide butter bar. There might be some delay before hiding so that butter bar |
* would be shown for no less than the minimal time. |
+ * @param {boolean} opt_force If true hide immediately. |
* @private |
*/ |
-ButterBar.prototype.hide_ = function() { |
- if (this.visible_) { |
- var delay = Math.max( |
- MINIMUM_BUTTER_DISPLAY_TIME_MS - (Date.now() - this.lastShowTime_), 0); |
- |
- var butter = this.butter_; |
- |
- function hideButter() { |
- butter.classList.remove('error'); |
- butter.classList.remove('after-show'); |
- butter.classList.add('before-show'); |
- butter.querySelector('.actions').classList.add('hide-in-butter'); |
- butter.querySelector('.progress-bar').classList.add('hide-in-butter'); |
- } |
+ButterBar.prototype.hide_ = function(opt_force) { |
+ this.clearHideTimeout_(); |
+ |
+ if (!this.isVisible_()) |
+ return; |
- setTimeout(function() { butter.classList.add('after-show'); }, delay); |
- setTimeout(hideButter, delay + 1000); |
- this.visible_ = false; |
+ var delay = |
+ MINIMUM_BUTTER_DISPLAY_TIME_MS - (Date.now() - this.lastShowTime_); |
+ |
+ if (opt_force || delay <= 0) { |
+ this.butter_.classList.remove('visible'); |
+ } else { |
+ // Reschedule hide to comply with the minimal display time. |
+ this.hideTimeout_ = setTimeout(function() { |
+ this.hideTimeout_ = null; |
+ this.hide_(true); |
+ }.bind(this), delay); |
} |
}; |
@@ -143,9 +155,8 @@ ButterBar.prototype.hide_ = function() { |
* @return {boolean} True if butter bar was closed. |
*/ |
ButterBar.prototype.hideError = function() { |
- if (this.visible_ && this.isError_) { |
- this.hide_(); |
- clearTimeout(this.hideTimeout_); |
+ if (this.isVisible_() && this.isError_()) { |
+ this.hide_(true /* force */); |
return true; |
} else { |
return false; |
@@ -153,10 +164,32 @@ ButterBar.prototype.hideError = function() { |
}; |
/** |
- * Init butter bar for showing copy progress. |
+ * Clear the show timeout if it is set. |
+ * @private |
+ */ |
+ButterBar.prototype.clearShowTimeout_ = function() { |
+ if (this.showTimeout_) { |
+ clearTimeout(this.hideTimeout_); |
+ this.showTimeout_ = null; |
+ } |
+}; |
+ |
+/** |
+ * Clear the hide timeout if it is set. |
+ * @private |
+ */ |
+ButterBar.prototype.clearHideTimeout_ = function() { |
+ if (this.hideTimeout_) { |
+ clearTimeout(this.hideTimeout_); |
+ this.hideTimeout_ = null; |
+ } |
+}; |
+ |
+/** |
+ * Set up butter bar for showing copy progress. |
* @private |
*/ |
-ButterBar.prototype.init_ = function() { |
+ButterBar.prototype.showProgress_ = function() { |
var progress = this.copyManager_.getProgress(); |
var options = {progress: progress.percentage, actions: {}, timeout: 0}; |
options.actions[str('CANCEL_LABEL')] = |
@@ -172,16 +205,19 @@ ButterBar.prototype.init_ = function() { |
ButterBar.prototype.onCopyProgress_ = function(event) { |
var progress = this.copyManager_.getProgress(); |
+ if (event.reason != 'PROGRESS') |
+ this.clearShowTimeout_(); |
+ |
switch (event.reason) { |
case 'BEGIN': |
- this.hide_(); |
- clearTimeout(this.timeout_); |
- // If the copy process lasts more than 500 ms, we show a progress bar. |
- this.showTimeout_ = setTimeout(this.init_.bind(this), 500); |
+ this.showTimeout_ = setTimeout(function() { |
+ this.showTimeout_ = null; |
+ this.showProgress_(); |
+ }.bind(this), 500); |
break; |
case 'PROGRESS': |
- if (this.visible_) { |
+ if (this.isVisible_()) { |
var options = {'progress': progress.percentage, timeout: 0}; |
this.update_(strf('PASTE_ITEMS_REMAINING', progress.pendingItems), |
options); |
@@ -189,7 +225,6 @@ ButterBar.prototype.onCopyProgress_ = function(event) { |
break; |
case 'SUCCESS': |
- clearTimeout(this.showTimeout_); |
this.hide_(); |
break; |
@@ -198,7 +233,6 @@ ButterBar.prototype.onCopyProgress_ = function(event) { |
break; |
case 'ERROR': |
- clearTimeout(this.showTimeout_); |
if (event.error.reason === 'TARGET_EXISTS') { |
var name = event.error.data.name; |
if (event.error.data.isDirectory) |