Working multiple-config login stub.
[guacamole.git] / src / main / webapp / index.xhtml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html>
3
4 <!--
5     Guacamole - Clientless Remote Desktop
6     Copyright (C) 2010  Michael Jumper
7
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU Affero General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Affero General Public License for more details.
17
18     You should have received a copy of the GNU Affero General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 -->
21
22 <html xmlns="http://www.w3.org/1999/xhtml">
23
24     <head>
25         <link rel="icon" type="image/png" href="images/guacamole-logo-64.png"/>
26         <link rel="stylesheet" type="text/css" href="styles/login.css"/>
27         <title>Guacamole ${project.version}</title>
28     </head>
29
30     <body>
31
32         <div id="login-ui" style="display: none">
33             <div id="login-dialog-middle">
34
35                 <div id="login-dialog">
36
37                     <p id="login-error"></p>
38
39                     <form id="login-form" action="#" method="post">
40
41                         <div id="login-fields">
42                             <table>
43                                 <tr>
44                                     <th>Username</th>
45                                     <td><input type="text" name="username" id="username" autofocus="autofocus"/></td>
46                                 </tr>
47                                 <tr>
48                                     <th>Password</th>
49                                     <td><input type="password" name="password" id="password"/></td>
50                                 </tr>
51                             </table>
52
53                             <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
54                         </div>
55
56                         <div id="buttons">
57                             <input type="submit" name="login" id="login" value="Login"/>
58                         </div>
59
60                     </form>
61                 </div>
62
63             </div>
64         </div>
65
66         <!-- Connection list UI -->
67         <div id="connection-list-ui" style="display: none">
68
69             <h1>
70                 <img class="logo" src="images/guacamole-logo-64.png" alt=""/>
71                 Available Connections
72             </h1>
73             
74             <table class="connections">
75                 <thead>
76                     <tr>
77                         <th class="protocol">Protocol</th>
78                         <th class="name">Name</th>
79                     </tr>
80                 </thead>
81                 <tbody id="connections-tbody">
82                 </tbody>
83             </table>
84
85         </div>
86
87         <div id="version-dialog">
88             Guacamole ${project.version}
89         </div>
90
91         <!-- Init -->
92         <script type="text/javascript"> /* <![CDATA[ */
93
94             function Config(protocol, id) {
95                 this.protocol = protocol;
96                 this.id = id;
97             }
98
99             function getConfigList() {
100
101                 // Get config list
102                 var xhr = new XMLHttpRequest();
103                 xhr.open("GET", "configs", false);
104                 xhr.send(null);
105
106                 // If fail, throw error
107                 if (xhr.status != 200)
108                     throw new Error(xhr.statusText);
109
110                 // Otherwise, get list
111                 var configs = new Array();
112
113                 var configElements = xhr.responseXML.getElementsByTagName("config");
114                 for (var i=0; i<configElements.length; i++) {
115                     configs.push(new Config(
116                         configElements[i].getAttribute("protocol"),
117                         configElements[i].getAttribute("id")
118                     ));
119                 }
120
121                 return configs;
122                 
123             }
124
125             function resetUI() {
126
127                 var configs;
128                 try {
129                     configs = getConfigList();
130                 }
131                 catch (e) {
132
133                     console.log(e);
134
135                     // Show login UI if unable to get configs
136                     loginUI.style.display = "";
137                     connectionListUI.style.display = "none";
138
139                     return;
140
141                 }
142
143                 // Remove all rows from connections list
144                 var tbody = document.getElementById("connections-tbody");
145                 tbody.innerHTML = "";
146                 
147                 // Add one row per connection
148                 for (var i=0; i<configs.length; i++) {
149
150                     // Create row and cells
151                     var tr = document.createElement("tr");
152                     var protocol = document.createElement("td");
153                     var id = document.createElement("td");
154
155                     // Set CSS
156                     protocol.className = "protocol";
157                     id.className = "name";
158
159                     // Create link to client
160                     var clientLink = document.createElement("a");
161                     clientLink.setAttribute("href",
162                         "client.xhtml?" + encodeURIComponent(configs[i].id));
163
164                     // Set cell contents
165                     protocol.textContent   = configs[i].protocol;
166                     clientLink.textContent = configs[i].id;
167                     id.appendChild(clientLink);
168
169                     // Add cells
170                     tr.appendChild(protocol);
171                     tr.appendChild(id);
172
173                     // Add row
174                     tbody.appendChild(tr);
175
176                 }
177
178                 // If configs could be retrieved, display list
179                 loginUI.style.display = "none";
180                 connectionListUI.style.display = "";
181
182             }
183
184             var loginForm = document.getElementById("login-form");
185             var loginUI = document.getElementById("login-ui");
186             var connectionListUI = document.getElementById("connection-list-ui");
187
188             // TODO: Get connection list
189             // On no-auth fail, show login UI 
190
191             loginForm.onsubmit = function() {
192
193                 var username = document.getElementById("username");
194                 var password = document.getElementById("password");
195
196                 var data =
197                        "username=" + encodeURIComponent(username.value)
198                     + "&password=" + encodeURIComponent(password.value)
199
200                 try {
201
202                     // Log in
203                     var xhr = new XMLHttpRequest();
204                     xhr.open("POST", "login", false);
205                     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
206                     xhr.send(data);
207
208                     // Handle failures
209                     if (xhr.status != 200)
210                         throw new Error("Invalid login");
211
212                     resetUI();
213
214                 }
215                 catch (e) {
216
217                     var loginError = document.getElementById("login-error");
218
219                     // Display error, reset and refocus password field
220                     loginError.textContent = e.message;
221                     password.value = "";
222                     password.focus();
223
224                     return false;
225
226                 }
227
228                 // On success, hide loginUI, get and show connection list.
229                 return false;
230
231             }
232
233             resetUI();
234
235             /* ]]> */ </script>
236
237     </body>
238
239 </html>