1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
function onLoad() {
var goHome = function() {
remoting.setMode(remoting.AppMode.HOME);
};
var goEnterAccessCode = function() {
// We don't need a token until we authenticate, but asking for one here
// handles the token-expired case earlier, avoiding asking the user for
// the access code both before and after re-authentication.
remoting.identity.callWithToken(
/** @param {string} token */
function(token) {
remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
},
remoting.showErrorMessage);
};
var goFinishedIT2Me = function() {
if (remoting.currentMode == remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME) {
remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
} else {
goHome();
}
};
/** @param {Event} event The event. */
var sendAccessCode = function(event) {
remoting.connectIT2Me();
event.preventDefault();
};
var reconnect = function() {
remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
remoting.connector.reconnect();
};
var doAuthRedirect = function() {
if (!remoting.isAppsV2) {
remoting.oauth2.doAuthRedirect();
}
};
/** @param {Event} event The event. */
var stopDaemon = function(event) {
remoting.hostSetupDialog.showForStop();
event.stopPropagation();
};
var cancelAccessCode = function() {
remoting.setMode(remoting.AppMode.HOME);
document.getElementById('access-code-entry').value = '';
};
/** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
var it2me_actions = [
{ event: 'click', id: 'access-mode-button', fn: goEnterAccessCode },
{ event: 'submit', id: 'access-code-form', fn: sendAccessCode },
{ event: 'click', id: 'cancel-access-code-button', fn: cancelAccessCode},
{ event: 'click', id: 'cancel-share-button', fn: remoting.cancelShare },
{ event: 'click', id: 'client-finished-it2me-button', fn: goHome },
{ event: 'click', id: 'get-started-it2me',
fn: remoting.showIT2MeUiAndSave },
{ event: 'click', id: 'host-finished-button', fn: goHome },
{ event: 'click', id: 'share-button', fn: remoting.tryShare }
];
/** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
var me2me_actions = [
{ event: 'click', id: 'change-daemon-pin',
fn: function() { remoting.hostSetupDialog.showForPin(); } },
{ event: 'click', id: 'client-finished-me2me-button', fn: goHome },
{ event: 'click', id: 'client-reconnect-button', fn: reconnect },
{ event: 'click', id: 'daemon-pin-cancel', fn: goHome },
{ event: 'click', id: 'get-started-me2me',
fn: remoting.showMe2MeUiAndSave },
{ event: 'click', id: 'start-daemon',
fn: function() { remoting.hostSetupDialog.showForStart(); } },
{ event: 'click', id: 'stop-daemon', fn: stopDaemon }
];
/** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
var host_actions = [
{ event: 'click', id: 'close-paired-client-manager-dialog', fn: goHome },
{ event: 'click', id: 'host-config-done-dismiss', fn: goHome },
{ event: 'click', id: 'host-config-error-dismiss', fn: goHome },
{ event: 'click', id: 'open-paired-client-manager-dialog',
fn: remoting.setMode.bind(null,
remoting.AppMode.HOME_MANAGE_PAIRINGS) },
{ event: 'click', id: 'stop-sharing-button', fn: remoting.cancelShare }
];
/** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
var auth_actions = [
{ event: 'click', id: 'auth-button', fn: doAuthRedirect },
{ event: 'click', id: 'cancel-connect-button', fn: goHome },
{ event: 'click', id: 'token-refresh-error-ok', fn: goHome },
{ event: 'click', id: 'token-refresh-error-sign-in', fn: doAuthRedirect }
];
registerEventListeners(it2me_actions);
registerEventListeners(me2me_actions);
registerEventListeners(host_actions);
registerEventListeners(auth_actions);
remoting.init();
window.addEventListener('resize', remoting.onResize, false);
// When a window goes full-screen, a resize event is triggered, but the
// Fullscreen.isActive call is not guaranteed to return true until the
// full-screen event is triggered. In apps v2, the size of the window's
// client area is calculated differently in full-screen mode, so register
// for both events.
remoting.fullscreen.addListener(remoting.onResize);
if (!remoting.isAppsV2) {
window.addEventListener('beforeunload', remoting.promptClose, false);
window.addEventListener('unload', remoting.disconnect, false);
}
}
/**
* @param {Array.<{event: string, id: string,
* fn: function(Event):void}>} actions Array of actions to register.
*/
function registerEventListeners(actions) {
for (var i = 0; i < actions.length; ++i) {
var action = actions[i];
registerEventListener(action.id, action.event, action.fn);
}
}
/**
* Add an event listener to the specified element.
* @param {string} id Id of element.
* @param {string} eventname Event name.
* @param {function(Event):void} fn Event handler.
*/
function registerEventListener(id, eventname, fn) {
var element = document.getElementById(id);
if (element) {
element.addEventListener(eventname, fn, false);
} else {
console.error('Could not set ' + eventname +
' event handler on element ' + id +
': element not found.');
}
}
window.addEventListener('load', onLoad, false);
|