browser: prevent rounding errors in usage percentage
[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                 if (!key.onclick) continue;
37                 var keychar = key.className.match(/ chr(\d+)$/);
38                 if (!keychar) continue; // not enterable
39                 keychar = keychar[1];
40                 if (keychar != input) continue; // different key
41                 var keymod = key.parentNode.className;
42                 if ((keymod.search(/\bctrl\b/) != -1) != e.ctrlKey) continue; // modifier mismatch
43                 if ((keymod.search(/\bmeta\b/) != -1) != e.altKey) continue;
44                 var row = key.parentNode.parentNode;
45                 var shown = row.style.display != 'none';
46                 if (!shown) continue; // foreign mode
47                 if (keyfocus) keyfocus.style.outline = '';
48                 key.style.outline = '1px solid red';
49                 keyfocus = key;
50                 if (key.onclick) key.onclick();
51                 return false;
52         }
53 }