Commit | Line | Data |
---|---|---|
44885208 RK |
1 | // Copyright (c) 2011 Google, Inc. |
2 | // | |
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
4 | // of this software and associated documentation files (the "Software"), to deal | |
5 | // in the Software without restriction, including without limitation the rights | |
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
7 | // copies of the Software, and to permit persons to whom the Software is | |
8 | // furnished to do so, subject to the following conditions: | |
9 | // | |
10 | // The above copyright notice and this permission notice shall be included in | |
11 | // all copies or substantial portions of the Software. | |
12 | // | |
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
19 | // THE SOFTWARE. | |
20 | ||
21 | /** | |
22 | * @fileoverview Dygraphs options palette tooltip. | |
23 | * | |
24 | * @author konigsberg@google.com (Robert Konigsberg) | |
25 | */ | |
26 | "use strict"; | |
27 | ||
28 | function Tooltip(parent) { | |
29 | if (!parent) { | |
30 | parent = document.getElementsByTagName("body")[0]; | |
31 | } | |
9390f624 RK |
32 | this.elem = Tooltip.createChild("div", parent); |
33 | this.title = Tooltip.createChild("div", this.elem); | |
44885208 RK |
34 | this.elem.className = "tooltip"; |
35 | this.title.className = "title"; | |
9390f624 | 36 | this.type = Tooltip.createChild("div", this.elem); |
44885208 | 37 | this.type.className = "type"; |
9390f624 | 38 | this.body = Tooltip.createChild("div", this.elem); |
44885208 RK |
39 | this.body.className = "body"; |
40 | this.hide(); | |
41 | } | |
42 | ||
9390f624 RK |
43 | Tooltip.createChild = function(type, parentElement, className) { |
44 | var element = document.createElement(type); | |
45 | parentElement.appendChild(element); | |
46 | if (className) { | |
47 | element.className = className; | |
48 | } | |
49 | return element; | |
50 | }; | |
51 | ||
58a18b02 | 52 | Tooltip.prototype.show = function(source, title, type, body) { |
44885208 RK |
53 | this.title.innerHTML = title; |
54 | this.body.innerHTML = body; | |
21285cf9 | 55 | this.type.textContent = type; // textContent for arrays. |
44885208 RK |
56 | |
57 | var getTopLeft = function(element) { | |
58 | var x = element.offsetLeft; | |
59 | var y = element.offsetTop; | |
60 | element = element.offsetParent; | |
61 | ||
62 | while(element != null) { | |
63 | x = parseInt(x) + parseInt(element.offsetLeft); | |
64 | y = parseInt(y) + parseInt(element.offsetTop); | |
65 | element = element.offsetParent; | |
66 | } | |
67 | return [y, x]; | |
68 | } | |
69 | ||
70 | this.elem.style.height = source.style.height; | |
71 | this.elem.style.width = "280"; | |
72 | var topLeft = getTopLeft(source); | |
73 | this.elem.style.top = parseInt(topLeft[0] + source.offsetHeight) + 'px'; | |
74 | this.elem.style.left = parseInt(topLeft[1] + 10) + 'px'; | |
75 | this.elem.style.visibility = "visible"; | |
76 | } | |
77 | ||
78 | Tooltip.prototype.hide = function() { | |
79 | this.elem.style.visibility = "hidden"; | |
80 | } |