Moved tooltip to its own file.
[dygraphs.git] / experimental / palette / tooltip.js
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 }
32 this.elem = Palette.createChild("div", parent);
33 this.title = Palette.createChild("div", this.elem);
34 this.elem.className = "tooltip";
35 this.title.className = "title";
36 this.type = Palette.createChild("div", this.elem);
37 this.type.className = "type";
38 this.body = Palette.createChild("div", this.elem);
39 this.body.className = "body";
40 this.hide();
41 }
42
43 Tooltip.prototype.show = function(source, event, title, type, body) {
44 this.title.innerHTML = title;
45 this.body.innerHTML = body;
46 this.type.innerText = type; // innerText for arrays.
47
48 var getTopLeft = function(element) {
49 var x = element.offsetLeft;
50 var y = element.offsetTop;
51 element = element.offsetParent;
52
53 while(element != null) {
54 x = parseInt(x) + parseInt(element.offsetLeft);
55 y = parseInt(y) + parseInt(element.offsetTop);
56 element = element.offsetParent;
57 }
58 return [y, x];
59 }
60
61 this.elem.style.height = source.style.height;
62 this.elem.style.width = "280";
63 var topLeft = getTopLeft(source);
64 this.elem.style.top = parseInt(topLeft[0] + source.offsetHeight) + 'px';
65 this.elem.style.left = parseInt(topLeft[1] + 10) + 'px';
66 this.elem.style.visibility = "visible";
67 }
68
69 Tooltip.prototype.hide = function() {
70 this.elem.style.visibility = "hidden";
71 }