OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** @suppress {duplicate} */ | 5 /** @suppress {duplicate} */ |
6 var remoting = remoting || {}; | 6 var remoting = remoting || {}; |
7 | 7 |
8 (function() { | 8 (function() { |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 * @param {remoting.MessageDialog.Result} result | 151 * @param {remoting.MessageDialog.Result} result |
152 * @return {Function} | 152 * @return {Function} |
153 * @private | 153 * @private |
154 */ | 154 */ |
155 remoting.MessageDialog.prototype.onClicked_ = function(result) { | 155 remoting.MessageDialog.prototype.onClicked_ = function(result) { |
156 this.deferred_.resolve(result); | 156 this.deferred_.resolve(result); |
157 this.deferred_ = null; | 157 this.deferred_ = null; |
158 this.dispose(); | 158 this.dispose(); |
159 }; | 159 }; |
160 | 160 |
| 161 |
| 162 |
| 163 /** |
| 164 * A promise-based dialog implementation using the <dialog> element. |
| 165 * |
| 166 * @param {remoting.Html5ModalDialog.Params} params |
| 167 * @constructor |
| 168 * |
| 169 * @implements {remoting.WindowShape.ClientUI} |
| 170 * @implements {base.Disposable} |
| 171 */ |
| 172 remoting.Html5ModalDialog = function(params) { |
| 173 /** @private */ |
| 174 this.dialog_ = params.dialog; |
| 175 |
| 176 /** @private {base.Disposables} */ |
| 177 this.eventHooks_ = new base.Disposables( |
| 178 new base.DomEventHook( |
| 179 this.dialog_, 'cancel', this.onCancel_.bind(this), false), |
| 180 new base.DomEventHook( |
| 181 params.primaryButton, 'click', |
| 182 this.close.bind(this, remoting.MessageDialog.Result.PRIMARY), false) |
| 183 ); |
| 184 |
| 185 if (params.secondaryButton) { |
| 186 this.eventHooks_.add(new base.DomEventHook( |
| 187 params.secondaryButton, 'click', |
| 188 this.close.bind(this, remoting.MessageDialog.Result.SECONDARY), false)); |
| 189 } |
| 190 |
| 191 /** @private */ |
| 192 this.closeOnEscape_ = Boolean(params.closeOnEscape); |
| 193 |
| 194 /** @private {base.Deferred} */ |
| 195 this.deferred_ = null; |
| 196 }; |
| 197 |
| 198 remoting.Html5ModalDialog.prototype.dispose = function() { |
| 199 base.dispose(this.eventHooks_); |
| 200 this.eventHookes_ = null; |
| 201 }; |
| 202 |
| 203 /** |
| 204 * @return {Promise<remoting.MessageDialog.Result>} Promise that resolves with |
| 205 * the button clicked. |
| 206 */ |
| 207 remoting.Html5ModalDialog.prototype.show = function() { |
| 208 base.debug.assert(this.deferred_ === null); |
| 209 this.deferred_ = new base.Deferred(); |
| 210 this.dialog_.showModal(); |
| 211 remoting.windowShape.registerClientUI(this); |
| 212 remoting.windowShape.centerToDesktop(this.dialog_); |
| 213 return this.deferred_.promise(); |
| 214 }; |
| 215 |
| 216 /** @param {Event} e */ |
| 217 remoting.Html5ModalDialog.prototype.onCancel_ = function(e) { |
| 218 e.preventDefault(); |
| 219 if (this.closeOnEscape_) { |
| 220 this.close(remoting.MessageDialog.Result.CANCEL); |
| 221 } |
| 222 }; |
| 223 |
| 224 /** |
| 225 * @param {remoting.MessageDialog.Result} result |
| 226 */ |
| 227 remoting.Html5ModalDialog.prototype.close = function(result) { |
| 228 if (!this.dialog_.open) { |
| 229 return; |
| 230 } |
| 231 this.dialog_.close(); |
| 232 this.deferred_.resolve(result); |
| 233 this.deferred_ = null; |
| 234 remoting.windowShape.unregisterClientUI(this); |
| 235 }; |
| 236 |
| 237 remoting.Html5ModalDialog.prototype.addToRegion = function(rects) { |
| 238 var rect = /** @type {ClientRect} */(this.dialog_.getBoundingClientRect()); |
| 239 |
| 240 // If the dialog is repositioned by setting the left and top, it takes a while |
| 241 // for getBoundingClientRect() to update the rectangle. |
| 242 var left = this.dialog_.style.left; |
| 243 var top = this.dialog_.style.top; |
| 244 |
| 245 rects.push({ |
| 246 left: (left === '') ? rect.left : parseInt(left, 10), |
| 247 top: (top === '') ? rect.top : parseInt(top, 10), |
| 248 width: rect.width, |
| 249 height: rect.height |
| 250 }); |
| 251 }; |
| 252 |
| 253 |
161 /** | 254 /** |
162 * @param {Function} cancelCallback The callback to invoke when the user clicks | 255 * @param {Function} cancelCallback The callback to invoke when the user clicks |
163 * on the cancel button. | 256 * on the cancel button. |
164 * @constructor | 257 * @constructor |
165 */ | 258 */ |
166 remoting.ConnectingDialog = function(cancelCallback) { | 259 remoting.ConnectingDialog = function(cancelCallback) { |
167 /** @private */ | 260 /** @private */ |
168 this.dialog_ = new remoting.MessageDialog( | 261 this.dialog_ = new remoting.MessageDialog( |
169 remoting.AppMode.CLIENT_CONNECTING, | 262 remoting.AppMode.CLIENT_CONNECTING, |
170 document.getElementById('cancel-connect-button')); | 263 document.getElementById('cancel-connect-button')); |
(...skipping 16 matching lines...) Expand all Loading... |
187 | 280 |
188 })(); | 281 })(); |
189 | 282 |
190 /** | 283 /** |
191 * Define the enum at the end of file as JSCompile doesn't understand enums that | 284 * Define the enum at the end of file as JSCompile doesn't understand enums that |
192 * are defined within an IIFE (Immediately Invoked Function Expression). | 285 * are defined within an IIFE (Immediately Invoked Function Expression). |
193 * @enum {number} | 286 * @enum {number} |
194 */ | 287 */ |
195 remoting.MessageDialog.Result = { | 288 remoting.MessageDialog.Result = { |
196 PRIMARY: 0, | 289 PRIMARY: 0, |
197 SECONDARY: 1 | 290 SECONDARY: 1, |
198 }; | 291 CANCEL: 2 // User presses the escape button. |
| 292 }; |
| 293 |
| 294 /** |
| 295 * Parameters for the remoting.Html5ModalDialog constructor. Unless otherwise |
| 296 * noted, all parameters are optional. |
| 297 * |
| 298 * dialog: (required) The HTML dialog element. |
| 299 * |
| 300 * primaryButton: (required) The HTML element of the primary button. |
| 301 * |
| 302 * secondaryButton: The HTML element of the secondary button. |
| 303 * |
| 304 * closeOnEscape: Whether the user can dismiss the dialog by pressing the escape |
| 305 * key. Default to false. |
| 306 * |
| 307 * @typedef {{ |
| 308 * dialog: HTMLDialogElement, |
| 309 * primaryButton:HTMLElement, |
| 310 * secondaryButton:(HTMLElement|undefined), |
| 311 * closeOnEscape:(boolean|undefined) |
| 312 * }} |
| 313 */ |
| 314 remoting.Html5ModalDialog.Params; |
OLD | NEW |