Index: chrome/browser/resources/bluetooth_internals/snackbar.js |
diff --git a/chrome/browser/resources/bluetooth_internals/snackbar.js b/chrome/browser/resources/bluetooth_internals/snackbar.js |
index 9cca49e23420cdf81f5cd01551ce2acb0a2e3e16..f01cddaf11594b7c6fa1fdb29fb217bfd61d0e80 100644 |
--- a/chrome/browser/resources/bluetooth_internals/snackbar.js |
+++ b/chrome/browser/resources/bluetooth_internals/snackbar.js |
@@ -17,6 +17,7 @@ cr.define('snackbar', function() { |
var SnackbarOptions; |
/** @const {number} */ var SHOW_DURATION = 5000; |
+ /** @const {number} */ var TRANSITION_DURATION = 225; |
/** |
* Enum of Snackbar types. Used by Snackbar to determine the styling for the |
@@ -77,7 +78,7 @@ cr.define('snackbar', function() { |
}, |
/** |
- * Shows the Snackbar. |
+ * Shows the Snackbar and dispatches the 'showed' event. |
*/ |
show: function() { |
this.classList.add('open'); |
@@ -86,23 +87,49 @@ cr.define('snackbar', function() { |
document.addEventListener('contentfocus', this.boundStartTimeout_); |
document.addEventListener('contentblur', this.boundStopTimeout_); |
+ this.dispatchEvent(new CustomEvent('showed')); |
}, |
/** |
* Dismisses the Snackbar. Once the Snackbar is completely hidden, the |
- * 'dismissed' event is fired. |
+ * 'dismissed' event is fired and the returned Promise is resolved. If the |
+ * snackbar is already hidden, a resolved Promise is returned. |
+ * @return {!Promise} |
*/ |
dismiss: function() { |
- this.addEventListener('webkitTransitionEnd', function(event) { |
- if (event.propertyName === 'transform') |
- this.dispatchEvent(new CustomEvent('dismissed')); |
- }.bind(this)); |
+ this.stopTimeout_(); |
+ |
+ if (!this.classList.contains('open')) { |
+ return Promise.resolve(); |
+ } |
Dan Beam
2017/01/04 01:44:09
no curlies
mbrunson
2017/01/04 03:46:19
Done.
|
- ensureTransitionEndEvent(this, SHOW_DURATION); |
- this.classList.remove('open'); |
+ return new Promise(function(resolve) { |
+ this.boundOnTransitionEnd_ = this.onTransitionEnd_(resolve).bind(this); |
Dan Beam
2017/01/04 01:44:09
nit: can we use listenOnce from util.js instead?
mbrunson
2017/01/04 03:46:19
Done.
|
+ this.addEventListener( |
+ 'webkitTransitionEnd', this.boundOnTransitionEnd_); |
- document.removeEventListener('contentfocus', this.boundStartTimeout_); |
- document.removeEventListener('contentblur', this.boundStopTimeout_); |
+ ensureTransitionEndEvent(this, TRANSITION_DURATION); |
+ this.classList.remove('open'); |
+ |
+ document.removeEventListener('contentfocus', this.boundStartTimeout_); |
+ document.removeEventListener('contentblur', this.boundStopTimeout_); |
+ }.bind(this)); |
+ }, |
+ |
+ /** |
+ * Generates a function that dispatches the 'dismissed' event and removes |
+ * itself from the webkitTransitionEnd event when called. |
+ * @param {!function()} resolve Promise resolve function. |
+ * @return {!function()} Function that is called on webkitTransitionEnd. |
+ */ |
+ onTransitionEnd_: function(resolve) { |
+ return function() { |
+ this.removeEventListener( |
+ 'webkitTransitionEnd', this.boundOnTransitionEnd_); |
+ this.boundOnTransitionEnd_ = null; |
+ this.dispatchEvent(new CustomEvent('dismissed')); |
+ resolve(); |
+ }; |
}, |
/** |
@@ -214,7 +241,8 @@ cr.define('snackbar', function() { |
*/ |
Snackbar.dismiss = function(clearQueue) { |
if (clearQueue) Snackbar.queue_ = []; |
- if (Snackbar.current_) Snackbar.current_.dismiss(); |
+ if (Snackbar.current_) return Snackbar.current_.dismiss(); |
+ return Promise.resolve(); |
}; |