Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / webapp / event_handlers.js
blobbcbc2010ab8823ec82fd493dc930309a1a482d9f
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 function onLoad() {
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 */
20 function(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);
28 } else {
29 goHome();
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 (!remoting.isAppsV2) {
43 remoting.oauth2.doAuthRedirect();
46 /** @param {Event} event The event. */
47 var stopDaemon = function(event) {
48 remoting.hostSetupDialog.showForStop();
49 event.stopPropagation();
51 var cancelAccessCode = function() {
52 remoting.setMode(remoting.AppMode.HOME);
53 document.getElementById('access-code-entry').value = '';
55 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
56 var it2me_actions = [
57 { event: 'click', id: 'access-mode-button', fn: goEnterAccessCode },
58 { event: 'submit', id: 'access-code-form', fn: sendAccessCode },
59 { event: 'click', id: 'cancel-access-code-button', fn: cancelAccessCode},
60 { event: 'click', id: 'cancel-share-button', fn: remoting.cancelShare },
61 { event: 'click', id: 'client-finished-it2me-button', fn: goHome },
62 { event: 'click', id: 'get-started-it2me',
63 fn: remoting.showIT2MeUiAndSave },
64 { event: 'click', id: 'host-finished-button', fn: goHome },
65 { event: 'click', id: 'share-button', fn: remoting.tryShare }
67 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
68 var me2me_actions = [
69 { event: 'click', id: 'change-daemon-pin',
70 fn: function() { remoting.hostSetupDialog.showForPin(); } },
71 { event: 'click', id: 'client-finished-me2me-button', fn: goHome },
72 { event: 'click', id: 'client-reconnect-button', fn: reconnect },
73 { event: 'click', id: 'daemon-pin-cancel', fn: goHome },
74 { event: 'click', id: 'get-started-me2me',
75 fn: remoting.showMe2MeUiAndSave },
76 { event: 'click', id: 'start-daemon',
77 fn: function() { remoting.hostSetupDialog.showForStart(); } },
78 { event: 'click', id: 'stop-daemon', fn: stopDaemon }
80 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
81 var host_actions = [
82 { event: 'click', id: 'close-paired-client-manager-dialog', fn: goHome },
83 { event: 'click', id: 'host-config-done-dismiss', fn: goHome },
84 { event: 'click', id: 'host-config-error-dismiss', fn: goHome },
85 { event: 'click', id: 'open-paired-client-manager-dialog',
86 fn: remoting.setMode.bind(null,
87 remoting.AppMode.HOME_MANAGE_PAIRINGS) },
88 { event: 'click', id: 'stop-sharing-button', fn: remoting.cancelShare }
90 /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
91 var auth_actions = [
92 { event: 'click', id: 'auth-button', fn: doAuthRedirect },
93 { event: 'click', id: 'cancel-connect-button', fn: goHome },
94 { event: 'click', id: 'token-refresh-error-ok', fn: goHome },
95 { event: 'click', id: 'token-refresh-error-sign-in', fn: doAuthRedirect }
97 registerEventListeners(it2me_actions);
98 registerEventListeners(me2me_actions);
99 registerEventListeners(host_actions);
100 registerEventListeners(auth_actions);
101 remoting.init();
103 window.addEventListener('resize', remoting.onResize, false);
104 if (!remoting.isAppsV2) {
105 window.addEventListener('beforeunload', remoting.promptClose, false);
106 window.addEventListener('unload', remoting.disconnect, false);
111 * @param {Array.<{event: string, id: string,
112 * fn: function(Event):void}>} actions Array of actions to register.
114 function registerEventListeners(actions) {
115 for (var i = 0; i < actions.length; ++i) {
116 var action = actions[i];
117 registerEventListener(action.id, action.event, action.fn);
122 * Add an event listener to the specified element.
123 * @param {string} id Id of element.
124 * @param {string} eventname Event name.
125 * @param {function(Event):void} fn Event handler.
127 function registerEventListener(id, eventname, fn) {
128 var element = document.getElementById(id);
129 if (element) {
130 element.addEventListener(eventname, fn, false);
131 } else {
132 console.error('Could not set ' + eventname +
133 ' event handler on element ' + id +
134 ': element not found.');
138 window.addEventListener('load', onLoad, false);