-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpega_yui_treeview.js
2248 lines (1928 loc) · 62.5 KB
/
pega_yui_treeview.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.2.2
*/
/**
* The treeview widget is a generic tree building tool.
* @module treeview
* @title TreeView Widget
* @requires pega, event
* @optional animation
* @namespace pega.widget
*/
/**
* Contains the tree view state data and the root node.
*
* @class TreeView
* @uses pega.util.EventProvider
* @constructor
* @param {string|HTMLElement} id The id of the element, or the element
* itself that the tree will be inserted into.
*/
pega.widget.TreeView = function(id) {
if (id) { this.init(id); }
};
pega.widget.TreeView.prototype = {
/**
* The id of tree container element
* @property id
* @type String
*/
id: null,
/**
* The host element for this tree
* @property _el
* @private
*/
_el: null,
/**
* Flat collection of all nodes in this tree. This is a sparse
* array, so the length property can't be relied upon for a
* node count for the tree.
* @property _nodes
* @type Node[]
* @private
*/
_nodes: null,
/**
* We lock the tree control while waiting for the dynamic loader to return
* @property locked
* @type boolean
*/
locked: false,
/**
* The animation to use for expanding children, if any
* @property _expandAnim
* @type string
* @private
*/
_expandAnim: null,
/**
* The animation to use for collapsing children, if any
* @property _collapseAnim
* @type string
* @private
*/
_collapseAnim: null,
/**
* The current number of animations that are executing
* @property _animCount
* @type int
* @private
*/
_animCount: 0,
/**
* The maximum number of animations to run at one time.
* @property maxAnim
* @type int
*/
maxAnim: 2,
/**
* Sets up the animation for expanding children
* @method setExpandAnim
* @param {string} type the type of animation (acceptable values defined
* in pega.widget.TVAnim)
*/
setExpandAnim: function(type) {
if (pega.widget.TVAnim.isValid(type)) {
this._expandAnim = type;
}
},
/**
* Sets up the animation for collapsing children
* @method setCollapseAnim
* @param {string} the type of animation (acceptable values defined in
* pega.widget.TVAnim)
*/
setCollapseAnim: function(type) {
if (pega.widget.TVAnim.isValid(type)) {
this._collapseAnim = type;
}
},
/**
* Perform the expand animation if configured, or just show the
* element if not configured or too many animations are in progress
* @method animateExpand
* @param el {HTMLElement} the element to animate
* @param node {pega.util.Node} the node that was expanded
* @return {boolean} true if animation could be invoked, false otherwise
*/
animateExpand: function(el, node) {
if (this._expandAnim && this._animCount < this.maxAnim) {
// this.locked = true;
var tree = this;
var a = pega.widget.TVAnim.getAnim(this._expandAnim, el,
function() { tree.expandComplete(node); });
if (a) {
++this._animCount;
this.fireEvent("animStart", {
"node": node,
"type": "expand"
});
a.animate();
}
return true;
}
return false;
},
/**
* Perform the collapse animation if configured, or just show the
* element if not configured or too many animations are in progress
* @method animateCollapse
* @param el {HTMLElement} the element to animate
* @param node {pega.util.Node} the node that was expanded
* @return {boolean} true if animation could be invoked, false otherwise
*/
animateCollapse: function(el, node) {
if (this._collapseAnim && this._animCount < this.maxAnim) {
// this.locked = true;
var tree = this;
var a = pega.widget.TVAnim.getAnim(this._collapseAnim, el,
function() { tree.collapseComplete(node); });
if (a) {
++this._animCount;
this.fireEvent("animStart", {
"node": node,
"type": "collapse"
});
a.animate();
}
return true;
}
return false;
},
/**
* Function executed when the expand animation completes
* @method expandComplete
*/
expandComplete: function(node) {
--this._animCount;
this.fireEvent("animComplete", {
"node": node,
"type": "expand"
});
// this.locked = false;
},
/**
* Function executed when the collapse animation completes
* @method collapseComplete
*/
collapseComplete: function(node) {
--this._animCount;
this.fireEvent("animComplete", {
"node": node,
"type": "collapse"
});
// this.locked = false;
},
/**
* Initializes the tree
* @method init
* @parm {string|HTMLElement} id the id of the element that will hold the tree
* @private
*/
init: function(id) {
this.id = id;
if ("string" !== typeof id) {
this._el = id;
this.id = this.generateId(id);
}
/**
* When animation is enabled, this event fires when the animation
* starts
* @event animStart
* @type CustomEvent
* @param {pega.widget.Node} node the node that is expanding/collapsing
* @parm {String} type the type of animation ("expand" or "collapse")
*/
this.createEvent("animStart", this);
/**
* When animation is enabled, this event fires when the animation
* completes
* @event animComplete
* @type CustomEvent
* @param {pega.widget.Node} node the node that is expanding/collapsing
* @parm {String} type the type of animation ("expand" or "collapse")
*/
this.createEvent("animComplete", this);
/**
* Fires when a node is going to be collapsed. Return false to stop
* the collapse.
* @event collapse
* @type CustomEvent
* @param {pega.widget.Node} node the node that is collapsing
*/
this.createEvent("collapse", this);
/**
* Fires after a node is successfully collapsed. This event will not fire
* if the "collapse" event was cancelled.
* @event collapseComplete
* @type CustomEvent
* @param {pega.widget.Node} node the node that was collapsed
*/
this.createEvent("collapseComplete", this);
/**
* Fires when a node is going to be expanded. Return false to stop
* the collapse.
* @event expand
* @type CustomEvent
* @param {pega.widget.Node} node the node that is expanding
*/
this.createEvent("expand", this);
/**
* Fires after a node is successfully expanded. This event will not fire
* if the "expand" event was cancelled.
* @event expandComplete
* @type CustomEvent
* @param {pega.widget.Node} node the node that was expanded
*/
this.createEvent("expandComplete", this);
this._nodes = [];
// store a global reference
pega.widget.TreeView.trees[this.id] = this;
// Set up the root node
this.root = new pega.widget.RootNode(this);
//pega.util.Event.onContentReady(this.id, this.handleAvailable, this, true);
pega.util.Event.on(this.id, "click", this.handleClick, this, true);
},
//handleAvailable: function() {
//var Event = pega.util.Event;
//Event.on(this.id,
//},
/**
* Renders the tree boilerplate and visible nodes
* @method draw
*/
draw: function() {
var html = this.root.getHtml();
this.getEl().innerHTML = html;
this.firstDraw = false;
},
/**
* Returns the tree's host element
* @method getEl
* @return {HTMLElement} the host element
*/
getEl: function() {
if (! this._el) {
this._el = document.getElementById(this.id);
}
return this._el;
},
/**
* Nodes register themselves with the tree instance when they are created.
* @method regNode
* @param node {Node} the node to register
* @private
*/
regNode: function(node) {
this._nodes[node.index] = node;
},
/**
* Returns the root node of this tree
* @method getRoot
* @return {Node} the root node
*/
getRoot: function() {
return this.root;
},
/**
* Configures this tree to dynamically load all child data
* @method setDynamicLoad
* @param {function} fnDataLoader the function that will be called to get the data
* @param iconMode {int} configures the icon that is displayed when a dynamic
* load node is expanded the first time without children. By default, the
* "collapse" icon will be used. If set to 1, the leaf node icon will be
* displayed.
*/
setDynamicLoad: function(fnDataLoader, iconMode) {
this.root.setDynamicLoad(fnDataLoader, iconMode);
},
/**
* Expands all child nodes. Note: this conflicts with the "multiExpand"
* node property. If expand all is called in a tree with nodes that
* do not allow multiple siblings to be displayed, only the last sibling
* will be expanded.
* @method expandAll
*/
expandAll: function() {
if (!this.locked) {
this.root.expandAll();
}
},
/**
* Collapses all expanded child nodes in the entire tree.
* @method collapseAll
*/
collapseAll: function() {
if (!this.locked) {
this.root.collapseAll();
}
},
/**
* Returns a node in the tree that has the specified index (this index
* is created internally, so this function probably will only be used
* in html generated for a given node.)
* @method getNodeByIndex
* @param {int} nodeIndex the index of the node wanted
* @return {Node} the node with index=nodeIndex, null if no match
*/
getNodeByIndex: function(nodeIndex) {
var n = this._nodes[nodeIndex];
return (n) ? n : null;
},
/**
* Returns a node that has a matching property and value in the data
* object that was passed into its constructor.
* @method getNodeByProperty
* @param {object} property the property to search (usually a string)
* @param {object} value the value we want to find (usuall an int or string)
* @return {Node} the matching node, null if no match
*/
getNodeByProperty: function(property, value) {
for (var i in this._nodes) {
var n = this._nodes[i];
if (n.data && value == n.data[property]) {
return n;
}
}
return null;
},
/**
* Returns a collection of nodes that have a matching property
* and value in the data object that was passed into its constructor.
* @method getNodesByProperty
* @param {object} property the property to search (usually a string)
* @param {object} value the value we want to find (usuall an int or string)
* @return {Array} the matching collection of nodes, null if no match
*/
getNodesByProperty: function(property, value) {
var values = [];
for (var i in this._nodes) {
var n = this._nodes[i];
if (n.data && value == n.data[property]) {
values.push(n);
}
}
return (values.length) ? values : null;
},
/**
* Removes the node and its children, and optionally refreshes the
* branch of the tree that was affected.
* @method removeNode
* @param {Node} The node to remove
* @param {boolean} autoRefresh automatically refreshes branch if true
* @return {boolean} False is there was a problem, true otherwise.
*/
removeNode: function(node, autoRefresh) {
// Don't delete the root node
if (node.isRoot()) {
return false;
}
// Get the branch that we may need to refresh
var p = node.parent;
if (p.parent) {
p = p.parent;
}
// Delete the node and its children
this._deleteNode(node);
// Refresh the parent of the parent
if (autoRefresh && p && p.childrenRendered) {
p.refresh();
}
return true;
},
/**
* Deletes this nodes child collection, recursively. Also collapses
* the node, and resets the dynamic load flag. The primary use for
* this method is to purge a node and allow it to fetch its data
* dynamically again.
* @method removeChildren
* @param {Node} node the node to purge
*/
removeChildren: function(node) {
while (node.children.length) {
this._deleteNode(node.children[0]);
}
node.childrenRendered = false;
node.dynamicLoadComplete = false;
if (node.expanded) {
node.collapse();
} else {
node.updateIcon();
}
},
/**
* Deletes the node and recurses children
* @method _deleteNode
* @private
*/
_deleteNode: function(node) {
// Remove all the child nodes first
this.removeChildren(node);
// Remove the node from the tree
this.popNode(node);
},
/**
* Removes the node from the tree, preserving the child collection
* to make it possible to insert the branch into another part of the
* tree, or another tree.
* @method popNode
* @param {Node} the node to remove
*/
popNode: function(node) {
var p = node.parent;
// Update the parent's collection of children
var a = [];
for (var i=0, len=p.children.length;i<len;++i) {
if (p.children[i] != node) {
a[a.length] = p.children[i];
}
}
p.children = a;
// reset the childrenRendered flag for the parent
p.childrenRendered = false;
// Update the sibling relationship
if (node.previousSibling) {
node.previousSibling.nextSibling = node.nextSibling;
}
if (node.nextSibling) {
node.nextSibling.previousSibling = node.previousSibling;
}
node.parent = null;
node.previousSibling = null;
node.nextSibling = null;
node.tree = null;
// Update the tree's node collection
delete this._nodes[node.index];
},
/**
* TreeView instance toString
* @method toString
* @return {string} string representation of the tree
*/
toString: function() {
return "TreeView " + this.id;
},
/**
* Generates an unique id for an element if it doesn't yet have one
* @method generateId
* @private
*/
generateId: function(el) {
var id = el.id;
if (!id) {
id = "yui-tv-auto-id-" + pega.widget.TreeView.counter;
++pega.widget.TreeView.counter;
}
return id;
},
/**
* Abstract method that is executed when a node is expanded
* @method onExpand
* @param node {Node} the node that was expanded
* @deprecated use treeobj.subscribe("expand") instead
*/
onExpand: function(node) { },
/**
* Abstract method that is executed when a node is collapsed.
* @method onCollapse
* @param node {Node} the node that was collapsed.
* @deprecated use treeobj.subscribe("collapse") instead
*/
onCollapse: function(node) { }
};
pega.augment(pega.widget.TreeView, pega.util.EventProvider);
/**
* Running count of all nodes created in all trees. This is
* used to provide unique identifies for all nodes. Deleting
* nodes does not change the nodeCount.
* @property pega.widget.TreeView.nodeCount
* @type int
* @static
*/
pega.widget.TreeView.nodeCount = 0;
/**
* Global cache of tree instances
* @property pega.widget.TreeView.trees
* @type Array
* @static
* @private
*/
pega.widget.TreeView.trees = [];
/**
* Counter for generating a new unique element id
* @property pega.widget.TreeView.counter
* @static
* @private
*/
pega.widget.TreeView.counter = 0;
/**
* Global method for getting a tree by its id. Used in the generated
* tree html.
* @method pega.widget.TreeView.getTree
* @param treeId {String} the id of the tree instance
* @return {TreeView} the tree instance requested, null if not found.
* @static
*/
pega.widget.TreeView.getTree = function(treeId) {
var t = pega.widget.TreeView.trees[treeId];
return (t) ? t : null;
};
/**
* Global method for getting a node by its id. Used in the generated
* tree html.
* @method pega.widget.TreeView.getNode
* @param treeId {String} the id of the tree instance
* @param nodeIndex {String} the index of the node to return
* @return {Node} the node instance requested, null if not found
* @static
*/
pega.widget.TreeView.getNode = function(treeId, nodeIndex) {
var t = pega.widget.TreeView.getTree(treeId);
return (t) ? t.getNodeByIndex(nodeIndex) : null;
};
/**
* Add a DOM event
* @method pega.widget.TreeView.addHandler
* @param el the elment to bind the handler to
* @param {string} sType the type of event handler
* @param {function} fn the callback to invoke
* @static
*/
pega.widget.TreeView.addHandler = function (el, sType, fn) {
if (el.addEventListener) {
el.addEventListener(sType, fn, false);
} else if (el.attachEvent) {
el.attachEvent("on" + sType, fn);
}
};
/**
* Remove a DOM event
* @method pega.widget.TreeView.removeHandler
* @param el the elment to bind the handler to
* @param {string} sType the type of event handler
* @param {function} fn the callback to invoke
* @static
*/
pega.widget.TreeView.removeHandler = function (el, sType, fn) {
if (el.removeEventListener) {
el.removeEventListener(sType, fn, false);
} else if (el.detachEvent) {
el.detachEvent("on" + sType, fn);
}
};
/**
* Attempts to preload the images defined in the styles used to draw the tree by
* rendering off-screen elements that use the styles.
* @method pega.widget.TreeView.preload
* @param {string} prefix the prefix to use to generate the names of the
* images to preload, default is ygtv
* @static
*/
pega.widget.TreeView.preload = function(prefix) {
prefix = prefix || "ygtv";
var styles = ["tn","tm","tmh","tp","tph","ln","lm","lmh","lp","lph","loading"];
var sb = [];
for (var i = 0; i < styles.length; ++i) {
sb[sb.length] = '<span class="' + prefix + styles[i] + '"> </span>';
}
var f = document.createElement("div");
var s = f.style;
s.position = "absolute";
s.top = "-1000px";
s.left = "-1000px";
f.innerHTML = sb.join("");
document.body.appendChild(f);
pega.widget.TreeView.removeHandler(window,
"load", pega.widget.TreeView.preload);
};
pega.widget.TreeView.addHandler(window,
"load", pega.widget.TreeView.preload);
/**
* The base class for all tree nodes. The node's presentation and behavior in
* response to mouse events is handled in Node subclasses.
* @namespace pega.widget
* @class Node
* @uses pega.util.EventProvider
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
* @constructor
*/
pega.widget.Node = function(oData, oParent, expanded) {
if (oData) { this.init(oData, oParent, expanded); }
};
pega.widget.Node.prototype = {
/**
* The index for this instance obtained from global counter in pega.widget.TreeView.
* @property index
* @type int
*/
index: 0,
/**
* This node's child node collection.
* @property children
* @type Node[]
*/
children: null,
/**
* Tree instance this node is part of
* @property tree
* @type TreeView
*/
tree: null,
/**
* The data linked to this node. This can be any object or primitive
* value, and the data can be used in getNodeHtml().
* @property data
* @type object
*/
data: null,
/**
* Parent node
* @property parent
* @type Node
*/
parent: null,
/**
* The depth of this node. We start at -1 for the root node.
* @property depth
* @type int
*/
depth: -1,
/**
* The href for the node's label. If one is not specified, the href will
* be set so that it toggles the node.
* @property href
* @type string
*/
href: null,
/**
* The label href target, defaults to current window
* @property target
* @type string
*/
target: "_self",
/**
* The node's expanded/collapsed state
* @property expanded
* @type boolean
*/
expanded: false,
/**
* Can multiple children be expanded at once?
* @property multiExpand
* @type boolean
*/
multiExpand: true,
/**
* Should we render children for a collapsed node? It is possible that the
* implementer will want to render the hidden data... @todo verify that we
* need this, and implement it if we do.
* @property renderHidden
* @type boolean
*/
renderHidden: false,
/**
* This flag is set to true when the html is generated for this node's
* children, and set to false when new children are added.
* @property childrenRendered
* @type boolean
*/
childrenRendered: false,
/**
* Dynamically loaded nodes only fetch the data the first time they are
* expanded. This flag is set to true once the data has been fetched.
* @property dynamicLoadComplete
* @type boolean
*/
dynamicLoadComplete: false,
/**
* This node's previous sibling
* @property previousSibling
* @type Node
*/
previousSibling: null,
/**
* This node's next sibling
* @property nextSibling
* @type Node
*/
nextSibling: null,
/**
* We can set the node up to call an external method to get the child
* data dynamically.
* @property _dynLoad
* @type boolean
* @private
*/
_dynLoad: false,
/**
* Function to execute when we need to get this node's child data.
* @property dataLoader
* @type function
*/
dataLoader: null,
/**
* This is true for dynamically loading nodes while waiting for the
* callback to return.
* @property isLoading
* @type boolean
*/
isLoading: false,
/**
* The toggle/branch icon will not show if this is set to false. This
* could be useful if the implementer wants to have the child contain
* extra info about the parent, rather than an actual node.
* @property hasIcon
* @type boolean
*/
hasIcon: true,
/**
* Used to configure what happens when a dynamic load node is expanded
* and we discover that it does not have children. By default, it is
* treated as if it still could have children (plus/minus icon). Set
* iconMode to have it display like a leaf node instead.
* @property iconMode
* @type int
*/
iconMode: 0,
/**
* Specifies whether or not the content area of the node should be allowed
* to wrap.
* @property nowrap
* @type boolean
* @default false
*/
nowrap: false,
/**
* The node type
* @property _type
* @private
*/
_type: "Node",
/*
spacerPath: "http://us.i1.yimg.com/us.yimg.com/i/space.gif",
expandedText: "Expanded",
collapsedText: "Collapsed",
loadingText: "Loading",
*/
/**
* Initializes this node, gets some of the properties from the parent
* @method init
* @param oData {object} a string or object containing the data that will
* be used to render this node
* @param oParent {Node} this node's parent node
* @param expanded {boolean} the initial expanded/collapsed state
*/
init: function(oData, oParent, expanded) {
this.data = oData;
this.children = [];
this.index = pega.widget.TreeView.nodeCount;
++pega.widget.TreeView.nodeCount;
this.expanded = expanded;
/**
* The parentChange event is fired when a parent element is applied
* to the node. This is useful if you need to apply tree-level
* properties to a tree that need to happen if a node is moved from
* one tree to another.
*
* @event parentChange
* @type CustomEvent
*/
this.createEvent("parentChange", this);
// oParent should never be null except when we create the root node.
if (oParent) {
oParent.appendChild(this);
}
},
/**
* Certain properties for the node cannot be set until the parent
* is known. This is called after the node is inserted into a tree.
* the parent is also applied to this node's children in order to
* make it possible to move a branch from one tree to another.
* @method applyParent
* @param {Node} parentNode this node's parent node
* @return {boolean} true if the application was successful
*/
applyParent: function(parentNode) {
if (!parentNode) {
return false;
}
this.tree = parentNode.tree;
this.parent = parentNode;
this.depth = parentNode.depth + 1;
if (!this.href) {
this.href = "javascript:" + this.getToggleLink();
}
// @todo why was this put here. This causes new nodes added at the
// root level to lose the menu behavior.
// if (! this.multiExpand) {
// this.multiExpand = parentNode.multiExpand;
// }
this.tree.regNode(this);
parentNode.childrenRendered = false;
// cascade update existing children
for (var i=0, len=this.children.length;i<len;++i) {
this.children[i].applyParent(this);
}
this.fireEvent("parentChange");
return true;
},
/**
* Appends a node to the child collection.
* @method appendChild
* @param childNode {Node} the new node
* @return {Node} the child node
* @private
*/
appendChild: function(childNode) {
if (this.hasChildren()) {
var sib = this.children[this.children.length - 1];
sib.nextSibling = childNode;
childNode.previousSibling = sib;
}
this.children[this.children.length] = childNode;
childNode.applyParent(this);
return childNode;
},
/**
* Appends this node to the supplied node's child collection
* @method appendTo
* @param parentNode {Node} the node to append to.
* @return {Node} The appended node
*/
appendTo: function(parentNode) {
return parentNode.appendChild(this);
},
/**
* Inserts this node before this supplied node