Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Javascript for Snackbar controls, served from chrome://bluetooth-internals/. | |
| 7 */ | |
| 8 | |
| 9 cr.define('snackbar', function() { | |
| 10 /** @typedef {{ | |
| 11 * message: string, | |
| 12 * type: string, | |
| 13 * actionText: string|undefined, | |
|
Dan Beam
2016/12/16 08:42:58
I think you need parens around, i.e. (string|undef
mbrunson
2016/12/16 21:42:30
Done.
| |
| 14 * action: function()|undefined | |
| 15 * }} | |
| 16 */ | |
| 17 var SnackbarOptions; | |
| 18 | |
| 19 /** @const {number} */ var SHOW_DURATION = 5000; | |
| 20 | |
| 21 /** | |
| 22 * Enum of Snackbar types. Used by Snackbar to determine the styling for the | |
| 23 * Snackbar. | |
| 24 * @enum {string} | |
| 25 */ | |
| 26 var SnackbarType = { | |
| 27 INFO: 'info', | |
| 28 SUCCESS: 'success', | |
| 29 WARNING: 'warning', | |
| 30 ERROR: 'error', | |
| 31 }; | |
| 32 | |
| 33 /** | |
| 34 * Notification bar for displaying a simple message with an action link. | |
| 35 * This element should not be instantiated directly. Instead, users should | |
| 36 * use the Snackbar.show and Snackbar.dismiss functions to ensure proper | |
| 37 * queuing of messages. | |
| 38 */ | |
| 39 var Snackbar = cr.ui.define('div'); | |
| 40 | |
| 41 Snackbar.prototype = { | |
| 42 __proto__: HTMLDivElement.prototype, | |
| 43 | |
| 44 /** | |
| 45 * Decorates an element as a UI element class. Creates the message div and | |
| 46 * action link for the new Snackbar. | |
| 47 */ | |
| 48 decorate: function() { | |
| 49 this.classList.add('snackbar'); | |
| 50 this.messageDiv_ = document.createElement('div'); | |
| 51 this.appendChild(this.messageDiv_); | |
| 52 this.actionLink_ = document.createElement('a', 'action-link'); | |
| 53 this.appendChild(this.actionLink_); | |
| 54 | |
| 55 this.timeoutFn_ = null; | |
| 56 this.addEventListener('mouseleave', this.startTimeout_.bind(this)); | |
| 57 this.addEventListener('mouseenter', this.stopTimeout_.bind(this)); | |
| 58 | |
| 59 document.addEventListener('contentfocus', this.startTimeout_.bind(this)); | |
| 60 document.addEventListener('contentblur', this.stopTimeout_.bind(this)); | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * Initializes the content of the Snackbar with the given |options| | |
| 65 * including the message, action link text, and click action of the link. | |
| 66 * @param {!SnackbarOptions} options | |
| 67 */ | |
| 68 initialize: function(options) { | |
| 69 this.messageDiv_.textContent = options.message; | |
| 70 this.classList.add(options.type); | |
| 71 | |
| 72 if (options.actionText) { | |
| 73 this.actionLink_.textContent = options.actionText; | |
| 74 } else { | |
| 75 this.actionLink_.textContent = 'Dismiss'; | |
| 76 } | |
|
Dan Beam
2016/12/16 08:42:58
no curlies, also:
this.actionLink_.textContent =
mbrunson
2016/12/16 21:42:30
Done.
| |
| 77 | |
| 78 if (options.action) { | |
| 79 this.actionLink_.addEventListener('click', function() { | |
| 80 options.action(); | |
| 81 this.dismiss(); | |
| 82 }.bind(this)); | |
| 83 } else { | |
| 84 this.actionLink_.addEventListener('click', this.dismiss.bind(this)); | |
|
Dan Beam
2016/12/16 08:42:58
nit:
this.actionLink_.addEventListener('click', f
mbrunson
2016/12/16 21:42:30
Done.
| |
| 85 } | |
| 86 }, | |
| 87 | |
| 88 /** | |
| 89 * Shows the Snackbar. | |
| 90 */ | |
| 91 show: function() { | |
| 92 this.classList.add('open'); | |
| 93 this.startTimeout_(); | |
| 94 }, | |
| 95 | |
| 96 /** | |
| 97 * Dismisses the Snackbar. Once the Snackbar is completely hidden, the | |
| 98 * 'dismissed' event is fired. | |
| 99 */ | |
| 100 dismiss: function() { | |
| 101 this.addEventListener('webkitTransitionEnd', function(event) { | |
| 102 if (event.propertyName === 'transform') { | |
| 103 this.dispatchEvent(new CustomEvent('dismissed')); | |
| 104 } | |
|
Dan Beam
2016/12/16 08:42:58
nit: no curlies
mbrunson
2016/12/16 21:42:30
Done.
| |
| 105 }.bind(this)); | |
| 106 | |
| 107 ensureTransitionEndEvent(this, SHOW_DURATION); | |
| 108 this.classList.remove('open'); | |
| 109 }, | |
| 110 | |
| 111 /** | |
| 112 * Starts the timeout for dismissing the Snackbar. | |
| 113 * @private | |
| 114 */ | |
| 115 startTimeout_: function() { | |
| 116 this.timeoutFn_ = setTimeout(function() { | |
|
Dan Beam
2016/12/16 08:42:58
timeoutFn_ -> startTimeoutInterval_? this type is
mbrunson
2016/12/16 21:42:30
Ah yes. Since setTimeout returns an ID, I should c
| |
| 117 this.dismiss(); | |
| 118 }.bind(this), SHOW_DURATION); | |
| 119 }, | |
| 120 | |
| 121 /** | |
| 122 * Stops the timeout for dismissing the Snackbar. Only clears the timeout | |
| 123 * when the Snackbar is open. | |
| 124 * @private | |
| 125 */ | |
| 126 stopTimeout_: function() { | |
| 127 if (this.classList.contains('open')) { | |
| 128 clearTimeout(this.timeoutFn_); | |
| 129 this.timeoutFn_ = null; | |
| 130 } | |
| 131 }, | |
| 132 }; | |
| 133 | |
| 134 /** @private {Snackbar} */ | |
|
Dan Beam
2016/12/16 08:42:58
nit: ?Snackbar
mbrunson
2016/12/16 21:42:30
Done.
| |
| 135 Snackbar.current_ = null; | |
| 136 | |
| 137 /** @private {!Array<!SnackbarOptions>} */ | |
| 138 Snackbar.queue_ = []; | |
|
Dan Beam
2016/12/16 08:42:58
do you actually need to queue these right now? wh
mbrunson
2016/12/16 21:42:30
A user may attempt to inspect multiple devices at
| |
| 139 | |
| 140 /** | |
| 141 * Creates a Snackbar and shows it if one is not showing already. If a | |
| 142 * Snackbar is already active, the next Snackbar is queued. | |
| 143 * @param {string} message The message to display in the Snackbar. | |
| 144 * @param {string=} opt_type A string determining the Snackbar type: info, | |
| 145 * success, warning, error. If not provided, info type is used. | |
| 146 * @param {string=} opt_actionText The text to display for the action link. | |
| 147 * @param {function()=} opt_action A function to be called when the user | |
| 148 * presses the action link. | |
| 149 * @return {!Snackbar} | |
| 150 */ | |
| 151 Snackbar.show = function(message, opt_type, opt_actionText, opt_action) { | |
| 152 var options = { | |
| 153 message: message, | |
| 154 type: opt_type || SnackbarType.INFO, | |
| 155 actionText: opt_actionText, | |
| 156 action: opt_action, | |
| 157 }; | |
| 158 | |
| 159 var newSnackbar = new Snackbar(); | |
| 160 newSnackbar.initialize(options); | |
| 161 | |
| 162 if (Snackbar.current_) { | |
| 163 Snackbar.queue_.push(newSnackbar); | |
| 164 return newSnackbar; | |
| 165 } | |
| 166 | |
| 167 Snackbar.show_(newSnackbar); | |
| 168 return newSnackbar; | |
| 169 }; | |
| 170 | |
| 171 /** | |
| 172 * Creates a Snackbar and sets events for queuing the next Snackbar to show. | |
| 173 * @param {!Snackbar} newSnackbar | |
| 174 * @private | |
| 175 */ | |
| 176 Snackbar.show_ = function(newSnackbar) { | |
| 177 $('snackbar-container').appendChild(newSnackbar); | |
| 178 | |
| 179 newSnackbar.addEventListener('dismissed', function() { | |
| 180 $('snackbar-container').removeChild(Snackbar.current_); | |
| 181 | |
| 182 var newSnackbar = Snackbar.queue_.shift(); | |
| 183 if (newSnackbar) { | |
| 184 Snackbar.show_(newSnackbar); | |
| 185 return; | |
| 186 } | |
| 187 | |
| 188 Snackbar.current_ = null; | |
| 189 }); | |
| 190 | |
| 191 Snackbar.current_ = newSnackbar; | |
| 192 | |
| 193 // Show the Snackbar after a slight delay to allow for a layout reflow. | |
| 194 setTimeout(function() { | |
| 195 newSnackbar.show(); | |
| 196 }, 10); | |
| 197 }; | |
| 198 | |
| 199 /** | |
| 200 * Dismisses the Snackbar currently showing. | |
| 201 * @param {boolean} clearQueue If true, clears the Snackbar queue before | |
| 202 * dismissing. | |
| 203 */ | |
| 204 Snackbar.dismiss = function(clearQueue) { | |
| 205 if (clearQueue) Snackbar.queue_ = []; | |
| 206 if (Snackbar.current_) Snackbar.current_.dismiss(); | |
| 207 }; | |
| 208 | |
| 209 return { | |
| 210 Snackbar: Snackbar, | |
| 211 SnackbarType: SnackbarType, | |
| 212 }; | |
| 213 }); | |
| OLD | NEW |