Fixed parsing.
[guacamole-common-js.git] / src / main / resources / oskeyboard.js
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is guacamole-common-js.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
40
41 /**
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.
45  * 
46  * @constructor
47  * @param {String} url The URL of an XML keyboard layout file.
48  */
49 Guacamole.OnScreenKeyboard = function(url) {
50
51     // For each child of element, call handler defined in next
52     function parseChildren(element, next) {
53
54         var children = element.childNodes;
55         for (var i=0; i<children.length; i++) {
56
57             // Get child node
58             var child = children[i];
59
60             // Do not parse text nodes
61             if (!child.tagName)
62                 continue;
63
64             // Get handler for node
65             var handler = next[child.tagName];
66
67             // Call handler if defined
68             if (handler)
69                 handler(child);
70
71             // Throw exception if no handler
72             else
73                 throw new Error(
74                       "Unexpected " + child.tagName
75                     + " within " + element.tagName
76                 );
77
78         }
79
80     }
81
82     // Create keyboard
83     var keyboard = document.createElement("div");
84     keyboard.className = "keyboard";
85
86     // Retrieve keyboard XML
87     var xmlhttprequest = new XMLHttpRequest();
88     xmlhttprequest.open("GET", url, false);
89     xmlhttprequest.send(null);
90
91     var xml = xmlhttprequest.responseXML;
92
93     if (xml) {
94
95         function parse_row(e) {
96             
97             var row = document.createElement("div");
98             row.className = "row";
99
100             parseChildren(e, {
101                 
102                 "column": function(e) {
103                     row.appendChild(parse_column(e));
104                 },
105                 
106                 "gap": function parse_gap(e) {
107
108                     // Get attributes
109                     var gap_size = e.attributes["size"];
110
111                     // Create element
112                     var gap = document.createElement("div");
113                     gap.className = "gap";
114                     gap.textContent = " ";
115
116                     if (gap_size)
117                         gap.style.width = gap.style.height =
118                             parseFloat(gap_size.value) + "em";
119
120                     row.appendChild(gap);
121
122                 },
123                 
124                 "key": function parse_key(e) {
125                     
126                     // Get attributes
127                     var key_size = e.attributes["size"];
128
129                     // Create element
130                     var key = document.createElement("div");
131                     key.className = "key";
132                     key.textContent = "K";
133
134                     parseChildren(e, {
135                         "cap": function cap(e) {
136
137                             // Get attributes
138                             var required = e.attributes["if"];
139                             var modifier = e.attributes["modifier"];
140                             var keysym   = e.attributes["keysym"];
141                             var sticky   = e.attributes["sticky"];
142                             
143                             // Get content of key cap
144                             var content = e.textContent;
145                             
146                             // If no requirements, then show cap by default
147                             if (!required) {
148                                 key.textContent = content;
149                             }
150
151                         }
152                     });
153
154                     row.appendChild(key);
155
156                 }
157                 
158             });
159
160             return row;
161
162         }
163
164         function parse_column(e) {
165             
166             var col = document.createElement("div");
167             col.className = "col";
168
169             var align = col.attributes["align"];
170
171             if (align)
172                 col.style.textAlign = align.value;
173
174             // Columns can only contain rows
175             parseChildren(e, {
176                 "row": function(e) {
177                     col.appendChild(parse_row(e));
178                 }
179             });
180
181             return col;
182
183         }
184
185
186         // Parse document
187         var keyboard_element = xml.documentElement;
188         if (keyboard_element.tagName != "keyboard")
189             throw new Error("Root element must be keyboard");
190
191         // Get attributes
192         var keyboard_size = keyboard_element.attributes["size"];
193         
194         parseChildren(keyboard_element, {
195             
196             "row": function(e) {
197                 keyboard.appendChild(parse_row(e));
198             },
199             
200             "column": function(e) {
201                 keyboard.appendChild(parse_column(e));
202             }
203             
204         });
205
206     }
207
208     // Do not allow selection or mouse movement to propagate/register.
209     keyboard.onselectstart =
210     keyboard.onmousemove   =
211     keyboard.onmouseup     =
212     keyboard.onmousedown   =
213     function(e) {
214         e.stopPropagation();
215         return false;
216     };
217
218
219     this.onkeypressed  = null;
220     this.onkeyreleased = null;
221
222     this.getElement = function() {
223         return keyboard;
224     };
225
226 };
227
228 Guacamole.OnScreenKeyboard.Key = function() {
229
230     /**
231      * Width of the key, relative to the size of the keyboard.
232      */
233     this.size = 1;
234
235     /**
236      * Whether this key is currently pressed.
237      */
238     this.pressed = false;
239
240     /**
241      * An associative map of all caps by modifier.
242      */
243     this.caps = {};
244
245 }
246
247 Guacamole.OnScreenKeyboard.Cap = function(text, keycode, modifier) {
248     
249     /**
250      * Modifier represented by this keycap
251      */
252     this.modifier = 0;
253     
254     /**
255      * The text to be displayed within this keycap
256      */
257     this.text = text;
258
259     /**
260      * The keycode this cap sends when its associated key is pressed/released
261      */
262     this.keycode = keycode;
263
264     // Set modifier if provided
265     if (modifier) this.modifier = modifier;
266     
267 }