- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / webapp / event_handlers.js
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} */
8 var remoting = remoting || {};
9
10 function 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       remoting.setMode(remoting.AppMode.HOME);
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   /** @param {Event} event The event. */
47   var stopDaemon = function(event) {
48     remoting.hostSetupDialog.showForStop();
49     event.stopPropagation();
50   };
51   var cancelAccessCode = function() {
52     remoting.setMode(remoting.AppMode.HOME);
53     document.getElementById('access-code-entry').value = '';
54   };
55   var newWindow = function() {
56     chrome.app.window.create('main.html', { 'width': 800, 'height': 600 });
57   };
58   /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
59   var actions = [
60       { event: 'click', id: 'sign-out', fn: remoting.signOut },
61       { event: 'click', id: 'toolbar-disconnect', fn: remoting.disconnect },
62       { event: 'click', id: 'send-ctrl-alt-del',
63         fn: remoting.sendCtrlAltDel },
64       { event: 'click', id: 'send-print-screen',
65         fn: remoting.sendPrintScreen },
66       { event: 'click', id: 'auth-button', fn: doAuthRedirect },
67       { event: 'click', id: 'share-button', fn: remoting.tryShare },
68       { event: 'click', id: 'access-mode-button', fn: goEnterAccessCode },
69       { event: 'click', id: 'cancel-share-button', fn: remoting.cancelShare },
70       { event: 'click', id: 'stop-sharing-button', fn: remoting.cancelShare },
71       { event: 'click', id: 'host-finished-button', fn: goHome },
72       { event: 'click', id: 'client-finished-it2me-button', fn: goHome },
73       { event: 'click', id: 'client-finished-me2me-button', fn: goHome },
74       { event: 'click', id: 'client-reconnect-button', fn: reconnect },
75       { event: 'click', id: 'cancel-access-code-button', fn: cancelAccessCode},
76       { event: 'click', id: 'cancel-connect-button', fn: goHome },
77       { event: 'click', id: 'toolbar-stub',
78         fn: function() { remoting.toolbar.toggle(); } },
79       { event: 'click', id: 'start-daemon',
80         fn: function() { remoting.hostSetupDialog.showForStart(); } },
81       { event: 'click', id: 'change-daemon-pin',
82         fn: function() { remoting.hostSetupDialog.showForPin(); } },
83       { event: 'click', id: 'stop-daemon', fn: stopDaemon },
84       { event: 'submit', id: 'access-code-form', fn: sendAccessCode },
85       { event: 'click', id: 'get-started-it2me',
86         fn: remoting.showIT2MeUiAndSave },
87       { event: 'click', id: 'get-started-me2me',
88         fn: remoting.showMe2MeUiAndSave },
89       { event: 'click', id: 'daemon-pin-cancel', fn: goHome },
90       { event: 'click', id: 'host-config-done-dismiss', fn: goHome },
91       { event: 'click', id: 'host-config-error-dismiss', fn: goHome },
92       { event: 'click', id: 'token-refresh-error-ok', fn: goHome },
93       { event: 'click', id: 'token-refresh-error-sign-in', fn: doAuthRedirect },
94       { event: 'click', id: 'open-paired-client-manager-dialog',
95         fn: remoting.setMode.bind(null,
96                                   remoting.AppMode.HOME_MANAGE_PAIRINGS) },
97       { event: 'click', id: 'close-paired-client-manager-dialog', fn: goHome },
98       { event: 'click', id: 'new-connection', fn: newWindow }
99   ];
100
101   for (var i = 0; i < actions.length; ++i) {
102     var action = actions[i];
103     var element = document.getElementById(action.id);
104     if (element) {
105       element.addEventListener(action.event, action.fn, false);
106     } else {
107       console.error('Could not set ' + action.event +
108                     ' event handler on element ' + action.id +
109                     ': element not found.');
110     }
111   }
112   remoting.init();
113
114   window.addEventListener('resize', remoting.onResize, false);
115   if (!remoting.isAppsV2) {
116     window.addEventListener('beforeunload', remoting.promptClose, false);
117     window.addEventListener('unload', remoting.disconnect, false);
118   }
119 }
120
121 window.addEventListener('load', onLoad, false);