1 // Copyright (c) 2012 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.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 var goHome = function() {
12 remoting.setMode(remoting.AppMode.HOME);
14 var goEnterAccessCode = function() {
15 // We don't need a token until we authenticate, but asking for one here
16 // handles the token-expired case earlier, avoiding asking the user for
17 // the access code both before and after re-authentication.
18 remoting.identity.callWithToken(
19 /** @param {string} token */
21 remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
23 remoting.showErrorMessage);
25 var goFinishedIT2Me = function() {
26 if (remoting.currentMode == remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME) {
27 remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
32 /** @param {Event} event The event. */
33 var sendAccessCode = function(event) {
34 remoting.connectIT2Me();
35 event.preventDefault();
37 var reconnect = function() {
38 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
39 remoting.connector.reconnect();
41 var doAuthRedirect = function() {
42 if (!base.isAppsV2()) {
43 remoting.oauth2.doAuthRedirect();
46 var fixAuthError = function() {
47 if (base.isAppsV2()) {
48 var onRefresh = function() {
49 remoting.hostList.display();
51 var refreshHostList = function() {
53 remoting.hostList.refresh(onRefresh);
55 remoting.identity.removeCachedAuthToken(refreshHostList);
60 /** @param {Event} event The event. */
61 var stopDaemon = function(event) {
62 remoting.hostSetupDialog.showForStop();
63 event.stopPropagation();
65 var cancelAccessCode = function() {
66 remoting.setMode(remoting.AppMode.HOME);
67 document.getElementById('access-code-entry').value = '';
69 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
71 { event: 'click', id: 'access-mode-button', fn: goEnterAccessCode },
72 { event: 'submit', id: 'access-code-form', fn: sendAccessCode },
73 { event: 'click', id: 'cancel-access-code-button', fn: cancelAccessCode},
74 { event: 'click', id: 'cancel-share-button', fn: remoting.cancelShare },
75 { event: 'click', id: 'client-finished-it2me-button', fn: goHome },
76 { event: 'click', id: 'get-started-it2me',
77 fn: remoting.showIT2MeUiAndSave },
78 { event: 'click', id: 'host-finished-button', fn: goHome },
79 { event: 'click', id: 'share-button', fn: remoting.tryShare }
81 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
83 { event: 'click', id: 'change-daemon-pin',
84 fn: function() { remoting.hostSetupDialog.showForPin(); } },
85 { event: 'click', id: 'client-finished-me2me-button', fn: goHome },
86 { event: 'click', id: 'client-reconnect-button', fn: reconnect },
87 { event: 'click', id: 'daemon-pin-cancel', fn: goHome },
88 { event: 'click', id: 'get-started-me2me',
89 fn: remoting.showMe2MeUiAndSave },
90 { event: 'click', id: 'start-daemon',
91 fn: function() { remoting.hostSetupDialog.showForStart(); } },
92 { event: 'click', id: 'stop-daemon', fn: stopDaemon }
94 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
96 { event: 'click', id: 'close-paired-client-manager-dialog', fn: goHome },
97 { event: 'click', id: 'host-config-done-dismiss', fn: goHome },
98 { event: 'click', id: 'host-config-error-dismiss', fn: goHome },
99 { event: 'click', id: 'open-paired-client-manager-dialog',
100 fn: remoting.setMode.bind(null,
101 remoting.AppMode.HOME_MANAGE_PAIRINGS) },
102 { event: 'click', id: 'stop-sharing-button', fn: remoting.cancelShare }
104 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
106 { event: 'click', id: 'auth-button', fn: doAuthRedirect },
107 { event: 'click', id: 'cancel-connect-button', fn: goHome },
108 { event: 'click', id: 'sign-out', fn:remoting.signOut },
109 { event: 'click', id: 'token-refresh-error-ok', fn: goHome },
110 { event: 'click', id: 'token-refresh-error-sign-in', fn: fixAuthError }
112 registerEventListeners(it2me_actions);
113 registerEventListeners(me2me_actions);
114 registerEventListeners(host_actions);
115 registerEventListeners(auth_actions);
118 window.addEventListener('resize', remoting.onResize, false);
119 // When a window goes full-screen, a resize event is triggered, but the
120 // Fullscreen.isActive call is not guaranteed to return true until the
121 // full-screen event is triggered. In apps v2, the size of the window's
122 // client area is calculated differently in full-screen mode, so register
124 remoting.fullscreen.addListener(remoting.onResize);
125 if (!base.isAppsV2()) {
126 window.addEventListener('beforeunload', remoting.promptClose, false);
127 window.addEventListener('unload', remoting.disconnect, false);
132 * @param {Array.<{event: string, id: string,
133 * fn: function(Event):void}>} actions Array of actions to register.
135 function registerEventListeners(actions) {
136 for (var i = 0; i < actions.length; ++i) {
137 var action = actions[i];
138 registerEventListener(action.id, action.event, action.fn);
143 * Add an event listener to the specified element.
144 * @param {string} id Id of element.
145 * @param {string} eventname Event name.
146 * @param {function(Event):void} fn Event handler.
148 function registerEventListener(id, eventname, fn) {
149 var element = document.getElementById(id);
151 element.addEventListener(eventname, fn, false);
153 console.error('Could not set ' + eventname +
154 ' event handler on element ' + id +
155 ': element not found.');
159 window.addEventListener('load', onLoad, false);