From e461df3523344ac25a3d472864e76aaad81da85c Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 6 Nov 2012 09:37:52 -0800 Subject: [PATCH] Organize root UI into separate JavaScript and HTML files. --- guacamole/src/main/webapp/index.xhtml | 242 +----------- guacamole/src/main/webapp/scripts/root-ui.js | 390 +++++++++++++++++++ 2 files changed, 391 insertions(+), 241 deletions(-) create mode 100644 guacamole/src/main/webapp/scripts/root-ui.js diff --git a/guacamole/src/main/webapp/index.xhtml b/guacamole/src/main/webapp/index.xhtml index 470c7d46b..902012bbd 100644 --- a/guacamole/src/main/webapp/index.xhtml +++ b/guacamole/src/main/webapp/index.xhtml @@ -138,247 +138,7 @@ - - - + diff --git a/guacamole/src/main/webapp/scripts/root-ui.js b/guacamole/src/main/webapp/scripts/root-ui.js new file mode 100644 index 000000000..60546e510 --- /dev/null +++ b/guacamole/src/main/webapp/scripts/root-ui.js @@ -0,0 +1,390 @@ + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/** + * General set of UI elements and UI-related functions regarding user login and + * connection management. + */ +var GuacamoleRootUI = { + + "sections": { + "login_form" : document.getElementById("login-form"), + "recent_connections" : document.getElementById("recent-connections"), + "other_connections" : document.getElementById("other-connections") + }, + + "messages": { + "login_error" : document.getElementById("login-error"), + "no_recent_connections" : document.getElementById("no-recent") + }, + + "fields": { + "username" : document.getElementById("username"), + "password" : document.getElementById("password"), + "clipboard" : document.getElementById("clipboard") + }, + + "buttons": { + "login" : document.getElementById("login"), + "logout" : document.getElementById("logout") + }, + + "settings": { + "auto_fit" : document.getElementById("auto-fit"), + "disable_sound" : document.getElementById("disable-sound") + }, + + "views": { + "login" : document.getElementById("login-ui"), + "connections" : document.getElementById("connection-list-ui") + }, + + "session_state" : new GuacamoleSessionState() + +}; + +/** + * Attempts to login the given user using the given password, throwing an + * error if the process fails. + * + * @param {String} username The name of the user to login as. + * @param {String} password The password to use to authenticate the user. + */ +GuacamoleRootUI.login = function(username, password) { + + // Get parameters from query string + var parameters = window.location.search.substring(1); + + // Get username and password from form + var data = + "username=" + encodeURIComponent(username) + + "&password=" + encodeURIComponent(password) + + // Include query parameters in submission data + if (parameters) data += "&" + parameters; + + // Log in + var xhr = new XMLHttpRequest(); + xhr.open("POST", "login", false); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.send(data); + + // Handle failures + if (xhr.status != 200) + throw new Error("Invalid login"); + +}; + +/** + * Set of thumbnails for each connection, indexed by ID. + */ +GuacamoleRootUI.Thumbnails = new (function() { + + var thumbnails = + JSON.parse(localStorage.getItem("GUAC_THUMBNAILS") || "{}"); + + /** + * Returns the URL for the thumbnail of the connection with the given ID, + * or undefined if no thumbnail is associated with that connection. + */ + this.getURL = function(id) { + return thumbnails[id]; + }; + +}); + +/** + * A connection UI object which can be easily added to a list of connections + * for sake of display. + */ +GuacamoleRootUI.Connection = function(id, protocol_name) { + + function element(tagname, classname) { + var new_element = document.createElement(tagname); + new_element.className = classname; + return new_element; + } + + // Create connection display elements + var connection = element("div", "connection"); + var caption = element("div", "caption"); + var protocol = element("div", "protocol"); + var name = element("span", "name"); + var protocol_icon = element("div", "icon " + protocol_name); + var thumbnail = element("div", "thumbnail"); + + // Get URL + var url = "client.xhtml?id=" + encodeURIComponent(id); + + // Create link to client + connection.onclick = function() { + + // Attempt to focus existing window + var current = window.open(null, id); + + // If window did not already exist, set up as + // Guacamole client + if (!current.GuacamoleUI) + window.open(url, id); + + }; + + // Add icon + protocol.appendChild(protocol_icon); + + // Set name + name.textContent = id; + + // Assemble caption + caption.appendChild(protocol); + caption.appendChild(name); + + // Assemble connection icon + connection.appendChild(thumbnail); + connection.appendChild(caption); + + // Add screenshot if available + var thumbnail_url = GuacamoleRootUI.Thumbnails.getURL(id); + if (thumbnail_url) { + + // Create thumbnail element + var img = document.createElement("img"); + img.src = thumbnail_url; + thumbnail.appendChild(img); + + } + + /** + * Returns the DOM element representing this connection. + */ + this.getElement = function() { + return connection; + }; + + /** + * Returns whether this connection has an associated thumbnail. + */ + this.hasThumbnail = function() { + return thumbnail_url && true; + }; + +}; + +/** + * Resets the interface such that the login UI is displayed if + * the user is not authenticated (or authentication fails) and + * the connection list UI (or the client for the only available + * connection, if there is only one) is displayed if the user is + * authenticated. + */ +GuacamoleRootUI.reset = function() { + + // Get parameters from query string + var parameters = window.location.search.substring(1); + + // Read configs + var configs; + try { + configs = getConfigList(parameters); + } + catch (e) { + + // Show login UI if unable to get configs + GuacamoleRootUI.views.login.style.display = ""; + GuacamoleRootUI.views.connections.style.display = "none"; + + return; + + } + + // Add connection icons + for (var i=0; i