5dbfb104f3f34cfa847c4c79264763ec6eafbc6c
[sheet.git] / keys.js
1 function setmode(classname) {
2         // set style for each #rows>li>ul>li to display:none unless it matches classname
3         var showclass = classname ? ' '+classname+'(?!\\w)' : '^$';
4         var parentskip = /^keys/;
5         var row = document.getElementById('rows').firstChild;
6         do {
7                 if (row.tagName == 'LI' && row.firstChild.tagName == 'UL'
8                 && !row.firstChild.className.match(parentskip)) {
9                         var el = row.firstChild.firstChild;
10                         if (el) do {
11                                 if (el.tagName == 'LI') {
12                                         el.style.display = el.className.match(showclass) ? 'block' : 'none';
13                                 }
14                         } while (el = el.nextSibling);
15                 }
16         } while (row = row.nextSibling);
17
18         // update H2 to reflect the first part of a currently active (but hidden) H3
19         var h3s = document.getElementsByTagName('H3');
20         for (var i = 0; i < h3s.length; i++) {
21                 if (h3s[i].parentNode.style.display != 'block') continue;
22                 document.getElementsByTagName('H2')[0].innerHTML = h3s[i].firstChild.data;
23         }
24 }
25
26 var keyfocus = undefined;
27 document.onkeypress = function(e) {
28         var keylabels = document.getElementById('rows').getElementsByTagName('B');
29         var keys = {};
30         for (var i = 0; i < keylabels.length; i++) {
31                 keys[keylabels[i].innerHTML] = keylabels[i].parentNode;
32         }
33         var input = e.charCode || e.keyCode;
34         for (var i = 0; i < keylabels.length; i++) {
35                 var key = keylabels[i].parentNode;
36                 var keychar = key.className.match(/ chr(\d+)$/);
37                 if (!keychar) continue; // not enterable
38                 keychar = keychar[1];
39                 if (keychar != input) continue; // different key
40                 var keymod = key.parentNode.className;
41                 if ((keymod.search(/\bctrl\b/) != -1) != e.ctrlKey) continue; // modifier mismatch
42                 if ((keymod.search(/\bmeta\b/) != -1) != e.altKey) continue;
43                 var row = key.parentNode.parentNode;
44                 var shown = row.style.display != 'none';
45                 if (!shown) continue; // foreign mode
46                 if (keyfocus) keyfocus.style.outline = '';
47                 key.style.outline = '1px solid red';
48                 keyfocus = key;
49                 if (key.onclick) key.onclick();
50                 return false;
51         }
52 }