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.
4
5'use strict';
6
7/** @suppress {duplicate} */
8var remoting = remoting || {};
9
10function onLoad() {
11  var goHome = function() {
12    remoting.setMode(remoting.AppMode.HOME);
13  };
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 */
20        function(token) {
21          remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
22        },
23        remoting.showErrorMessage);
24  };
25  var goFinishedIT2Me = function() {
26    if (remoting.currentMode == remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME) {
27      remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
28    } else {
29      goHome();
30    }
31  };
32  /** @param {Event} event The event. */
33  var sendAccessCode = function(event) {
34    remoting.connectIT2Me();
35    event.preventDefault();
36  };
37  var reconnect = function() {
38    remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
39    remoting.connector.reconnect();
40  };
41  var doAuthRedirect = function() {
42    if (!remoting.isAppsV2) {
43      remoting.oauth2.doAuthRedirect();
44    }
45  };
46  var fixAuthError = function() {
47    if (remoting.isAppsV2) {
48      var onRefresh = function() {
49        remoting.hostList.display();
50      };
51      var refreshHostList = function() {
52        goHome();
53        remoting.hostList.refresh(onRefresh);
54      };
55      remoting.identity.removeCachedAuthToken(refreshHostList);
56    } else {
57      doAuthRedirect();
58    }
59  };
60  /** @param {Event} event The event. */
61  var stopDaemon = function(event) {
62    remoting.hostSetupDialog.showForStop();
63    event.stopPropagation();
64  };
65  var cancelAccessCode = function() {
66    remoting.setMode(remoting.AppMode.HOME);
67    document.getElementById('access-code-entry').value = '';
68  };
69  /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
70  var it2me_actions = [
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 }
80  ];
81  /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
82  var me2me_actions = [
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 }
93  ];
94  /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
95  var host_actions = [
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 }
103  ];
104  /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
105  var auth_actions = [
106      { event: 'click', id: 'auth-button', fn: doAuthRedirect },
107      { event: 'click', id: 'cancel-connect-button', fn: goHome },
108      { event: 'click', id: 'token-refresh-error-ok', fn: goHome },
109      { event: 'click', id: 'token-refresh-error-sign-in', fn: fixAuthError }
110  ];
111  registerEventListeners(it2me_actions);
112  registerEventListeners(me2me_actions);
113  registerEventListeners(host_actions);
114  registerEventListeners(auth_actions);
115  remoting.init();
116
117  window.addEventListener('resize', remoting.onResize, false);
118  // When a window goes full-screen, a resize event is triggered, but the
119  // Fullscreen.isActive call is not guaranteed to return true until the
120  // full-screen event is triggered. In apps v2, the size of the window's
121  // client area is calculated differently in full-screen mode, so register
122  // for both events.
123  remoting.fullscreen.addListener(remoting.onResize);
124  if (!remoting.isAppsV2) {
125    window.addEventListener('beforeunload', remoting.promptClose, false);
126    window.addEventListener('unload', remoting.disconnect, false);
127  }
128}
129
130/**
131 * @param {Array.<{event: string, id: string,
132 *     fn: function(Event):void}>} actions Array of actions to register.
133 */
134function registerEventListeners(actions) {
135  for (var i = 0; i < actions.length; ++i) {
136    var action = actions[i];
137    registerEventListener(action.id, action.event, action.fn);
138  }
139}
140
141/**
142 * Add an event listener to the specified element.
143 * @param {string} id Id of element.
144 * @param {string} eventname Event name.
145 * @param {function(Event):void} fn Event handler.
146 */
147function registerEventListener(id, eventname, fn) {
148  var element = document.getElementById(id);
149  if (element) {
150    element.addEventListener(eventname, fn, false);
151  } else {
152    console.error('Could not set ' + eventname +
153        ' event handler on element ' + id +
154        ': element not found.');
155  }
156}
157
158window.addEventListener('load', onLoad, false);
159