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 (!base.isAppsV2()) { 43 remoting.oauth2.doAuthRedirect(); 44 } 45 }; 46 var fixAuthError = function() { 47 if (base.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: '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 } 111 ]; 112 registerEventListeners(it2me_actions); 113 registerEventListeners(me2me_actions); 114 registerEventListeners(host_actions); 115 registerEventListeners(auth_actions); 116 remoting.init(); 117 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 123 // for both events. 124 remoting.fullscreen.addListener(remoting.onResize); 125 if (!base.isAppsV2()) { 126 window.addEventListener('beforeunload', remoting.promptClose, false); 127 window.addEventListener('unload', remoting.disconnect, false); 128 } 129} 130 131/** 132 * @param {Array.<{event: string, id: string, 133 * fn: function(Event):void}>} actions Array of actions to register. 134 */ 135function registerEventListeners(actions) { 136 for (var i = 0; i < actions.length; ++i) { 137 var action = actions[i]; 138 registerEventListener(action.id, action.event, action.fn); 139 } 140} 141 142/** 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. 147 */ 148function registerEventListener(id, eventname, fn) { 149 var element = document.getElementById(id); 150 if (element) { 151 element.addEventListener(eventname, fn, false); 152 } else { 153 console.error('Could not set ' + eventname + 154 ' event handler on element ' + id + 155 ': element not found.'); 156 } 157} 158 159window.addEventListener('load', onLoad, false); 160