Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/snackbar.js

Issue 2602503002: bluetooth: Fix flaky bluetooth internals browser test. (Closed)
Patch Set: Fix up tests, clean up formatting Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * Javascript for Snackbar controls, served from chrome://bluetooth-internals/. 6 * Javascript for Snackbar controls, served from chrome://bluetooth-internals/.
7 */ 7 */
8 8
9 cr.define('snackbar', function() { 9 cr.define('snackbar', function() {
10 /** @typedef {{ 10 /** @typedef {{
11 * message: string, 11 * message: string,
12 * type: string, 12 * type: string,
13 * actionText: (string|undefined), 13 * actionText: (string|undefined),
14 * action: (function()|undefined) 14 * action: (function()|undefined)
15 * }} 15 * }}
16 */ 16 */
17 var SnackbarOptions; 17 var SnackbarOptions;
18 18
19 /** @const {number} */ var SHOW_DURATION = 5000; 19 /** @const {number} */ var SHOW_DURATION = 5000;
20 /** @const {number} */ var TRANSITION_DURATION = 225;
20 21
21 /** 22 /**
22 * Enum of Snackbar types. Used by Snackbar to determine the styling for the 23 * Enum of Snackbar types. Used by Snackbar to determine the styling for the
23 * Snackbar. 24 * Snackbar.
24 * @enum {string} 25 * @enum {string}
25 */ 26 */
26 var SnackbarType = { 27 var SnackbarType = {
27 INFO: 'info', 28 INFO: 'info',
28 SUCCESS: 'success', 29 SUCCESS: 'success',
29 WARNING: 'warning', 30 WARNING: 'warning',
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 this.classList.add(options.type); 71 this.classList.add(options.type);
71 this.actionLink_.textContent = options.actionText || 'Dismiss'; 72 this.actionLink_.textContent = options.actionText || 'Dismiss';
72 73
73 this.actionLink_.addEventListener('click', function() { 74 this.actionLink_.addEventListener('click', function() {
74 if (options.action) options.action(); 75 if (options.action) options.action();
75 this.dismiss(); 76 this.dismiss();
76 }.bind(this)); 77 }.bind(this));
77 }, 78 },
78 79
79 /** 80 /**
80 * Shows the Snackbar. 81 * Shows the Snackbar and dispatches the 'showed' event.
81 */ 82 */
82 show: function() { 83 show: function() {
83 this.classList.add('open'); 84 this.classList.add('open');
84 if (Snackbar.hasContentFocus_) this.startTimeout_(); 85 if (Snackbar.hasContentFocus_) this.startTimeout_();
85 else this.stopTimeout_(); 86 else this.stopTimeout_();
86 87
87 document.addEventListener('contentfocus', this.boundStartTimeout_); 88 document.addEventListener('contentfocus', this.boundStartTimeout_);
88 document.addEventListener('contentblur', this.boundStopTimeout_); 89 document.addEventListener('contentblur', this.boundStopTimeout_);
90 this.dispatchEvent(new CustomEvent('showed'));
89 }, 91 },
90 92
91 /** 93 /**
92 * Dismisses the Snackbar. Once the Snackbar is completely hidden, the 94 * Dismisses the Snackbar. Once the Snackbar is completely hidden, the
93 * 'dismissed' event is fired. 95 * 'dismissed' event is fired and the returned Promise is resolved. If the
96 * snackbar is already hidden, a resolved Promise is returned.
97 * @return {!Promise}
94 */ 98 */
95 dismiss: function() { 99 dismiss: function() {
96 this.addEventListener('webkitTransitionEnd', function(event) { 100 this.stopTimeout_();
97 if (event.propertyName === 'transform') 101
98 this.dispatchEvent(new CustomEvent('dismissed')); 102 if (!this.classList.contains('open')) return Promise.resolve();
Dan Beam 2017/01/04 22:28:59 this differs from our clang-format rules and will
mbrunson 2017/01/05 01:03:27 Done.
103
104 return new Promise(function(resolve) {
105 listenOnce(this, 'webkitTransitionEnd',
106 this.onTransitionEnd_(resolve).bind(this));
Dan Beam 2017/01/04 22:28:59 why not just listenOnce(this, 'webkitTransitionEn
mbrunson 2017/01/05 01:03:27 Done.
107
108 ensureTransitionEndEvent(this, TRANSITION_DURATION);
109 this.classList.remove('open');
110
111 document.removeEventListener('contentfocus', this.boundStartTimeout_);
112 document.removeEventListener('contentblur', this.boundStopTimeout_);
99 }.bind(this)); 113 }.bind(this));
100
101 ensureTransitionEndEvent(this, SHOW_DURATION);
102 this.classList.remove('open');
103
104 document.removeEventListener('contentfocus', this.boundStartTimeout_);
105 document.removeEventListener('contentblur', this.boundStopTimeout_);
106 }, 114 },
107 115
108 /** 116 /**
117 * Generates a function that dispatches the 'dismissed' event and removes
118 * itself from the webkitTransitionEnd event when called.
119 * @param {!function()} resolve Promise resolve function.
120 * @return {!function()} Function that is called on webkitTransitionEnd.
121 */
122 onTransitionEnd_: function(resolve) {
123 return function() {
124 this.dispatchEvent(new CustomEvent('dismissed'));
125 resolve();
126 };
127 },
128
129 /**
109 * Starts the timeout for dismissing the Snackbar. 130 * Starts the timeout for dismissing the Snackbar.
110 * @private 131 * @private
111 */ 132 */
112 startTimeout_: function() { 133 startTimeout_: function() {
113 this.timeoutId_ = setTimeout(function() { 134 this.timeoutId_ = setTimeout(function() {
114 this.dismiss(); 135 this.dismiss();
115 }.bind(this), SHOW_DURATION); 136 }.bind(this), SHOW_DURATION);
116 }, 137 },
117 138
118 /** 139 /**
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 }, 10); 228 }, 10);
208 }; 229 };
209 230
210 /** 231 /**
211 * Dismisses the Snackbar currently showing. 232 * Dismisses the Snackbar currently showing.
212 * @param {boolean} clearQueue If true, clears the Snackbar queue before 233 * @param {boolean} clearQueue If true, clears the Snackbar queue before
213 * dismissing. 234 * dismissing.
214 */ 235 */
215 Snackbar.dismiss = function(clearQueue) { 236 Snackbar.dismiss = function(clearQueue) {
216 if (clearQueue) Snackbar.queue_ = []; 237 if (clearQueue) Snackbar.queue_ = [];
217 if (Snackbar.current_) Snackbar.current_.dismiss(); 238 if (Snackbar.current_) return Snackbar.current_.dismiss();
239 return Promise.resolve();
218 }; 240 };
219 241
220 242
221 243
222 return { 244 return {
223 Snackbar: Snackbar, 245 Snackbar: Snackbar,
224 SnackbarType: SnackbarType, 246 SnackbarType: SnackbarType,
225 }; 247 };
226 }); 248 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698