OLD | NEW |
---|---|
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 {{ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 this.classList.add(options.type); | 70 this.classList.add(options.type); |
71 this.actionLink_.textContent = options.actionText || 'Dismiss'; | 71 this.actionLink_.textContent = options.actionText || 'Dismiss'; |
72 | 72 |
73 this.actionLink_.addEventListener('click', function() { | 73 this.actionLink_.addEventListener('click', function() { |
74 if (options.action) options.action(); | 74 if (options.action) options.action(); |
75 this.dismiss(); | 75 this.dismiss(); |
76 }.bind(this)); | 76 }.bind(this)); |
77 }, | 77 }, |
78 | 78 |
79 /** | 79 /** |
80 * Shows the Snackbar. | 80 * Shows the Snackbar and dispatches the 'showed' event. |
81 */ | 81 */ |
82 show: function() { | 82 show: function() { |
83 this.classList.add('open'); | 83 this.classList.add('open'); |
84 if (Snackbar.hasContentFocus_) this.startTimeout_(); | 84 if (Snackbar.hasContentFocus_) this.startTimeout_(); |
85 else this.stopTimeout_(); | 85 else this.stopTimeout_(); |
86 | 86 |
87 document.addEventListener('contentfocus', this.boundStartTimeout_); | 87 document.addEventListener('contentfocus', this.boundStartTimeout_); |
88 document.addEventListener('contentblur', this.boundStopTimeout_); | 88 document.addEventListener('contentblur', this.boundStopTimeout_); |
89 this.dispatchEvent(new CustomEvent('showed')); | |
89 }, | 90 }, |
90 | 91 |
91 /** | 92 /** |
92 * Dismisses the Snackbar. Once the Snackbar is completely hidden, the | 93 * Dismisses the Snackbar. Once the Snackbar is completely hidden, the |
93 * 'dismissed' event is fired. | 94 * 'dismissed' event is fired and the returned Promise is resolved. If the |
95 * snackbar is already hidden, a resolved Promise is returned. | |
96 * @return {!Promise} | |
94 */ | 97 */ |
95 dismiss: function() { | 98 dismiss: function() { |
96 this.addEventListener('webkitTransitionEnd', function(event) { | 99 this.stopTimeout_(); |
97 if (event.propertyName === 'transform') | 100 |
98 this.dispatchEvent(new CustomEvent('dismissed')); | 101 if (!this.classList.contains('open')) { |
102 return Promise.resolve(); | |
103 } | |
104 | |
105 return new Promise(function(resolve) { | |
106 this.boundOnTransitionEnd_ = this.onTransitionEnd_(resolve).bind(this); | |
107 this.addEventListener( | |
108 'webkitTransitionEnd', this.boundOnTransitionEnd_); | |
109 | |
110 ensureTransitionEndEvent(this, 225); | |
scheib
2017/01/03 22:50:58
Wny dropping 'SHOW_DURATION'?
mbrunson
2017/01/04 00:41:22
The SHOW_DURATION value was actually the wrong num
| |
111 this.classList.remove('open'); | |
112 | |
113 document.removeEventListener('contentfocus', this.boundStartTimeout_); | |
114 document.removeEventListener('contentblur', this.boundStopTimeout_); | |
99 }.bind(this)); | 115 }.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 }, | 116 }, |
107 | 117 |
108 /** | 118 /** |
119 * Generates a function that dispatches the 'dismissed' event and removes | |
120 * itself from the webkitTransitionEnd event when called. | |
121 * @param {!function()} resolve Promise resolve function. | |
122 * @return {!function()} Function that is called on webkitTransitionEnd. | |
123 */ | |
124 onTransitionEnd_: function(resolve) { | |
125 return function() { | |
126 this.removeEventListener( | |
127 'webkitTransitionEnd', this.boundOnTransitionEnd_); | |
128 this.boundOnTransitionEnd_ = null; | |
129 this.dispatchEvent(new CustomEvent('dismissed')); | |
130 resolve(); | |
131 }; | |
132 }, | |
133 | |
134 /** | |
109 * Starts the timeout for dismissing the Snackbar. | 135 * Starts the timeout for dismissing the Snackbar. |
110 * @private | 136 * @private |
111 */ | 137 */ |
112 startTimeout_: function() { | 138 startTimeout_: function() { |
113 this.timeoutId_ = setTimeout(function() { | 139 this.timeoutId_ = setTimeout(function() { |
114 this.dismiss(); | 140 this.dismiss(); |
115 }.bind(this), SHOW_DURATION); | 141 }.bind(this), SHOW_DURATION); |
116 }, | 142 }, |
117 | 143 |
118 /** | 144 /** |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 }, 10); | 233 }, 10); |
208 }; | 234 }; |
209 | 235 |
210 /** | 236 /** |
211 * Dismisses the Snackbar currently showing. | 237 * Dismisses the Snackbar currently showing. |
212 * @param {boolean} clearQueue If true, clears the Snackbar queue before | 238 * @param {boolean} clearQueue If true, clears the Snackbar queue before |
213 * dismissing. | 239 * dismissing. |
214 */ | 240 */ |
215 Snackbar.dismiss = function(clearQueue) { | 241 Snackbar.dismiss = function(clearQueue) { |
216 if (clearQueue) Snackbar.queue_ = []; | 242 if (clearQueue) Snackbar.queue_ = []; |
217 if (Snackbar.current_) Snackbar.current_.dismiss(); | 243 if (Snackbar.current_) return Snackbar.current_.dismiss(); |
244 return Promise.resolve(); | |
218 }; | 245 }; |
219 | 246 |
220 | 247 |
221 | 248 |
222 return { | 249 return { |
223 Snackbar: Snackbar, | 250 Snackbar: Snackbar, |
224 SnackbarType: SnackbarType, | 251 SnackbarType: SnackbarType, |
225 }; | 252 }; |
226 }); | 253 }); |
OLD | NEW |