-
Notifications
You must be signed in to change notification settings - Fork 6
/
TreeGraph.js
763 lines (726 loc) · 27 KB
/
TreeGraph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*!
* Tree graph with HTML5 and JavaScript.
* Date: Sep 17, 2012.
* Dependencies: none.
* Source: https://github.com/rodrigocfd/html5-tree-graph
*
* Copyright (c) 2012 Rodrigo Cesar de Freitas Dias
* Released under the MIT license, see license.txt for details.
*/
function TreeGraph(canvasId) {
var CONSTANTS = {
xBoxPadding: 4,
yNodePadding: 5,
xMinPadding: 30,
yBranchGap: 10,
xBezierRadius: 14,
imgSize: 22, // if not null, will force size for all images
font: '11pt "Times New Roman"',
bgColor: 'rgba(255,255,255,0.5)', // default for nodes without color
textColor: '#121212',
borderColor: '#888',
lineColor: '#AAA',
animateTime: 100,
storageKey: 'TreeGraph_' + canvasId
};
var Us = {
retObj: { }, // object to be returned to TreeGraph() function caller
context: document.getElementById(canvasId).getContext('2d'),
rootNode: null,
callbacks: { Click:null, CtrlClick:null },
isRendering: false,
isDragging: false,
baseDragPos: null // when dragging, this is set
};
var Util = {
AddPropertiesIfNotExist: function AddPropertiesIfNotExist(target, otherObj) {
if(target === undefined) target = { };
for(var prop in otherObj) { // http:https://stackoverflow.com/questions/12317003/something-like-jquery-extend-but-standalone
if(target[prop] === undefined) {
if(otherObj[prop] !== null && typeof otherObj[prop] === 'object') {
if(otherObj[prop] instanceof Array) { // http:https://javascript.crockford.com/remedial.html
target[prop] = [];
for(var i = 0; i < otherObj[prop].length; ++i)
target[prop].push(AddPropertiesIfNotExist({ }, otherObj[prop]));
} else {
AddPropertiesIfNotExist(target[prop], otherObj[prop]);
}
} else {
target[prop] = otherObj[prop];
}
}
}
return target;
},
GetPos: function(obj) {
// http:https://www.codingforums.com/showthread.php?t=126325
var ret = { x:obj.offsetLeft, y:obj.offsetTop };
while(obj.offsetParent) {
if(obj === document.getElementsByTagName('body')[0]) {
break;
} else {
ret.x += obj.offsetParent.offsetLeft;
ret.y += obj.offsetParent.offsetTop;
obj = obj.offsetParent;
}
}
return ret;
},
CalcTextRect: function CalcTextRect(context, text) {
if(CalcTextRect.cache === undefined)
CalcTextRect.cache = []; // static variable to cache the calculated heights
if(CalcTextRect.cache[context.font] === undefined) {
var span = document.createElement('span'),
body = document.getElementsByTagName('body')[0];
span.style.font = context.font;
span.textContent = 'gM'; // http:https://www.html5rocks.com/en/tutorials/canvas/texteffects/
body.appendChild(span);
CalcTextRect.cache[context.font] = span.offsetHeight;
body.removeChild(span);
}
return {
cx: context.measureText(text).width + CONSTANTS.xBoxPadding * 2,
cy: CalcTextRect.cache[context.font]
};
},
DrawRect: function(context, x, y, cx, cy, borderColor, bgColor) {
x += 0.5; y += 0.5; // http:https://stackoverflow.com/questions/7545013/canvas-draws-lines-too-thick
context.save();
if(borderColor !== undefined && borderColor !== null)
context.strokeStyle = borderColor;
context.beginPath();
context.rect(x, y, cx, cy);
if(bgColor !== undefined && bgColor !== null) {
context.fillStyle = bgColor;
context.fill();
}
context.stroke();
context.restore();
},
DrawBezier: function(context, x0, y0, x1, y1) {
context.beginPath();
context.moveTo(x0, y0);
context.bezierCurveTo(x0 + CONSTANTS.xBezierRadius, y0,
x1 - CONSTANTS.xBezierRadius, y1,
x1, y1);
context.stroke(); // use current strokeStyle
},
DrawHalfCircle: function(context, x, y) {
context.beginPath();
context.arc(x, y, 4, Math.PI * 0.5, Math.PI * 1.5, true);
context.stroke(); // use current strokeStyle
},
Animate: function(duration, AnimateCallback, DoneCallback) {
var RequestAnimationFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame;
var t0 = window.mozAnimationStartTime || Date.now();
var Redraw = function(timestamp) {
var elapsed = (timestamp || Date.now()) - t0;
var pct = elapsed / duration;
if(pct > 1) pct = 1;
AnimateCallback.call(window, pct);
if(pct < 1)
RequestAnimationFrame(Redraw);
else if(DoneCallback !== undefined && DoneCallback !== null)
DoneCallback.call(window);
};
RequestAnimationFrame(Redraw);
}
};
var Node = {
FlushToStorage: function() {
function StripNode(node) {
var ret = {
text: node.text,
isExpanded: node.isExpanded,
pos: { x:node.rect.x, y:node.rect.y }, // current node position
children: []
};
for(var i = 0; i < node.children.length; ++i)
ret.children.push(StripNode(node.children[i]));
return ret;
}
localStorage.setItem(CONSTANTS.storageKey,
JSON.stringify( StripNode(Us.rootNode) ));
},
CheckStorage: function() {
var oldTree = null;
if(localStorage.getItem(CONSTANTS.storageKey) !== null) { // we previously stored this tree
function IsSameTree(root1, root2) {
if(root1 === null || root2 === null) {
return false;
} else if(root1.text !== root2.text || root1.children.length !== root2.children.length) {
return false;
} else {
for(var i = 0; i < root1.children.length; ++i)
if(!IsSameTree(root1.children[i], root2.children[i]))
return false;
}
return true;
}
oldTree = JSON.parse(localStorage.getItem(CONSTANTS.storageKey));
if(IsSameTree(Us.rootNode, oldTree)) { // we're reloading the tree, so copy the node folding
function CopyNodeFolding(destNode, srcNode) {
destNode.isExpanded = srcNode.isExpanded;
for(var i = 0; i < destNode.children.length; ++i) // supposedly the same tree
CopyNodeFolding(destNode.children[i], srcNode.children[i]);
}
CopyNodeFolding(Us.rootNode, oldTree);
} else { // tree structure has changed
oldTree = null;
Node.FlushToStorage();
}
} else { // this tree never have been stored
Node.FlushToStorage();
}
return oldTree;
},
Load: function(rootNode) {
Us.context.save();
Us.context.font = CONSTANTS.font;
Us.rootNode = rootNode;
Node.Init();
var oldTree = Node.CheckStorage();
Node.LoadImages(function() {
var matrix = Node.VisibleMatrix(); // first positioning
Placement.Calc(matrix);
if(oldTree !== null) { // this tree is being reloaded
function SetPosFromOldNode(destNode, srcNode) {
destNode.posSch = { x:srcNode.pos.x, y:srcNode.pos.y }; // schedule
for(var i = 0; i < destNode.children.length; ++i) // supposedly the same tree
SetPosFromOldNode(destNode.children[i], srcNode.children[i]);
}
if(oldTree.pos.x == 0 && oldTree.pos.y == 0) Placement.ResetRootPos(matrix); // previous tree was neved moved
else SetPosFromOldNode(Us.rootNode, oldTree);
} else { // this tree has never been stored
Placement.ResetRootPos(matrix);
}
Node.Render(matrix);
var events = [ 'mousedown', 'mouseup', 'mouseout', 'mousemove', 'click', 'selectstart' ];
var handlers = [ Events.MouseDown, Events.MouseUp, Events.MouseOut,
Events.MouseMove, Events.Click, Events.SelectStart ];
for(var i = 0; i < events.length; ++i) {
Us.context.canvas.removeEventListener(events[i], handlers[i]);
Us.context.canvas.addEventListener(events[i], handlers[i]);
}
Us.context.restore();
});
},
Init: function Init() {
if(Init.seq === undefined)
Init.seq = 0; // static variable to hold the unique ID
function SetupNode(node, depth, parent) {
Util.AddPropertiesIfNotExist(node, { // these are the properties available to the user
text: '(NO TEXT)',
children: [],
color: CONSTANTS.bgColor,
image: null, // URL
data: null // any user data, will be preserved when returning the node at events
});
node.id = Init.seq++;
node.parent = function() { return parent; };
node.depth = depth;
node.isExpanded = false;
node.imageObj = null; // created if an URL exists in 'image' property
var rcText = Util.CalcTextRect(Us.context, node.text);
node.rect = { x:0, y:0, cx:rcText.cx, cy:rcText.cy };
node.posSch = null; // scheduled position to move to; {x,y}
node.posTmp = null; // used within animation
for(var i = 0; i < node.children.length; ++i)
SetupNode(node.children[i], depth + 1, node);
}
SetupNode(Us.rootNode, 0, null);
},
LoadImages: function(OnComplete) {
var total = 0; // how many images in the whole tree
function CountImages(node) {
total += (node.image === null) ? 0 : 1;
for(var i = 0; i < node.children.length; ++i)
CountImages(node.children[i]);
}
function CreateImageObj(node) {
function ImageDone(status) {
if(status.type === 'error') // won't affect the rendering, image just won't show
console.log('Failed to load ' + status.target.src + '".');
if(--total === 0 && OnComplete !== undefined)
OnComplete();
}
if(node.image !== null) {
node.imageObj = new Image();
node.imageObj.src = node.image;
node.imageObj.onload = function(status) { // image successfully loaded
node.rect.cx += (CONSTANTS.imgSize !== null ? CONSTANTS.imgSize : node.imageObj.width) + 1;
node.rect.cy = Math.max(node.rect.cy,
(CONSTANTS.imgSize !== null ? CONSTANTS.imgSize : node.imageObj.height) + 1);
ImageDone(status);
};
node.imageObj.onerror = function(status) { // image failed to load
node.imageObj = null;
ImageDone(status);
};
}
for(var i = 0; i < node.children.length; ++i)
CreateImageObj(node.children[i]);
}
CountImages(Us.rootNode);
if(total === 0) { // no images to render
if(OnComplete !== undefined) OnComplete();
} else {
CreateImageObj(Us.rootNode); // preload all images
}
},
VisibleMatrix: function() {
var matrix = []; // a linear view of the nodes that should be rendered
function CreateMatrix(node) {
if(matrix[node.depth] === undefined)
matrix[node.depth] = [];
matrix[node.depth].push(node);
if(node.isExpanded)
for(var i = 0; i < node.children.length; ++i)
CreateMatrix(node.children[i]);
}
if(Us.rootNode !== null) CreateMatrix(Us.rootNode);
return matrix;
},
Count: function() {
function CountChildren(node) {
var c = 1;
for(var i = 0; i < node.children.length; ++i)
c += CountChildren(node.children[i]);
return c;
}
return CountChildren(Us.rootNode); // whole tree node count
},
AtPoint: function(x, y, visibleMatrix) {
for(var i = 0; i < visibleMatrix.length; ++i) {
for(var j = 0; j < visibleMatrix[i].length; ++j) {
var node = visibleMatrix[i][j];
if( (x >= node.rect.x) && (x < node.rect.x + node.rect.cx) &&
(y >= node.rect.y) && (y < node.rect.y + node.rect.cy + 4) ) return node; // 4px adjustment, dunno why
}
}
return null; // no node at given point
},
CollapseAll: function() {
if(Us.isRendering) return;
var matrix = Node.VisibleMatrix();
for(var i = 0; i < matrix.length; ++i) {
for(var j = 0; j < matrix[i].length; ++j) {
matrix[i][j].posSch = { x:0, y:CONSTANTS.yNodePadding };
matrix[i][j].isExpanded = false;
}
}
Placement.ResetRootPos(matrix);
Node.Render(matrix, function() {
Us.rootNode.isExpanded = false;
matrix = Node.VisibleMatrix();
Placement.Calc(matrix);
Placement.ResetRootPos(matrix);
Node.Render(matrix, function() { // no animation, remove collapsed
Node.FlushToStorage();
});
});
},
ExpandAll: function() {
if(Us.isRendering) return;
function MakeExpand(node) {
if(node.children.length === 0) return;
node.isExpanded = true;
for(var i = 0; i < node.children.length; ++i)
MakeExpand(node.children[i]);
}
MakeExpand(Us.rootNode);
var matrix = Node.VisibleMatrix(); // will have all the nodes
Placement.Calc(matrix);
Placement.ResetRootPos(matrix);
Node.Render(matrix, function() { Node.FlushToStorage(); });
},
FitParentContainer: function() {
// Sample container that fills a whole page:
// div#one { width:100%; height:100%; }
Us.context.canvas.style.width = '100%';
Us.context.canvas.style.height = '100%';
Us.context.canvas.width = Us.context.canvas.offsetWidth;
Us.context.canvas.height = Us.context.canvas.offsetHeight;
Node.Render(Node.VisibleMatrix()); // redraw
},
Paint: function(visibleMatrix, pct) {
Us.context.save();
Us.context.clearRect(0, 0, Us.context.canvas.width, Us.context.canvas.height);
Us.context.font = CONSTANTS.font;
Us.context.textBaseline = 'middle';
for(var i = 0; i < visibleMatrix.length; ++i) {
for(var j = 0; j < visibleMatrix[i].length; ++j) {
var node = visibleMatrix[i][j];
node.posTmp = { x:node.rect.x, y:node.rect.y };
if(node.posSch !== null) { // will move
node.posTmp.x += (node.posSch.x - node.posTmp.x) * pct; // pct 0 to 1
node.posTmp.y += (node.posSch.y - node.posTmp.y) * pct;
}
Us.context.save();
Us.context.strokeStyle = CONSTANTS.lineColor;
var parent = node.parent();
if(parent !== null) {
Util.DrawBezier(Us.context, // apply coordinate translation
parent.posTmp.x + parent.rect.cx,
parent.posTmp.y + parent.rect.cy / 2,
node.posTmp.x,
node.posTmp.y + node.rect.cy / 2);
}
if(node.children.length > 0 && !node.isExpanded) {
Util.DrawHalfCircle(Us.context,
node.posTmp.x + node.rect.cx + 1,
node.posTmp.y + node.rect.cy / 2 + 1);
}
Us.context.restore();
Util.DrawRect(Us.context,
node.posTmp.x, node.posTmp.y,
node.rect.cx, node.rect.cy,
CONSTANTS.borderColor, node.color);
if(node.imageObj !== null) {
if(CONSTANTS.imgSize !== null) { // force image size
Us.context.drawImage(node.imageObj,
node.posTmp.x + 1, node.posTmp.y + 1,
CONSTANTS.imgSize, CONSTANTS.imgSize);
} else {
Us.context.drawImage(node.imageObj,
node.posTmp.x + 1, node.posTmp.y + 1);
}
}
Us.context.save();
Us.context.fillStyle = CONSTANTS.textColor;
Us.context.fillText(node.text,
node.posTmp.x + CONSTANTS.xBoxPadding +
(node.imageObj !== null ?
(CONSTANTS.imgSize !== null ? CONSTANTS.imgSize : node.imageObj.width) + 1
: 0),
node.posTmp.y + node.rect.cy / 2);
Us.context.restore();
}
}
Us.context.restore();
},
Render: function(visibleMatrix, OnComplete) {
function HasMove(visibleMatrix) {
for(var i = 0; i < visibleMatrix.length; ++i)
for(var j = 0; j < visibleMatrix[i].length; ++j)
if(visibleMatrix[i][j].posSch !== null)
return true; // at least one node will be moved
return false;
}
if(Us.isRendering) return;
Us.isRendering = true; // set blocking flag
if(HasMove(visibleMatrix)) {
Util.Animate(CONSTANTS.animateTime, function(pct) {
Node.Paint(visibleMatrix, pct);
}, function() { // when animation is finished
for(var i = 0; i < visibleMatrix.length; ++i) {
for(var j = 0; j < visibleMatrix[i].length; ++j) {
var node = visibleMatrix[i][j];
if(node.posSch !== null) {
node.rect.x = node.posSch.x; // scheduled pos is the current pos now
node.rect.y = node.posSch.y;
node.posSch = null; // remove pos scheduling
node.posTmp = null;
}
}
}
Us.isRendering = false; // clear blocking flag
if(OnComplete !== undefined) OnComplete();
});
} else {
Node.Paint(visibleMatrix, 1); // no animation
Us.isRendering = false;
if(OnComplete !== undefined) OnComplete();
}
}
};
var Placement = {
Calc: function(visibleMatrix) {
Placement.PreliminarPos(visibleMatrix);
Placement.AdjustInternalNodes(visibleMatrix);
Placement.IncreaseBranchGap(visibleMatrix[0][0]);
Placement.AdjustHorizontally(visibleMatrix);
return visibleMatrix;
},
PreliminarPos: function(visibleMatrix) {
for(var i = 0; i < visibleMatrix.length; ++i) {
var yAccum = 0;
for(var j = 0; j < visibleMatrix[i].length; ++j) {
var node = visibleMatrix[i][j];
node.posSch = { // schedule new positioning
x:i * 80, // arbitrary
y:yAccum + CONSTANTS.yNodePadding
};
yAccum += node.rect.cy + CONSTANTS.yNodePadding * 2;
}
}
},
AdjustInternalNodes: function(visibleMatrix) {
for(var iDep = visibleMatrix.length - 2; iDep >= 0; --iDep) { // bypass deepmost level
var siblings = visibleMatrix[iDep]; // nodes of column iDep
var iLastParent = -1;
for(var iSib = 0; iSib < siblings.length; ++iSib) { // each node of column
if(siblings[iSib].children.length > 0 && siblings[iSib].isExpanded) { // parent, not internal leaf
Placement.AlignToChildren(siblings[iSib]);
if(iLastParent == -1) { // we're 1st parent of this column
var yAccum = siblings[iSib].posSch.y - CONSTANTS.yNodePadding;
for(var i = iSib - 1; i >= 0; --i) { // leaf nodes above 1st parent
yAccum -= siblings[i].rect.cy + 2 * CONSTANTS.yNodePadding;
siblings[i].posSch.y = CONSTANTS.yNodePadding + yAccum;
}
} else if(iLastParent > -1) { // parent node, but not 1st; fail-safe when root
var yTop = siblings[iLastParent].posSch.y +
siblings[iLastParent].rect.cy +
CONSTANTS.yNodePadding;
var yBot = siblings[iSib].posSch.y - CONSTANTS.yNodePadding;
var yMinRoom = 0;
for(var i = iLastParent + 1; i < iSib; ++i)
yMinRoom += siblings[i].rect.cy + 2 * CONSTANTS.yNodePadding;
if(yMinRoom > yBot - yTop)
for(var i = iSib; i < siblings.length; ++i) // everyone beyond moves down
Placement.MoveNodesDown(siblings[i], yMinRoom - (yBot - yTop));
for(var i = iLastParent + 1; i < iSib; ++i) { // internal leaves
var yPercent = (siblings[i].rect.cy + 2 * CONSTANTS.yNodePadding) / yMinRoom;
siblings[i].posSch.y = Math.round(
yTop +
yPercent * Math.max(yMinRoom, yBot - yTop) / 2 -
siblings[i].rect.cy / 2);
yTop += yPercent * Math.max(yMinRoom, yBot - yTop);
}
}
iLastParent = iSib; // we're last parent now
}
}
if(iLastParent > -1) { // fail-safe when root
var yAccum = siblings[iLastParent].posSch.y +
siblings[iLastParent].rect.cy +
CONSTANTS.yNodePadding;
for(var i = iLastParent + 1; i < siblings.length; ++i) { // leaf nodes beyond bottom parent
siblings[i].posSch.y = yAccum + CONSTANTS.yNodePadding;
yAccum += siblings[i].rect.cy + 2 * CONSTANTS.yNodePadding;
}
}
}
},
IncreaseBranchGap: function IncreaseBranchGap(node) {
if(!node.isExpanded) return 0;
var yMoved = 0; // how many pixels the node has moved down
var firstParent = true;
for(var i = 0; i < node.children.length; ++i) {
var child = node.children[i];
if(child.children.length && child.isExpanded) { // parent, not leaf
if(firstParent) {
firstParent = false; // skip 1st parent
} else {
for(var n = i; n < node.children.length; ++n)
Placement.MoveNodesDown(node.children[n], CONSTANTS.yBranchGap); // move down us and beyond
yMoved += CONSTANTS.yBranchGap;
}
var yInc = IncreaseBranchGap(child);
for(var n = i + 1; n < node.children.length; ++n)
Placement.MoveNodesDown(node.children[n], yInc); // move down again, everyone beyond
yMoved += yInc;
}
}
Placement.AlignToChildren(node);
return yMoved;
},
AdjustHorizontally: function(visibleMatrix) {
for(var iDep = 1; iDep < visibleMatrix.length; ++iDep) {
var ourNodes = visibleMatrix[iDep];
if(!ourNodes.length) break;
var off = 0;
var prevNodes = visibleMatrix[iDep - 1];
for(var i = 0; i < prevNodes.length; ++i) {
if(off < prevNodes[i].posSch.x + prevNodes[i].rect.cx + CONSTANTS.xMinPadding)
off = prevNodes[i].posSch.x + prevNodes[i].rect.cx + CONSTANTS.xMinPadding;
}
for(var i = 0; i < ourNodes.length; ++i)
ourNodes[i].posSch.x = off;
}
},
AlignToChildren: function(node) {
if(node.children.length === 0 || !node.isExpanded) return;
var yTop = node.children[0].posSch.y - CONSTANTS.yNodePadding;
var yBot = node.children[node.children.length - 1].posSch.y +
node.children[node.children.length - 1].rect.cy +
CONSTANTS.yNodePadding;
node.posSch.y = Math.round(yTop + (yBot - yTop) / 2 - node.rect.cy / 2);
},
MoveNodesDown: function MoveNodesDown(node, inc) {
node.posSch.y += inc;
if(node.isExpanded)
for(var i = 0; i < node.children.length; ++i)
MoveNodesDown(node.children[i], inc);
},
ResetRootPos: function(visibleMatrix) {
var difx = visibleMatrix[0][0].posSch.x - Math.round(Us.context.canvas.width / 10);
var dify = visibleMatrix[0][0].posSch.y -
Math.round(Us.context.canvas.height / 2 - visibleMatrix[0][0].rect.cy / 2);
for(var i = 0; i < visibleMatrix.length; ++i) {
for(var j = 0; j < visibleMatrix[i].length; ++j) {
visibleMatrix[i][j].posSch.x -= difx; // schedule
visibleMatrix[i][j].posSch.y -= dify;
}
}
return visibleMatrix;
}
};
var Events = {
MouseDown: function(ev) {
if(Us.isRendering) return;
var canvasxy = Util.GetPos(Us.context.canvas);
Us.baseDragPos = { x:ev.pageX - canvasxy.x,
y:ev.pageY - canvasxy.y }; // we're ready to drag now
Us.context.canvas.style.cursor = 'move';
ev.preventDefault();
},
MouseUp: function(ev) {
if(Us.baseDragPos !== null) { // if we're dragging
window.setTimeout(function() { // little delay so click event won't get it
Us.baseDragPos = null;
Us.isDragging = false;
Node.FlushToStorage();
}, 40); // not dragging anymore
var canvasxy = Util.GetPos(Us.context.canvas);
Us.context.canvas.style.cursor =
Node.AtPoint(ev.pageX - canvasxy.x, ev.pageY - canvasxy.y,
Node.VisibleMatrix()) === null ? 'auto' : 'pointer';
ev.preventDefault();
}
},
MouseOut: function() {
if(Us.baseDragPos !== null) {
Us.baseDragPos = null;
Us.isDragging = false;
Node.FlushToStorage();
Us.context.canvas.style.cursor = 'auto';
}
},
MouseMove: function MouseMove(ev) {
if(Us.isRendering) return;
var canvasxy = Util.GetPos(Us.context.canvas);
var pos = { x:ev.pageX - canvasxy.x,
y:ev.pageY - canvasxy.y };
var matrix = Node.VisibleMatrix();
if(Us.baseDragPos === null) { // we're not dragging
function SameNode(node1, node2) { // are the two nodes the same one?
if(node1 === null && node2 === null) return true;
if(node1 !== null || node2 !== null) return false;
return node1.id === node2.id;
}
if(MouseMove.prev === undefined)
MouseMove.prev = null; // static variable to hold previous hovered
var target = Node.AtPoint(pos.x, pos.y, matrix);
if(!SameNode(target, MouseMove.prev)) {
MouseMove.prev = target;
Us.context.canvas.style.cursor = (target !== null) ? 'pointer' : 'auto';
}
} else { // we're dragging
Us.isDragging = true; // will abort click event after mouseup
for(var i = 0; i < matrix.length; ++i) { // apply coordinates to all visible nodes
for(var j = 0; j < matrix[i].length; ++j) {
matrix[i][j].rect.x += pos.x - Us.baseDragPos.x;
matrix[i][j].rect.y += pos.y - Us.baseDragPos.y;
}
}
Us.baseDragPos = { x:pos.x, y:pos.y };
Node.Paint(matrix, 1); // no animation
}
ev.preventDefault();
},
Click: function(ev) {
if(Us.isRendering || Us.isDragging) return;
var canvasxy = Util.GetPos(Us.context.canvas);
var matrix = Node.VisibleMatrix(),
cursorPt = { x:ev.pageX - canvasxy.x, y:ev.pageY - canvasxy.y },
target = Node.AtPoint(cursorPt.x, cursorPt.y, matrix); // null if none
if(target === null) return;
if(ev.ctrlKey) { // Ctrl+click
if(Us.callbacks.CtrlClick !== null)
Us.callbacks.CtrlClick.call(Us.retObj, target, ev); // pass clicked node and event as arguments
} else { // non-Ctrl click
if(target.children.length > 0) { // clicked node has children
function SetChildrenPos(node, pos, posSch) {
for(var i = 0; i < node.children.length; ++i) {
if(pos !== null) { // set current position
node.children[i].rect.x = pos.x;
node.children[i].rect.y = pos.y;
}
if(posSch !== undefined && posSch !== null) // set scheduled position
node.children[i].posSch = { x:posSch.x, y:posSch.y };
SetChildrenPos(node.children[i], pos, posSch);
}
}
function CenterNodeAtPoint(x, y, node, visibleMatrix) {
var off = {
x: x - (node.posSch !== null ? node.posSch.x : node.rect.x) - (x - node.rect.x),
y: y - (node.posSch !== null ? node.posSch.y : node.rect.y) - (y - node.rect.y)
};
for(var i = 0; i < visibleMatrix.length; ++i) {
for(var j = 0; j < visibleMatrix[i].length; ++j) {
var cur = visibleMatrix[i][j];
if(cur.posSch !== null) { // set scheduled position
cur.posSch.x += off.x;
cur.posSch.y += off.y;
} else { // set current (static) position
cur.rect.x += off.x;
cur.rect.y += off.y;
}
}
}
return visibleMatrix;
}
if(!target.isExpanded) { // node will be expanded
target.isExpanded = true;
SetChildrenPos(target, target.rect, null);
matrix = Node.VisibleMatrix();
Placement.Calc(matrix);
CenterNodeAtPoint(cursorPt.x, cursorPt.y, target, matrix);
Node.Render(matrix, function() {
Node.FlushToStorage();
if(Us.callbacks.Click !== null)
Us.callbacks.Click.call(Us.retObj, target, ev);
});
} else { // node will be collapsed
target.isExpanded = false;
matrix = Placement.Calc(Node.VisibleMatrix()); // will calc final pos of clicked node (and above) only
target.isExpanded = true;
SetChildrenPos(target, null, target.posSch);
matrix = Node.VisibleMatrix();
CenterNodeAtPoint(cursorPt.x, cursorPt.y, target, matrix);
Node.Render(matrix, function() {
target.isExpanded = false;
matrix = Node.VisibleMatrix();
Placement.Calc(matrix);
CenterNodeAtPoint(cursorPt.x, cursorPt.y, target, matrix);
Node.Render(matrix, function() { // no animation, remove collapsed
Node.FlushToStorage();
if(Us.callbacks.Click !== null)
Us.callbacks.Click.call(Us.retObj, target, ev);
});
});
}
} else { // clicked node has no children
if(Us.callbacks.Click !== null)
Us.callbacks.Click.call(Us.retObj, target, ev);
}
}
},
SelectStart: function(ev) {
ev.preventDefault();
}
};
return Util.AddPropertiesIfNotExist(Us.retObj, {
click: function(Callback) { Us.callbacks.Click = Callback; return Us.retObj; },
ctrlClick: function(Callback) { Us.callbacks.CtrlClick = Callback; return Us.retObj; },
load: function(rootNode) { Node.Load(rootNode); return Us.retObj; },
collapseAll: function() { Node.CollapseAll(); return Us.retObj; },
expandAll: function() { Node.ExpandAll(); return Us.retObj; },
countNodes: function() { return Node.Count(); },
redraw: function() { Node.Render(Node.VisibleMatrix()); return Us.retObj; },
clear: function() { Us.context.clearRect(0, 0, Us.context.canvas.width, Us.context.canvas.height); return Us.retObj; },
fitParentContainer: function() { Node.FitParentContainer(); return Us.retObj; }
});
}