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 guac-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 ***** */
39 * Namespace for all Guacamole JavaScript objects.
42 var Guacamole = Guacamole || {};
45 * Dynamic on-screen keyboard. Given the URL to an XML keyboard layout file,
46 * this object will download and use the XML to construct a clickable on-screen
47 * keyboard with its own key events.
50 * @param {String} url The URL of an XML keyboard layout file.
52 Guacamole.OnScreenKeyboard = function(url) {
54 var on_screen_keyboard = this;
57 * State of all modifiers. This is the bitwise OR of all active modifier
64 var scaledElements = [];
67 var currentModifier = 1;
70 * Adds a class to an element.
74 * @param {Element} element The element to add a class to.
75 * @param {String} classname The name of the class to add.
80 * Removes a class from an element.
84 * @param {Element} element The element to remove a class from.
85 * @param {String} classname The name of the class to remove.
89 // If Node.classList is supported, implement addClass/removeClass using that
93 addClass = function(element, classname) {
94 element.classList.add(classname);
98 removeClass = function(element, classname) {
99 element.classList.remove(classname);
104 // Otherwise, implement own
108 addClass = function(element, classname) {
110 // Simply add new class
111 element.className += " " + classname;
116 removeClass = function(element, classname) {
118 // Filter out classes with given name
119 element.className = element.className.replace(/([^ ]+)[ ]*/g,
120 function(match, testClassname, spaces, offset, string) {
122 // If same class, remove
123 if (testClassname == classname)
136 // Returns a unique power-of-two value for the modifier with the
137 // given name. The same value will be returned for the same modifier.
138 function getModifier(name) {
140 var value = modifiers[name];
143 // Get current modifier, advance to next
144 value = currentModifier;
145 currentModifier <<= 1;
147 // Store value of this modifier
148 modifiers[name] = value;
156 function ScaledElement(element, width, height, scaleFont) {
159 this.height = height;
161 this.scale = function(pixels) {
162 element.style.width = (width * pixels) + "px";
163 element.style.height = (height * pixels) + "px";
166 element.style.lineHeight = (height * pixels) + "px";
167 element.style.fontSize = pixels + "px";
173 // For each child of element, call handler defined in next
174 function parseChildren(element, next) {
176 var children = element.childNodes;
177 for (var i=0; i<children.length; i++) {
180 var child = children[i];
182 // Do not parse text nodes
186 // Get handler for node
187 var handler = next[child.tagName];
189 // Call handler if defined
193 // Throw exception if no handler
196 "Unexpected " + child.tagName
197 + " within " + element.tagName
205 var keyboard = document.createElement("div");
206 keyboard.className = "guac-keyboard";
208 // Retrieve keyboard XML
209 var xmlhttprequest = new XMLHttpRequest();
210 xmlhttprequest.open("GET", url, false);
211 xmlhttprequest.send(null);
213 var xml = xmlhttprequest.responseXML;
217 function parse_row(e) {
219 var row = document.createElement("div");
220 row.className = "guac-keyboard-row";
224 "column": function(e) {
225 row.appendChild(parse_column(e));
228 "gap": function parse_gap(e) {
231 var gap = document.createElement("div");
232 gap.className = "guac-keyboard-gap";
236 if (e.getAttribute("size"))
237 gap_units = parseFloat(e.getAttribute("size"));
239 scaledElements.push(new ScaledElement(gap, gap_units, gap_units));
240 row.appendChild(gap);
244 "key": function parse_key(e) {
247 var key_element = document.createElement("div");
248 key_element.className = "guac-keyboard-key";
250 // Append class if specified
251 if (e.getAttribute("class"))
252 key_element.className += " " + e.getAttribute("class");
254 // Position keys using container div
255 var key_container_element = document.createElement("div");
256 key_container_element.className = "guac-keyboard-key-container";
257 key_container_element.appendChild(key_element);
260 var key = new Guacamole.OnScreenKeyboard.Key();
264 if (e.getAttribute("size"))
265 key_units = parseFloat(e.getAttribute("size"));
267 key.size = key_units;
270 "cap": function parse_cap(e) {
272 // TODO: Handle "sticky" attribute
274 // Get content of key cap
275 var content = e.textContent || e.text;
277 // If read as blank, assume cap is a single space.
278 if (content.length == 0)
282 var real_keysym = null;
283 if (e.getAttribute("keysym"))
284 real_keysym = parseInt(e.getAttribute("keysym"));
286 // If no keysym specified, try to get from key content
287 else if (content.length == 1) {
289 var charCode = content.charCodeAt(0);
290 if (charCode >= 0x0000 && charCode <= 0x00FF)
291 real_keysym = charCode;
292 else if (charCode >= 0x0100 && charCode <= 0x10FFFF)
293 real_keysym = 0x01000000 | charCode;
298 var cap = new Guacamole.OnScreenKeyboard.Cap(content, real_keysym);
300 if (e.getAttribute("modifier"))
301 cap.modifier = e.getAttribute("modifier");
303 // Create cap element
304 var cap_element = document.createElement("div");
305 cap_element.className = "guac-keyboard-cap";
306 cap_element.textContent = content;
307 key_element.appendChild(cap_element);
309 // Append class if specified
310 if (e.getAttribute("class"))
311 cap_element.className += " " + e.getAttribute("class");
313 // Get modifier value
314 var modifierValue = 0;
315 if (e.getAttribute("if")) {
317 // Get modifier value for specified comma-delimited
318 // list of required modifiers.
319 var requirements = e.getAttribute("if").split(",");
320 for (var i=0; i<requirements.length; i++) {
321 modifierValue |= getModifier(requirements[i]);
322 addClass(cap_element, "guac-keyboard-requires-" + requirements[i]);
323 addClass(key_element, "guac-keyboard-uses-" + requirements[i]);
329 key.modifierMask |= modifierValue;
330 key.caps[modifierValue] = cap;
335 scaledElements.push(new ScaledElement(key_container_element, key_units, 1, true));
336 row.appendChild(key_container_element);
338 // Set up click handler for key
341 // Press key if not yet pressed
344 addClass(key_element, "guac-keyboard-pressed");
346 // Get current cap based on modifier state
347 var cap = key.getCap(modifiers);
349 // Update modifier state
352 // Construct classname for modifier
353 var modifierClass = "guac-keyboard-modifier-" + cap.modifier;
354 var modifierFlag = getModifier(cap.modifier);
356 // Toggle modifier state
357 modifiers ^= modifierFlag;
359 // Activate modifier if pressed
360 if (modifiers & modifierFlag) {
362 addClass(keyboard, modifierClass);
365 if (on_screen_keyboard.onkeydown && cap.keysym)
366 on_screen_keyboard.onkeydown(cap.keysym);
370 // Deactivate if not pressed
373 removeClass(keyboard, modifierClass);
376 if (on_screen_keyboard.onkeyup && cap.keysym)
377 on_screen_keyboard.onkeyup(cap.keysym);
383 // If not modifier, send key event now
384 else if (on_screen_keyboard.onkeydown && cap.keysym)
385 on_screen_keyboard.onkeydown(cap.keysym);
387 // Mark key as pressed
396 function release(e) {
398 // Release key if currently pressed
401 // Get current cap based on modifier state
402 var cap = key.getCap(modifiers);
404 removeClass(key_element, "guac-keyboard-pressed");
406 // Send key event if not a modifier key
407 if (!cap.modifier && on_screen_keyboard.onkeyup && cap.keysym)
408 on_screen_keyboard.onkeyup(cap.keysym);
410 // Mark key as released
419 key_element.addEventListener("mousedown", press, true);
420 key_element.addEventListener("touchstart", press, true);
422 key_element.addEventListener("mouseup", release, true);
423 key_element.addEventListener("mouseout", release, true);
424 key_element.addEventListener("touchend", release, true);
434 function parse_column(e) {
436 var col = document.createElement("div");
437 col.className = "guac-keyboard-column";
439 if (col.getAttribute("align"))
440 col.style.textAlign = col.getAttribute("align");
442 // Columns can only contain rows
445 col.appendChild(parse_row(e));
455 var keyboard_element = xml.documentElement;
456 if (keyboard_element.tagName != "keyboard")
457 throw new Error("Root element must be keyboard");
460 if (!keyboard_element.getAttribute("size"))
461 throw new Error("size attribute is required for keyboard");
463 var keyboard_size = parseFloat(keyboard_element.getAttribute("size"));
465 parseChildren(keyboard_element, {
468 keyboard.appendChild(parse_row(e));
471 "column": function(e) {
472 keyboard.appendChild(parse_column(e));
479 // Do not allow selection or mouse movement to propagate/register.
480 keyboard.onselectstart =
481 keyboard.onmousemove =
483 keyboard.onmousedown =
490 * Fired whenever the user presses a key on this Guacamole.OnScreenKeyboard.
493 * @param {Number} keysym The keysym of the key being pressed.
495 this.onkeydown = null;
498 * Fired whenever the user releases a key on this Guacamole.OnScreenKeyboard.
501 * @param {Number} keysym The keysym of the key being released.
506 * Returns the element containing the entire on-screen keyboard.
507 * @returns {Element} The element containing the entire on-screen keyboard.
509 this.getElement = function() {
514 * Resizes all elements within this Guacamole.OnScreenKeyboard such that
515 * the width is close to but does not exceed the specified width. The
516 * height of the keyboard is determined based on the width.
518 * @param {Number} width The width to resize this Guacamole.OnScreenKeyboard
521 this.resize = function(width) {
523 // Get pixel size of a unit
524 var unit = Math.floor(width * 10 / keyboard_size) / 10;
526 // Resize all scaled elements
527 for (var i=0; i<scaledElements.length; i++) {
528 var scaledElement = scaledElements[i];
529 scaledElement.scale(unit)
538 * Basic representation of a single key of a keyboard. Each key has a set of
539 * caps associated with tuples of modifiers. The cap determins what happens
540 * when a key is pressed, while it is the state of modifier keys that determines
541 * what cap is in effect on any particular key.
545 Guacamole.OnScreenKeyboard.Key = function() {
550 * Whether this key is currently pressed.
552 this.pressed = false;
555 * Width of the key, relative to the size of the keyboard.
560 * An associative map of all caps by modifier.
565 * Bit mask with all modifiers that affect this key set.
567 this.modifierMask = 0;
570 * Given the bitwise OR of all active modifiers, returns the key cap
573 this.getCap = function(modifier) {
574 return key.caps[modifier & key.modifierMask];
580 * Basic representation of a cap of a key. The cap is the visible part of a key
581 * and determines the active behavior of a key when pressed. The state of all
582 * modifiers on the keyboard determines the active cap for all keys, thus
583 * each cap is associated with a set of modifiers.
586 * @param {String} text The text to be displayed within this cap.
587 * @param {Number} keysym The keysym this cap sends when its associated key is
588 * pressed or released.
589 * @param {String} modifier The modifier represented by this cap.
591 Guacamole.OnScreenKeyboard.Cap = function(text, keysym, modifier) {
594 * Modifier represented by this keycap
596 this.modifier = null;
599 * The text to be displayed within this keycap
604 * The keysym this cap sends when its associated key is pressed/released
606 this.keysym = keysym;
608 // Set modifier if provided
609 if (modifier) this.modifier = modifier;