/* * 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 . */ /** * Main Guacamole UI namespace. * @namespace */ var GuacUI = GuacUI || {}; /** * Current session state, including settings. */ GuacUI.sessionState = new GuacamoleSessionState(); /** * Creates a new element having the given tagname and CSS class. */ GuacUI.createElement = function(tagname, classname) { var new_element = document.createElement(tagname); if (classname) new_element.className = classname; return new_element; }; /** * Creates a new element having the given tagname, CSS class, and specified * parent element. */ GuacUI.createChildElement = function(parent, tagname, classname) { var element = GuacUI.createElement(tagname, classname); parent.appendChild(element); return element; }; /** * Creates a new row within the given table having a single header cell * with the given title, and a single value cell. The value cell is returned. */ GuacUI.createTabulatedContainer = function(table, title) { // Create elements var row = GuacUI.createChildElement(table, "tr"); var header = GuacUI.createChildElement(row, "th"); var cell = GuacUI.createChildElement(row, "td"); // Set title, return cell header.textContent = title; return cell; }; /** * Adds the given CSS class to the given element. */ GuacUI.addClass = function(element, classname) { // If supported, use native classlist for addClass() if (Node.classlist) element.classList.add(classname); // Otherwise, simply add new class via string manipulation else element.className += " " + classname; }; /** * Removes the given CSS class from the given element. */ GuacUI.removeClass = function(element, classname) { // If supported, use native classlist for removeClass() if (Node.classlist) element.classList.remove(classname); // Otherwise, remove class via string manipulation else { // Filter out classes with given name element.className = element.className.replace(/([^ ]+)[ ]*/g, function(match, testClassname, spaces, offset, string) { // If same class, remove if (testClassname == classname) return ""; // Otherwise, allow return match; } ); } // end if no classlist support }; /** * Object describing the UI's level of audio support. If the user has request * that audio be disabled, this object will pretend that audio is not * supported. */ GuacUI.Audio = new (function() { var codecs = [ 'audio/ogg; codecs="vorbis"', 'audio/mp4; codecs="mp4a.40.5"', 'audio/mpeg; codecs="mp3"', 'audio/webm; codecs="vorbis"', 'audio/wav; codecs=1' ]; var probably_supported = []; var maybe_supported = []; /** * Array of all supported audio mimetypes, ordered by liklihood of * working. */ this.supported = []; // If sound disabled, we're done now. if (GuacUI.sessionState.getProperty("disable-sound")) return; // Build array of supported audio formats codecs.forEach(function(mimetype) { var audio = new Audio(); var support_level = audio.canPlayType(mimetype); // Trim semicolon and trailer var semicolon = mimetype.indexOf(";"); if (semicolon != -1) mimetype = mimetype.substring(0, semicolon); // Partition by probably/maybe if (support_level == "probably") probably_supported.push(mimetype); else if (support_level == "maybe") maybe_supported.push(mimetype); }); // Add probably supported types first Array.prototype.push.apply( this.supported, probably_supported); // Prioritize "maybe" supported types second Array.prototype.push.apply( this.supported, maybe_supported); })(); /** * Object describing the UI's level of video support. */ GuacUI.Video = new (function() { var codecs = [ 'video/ogg; codecs="theora, vorbis"', 'video/mp4; codecs="avc1.4D401E, mp4a.40.5"', 'video/webm; codecs="vp8.0, vorbis"' ]; var probably_supported = []; var maybe_supported = []; /** * Array of all supported video mimetypes, ordered by liklihood of * working. */ this.supported = []; // Build array of supported audio formats codecs.forEach(function(mimetype) { var video = document.createElement("video"); var support_level = video.canPlayType(mimetype); // Trim semicolon and trailer var semicolon = mimetype.indexOf(";"); if (semicolon != -1) mimetype = mimetype.substring(0, semicolon); // Partition by probably/maybe if (support_level == "probably") probably_supported.push(mimetype); else if (support_level == "maybe") maybe_supported.push(mimetype); }); // Add probably supported types first Array.prototype.push.apply( this.supported, probably_supported); // Prioritize "maybe" supported types second Array.prototype.push.apply( this.supported, maybe_supported); })(); /** * Central registry of all components for all states. */ GuacUI.StateManager = new (function() { /** * The current state. */ var current_state = null; /** * Array of arrays of components, indexed by the states they are in. */ var components = []; /** * Registers the given component with this state manager, to be shown * during the given states. * * @param {GuacUI.Component} component The component to register. * @param {Number} [...] The list of states this component should be * visible during. */ this.registerComponent = function(component) { // For each state specified, add the given component for (var i=1; i