2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacamole-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
42 * Dynamic on-screen keyboard. Given the URL to an XML keyboard layout file,
43 * this object will download and use the XML to construct a clickable on-screen
44 * keyboard with its own key events.
47 * @param {String} url The URL of an XML keyboard layout file.
49 Guacamole.OnScreenKeyboard = function(url) {
51 // For each child of element, call handler defined in next
52 function parseChildren(element, next) {
54 var children = element.childNodes;
55 for (var i=0; i<children.length; i++) {
58 var child = children[i];
60 // Do not parse text nodes
64 // Get handler for node
65 var handler = next[child.tagName];
67 // Call handler if defined
71 // Throw exception if no handler
74 "Unexpected " + child.tagName
75 + " within " + element.tagName
83 var keyboard = document.createElement("div");
84 keyboard.className = "guacamole-keyboard";
86 // Retrieve keyboard XML
87 var xmlhttprequest = new XMLHttpRequest();
88 xmlhttprequest.open("GET", url, false);
89 xmlhttprequest.send(null);
91 var xml = xmlhttprequest.responseXML;
97 var unit = width / size;
99 function parse_row(e) {
101 var row = document.createElement("div");
102 row.className = "guacamole-keyboard-row";
106 "column": function(e) {
107 row.appendChild(parse_column(e));
110 "gap": function parse_gap(e) {
113 var gap_size = e.attributes["size"];
116 var gap = document.createElement("div");
117 gap.className = "guacamole-keyboard-gap";
118 gap.textContent = " ";
122 gap.style.width = gap.style.height =
123 parseFloat(gap_size.value)*unit + "px";
125 row.appendChild(gap);
129 "key": function parse_key(e) {
133 var key_size = e.attributes["size"];
135 // Create container element
136 var key_container = document.createElement("div");
137 key_container.className = "guacamole-keyboard-key-container";
138 key_container.style.display = "inline-block";
139 key_container.style.fontSize = unit + "px";
140 key_container.style.height = unit + "px";
141 key_container.style.lineHeight = unit + "px";
144 var key = document.createElement("div");
145 key.className = "guacamole-keyboard-key";
146 key_container.appendChild(key);
150 key_container.style.width = parseFloat(key_size.value)*unit + "px";
152 key_container.style.width = unit + "px";
155 "cap": function cap(e) {
158 var required = e.attributes["if"];
159 var modifier = e.attributes["modifier"];
160 var keysym = e.attributes["keysym"];
161 var sticky = e.attributes["sticky"];
163 // Get content of key cap
164 var content = e.textContent;
166 // If no requirements, then show cap by default
168 key.textContent = content;
174 row.appendChild(key_container);
184 function parse_column(e) {
186 var col = document.createElement("div");
187 col.className = "guacamole-keyboard-column";
189 var align = col.attributes["align"];
192 col.style.textAlign = align.value;
194 // Columns can only contain rows
197 col.appendChild(parse_row(e));
207 var keyboard_element = xml.documentElement;
208 if (keyboard_element.tagName != "keyboard")
209 throw new Error("Root element must be keyboard");
212 var keyboard_size = keyboard_element.attributes["size"];
214 parseChildren(keyboard_element, {
217 keyboard.appendChild(parse_row(e));
220 "column": function(e) {
221 keyboard.appendChild(parse_column(e));
228 // Do not allow selection or mouse movement to propagate/register.
229 keyboard.onselectstart =
230 keyboard.onmousemove =
232 keyboard.onmousedown =
239 this.onkeypressed = null;
240 this.onkeyreleased = null;
242 this.getElement = function() {
248 Guacamole.OnScreenKeyboard.Key = function() {
251 * Width of the key, relative to the size of the keyboard.
256 * Whether this key is currently pressed.
258 this.pressed = false;
261 * An associative map of all caps by modifier.
267 Guacamole.OnScreenKeyboard.Cap = function(text, keycode, modifier) {
270 * Modifier represented by this keycap
275 * The text to be displayed within this keycap
280 * The keycode this cap sends when its associated key is pressed/released
282 this.keycode = keycode;
284 // Set modifier if provided
285 if (modifier) this.modifier = modifier;