db4dfd2510b28ca83265e03c173bc30e04da520d
[p5sagit/Devel-Size.git] / static / public / tm.js
1 var labelType, useGradients, nativeTextSupport, animate;
2
3 (function() {
4   var ua = navigator.userAgent,
5       iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
6       typeOfCanvas = typeof HTMLCanvasElement,
7       nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
8       textSupport = nativeCanvasSupport 
9         && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
10   //I'm setting this based on the fact that ExCanvas provides text support for IE
11   //and that as of today iPhone/iPad current text support is lame
12   labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
13   nativeTextSupport = labelType == 'Native';
14   useGradients = nativeCanvasSupport;
15   animate = !(iStuff || !nativeCanvasSupport);
16 })();
17
18 var Log = {
19   elem: false,
20   write: function(text){
21     if (!this.elem) 
22       this.elem = document.getElementById('log');
23     this.elem.innerHTML = text;
24     this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
25   }
26 };
27
28
29 function init(){
30   //init data
31   //end
32   //init TreeMap
33   var tm = new $jit.TM.Squarified({
34     //where to inject the visualization
35     injectInto: 'infovis',
36     //show only one tree level
37     levelsToShow: 2,
38     //parent box title heights
39     titleHeight: 0,
40     //enable animations
41     animate: animate,
42     //box offsets
43     offset: 1,
44     //use canvas text
45     Label: {
46       type: labelType,
47       size: 9,
48       family: 'Tahoma, Verdana, Arial'
49     },
50     //enable specific canvas styles
51     //when rendering nodes
52     Node: {
53       CanvasStyles: {
54         shadowBlur: 0,
55         shadowColor: '#000'
56       }
57     },
58     //Attach left and right click events
59     Events: {
60       enable: true,
61       onClick: function(node) {
62         if(node) tm.enter(node);
63       },
64       onRightClick: function() {
65         tm.out();
66       },
67       //change node styles and canvas styles
68       //when hovering a node
69       onMouseEnter: function(node, eventInfo) {
70         if(node) {
71           //add node selected styles and replot node
72           node.setCanvasStyle('shadowBlur', 7);
73           node.setData('color', '#888');
74           tm.fx.plotNode(node, tm.canvas);
75           tm.labels.plotLabel(tm.canvas, node);
76         }
77       },
78       onMouseLeave: function(node) {
79         if(node) {
80           node.removeData('color');
81           node.removeCanvasStyle('shadowBlur');
82           tm.plot();
83         }
84       }
85     },
86     //duration of the animations
87     duration: 1000,
88     //Enable tips
89     Tips: {
90       enable: true,
91       type: 'Native',
92       //add positioning offsets
93       offsetX: 20,
94       offsetY: 20,
95       //implement the onShow method to
96       //add content to the tooltip when a node
97       //is hovered
98       onShow: function(tip, node, isLeaf, domElement) {
99         var html = "<div class=\"tip-title\">" + node.name 
100           + "</div><div class=\"tip-text\">";
101         var data = node.data;
102
103     //"child_seqns"     => 4,
104     //"depth"           => 2,
105     //"id"              => 3,
106     //"kids_node_count" => 4426,
107     //"kids_size"       => 560058,
108     //"name"            => "SV(PVHV)",
109     //"parent_seqn"     => 2,
110     //"self_size"       => 1080,
111
112         html += sprintf("Name: %s<br />\n", data.name);
113         html += sprintf("Size: %d (%d + %d)<br />", data.self_size+data.kids_size, data.self_size, data.kids_size);
114         if (data.child_count) {
115             html += sprintf("Children: %d of %d<br />", data.child_count, data.kids_node_count);
116         }
117
118         tip.innerHTML =  html; 
119       }  
120     },
121     //Implement this method for retrieving a requested  
122     //subtree that has as root a node with id = nodeId,  
123     //and level as depth. This method could also make a server-side  
124     //call for the requested subtree. When completed, the onComplete   
125     //callback method should be called.  
126     request: function(nodeId, level, onComplete){  
127         if (true) {
128             jQuery.getJSON('jit_tree/'+nodeId+'/1', function(data) {
129                 onComplete.onComplete(nodeId, data);  
130             });
131         }
132         else {
133             var tree = memnodes[0];
134             var subtree = $jit.json.getSubtree(tree, nodeId);  
135             $jit.json.prune(subtree, 2);  
136             onComplete.onComplete(nodeId, subtree);  
137         }
138     },
139     //Add the name of the node in the corresponding label
140     //This method is called once, on label creation and only for DOM labels.
141     onCreateLabel: function(domElement, node){
142         domElement.innerHTML = node.name;
143     }
144   });
145   
146 if(true) {
147     jQuery.getJSON('jit_tree/1/1', function(data) {
148   console.log(data);
149         tm.loadJSON(data);
150         tm.refresh();
151     });
152 }
153 else {
154   //var pjson = eval('(' + json + ')');  
155   var pjson = memnodes[0];
156   $jit.json.prune(pjson, 2);
157   console.log(pjson);
158   tm.loadJSON(pjson);
159   tm.refresh();
160 }
161
162   //add event to the back button
163   var back = $jit.id('back');
164   $jit.util.addEvent(back, 'click', function() {
165     tm.out();
166   });
167 }