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
3 // found in the LICENSE file.
5 /** @suppress {duplicate} */
6 var remoting
= remoting
|| {};
13 * A helper class for implementing dialogs with an input field using
16 * @param {remoting.AppMode} mode
17 * @param {HTMLElement} formElement
18 * @param {HTMLElement} inputField
19 * @param {HTMLElement} cancelButton
23 remoting
.InputDialog = function(mode
, formElement
, inputField
, cancelButton
) {
27 this.formElement_
= formElement
;
29 this.cancelButton_
= cancelButton
;
31 this.inputField_
= inputField
;
32 /** @private {base.Deferred} */
33 this.deferred_
= null;
34 /** @private {base.Disposables} */
35 this.eventHooks_
= null;
39 * @return {Promise<string>} Promise that resolves with the value of the
40 * inputField or rejects with |remoting.Error.CANCELLED| if the user clicks
41 * on the cancel button.
43 remoting
.InputDialog
.prototype.show = function() {
44 var onCancel
= this.createFormEventHandler_(this.onCancel_
.bind(this));
45 var onOk
= this.createFormEventHandler_(this.onSubmit_
.bind(this));
47 this.eventHooks_
= new base
.Disposables(
48 new base
.DomEventHook(this.formElement_
, 'submit', onOk
, false),
49 new base
.DomEventHook(this.cancelButton_
, 'click', onCancel
, false));
50 base
.debug
.assert(this.deferred_
=== null);
51 this.deferred_
= new base
.Deferred();
52 remoting
.setMode(this.appMode_
);
53 return this.deferred_
.promise();
57 remoting
.InputDialog
.prototype.onSubmit_ = function() {
58 this.deferred_
.resolve(this.inputField_
.value
);
62 remoting
.InputDialog
.prototype.onCancel_ = function() {
63 this.deferred_
.reject(new remoting
.Error(remoting
.Error
.Tag
.CANCELLED
));
67 * @param {function():void} handler
71 remoting
.InputDialog
.prototype.createFormEventHandler_ = function(handler
) {
73 return function (/** Event */ e
) {
74 // Prevents form submission from reloading the v1 app.
77 // Set the focus away from the password field. This has to be done
78 // before the password field gets hidden, to work around a Blink
79 // clipboard-handling bug - http://crbug.com/281523.
80 that
.cancelButton_
.focus();
82 base
.dispose(that
.eventHooks_
);
83 that
.eventHooks_
= null;
84 that
.deferred_
= null;