-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxtree.pas
2350 lines (2102 loc) · 75.6 KB
/
xtree.pas
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) 2018 Steve Wright
This unit is part of the XComponents package.
This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************
*)
unit XTree;
{$ifndef JScript}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils,TypInfo, NodeUtils,StringUtils,EventsInterface, Events,
{$ifndef JScript}
fpjson, jsonparser,LazLogger,
LResources, Forms, Controls, StdCtrls, Graphics, Dialogs, ExtCtrls,ComCtrls,
Propedits,RTTICtrls,
LazsUtils,
{$else}
HTMLUtils,
{$endif}
WrapperPanel;
type TTreeNodeHint = function(TreeLabelStr:String):String of object;
{$ifndef JScript}
type TXTree=class;
//type TTreeNodeDropAccepted = function(myTree:TObject; SourceName,SrcText,DstText:String):Boolean of object;
{$else}
//type TTreeNodeDropAccepted = function(myTree:TObject; SourceName,SrcText,DstText:String):Boolean of object;
const TreeNodeHighlightColor:String = '#ffff00'; //yellow
function addTreeStyles(dummy:string):string;
function addTreeNode(WrapperNodeId,NameSpace,parentName,NameOfDetailsList,SummaryText,HasChildren,color,isopen:string;
NodeHintFunc:TTreeNodeHint;Draggable:Boolean):string;
function addnode(WrapperNodeId,NameSpace,ParentName,currentNodeTree,IdOfNodeBeingAdded:string;
OpenToLevel,level:integer;normalColor:String;NodeHintFunc:TTreeNodeHint;Draggable:Boolean):string;
function SetOpenStatusOfNodeChildren(NodeName,NameOfSelectedNode:string;level:integer):boolean;
function GetTreeRootID(NodeID:String):String;
function deselectNodeChildren(NodeName,normalColor:String):string;
function clearNodeSelectedMarker(NameOfDetailsList,normalColor:String):string;
function OpenAndScrollToSelectedNode(NameOfDetailsList:string):string;
procedure clearTreeNode(parentName:string);
procedure HandleTreeNodeClick(WrapperNodeId,NameSpace,SelectedNodeObjectId:String);
procedure DragStart(nodeId,NameSpace,NameOfDetailsList,SummaryText:String);
function HandleTreeNodeDragOver(ob:TObject;DestTreeId,NameSpace,DstText:string): Boolean;
procedure HandleTreeNodeDrop(ob:TObject;DestTreeId,NameSpace,DstText:string);
function NodeIdFromText(TreeNode:TDataNode;NodeText:String):String;
procedure SetDraggableAttribute(NodeName,draggable:string);
{$endif}
{$ifndef JScript}
type
PNodeDataRec = ^TNodeDataRec;
TNodeDataRec = record
ID:String;
NodeHint: string;
OriginalText:String;
end;
type TMyTreeView = class(TTreeView)
public
var
lastHintNode : TTreeNode;
IsDropTarget:Boolean;
DropTargetNode:TTreeNode;
procedure CustomDrawTreeNode(Sender: TCustomTreeView;Node: TTreeNode; State: TCustomDrawState;var DefaultDraw: Boolean) ;
procedure TreeSelectedNodeChange(Sender: TObject; Node: TTreeNode);
procedure TreeEditingEnd(Sender:TObject; Node:TTreeNode; Cancel:Boolean);
procedure HandleClick(Sender: TObject) ;
procedure HandleMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
procedure HandleDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure HandleDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure HandleMouseLeave(Sender: TObject) ;
procedure KillEditing(Sender: TObject; Node:TTreeNode; xx:Boolean) ;
function NodeHint(tn: TTreeNode): string;
function AddATreeNode(ParentNode:TTreeNode; nodeText,TreeName:string):TTreeNode;
procedure PopulateMeFromJSONData(NodeTreeString,RootName:String;openlvl:integer);
procedure selectNodeByText(NodeText:String);
procedure selectNodeByTextPath(PathText:String);
procedure selectNodeById(NodeId:String);
function AddNewNode(NodeItems:TTreeNodes;ParentNode:TTreeNode; NodeString,TreeName:string):TTreeNode;
function AddChildren(jData: TJSONData; ParentNode:TTreeNode;RootName:String):TTreeNode;
procedure ExpandTreeNodes(Level: Integer);
function BuildExpandedText(thisNode:TTreeNode):String;
procedure SetNodeHint(Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
function BuildTreeDataString(FromNode:TTreeNode;str:String):String;
function FindNodeByText(AValue:String):TTreeNode;
function FindNodeByExpandedText(AValue:String):TTreeNode;
function NodeTextIsUnique(NodeText:String):Boolean;
function FindNodeById(NodeId:String):TTreeNode;
end;
{$endif}
type
TXTree = class(TWrapperPanel)
private
{ Private declarations }
fAllowDrop:Boolean;
fTreeNodeHint:TTreeNodeHint;
fSelectedNodeId:String;
{$ifndef JScript}
fHandleClick:TEventHandler;
fHandleChange:TEventHandler;
fHandleDragStart:TEventHandler;
fHandleDrop:TEventHandler;
fDropAccepted:TEventHandler;
procedure TreeClick(Sender:TObject);
{$else}
fNodeBeingDragged:String;
{$endif}
procedure SetMyEventTypes;
function GetTreeData:string;
function GetSelectedNodeText:string;
function GetSelectedTextPath:String;
function GetTreeWidth:string;
function GetTreeHeight:string;
function GetReadOnly:Boolean;
function GetDraggable:Boolean;
function GetopenTolevel:Integer;
procedure SetTreeData(NodeTreeString:string);
procedure SetSelectedNodeText(AValue:string);
procedure SetTreeWidth(AValue:string);
procedure SetTreeHeight(AValue:string);
procedure SetReadOnly(AValue:Boolean);
procedure SetDraggable(AValue:Boolean);
procedure SetOpenToLevel(AValue:Integer);
protected
{ Protected declarations }
{$ifndef JScript}
procedure DoConstructor(TheOwner:TComponent;IsDynamic:Boolean);
property ParentColor;
{$endif}
public
{ Public declarations }
{$ifndef JScript}
constructor Create(TheOwner: TComponent); override;
constructor Create(TheOwner: TComponent;IsDynamic:Boolean); override;
{$else}
constructor Create(MyForm:TForm;NodeName,NameSpace:String);
{$endif}
procedure DeSelectNode;
function InsertNewSiblingNode(NodeText:String):String;
function InsertNewChildNode(NodeText:String):String;
procedure DeleteSelectedNode;
procedure MoveNode(SourceNodeText,DestNodeText:String);
function NodeTextIsUnique(NodeText:String):Boolean;
function BuildTreeDataString:String;
procedure SelectTreeNodeById(NodeId:string);
procedure SetSelectedNodeId(AValue:String);
procedure SelectTreeNodeByPath(NodePath:string);
{$ifndef JScript}
procedure SelectTreeNodeByText(NodeText:string);
{$else}
function GetChildNodes(nodeId:String):TStringArray;
function TextOfNode(nodeId:String):String;
function NumChildNodes(nodeid:String):integer;
procedure SetNodeText(nodeid,newtext:String);
function getParentOfNode(nodeid:String):String;
{$endif}
published
{ Published declarations }
// Properties defined for this class...
property SelectedNodeText: String read GetSelectedNodeText write SetSelectedNodeText;
property SelectedTextPath:String read GetSelectedTextPath; //read-only property
property TreeData: String read GetTreeData write SetTreeData;
property TreeHeight: String read GetTreeHeight write SetTreeHeight;
property TreeWidth: String read GetTreeWidth write SetTreeWidth;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly;
property Draggable: Boolean read GetDraggable write SetDraggable;
property OpenToLevel: Integer read GetOpenToLevel write SetOpenToLevel;
property AllowDrop:Boolean read fAllowDrop write fAllowDrop;
// these properties will appear as events on Lazarus IDE...
property TreeNodeHintFunc: TTreeNodeHint read FTreeNodeHint write FTreeNodeHint;
{$ifndef JScript}
// Events to be visible in Lazarus IDE
property HandleClick: TEventHandler read FHandleClick write FHandleClick;
property HandleTreeNodeClick: TEventHandler read FHandleChange write FHandleChange;
property HandleDragStart: TEventHandler read FHandleDragStart write FHandleDragStart;
property HandleDrop: TEventHandler read FHandleDrop write FHandleDrop;
property HandleDropAccepted: TEventHandler read FDropAccepted write FDropAccepted;
{$else}
property NodeBeingDragged: String read fNodeBeingDragged write fNodeBeingDragged; // NodeBeingDragged is a SUMMARY item in the HTML tree structure
{$endif}
property SelectedNodeId: String read fSelectedNodeId write SetSelectedNodeId; // SelectedNodeId is a SUMMARY item in the HTML tree structure
end;
{$ifndef JScript}
//var
// TreeNodeIdCounter:longint;
procedure Register;
{$endif}
var
DraggingTree:TXTree;
implementation
const MyNodeType='TXTree';
const
ExampleNodeTree = '["Root",["Item1","Level2 Item"],"Item2","Item3",["Item4","Child Item"],"Item5"]';
var
myDefaultAttribs:TDefaultAttributesArray;
procedure TXTree.SetMyEventTypes;
begin
MyEventTypes.Add('Created');
MyEventTypes.Add('Click');
MyEventTypes.Add('TreeNodeClick');
MyEventTypes.Add('DragStart');
MyEventTypes.Add('Drop');
MyEventTypes.Add('DropAccepted');
end;
{$ifndef JScript}
procedure Register;
begin
{$I xtree_icon.lrs}
RegisterComponents('XComponents',[TXTree]);
// inherited from TWrapperPanel, not required here
RegisterPropertyEditor(TypeInfo(TColor), TXTree, 'BgColor', THiddenPropertyEditor);
// RegisterPropertyEditor(TypeInfo(TXPropertyLink), TXTree, 'Link', THiddenPropertyEditor);
end;
function GetNewNodeId(ParentNode:TTreeNode; TreeName:String):String;
var
str:String;
PNode,sibNode:TTreeNode;
c:integer;
begin
//TreeNodeIdCounter:=TreeNodeIdCounter+1;
//result:=inttostr(TreeNodeIdCounter);
pNode:=ParentNode;
if pNode<>nil then
begin
sibNode:=pNode.GetLastChild;
if sibNode<>nil then
c:=sibNode.Index+1
else
c:=0;
end
else
c:=0;
str:=intToStr(c);
while (pNode<>nil) do
begin
c:=PNode.Index; // num within sibling set??
str:=intToStr(c)+'_'+str;
pNode:=pNode.Parent;
end;
result:=TreeName+str;
end;
constructor TXTree.Create(TheOwner:TComponent);
begin
inherited Create(TheOwner,false);
DoConstructor(TheOwner,false);
end;
constructor TXTree.Create(TheOwner:TComponent;IsDynamic:Boolean);
begin
inherited Create(TheOwner,IsDynamic);
DoConstructor(TheOwner,IsDynamic);
end;
procedure TXTree.DoConstructor(TheOwner:TComponent;IsDynamic:Boolean);
begin
myControl:=TmyTreeView.Create(self);
myControl.Parent:=self;
myControl.SetSubComponent(true); // Tell the IDE to store the modified properties
// Make sure the embedded component can not be selected/deleted within the IDE
myControl.ControlStyle := myControl.ControlStyle - [csNoDesignSelectable];
TmyTreeView(myControl).DragMode:=dmAutomatic;
TmyTreeView(myControl).IsDropTarget:=false;
TmyTreeView(myControl).ShowHint:=true;
//myControl.Enabled:=true; //????
TmyTreeView(myControl).ReadOnly:=false; //????
//TmyTreeView(myControl).ReadOnly:=true; // prevent editing nodes. !!Lazarus. doesn't work
//TmyTreeView(myControl).OnEditing:=@TmyTreeView(myControl).KillEditing;
TmyTreeView(myControl).OnEditingEnd:=@TmyTreeView(myControl).KillEditing;
TmyTreeView(myControl).OnEditingEnd:=@TmyTreeView(myControl).TreeEditingEnd;
TmyTreeView(myControl).OnChange := @TmyTreeView(myControl).TreeSelectedNodeChange; // NB. async
TmyTreeView(myControl).OnCustomDrawItem:=@TmyTreeView(myControl).CustomDrawTreeNode;
TmyTreeView(myControl).OnClick := @TmyTreeView(myControl).HandleClick;
TmyTreeView(myControl).OnMouseMove := @TmyTreeView(myControl).HandleMouseMove; // set node hint; DragStart
TmyTreeView(myControl).OnMouseLeave:=@TmyTreeView(myControl).HandleMouseLeave; // IsDropTarget:=false
TmyTreeView(myControl).OnDragDrop := @TmyTreeView(myControl).HandleDragDrop; // 'Drop' - stop drag mode
TmyTreeView(myControl).OnDragOver := @TmyTreeView(myControl).HandleDragOver; // is drop allowed
TmyTreeView(myControl).Options:=TTreeView(myControl).Options - [tvoThemedDraw];
TmyTreeView(myControl).SelectionColor:=clYellow; // only works when options are set as above
self.SetMyEventTypes;
CreateComponentDataNode2(self,MyNodeType,myDefaultAttribs, self.myEventTypes, TheOwner,IsDynamic);
self.ParentColor:=true;
// Setting IsContainer false will prevent Laz IDE dropping new child controls into this one.
self.IsContainer:=false;
AddLabel(myControl);
end;
function CreateWidget(ParentNode:TDataNode;ScreenObjectName,NameSpace:string;position:integer;Alignment:String):TDataNode;
var
NewNode:TDataNode;
begin
NewNode:=CreateDynamicLazWidget('TXTree',ParentNode.MyForm,ParentNode,ScreenObjectName,NameSpace,Alignment,position);
result:=NewNode;
end;
function TmyTreeView.NodeHint(tn: TTreeNode): string;
var
MyRecPtr: PNodeDataRec;
begin
if tn<>nil then
begin
MyRecPtr:=tn.Data;
end;
if (MyRecPtr<>nil)
then
try
result:=MyRecPtr^.NodeHint
except
result:='';
end
else
result:='';
end;
procedure TmyTreeView.TreeEditingEnd(Sender:TObject; Node:TTreeNode; Cancel:Boolean);
begin
// Node.Text:=self.MakeTextUnique(Node.Text);
end;
procedure TmyTreeView.HandleDragDrop(Sender, Source: TObject; X, Y: Integer);
var
Src, Dst: TTreeNode;
e:TeventStatus;
DestName:String;
DestNode:TDataNode;
values:TNodeEventValue;
begin
Src := TmyTreeView(Source).Selected;
Dst := TmyTreeView(Sender).GetNodeAt(X,Y);
DestName:=TTreeView(Sender).parent.Name;
if (Src<>nil)
and (Dst<>nil) then
begin
TmyTreeView(Sender).DropTargetNode:=Dst;
e:=TEventStatus.Create('Drop',DestName);
DestNode:=TXTree(self.parent).myNode;
values:=TNodeEventValue.Create;
values.DstText:=Dst.Text;
values.myTree:=TXTree(self.Parent);
values.SrcText:=Src.Text;
values.SourceName:=TmyTreeView(Source).Parent.Name;
values.myNode:=DestNode;
e.ValueObject:=values;
CallHandleEvent(e,'Drop',Dst.Text,Sender);
end;
self.IsDropTarget:=false;
end;
procedure TmyTreeView.HandleDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
Src,DestTreeNode: TTreeNode;
DestNode:TDataNode;
SourceName, DestName, DstText:string;
i:integer;
e:TEventStatus;
ob:TNodeEventValue;
begin
// Sender is TTreeView
if (Source is TTreeView) then
begin
SourceName:=TTreeView(Source).parent.Name; //!!!!namespace????
DestName:=TTreeView(Sender).parent.Name;
DestTreeNode := TTreeNode(TTreeView(Sender).GetNodeAt(X, Y)) ;
Src := TTreeView(Source).Selected;
if (Src<>nil) then
begin
Accept:=true;
// Decide whether a drop is allowed here
// 1) is there a function? (in the form, or registered, or in user code)
// 2) execute the function - send in SourceName,Src.Text,DstText
// 3) get the result (e.ReturnString)
if (DestTreeNode<>nil) then
begin
DstText:=DestTreeNode.Text;
DestNode:=TXTree(self.parent).myNode;
e:=TEventStatus.Create('DropAccepted',DestName);
ob:=TNodeEventValue.Create;
ob.DstText:=DstText;
ob.myTree:=TXTree(self.Parent);
ob.SrcText:=Src.Text;
ob.SourceName:=SourceName;
ob.myNode:=DestTreeNode;
e.ValueObject:=ob;
ExecuteEventHandler(e,'DropAccepted',DestName,'',DestNode); //bypasses event trappers
Accept := MyStrToBool(e.ReturnString);
end;
if (Src<>nil)
and (self.IsDropTarget = false)
then
begin
if (Source=Sender) then
CallHandleEvent('DragStart',Src.Text,Sender); // pick up source node for dragging
if (Accept) then
begin
self.IsDropTarget:=true;
end;
end;
if (Accept)
and (DestTreeNode<>nil) then
begin
// mark the node as available for drop
if DestTreeNode.HasChildren then
TTreeView(Sender).SetInsertMark(DestTreeNode,tvimAsFirstChild)
else
TTreeView(Sender).SetInsertMark(DestTreeNode,tvimAsPrevSibling);
TTreeView(Sender).Update;
end;
end;
end;
end;
procedure TmyTreeView.SetNodeHint(Sender: TObject; Shift: TShiftState; X, Y: Integer) ; //
var
tree: TmyTreeView;
hoverNode: TTreeNode;
hitTest : THitTests;
begin
tree:=TmyTreeView(Sender);
hoverNode := TTreeNode(tree.GetNodeAt(X, Y)) ;
if hovernode<>nil then
begin
hitTest := tree.GetHitTestInfoAt(X, Y) ;
if (tree.lastHintNode <> hoverNode) then
begin
Application.CancelHint;
if (hitTest <= [htOnItem, htOnIcon, htOnLabel, htOnStateIcon]) then
begin
tree.lastHintNode := hoverNode;
tree.Hint := tree.NodeHint(hoverNode) ;
end;
end;
end;
end;
procedure TmyTreeView.HandleMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer) ;
begin
if not (ssLeft in Shift)
and (self.IsDropTarget = false) // do nothing if a drag/drop is in progress
then
begin
if (X>-1) and (Y>-1) then
SetNodeHint(Sender,Shift,X,Y);
end;
end;
procedure TmyTreeView.KillEditing(Sender: TObject; Node:TTreeNode; xx:Boolean) ;
var
MyRecPtr:PNodeDataRec;
begin
if self.ReadOnly then
begin
MyRecPtr:=Node.Data;
if MyRecPtr<>nil then
Node.Text:=MyRecPtr^.OriginalText;
end;
end;
procedure TmyTreeView.HandleClick(Sender: TObject) ;
begin
self.IsDropTarget:=false;
end;
procedure TmyTreeView.HandleMouseLeave(Sender: TObject) ;
begin
self.IsDropTarget:=false;
end;
procedure TmyTreeView.selectNodeByText(NodeText:String);
var
TargetNode:TTreeNode;
begin
if NodeText<>'' then
begin
TargetNode:=TTreeNode(self.Items.FindNodeWithText(NodeText));
if TargetNode<>nil then
begin
if TargetNode<>self.Selected then
self.Select(TargetNode); // In Lazarus this will automatically open any parent nodes // this fires a change event????
end;
end
else
self.ClearSelection();
end;
procedure TmyTreeView.selectNodeByTextPath(PathText:String);
// path is '/' delimited
var
TargetNode:TTreeNode;
begin
if PathText<>'' then
begin
TargetNode:=TTreeNode(self.Items.FindNodeWithTextPath(PathText));
if TargetNode<>nil then
begin
if TargetNode<>self.Selected then
self.Select(TargetNode); // In Lazarus this will automatically open any parent nodes // this fires a change event????
end;
end
else
self.ClearSelection();
end;
procedure TmyTreeView.selectNodeById(NodeId:String);
var
TargetNode:TTreeNode;
begin
if NodeId<>'' then
begin
TargetNode:=self.FindNodeById(NodeId);
if TargetNode<>nil then
begin
if TargetNode<>self.Selected then
self.Select(TargetNode); // In Lazarus this will automatically open any parent nodes
end;
end
else
self.ClearSelection();
end;
procedure TmyTreeView.TreeSelectedNodeChange(Sender: TObject; Node: TTreeNode);
var
outer:TObject;
NameSpace,nodeName,nodeID:String;
pth:String;
MyRecPtr: PNodeDataRec;
begin
if Assigned(Node) then
begin
// added 09/06/19 (so that selectednodetext is always set automatically. previously had to use the TreeNodeClick event)
nodeName := FindNodeId(Sender,outer,NameSpace);
if (outer<>nil)
and (outer is TXTree) then
begin
MyRecPtr:=Node.Data;
NodeID:=MyRecPtr^.ID;
TXTree(outer).myNode.SetAttributeValue('SelectedNodeText',Node.Text);
pth:=TXTree(outer).SelectedTextPath; // this sets the value for attribute 'SelectedTextPath'
CallHandleEvent('TreeNodeClick',NodeID,Sender);
end;
end;
end;
function TmyTreeView.FindNodeByText(AValue:String):TTreeNode;
var
i:integer;
FoundNode:TTreeNode;
function SearchChildNodes(StartNode:TTreeNode;AValue:String):TTreeNode;
var
j:integer;
FoundNode:TTreeNode;
begin
foundNode:=nil;
j:=0;
while j < StartNode.Count - 1 do
begin
if StartNode.Items[j].Text = AValue then
FoundNode:= StartNode.Items[j]
else
foundNode:=SearchChildNodes(StartNode.Items[j],AValue);
if foundNode<>nil then
j:=StartNode.Count;
j:=j+1;
end;
result:=foundNode;
end;
begin
foundNode:=nil;
i:=0;
while i < self.Items.Count do
begin
if self.Items[i].Text = AValue then
FoundNode:= self.Items[i]
else
foundNode:=SearchChildNodes(self.Items[i],AValue);
if foundNode<>nil then
i:=self.Items.Count;
i:=i+1;
end;
result:=foundNode;
end;
function TmyTreeView.FindNodeByExpandedText(AValue:String):TTreeNode;
var
i:integer;
FoundNode:TTreeNode;
function SearchChildNodes(StartNode:TTreeNode;AValue:String):TTreeNode;
var
j:integer;
FoundNode:TTreeNode;
begin
j:=0;
while j < StartNode.Count - 1 do
begin
if self.Items[j].GetTextPath = AValue then
FoundNode:= StartNode.Items[j]
else
foundNode:=SearchChildNodes(StartNode.Items[j],AValue);
if foundNode<>nil then
j:=StartNode.Count;
j:=j+1;
end;
result:=foundNode;
end;
begin
foundNode:=nil;
i:=0;
while i < self.Items.Count do
begin
if self.Items[i].GetTextPath = AValue then
FoundNode:= self.Items[i]
else
foundNode:=SearchChildNodes(self.Items[i],AValue);
if foundNode<>nil then
i:=self.Items.Count;
i:=i+1;
end;
result:=foundNode;
end;
function TmyTreeView.BuildExpandedText(thisNode:TTreeNode):String;
var
i:integer;
str:String;
parentNode:TTreeNode;
begin
str:=inttostr(thisNode.index)+'~:~'+thisNode.Text;
parentNode:=thisNode;
while parentNode.Parent<>nil do
begin
parentNode:=parentNode.Parent;
i:=parentNode.Index;
str:=inttostr(i)+'~:~'+parentNode.Text+'~/~'+str;
end;
result:=str;
end;
function TmyTreeView.NodeTextIsUnique(NodeText:String):Boolean;
var
tn:TTreeNode;
begin
tn:=self.FindNodeByText(NodeText);
if tn<>nil then
result:=false
else
result:=true;
end;
function TmyTreeView.FindNodeById(NodeId:String):TTreeNode;
function GetNodeId(ANode:TTreeNode):String;
var
MyRecPtr:PNodeDataRec;
begin
MyRecPtr:=ANode.Data;
result:=MyRecPtr^.ID;
end;
var
myItems:TTreeNodes;
myId:String;
nextNode:TTreeNode;
begin
myItems:=self.Items;
nextNode := myItems.GetFirstNode;
myId:=GetNodeId(nextNode);
while ((nextNode<>nil) and (myId <> NodeId)) do
begin
nextNode := nextNode.GetNext;
if nextNode<>nil then
myId:=GetNodeId(nextNode);
end;
result:=nextNode;
end;
procedure TmyTreeView.CustomDrawTreeNode(Sender: TCustomTreeView;Node: TTreeNode; State: TCustomDrawState;var DefaultDraw: Boolean) ;
begin
DefaultDraw:=true;
end;
function TmyTreeView.AddNewNode(NodeItems:TTreeNodes;ParentNode:TTreeNode; NodeString,TreeName:string):TTreeNode;
var
NewTreeNode:TTreeNode;
myHint,NodeId:String;
MyRecPtr: PNodeDataRec;
begin
New(MyRecPtr);
MyRecPtr^.ID:=GetNewNodeId(ParentNode,TreeName);
if ParentNode<>nil then
NewTreeNode:=NodeItems.AddChildObject(ParentNode,NodeString,MyRecPtr)
else
begin
NewTreeNode:=NodeItems.Add(nil,NodeString); //root node
NewTreeNode.Data:=MyRecPtr;
end;
// Create the Hint text for this node, if specified.
if (self.Parent<>nil) and (self.Parent is TXTree) then
if TXTree(self.Parent).TreeNodeHintFunc<>nil then
MyRecPtr^.NodeHint := TXTree(self.Parent).TreeNodeHintFunc(NodeString);
MyRecPtr^.OriginalText := NewTreeNode.Text; // save node text to workaround user edits (can't disable editing!!!!)
result:=NewTreeNode;
end;
procedure TMyTreeView.ExpandTreeNodes(Level: Integer);
var
I: Integer;
Nodes:TTreeNodes;
begin
Nodes:=self.Items;
Nodes.BeginUpdate;
try
for I := 0 to Nodes.Count - 1 do
if Nodes[I].Level < Level then
Nodes[I].Expand(False);
finally
Nodes.EndUpdate;
end;
end;
function TmyTreeView.BuildTreeDataString(FromNode:TTreeNode;str:String):String;
var
myTree:TmyTreeView;
ANode:TTreeNode;
i:integer;
begin
myTree:= self;
ANode := FromNode;
if ANode.HasChildren then
begin
str:=str+'[';
str:=str+'"'+ANode.Text+'"';
ANode:=ANode.GetFirstChild;
if ANode<>nil then
begin
str:=str+',';
str:=BuildTreeDataString(ANode,str);
repeat
ANode := ANode.getNextSibling;
if ANode<>nil then
begin
str:=str+',';
str:=BuildTreeDataString(ANode,str);
end;
until ANode = nil;
end;
str:=str+']';
end
else
begin
// no children
str:=str+'"'+ANode.Text+'"';
end;
result:=str;
end;
function TMyTreeView.AddATreeNode(ParentNode:TTreeNode; nodeText,TreeName:string):TTreeNode;
begin
result:= AddNewNode(self.Items,ParentNode, nodeText,TreeName);
end;
function TMyTreeView.AddChildren(jData: TJSONData; ParentNode:TTreeNode;RootName:String):TTreeNode;
var
i:integer;
jItem : TJSONData;
NewNode, tmpNode:TTreeNode;
object_type, nodename, tmp:string;
begin
i:=0;
if jData<>nil then
while i < jData.Count do
begin
jItem:=jData.Items[i];
tmp:=jItem.asJSON;
//showmessage('add node '+jItem.AsJSON);
object_type := GetEnumName(TypeInfo(TJSONtype), Ord(jItem.JSONType));
// expecting first item is always a string
if (i=0) and (object_type='jtString') then
begin
nodename:=jItem.AsString;
NewNode:=self.AddATreeNode(ParentNode,jItem.AsString,RootName);
end
else if (object_type='jtString') then
begin
tmpNode:=self.AddATreeNode(NewNode,jItem.AsString,RootName);
end
else if object_type='jtArray' then
begin
self.AddChildren(jItem,NewNode,RootName);
end;
i:=i+1;
end;
end;
procedure TMyTreeView.PopulateMeFromJSONData(NodeTreeString,RootName:String;openlvl:integer);
var
jData,JItem : TJSONData;
i:integer;
object_type, nodename, tmp:string;
newnode,parentnode:TTreeNode;
begin
parentnode:=nil;
with self.Items do
begin
Clear; { remove any existing nodes }
try
jData := GetJSON(NodeTreeString);
except
on E: Exception do
begin
showmessage(RootName+' JSON error: '+e.Message);
jData := nil;
end;
end;
if jData<>nil then
begin
i:=0;
// everything at this level is a child of the root node
while i < jData.Count do
begin
jItem := jData.Items[i];
tmp:=jItem.asJSON;
object_type := GetEnumName(TypeInfo(TJSONtype), Ord(jItem.JSONType));
if object_type='jtString' then
begin
nodename:=jItem.asString;
newnode:=self.AddATreeNode(parentnode,nodename,RootName);
if i=0 then parentnode:=newnode;
end
else if object_type='jtArray' then
begin
// first element in an array is the node, and following elements are node children
self.AddChildren(jItem,parentnode,RootName);
end;
i:=i+1;
end;
self.ExpandTreeNodes(openlvl);
jData.Free;
end;
end;
end;
procedure TXTree.TreeClick(Sender: TObject) ;
begin
if not (csDesigning in componentState) then
CallHandleEvent('Click',self.myNode.NodeName,self);
end;
function TXTree.NodeTextIsUnique(NodeText:String):Boolean;
begin
result:=TMyTreeView(self.myControl).NodeTextIsUnique(NodeText);
end;
function TXTree.BuildTreeDataString:String;
begin
result:=TMyTreeView(self.myControl).BuildTreeDataString(TMyTreeView(self.myControl).Items[0],'');
end;
procedure TXTree.SetTreeWidth(AValue:string);
var
tc:TControl;
begin
tc:=self.myControl;
myNode.SetAttributeValue('TreeWidth',AValue);
SetHeightWidth(self.myNode,tc,'TreeWidth','TreeHeight');
end;
procedure TXTree.SetTreeHeight(AValue:string);
var
tc:TControl;
begin
tc:=self.myControl;
myNode.SetAttributeValue('TreeHeight',AValue);
SetHeightWidth(self.myNode,tc,'TreeWidth','TreeHeight');
end;
procedure TXTree.SelectTreeNodeByText(NodeText:string);
var
ATreeView:TMyTreeView;
begin
// select the required node in the tree
//showmessage('select node with text: '+NodeText);
ATreeView:=TMyTreeView(self.myControl);
ATreeView.selectNodeByText(NodeText);
end;
procedure TXTree.SelectTreeNodeById(NodeId:string);
var
ATreeView:TMyTreeView;
begin
// select the required node in the tree
//showmessage('select node with text: '+NodeText);
ATreeView:=TMyTreeView(self.myControl);
ATreeView.selectNodeById(NodeId);
end;
{$else}
function addTreeStyles(dummy:string):string;
begin
asm
try{
// ----------------------------------------check if the style has already been set
var x = document.getElementsByTagName("STYLE");
var StyleIsSet = false;
if (x.length>0){
for (var i=0; i<x.length; i++){
var y= x[i].innerHTML;
if (y.indexOf("summary.hasChildren") !=-1) { StyleIsSet =true}
}
}
if (StyleIsSet == false){
//<-------------- Styles to switch the child marker on and off on the summary line ---------------
//var styletext = '<style> summary.noChildren::-webkit-details-marker { display:none; } </style>'
var styletext = '<style> summary.noChildren { list-style: none;} </style>'
+ '<style> summary.hasChildren {color:black; }</style> ';
//----------------------------- now append the style declarations to the head of the HTML page
document.head.innerHTML = document.head.innerHTML+styletext;