init threads a bit earlier and don't enter critical section
[xxxterm.git] / hinting.js
blobf5a5f5254570ddafdb55fe759ee4baa5098daf7b
1 /*
2 (c) 2009 by Leon Winter
3 (c) 2009, 2010 by Hannes Schueller
4 (c) 2010 by Hans-Peter Deifel
6 Copyright (c) 2009 Leon Winter
7 Copyright (c) 2009, 2010 Hannes Schueller
8 Copyright (c) 2009, 2010 Matto Fransen
9 Copyright (c) 2010 Hans-Peter Deifel
10 Copyright (c) 2010 Thomas Adam
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
31 function vimprobable_clearfocus() {
32 if(document.activeElement && document.activeElement.blur)
33 document.activeElement.blur();
36 function vimprobable_show_hints(inputText) {
37 if (document.getElementsByTagName("body")[0] !== null && typeof(document.getElementsByTagName("body")[0]) == "object") {
38 var height = window.innerHeight;
39 var width = window.innerWidth;
40 var scrollX = document.defaultView.scrollX;
41 var scrollY = document.defaultView.scrollY;
42 /* prefixing html: will result in namespace error */
43 var hinttags;
44 if (typeof(inputText) == "undefined" || inputText == "") {
45 hinttags = "//*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @role='link' or @href] | //input[not(@type='hidden')] | //a | //area | //iframe | //textarea | //button | //select";
46 } else {
47 /* only elements which match the text entered so far */
48 hinttags = "//*[(@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @role='link' or @href) and contains(., '" + inputText + "')] | //input[not(@type='hidden') and contains(., '" + inputText + "')] | //a[contains(., '" + inputText + "')] | //area[contains(., '" + inputText + "')] | //iframe[contains(@name, '" + inputText + "')] | //textarea[contains(., '" + inputText + "')] | //button[contains(@value, '" + inputText + "')] | //select[contains(., '" + inputText + "')]";
51 /* iterator type isn't suitable here, because: "DOMException NVALID_STATE_ERR: The document has been mutated since the result was returned." */
52 var r = document.evaluate(hinttags, document,
53 function(p) {
54 return 'http://www.w3.org/1999/xhtml';
55 }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
56 div = document.createElement("div");
57 /* due to the different XPath result type, we will need two counter variables */
58 vimprobable_j = 0;
59 var i;
60 vimprobable_a = [];
61 vimprobable_colors = [];
62 vimprobable_backgrounds = [];
63 for (i = 0; i < r.snapshotLength; i++)
65 var elem = r.snapshotItem(i);
66 rect = elem.getBoundingClientRect();
67 if (!rect || rect.top > height || rect.bottom < 0 || rect.left > width || rect.right < 0 || !(elem.getClientRects()[0]))
68 continue;
69 var computedStyle = document.defaultView.getComputedStyle(elem, null);
70 if (computedStyle.getPropertyValue("visibility") != "visible" || computedStyle.getPropertyValue("display") == "none")
71 continue;
72 var leftpos = Math.max((rect.left + scrollX), scrollX);
73 var toppos = Math.max((rect.top + scrollY), scrollY);
74 vimprobable_a.push(elem);
75 /* making this block DOM compliant */
76 var hint = document.createElement("span");
77 hint.setAttribute("class", "hinting_mode_hint");
78 hint.setAttribute("id", "vimprobablehint" + vimprobable_j);
79 hint.style.position = "absolute";
80 hint.style.left = leftpos + "px";
81 hint.style.top = toppos + "px";
82 hint.style.background = "red";
83 hint.style.color = "#fff";
84 hint.style.font = "bold 10px monospace";
85 hint.style.zIndex = "99";
86 var text = document.createTextNode(vimprobable_j + 1);
87 hint.appendChild(text);
88 div.appendChild(hint);
89 /* remember site-defined colour of this element */
90 vimprobable_colors[vimprobable_j] = elem.style.color;
91 vimprobable_backgrounds[vimprobable_j] = elem.style.background;
92 /* make the link black to ensure it's readable */
93 elem.style.color = "#000";
94 elem.style.background = "#ff0";
95 vimprobable_j++;
97 i = 0;
98 while (typeof(vimprobable_a[i]) != "undefined") {
99 vimprobable_a[i].className += " hinting_mode_hint";
100 i++;
102 document.getElementsByTagName("body")[0].appendChild(div);
103 vimprobable_clearfocus();
104 vimprobable_h = null;
105 if (i == 1) {
106 /* just one hinted element - might as well follow it */
107 return vimprobable_fire(1);
111 function vimprobable_fire(n)
113 if (typeof(vimprobable_a[n - 1]) != "undefined") {
114 el = vimprobable_a[n - 1];
115 tag = el.nodeName.toLowerCase();
116 vimprobable_clear();
117 if(tag == "iframe" || tag == "frame" || tag == "textarea" || tag == "input" && (el.type == "text" || el.type == "password" || el.type == "checkbox" || el.type == "radio") || tag == "select") {
118 el.focus();
119 if (tag == "textarea" || tag == "input")
120 console.log('insertmode_on');
121 } else {
122 if (el.onclick) {
123 var evObj = document.createEvent('MouseEvents');
124 evObj.initMouseEvent('click', true, true, window, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, null);
125 el.dispatchEvent(evObj);
126 } else if (el.href) {
127 if (el.href.match(/^javascript:/)) {
128 var evObj = document.createEvent('MouseEvents');
129 evObj.initMouseEvent('click', true, true, window, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, null);
130 el.dispatchEvent(evObj);
131 } else {
132 /* send signal to open link */
133 return "open;" + el.href;
135 } else {
136 var evObj = document.createEvent('MouseEvents');
137 evObj.initMouseEvent('click', true, true, window, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, null);
138 el.dispatchEvent(evObj);
143 function vimprobable_cleanup()
145 for(e in vimprobable_a) {
146 if (typeof(vimprobable_a[e].className) != "undefined") {
147 vimprobable_a[e].className = vimprobable_a[e].className.replace(/hinting_mode_hint/,'');
148 /* reset to site-defined colour */
149 vimprobable_a[e].style.color = vimprobable_colors[e];
150 vimprobable_a[e].style.background = vimprobable_backgrounds[e];
153 div.parentNode.removeChild(div);
154 window.onkeyup = null;
156 function vimprobable_clear()
158 vimprobable_cleanup();
159 console.log("hintmode_off")
162 function vimprobable_update_hints(n)
164 if(vimprobable_h != null) {
165 vimprobable_h.className = vimprobable_h.className.replace("_focus","");
166 vimprobable_h.style.background = "#ff0";
168 if (vimprobable_j - 1 < n * 10 && typeof(vimprobable_a[n - 1]) != "undefined") {
169 /* return signal to follow the link */
170 return "fire;" + n;
171 } else {
172 if (typeof(vimprobable_a[n - 1]) != "undefined") {
173 (vimprobable_h = vimprobable_a[n - 1]).className = vimprobable_a[n - 1].className.replace("hinting_mode_hint", "hinting_mode_hint_focus");
174 vimprobable_h.style.background = "#8f0";
179 function vimprobable_focus_input()
181 if (document.getElementsByTagName("body")[0] !== null && typeof(document.getElementsByTagName("body")[0]) == "object") {
182 /* prefixing html: will result in namespace error */
183 var hinttags = "//input[@type='text'] | //input[@type='password'] | //textarea";
184 var r = document.evaluate(hinttags, document,
185 function(p) {
186 return 'http://www.w3.org/1999/xhtml';
187 }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
188 var i;
189 var j = 0;
190 var k = 0;
191 var first = null;
192 for (i = 0; i < r.snapshotLength; i++) {
193 var elem = r.snapshotItem(i);
194 if (k == 0) {
195 if (elem.style.display != "none" && elem.style.visibility != "hidden") {
196 first = elem;
197 } else {
198 k--;
201 if (j == 1 && elem.style.display != "none" && elem.style.visibility != "hidden") {
202 elem.focus();
203 var tag = elem.nodeName.toLowerCase();
204 if (tag == "textarea" || tag == "input")
205 console.log('insertmode_on');
206 break;
207 } else {
208 if (elem == document.activeElement)
209 j = 1;
211 k++;
213 if (j == 0) {
214 /* no appropriate field found focused - focus the first one */
215 if (first !== null) {
216 first.focus();
217 var tag = elem.nodeName.toLowerCase();
218 if (tag == "textarea" || tag == "input")
219 console.log('insertmode_on');