tooltipxhr.js - jscancer - Javascript crap (relatively small)
(HTM) git clone git://git.codemadness.org/jscancer
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
tooltipxhr.js (2430B)
---
1 (function() {
2 var open = false, timershow;
3 var el = document.createElement("div");
4 el.style.display = "none";
5 el.className = "tooltip";
6 document.body.appendChild(el);
7
8 var position = function(x, y) {
9 // position optimally, try to adjust when outside window.
10 if (y + el.offsetHeight + 32 > window.innerHeight)
11 el.style.top = String(y - el.offsetHeight - 18) + "px";
12 else
13 el.style.top = String(y + 18) + "px"; // + space for cursor height.
14 // NOTE, this is a mess: document.body.clientWidth excludes scrollbar width.
15 // window.innerWidth is the window including scrollbar width.
16 // window.outerWidth is the window including scrollbar width and window border.
17 if (x + el.offsetWidth >= document.body.parentNode.offsetWidth) {
18 el.style.right = "0px";
19 el.style.left = "auto";
20 } else {
21 el.style.left = String(x) + "px";
22 el.style.right = "auto";
23 }
24 };
25 document.addEventListener("keyup", function(e) {
26 // close on escape key.
27 if (e.which === 27) {
28 open = false;
29 el.style.display = "none";
30 clearTimeout(timershow);
31 }
32 });
33 // don't close if mouse over popup or it's children.
34 document.addEventListener("mouseover", function(e) {
35 for (var p = e.target; !!p.parentNode; p = p.parentNode)
36 if (p === el)
37 return;
38 open = false;
39 el.style.display = "none";
40 clearTimeout(timershow);
41 return !!e.stopPropagation();
42 }, false);
43 document.addEventListener("mousemove", function(e) {
44 if (open)
45 return position(e.clientX, e.clientY);
46 clearTimeout(timershow);
47 timershow = setTimeout((function(target, x, y) {
48 return function() {
49 var uri = null;
50 for (var p = target; !!p.parentNode; p = p.parentNode)
51 if ((uri = p.getAttribute("data-tooltipuri")) !== null)
52 break;
53 if (uri === null)
54 return;
55
56 var xhr = new(XMLHttpRequest);
57 xhr.open("GET", uri + ((uri.indexOf("?") != -1) ? "&" : "?") + "t=" + String(new Date().getTime()), true);
58 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
59 xhr.onreadystatechange = function() {
60 if (xhr.readyState != 4 || [ 0, 200 ].indexOf(xhr.status) == -1)
61 return;
62 el.innerHTML = xhr.responseText || "";
63 el.style.display = "block";
64 open = true;
65 position(x, y);
66 };
67 xhr.timeout = 10000;
68 xhr.overrideMimeType("text/html");
69 xhr.send();
70 };
71 })(e.target, e.clientX, e.clientY), 500);
72 return !!e.stopPropagation();
73 }, false);
74 })();