-
Notifications
You must be signed in to change notification settings - Fork 10
/
DW.VCL.Common.pas
1055 lines (954 loc) · 34.6 KB
/
DW.VCL.Common.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
unit DW.VCL.Common;
interface
uses Classes, System.SysUtils, System.StrUtils, System.UITypes, VCL.Controls,
DWMarkupLinguageTag, DW.VCL.StyleRenderOptions, DWTypes, DWElementTag;
type
TDWGridOptions = class(TPersistent)
private
FFloat: TDWGridFloat;
FOwner: Tcontrol;
FGridXsOffset: integer;
FGridXsSpan: integer;
FGridSmOffset: integer;
FGridSmSpan: integer;
FGridMDOffset: integer;
FGridMdSpan: integer;
FGridLGOffset: integer;
FGridLGspan: integer;
FVisibilityXs: TDWGridVisibility;
FVisibilitySm: TDWGridVisibility;
FVisibilityMd: TDWGridVisibility;
FVisibilityLg: TDWGridVisibility;
FVisibilityPr: TDWGridVisibility;
procedure SetVisibilityLg(const Value: TDWGridVisibility);
procedure SetVisibilityMd(const Value: TDWGridVisibility);
procedure SetVisibilityPr(const Value: TDWGridVisibility);
procedure SetVisibilitySm(const Value: TDWGridVisibility);
procedure SetVisibilityXs(const Value: TDWGridVisibility);
procedure SetFloat(const Value: TDWGridFloat);
procedure SetGridLGOffset(const Value: integer);
procedure SetGridLGspan(const Value: integer);
procedure SetGridMDOffset(const Value: integer);
procedure SetGridMdSpan(const Value: integer);
procedure SetGridSmOffset(const Value: integer);
procedure SetGridSmSpan(const Value: integer);
procedure SetGridXsOffset(const Value: integer);
procedure SetGridXsSpan(const Value: integer);
public
constructor Create(AOwner: Tcontrol); virtual;
procedure Assign(Source: TPersistent); override;
function GetClassString(ACustomXsOffset, ACustomSmOffset, ACustomMdOffset,
ACustomLgOffset: integer): string; overload;
function GetClassString: string; overload;
published
property Float: TDWGridFloat read FFloat write SetFloat default bsgfNone;
property GridXsOffset: integer read FGridXsOffset write SetGridXsOffset default 0;
property GridXsSpan: integer read FGridXsSpan write SetGridXsSpan default 0;
property GridSmOffset: integer read FGridSmOffset write SetGridSmOffset default 0;
property GridSmSpan: integer read FGridSmSpan write SetGridSmSpan default 0;
property GridMdOffset: integer read FGridMDOffset write SetGridMDOffset default 0;
property GridMdSpan: integer read FGridMdSpan write SetGridMdSpan default 0;
property GridLgOffset: integer read FGridLGOffset write SetGridLGOffset default 0;
property GridLgSpan: integer read FGridLGspan write SetGridLGspan default 0;
property VisibilityXs: TDWGridVisibility read FVisibilityXs write SetVisibilityXs
default bsgvDefault;
property VisibilitySm: TDWGridVisibility read FVisibilitySm write SetVisibilitySm
default bsgvDefault;
property VisibilityMd: TDWGridVisibility read FVisibilityMd write SetVisibilityMd
default bsgvDefault;
property VisibilityLg: TDWGridVisibility read FVisibilityLg write SetVisibilityLg
default bsgvDefault;
property VisibilityPrint: TDWGridVisibility read FVisibilityPr write SetVisibilityPr
default bsgvDefault;
end;
TDWBSCommon = class
private
class procedure DoHtmlTag(AControl: TComponent; aTag: TDWCustomElement); static;
public
class procedure AddCssClass(var ACss: string; const AClass: string);
class procedure AsyncRemoveControl(const AHTMLName: string);
class procedure DoRender(AControl: TComponent);
class procedure DoAfterRender(AControl: TComponent);
class procedure RenderAsync(const AHTMLName: string; AControl: Tcontrol);
class function RenderHTMLTag(AControl: Tcontrol): string;
class function RenderStyle(AComponent: Tcontrol): string;
// Render All Scripts for Control and register CallBacks in DWApplication
// equivalent to IWBSRenderScript procedure in IWBootstrap
class procedure RenderScript(AControl: Tcontrol; var AHTMLTag: TDWElementTag);
class function ReplaceParams(AComponent: Tcontrol; const AScript: string;
AFrom: integer = 1): string;
class procedure SetNotVisible(AParams: TStrings);
class procedure ValidateParamName(const AName: string);
class procedure ValidateTagName(const AName: string);
class procedure SetAsyncDisabled(const HTMLName: string; Value: Boolean; var OldValue: Boolean);
class procedure SetAsyncReadOnly(const HTMLName: string; Value: Boolean; var OldValue: Boolean);
class procedure SetAsyncVisible(const HTMLName: string; Value: Boolean; var OldValue: Boolean);
class procedure SetAsyncClass(const HTMLName: string; const Value: string;
var OldValue: string);
class procedure SetAsyncStyle(const HTMLName: string; const Value: string;
var OldValue: string);
class procedure SetAsyncChecked(const HTMLName: string; const Value: Boolean;
var OldValue: Boolean);
class procedure SetAsyncText(const HTMLName: string; const Value: string; var OldValue: string);
class procedure SetAsyncHtml(const HTMLName: string; const Value: string; var OldValue: string);
class function TextToHTML(const AText: string; ReplaceEOLs: Boolean = True;
ReplaceSpaces: Boolean = False): string;
end;
TDWRegionCommon = class
public
class procedure CancelChildAsyncRender(AControl: TComponent);
class procedure DisableRenderOptions(StyleRenderOptions: TDWRenderOptions);
class procedure PrepareChildComponentsForRender(AContainer: Tcontrol);
class procedure RenderComponents(AContainer: Tcontrol; aTagParent: TDWElementTag);
end;
implementation
uses DWUtils, DWGlobal, DW.VCL.Container, DW.VCL.CustomForm,
DW.JSON.JsonData, DW.VCL.Frame, DW.VCL.CustomRegion, DW.VCL.Control,
DW.VCL.Interfaces, DWForm;
constructor TDWGridOptions.Create(AOwner: Tcontrol);
begin
inherited Create;
FOwner := AOwner;
end;
function TDWGridOptions.GetClassString(ACustomXsOffset, ACustomSmOffset, ACustomMdOffset,
ACustomLgOffset: integer): string;
procedure AddCssValue(var s: string; const Value: string);
begin
if s <> '' then
s := s + ' ';
s := s + Value;
end;
var
lNavBar: Boolean;
begin
Result := '';
if ACustomXsOffset + FGridXsOffset > 0 then
AddCssValue(Result, 'col-xs-offset-' + IntToStr(ACustomXsOffset + FGridXsOffset));
if ACustomSmOffset + FGridSmOffset > 0 then
AddCssValue(Result, 'col-sm-offset-' + IntToStr(ACustomSmOffset + FGridSmOffset));
if ACustomMdOffset + FGridMDOffset > 0 then
AddCssValue(Result, 'col-md-offset-' + IntToStr(ACustomMdOffset + FGridMDOffset));
if ACustomLgOffset + FGridLGOffset > 0 then
AddCssValue(Result, 'col-lg-offset-' + IntToStr(ACustomLgOffset + FGridLGOffset));
if (FGridXsSpan > 0) then
AddCssValue(Result, 'col-xs-' + IntToStr(FGridXsSpan));
if (FGridSmSpan > 0) then
AddCssValue(Result, 'col-sm-' + IntToStr(FGridSmSpan));
if (FGridMdSpan > 0) then
AddCssValue(Result, 'col-md-' + IntToStr(FGridMdSpan));
if (FGridLGspan > 0) then
AddCssValue(Result, 'col-lg-' + IntToStr(FGridLGspan));
if FVisibilityXs = bsgvBlock then
AddCssValue(Result, 'visible-xs-block')
else if FVisibilityXs = bsgvInline then
AddCssValue(Result, 'visible-xs-inline')
else if FVisibilityXs = bsgvInlineBlock then
AddCssValue(Result, 'visible-xs-inline-block')
else if FVisibilityXs = bsgvHidden then
AddCssValue(Result, 'hidden-xs');
if FVisibilitySm = bsgvBlock then
AddCssValue(Result, 'visible-sm-block')
else if FVisibilitySm = bsgvInline then
AddCssValue(Result, 'visible-sm-inline')
else if FVisibilitySm = bsgvInlineBlock then
AddCssValue(Result, 'visible-sm-inline-block')
else if FVisibilitySm = bsgvHidden then
AddCssValue(Result, 'hidden-sm');
if FVisibilityMd = bsgvBlock then
AddCssValue(Result, 'visible-md-block')
else if FVisibilityMd = bsgvInline then
AddCssValue(Result, 'visible-md-inline')
else if FVisibilityMd = bsgvInlineBlock then
AddCssValue(Result, 'visible-md-inline-block')
else if FVisibilityMd = bsgvHidden then
AddCssValue(Result, 'hidden-md');
if FVisibilityLg = bsgvBlock then
AddCssValue(Result, 'visible-lg-block')
else if FVisibilityLg = bsgvInline then
AddCssValue(Result, 'visible-lg-inline')
else if FVisibilityLg = bsgvInlineBlock then
AddCssValue(Result, 'visible-lg-inline-block')
else if FVisibilityLg = bsgvHidden then
AddCssValue(Result, 'hidden-lg');
if FVisibilityPr = bsgvBlock then
AddCssValue(Result, 'visible-print-block')
else if FVisibilityPr = bsgvInline then
AddCssValue(Result, 'visible-print-inline')
else if FVisibilityPr = bsgvInlineBlock then
AddCssValue(Result, 'visible-print-inline-block')
else if FVisibilityPr = bsgvHidden then
AddCssValue(Result, 'hidden-print');
if FFloat <> bsgfNone then
begin
lNavBar := (FOwner is TDWCustomRegion);
if FFloat = bsgfLeft then
if lNavBar then
AddCssValue(Result, 'navbar-left')
else
AddCssValue(Result, 'pull-left')
else if FFloat = bsgfRight then
if lNavBar then
AddCssValue(Result, 'navbar-right')
else
AddCssValue(Result, 'pull-right');
end;
end;
procedure TDWGridOptions.SetFloat(const Value: TDWGridFloat);
begin
FFloat := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridLGOffset(const Value: integer);
begin
FGridLGOffset := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridLGspan(const Value: integer);
begin
FGridLGspan := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridMDOffset(const Value: integer);
begin
FGridMDOffset := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridMdSpan(const Value: integer);
begin
FGridMdSpan := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridSmOffset(const Value: integer);
begin
FGridSmOffset := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridSmSpan(const Value: integer);
begin
FGridSmSpan := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridXsOffset(const Value: integer);
begin
FGridXsOffset := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetGridXsSpan(const Value: integer);
begin
FGridXsSpan := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetVisibilityLg(const Value: TDWGridVisibility);
begin
FVisibilityLg := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetVisibilityMd(const Value: TDWGridVisibility);
begin
FVisibilityMd := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetVisibilityPr(const Value: TDWGridVisibility);
begin
FVisibilityPr := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetVisibilitySm(const Value: TDWGridVisibility);
begin
FVisibilitySm := Value;
FOwner.Invalidate;
end;
procedure TDWGridOptions.SetVisibilityXs(const Value: TDWGridVisibility);
begin
FVisibilityXs := Value;
FOwner.Invalidate;
end;
function TDWGridOptions.GetClassString: string;
begin
Result := GetClassString(0, 0, 0, 0);
end;
procedure TDWGridOptions.Assign(Source: TPersistent);
begin
if Source is TDWGridOptions then
begin
GridXsOffset := TDWGridOptions(Source).GridXsOffset;
GridXsSpan := TDWGridOptions(Source).GridXsSpan;
GridSmOffset := TDWGridOptions(Source).GridSmOffset;
GridSmSpan := TDWGridOptions(Source).GridSmSpan;
GridMdOffset := TDWGridOptions(Source).GridMdOffset;
GridMdSpan := TDWGridOptions(Source).GridMdSpan;
GridLgOffset := TDWGridOptions(Source).GridLgOffset;
GridLgSpan := TDWGridOptions(Source).GridLgSpan;
end
else
inherited;
end;
class procedure TDWBSCommon.RenderScript(AControl: Tcontrol; var AHTMLTag: TDWElementTag);
var
LScriptTag: TDWElementTag;
LTempTag: TDWElementTag;
L_IControl: IDWControl;
LJScript: TstringList;
begin
if (not Supports(AControl, IDWControl, L_IControl)) or (L_IControl = nil) then
Exit;
LJScript := TstringList.Create;
try
// render Control Async Events and Register Callback in DWApplication
LJScript.Text := L_IControl.RenderAsyncEvents;
{ TODO 1 -oDELCIO -cIMPLEMENT : Render Hint Events }
// Render Control Scripts
L_IControl.InternalRenderScript(L_IControl.HTMLName, LJScript);
// Add Custom User Script
LJScript.AddStrings(L_IControl.Script);
if LJScript.Count > 0 then
begin
// change script params
{ TODO 1 -oDELCIO -cIMPROVE : SpeedUp this }
LJScript.Text := TDWBSCommon.ReplaceParams(AControl, LJScript.Text);
// if script not to be rendered inside Control Tag
if Not L_IControl.ScriptInsideTag then
begin
// Created an div tag and put Control and script inside this
LTempTag := TDWElementTag.CreateHTMLTag('div');
LTempTag.AddStringParam('id', L_IControl.HTMLName + '_WRP');
LTempTag.Contents.AddElemetAsObject(AHTMLTag);
AHTMLTag := LTempTag;
end;
// Created the script Tag and add to control main tag
LScriptTag := AHTMLTag.Contents.AddElement('script');
// Add Script Text to Script Tag
LScriptTag.Contents.AddText(LJScript.Text);
end;
finally
LJScript.Free;
end;
end;
class procedure TDWBSCommon.AddCssClass(var ACss: string; const AClass: string);
begin
if ACss <> '' then
ACss := ACss + ' ';
ACss := ACss + AClass;
end;
class procedure TDWBSCommon.AsyncRemoveControl(const AHTMLName: string);
begin
// IWBSExecuteAsyncJScript('AsyncDestroyControl("'+AHTMLName+'");', False, True);
DWApplication.CallbackResp.AddScriptToExecuteFirst('AsyncDestroyControl("' + AHTMLName +
'");', False);
{ TODO 1 -oDELCIO -cIMPLEMENT : Change AsyncDestroyControl on Javascript(original in iwbs.js) }
end;
class procedure TDWBSCommon.DoAfterRender(AControl: TComponent);
var
i: integer;
LComponent: TDWControl;
LContainer: TDWContainer;
begin
// if is an TDWControl
if AControl.InheritsFrom(TDWControl) then
begin
LComponent := TDWControl(AControl);
if LComponent <> nil then
begin
// Execute OnAfterRender Event
if Assigned(LComponent.OnAfterRender) then
LComponent.OnAfterRender(LComponent);
{ TODO 1 -oDELCIO -cIMPLEMENT : Implement Global OnAfterRender }
(* if Assigned(gIWBSOnAfterRender) then
gIWBSOnAfterRender(LComponent.InterfaceInstance); *)
end;
end;
// if is an TDWContainer
if AControl.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControl);
if LContainer <> nil then
begin
// Execute OnAfterRender Event for all Child TDWControls and TDWcontainers
for i := 0 to AControl.ComponentCount - 1 do
begin
if (LContainer.Components[i].InheritsFrom(TDWControl)) or
(LContainer.Components[i].InheritsFrom(TDWContainer)) then
DoAfterRender(LContainer.Components[i]);
end;
end;
end;
end;
class procedure TDWBSCommon.DoRender(AControl: TComponent);
var
i: integer;
LComponent: TDWControl;
LContainer: TDWContainer;
begin
// if is an TDWControl
if AControl.InheritsFrom(TDWControl) then
begin
LComponent := TDWControl(AControl);
if LComponent <> nil then
begin
// Execute OnRender Event
if Assigned(LComponent.OnRender) then
LComponent.OnRender(LComponent);
{ TODO 1 -oDELCIO -cIMPLEMENT : Implement Global OnRender }
(* if Assigned(gIWBSOnRender) then
gIWBSOnRender(LComponent); *)
end;
end;
// if is an TDWContainer
if AControl.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControl);
if LContainer <> nil then
begin
// Execute OnRender Event for all Child TDWControls and TDWcontainers
for i := 0 to AControl.ComponentCount - 1 do
begin
if (LContainer.Components[i].InheritsFrom(TDWControl)) or
(LContainer.Components[i].InheritsFrom(TDWContainer)) then
DoRender(LContainer.Components[i]);
end;
end;
end;
end;
class procedure TDWBSCommon.DoHtmlTag(AControl: TComponent; aTag: TDWCustomElement);
var
i: integer;
LComponent: TDWControl;
LContainer: TDWContainer;
begin
// if is an TDWControl
if AControl.InheritsFrom(TDWControl) then
begin
LComponent := TDWControl(AControl);
if LComponent <> nil then
begin
// Execute OnHTMLtag Event
if Assigned(LComponent.OnHTMLtag) then
LComponent.OnHTMLtag(aTag);
end;
end;
{ TODO 3 -oDELCIO -cIMPROVE : Put else here and in all Doxxx class methods }
// if is an TDWContainer
if AControl.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControl);
if LContainer <> nil then
begin
// Execute OnHTMLtag Event for all Child TDWControls and TDWcontainers
for i := 0 to AControl.ComponentCount - 1 do
begin
if (LContainer.Components[i].InheritsFrom(TDWControl)) or
(LContainer.Components[i].InheritsFrom(TDWContainer)) then
DoRender(LContainer.Components[i]);
end;
end;
end;
end;
class procedure TDWBSCommon.RenderAsync(const AHTMLName: string; AControl: Tcontrol);
function ParentTreeVisibility(AControlInt: TComponent): Boolean;
var
LContainer: TDWContainer;
LControl: TDWControl;
begin
Result := True;
LControl := nil;
LContainer := nil;
if AControlInt.InheritsFrom(TDWControl) then
begin
LControl := TDWControl(AControlInt);
if LControl <> nil then
begin
if not LControl.Visible then
begin
LContainer := LControl.ParentContainer;
if (LContainer <> nil) and (not LContainer.RenderInvisibleControls) then
Result := False;
end;
if Result and (LControl.ParentContainer <> nil) then
Result := ParentTreeVisibility(LControl.ParentContainer);
end;
end
else if AControlInt.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControlInt);
if LContainer <> nil then
begin
if LContainer.ParentContainer <> nil then
Result := ParentTreeVisibility(LContainer.ParentContainer);
end;
end;
end;
var
LParentContainer: TDWContainer;
LContainer: TDWContainer;
LParentSl: string;
LHtmlTag: string;
begin
// if not visible and parent.RenderInvisibleControls is false, do not render
if not ParentTreeVisibility(AControl) then
Exit;
// check if component is assigned
if AControl = nil then
raise Exception.Create('AControl not assigned in TDWCommon.RenderAsync');
// if is an DWContainer
if AControl.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControl);
// Execute OnRender Event for all Child DWCOntrols and DWContainers
if LContainer <> nil then
DoRender(LContainer);
end;
// get parent container
if AControl.InheritsFrom(TDWControl) then
LParentContainer := TDWControl(AControl).ParentContainer
else if AControl.InheritsFrom(TDWContainer) then
LParentContainer := TDWContainer(AControl).ParentContainer;
if LParentContainer <> nil then
begin
// if parent is an form
if LParentContainer.InheritsFrom(TDWCustomForm) then
LParentSl := 'body' // define ajax script creation parent as body
else
LParentSl := '#' + LParentContainer.HTMLName;
// Render Component Tag to after add it to CallBack Ajax Creation Script
LHtmlTag := RenderHTMLTag(AControl);
// Add Script for Ajax Creation of Components in CallBackResponse
// the creation of the controls is executed as first script in the callback response, so further scripts in callback could reference them
// IWBSExecuteAsyncJScript(AContext.WebApplication, 'AsyncRenderControl("'+AHtmlName+'", "'+LParentSl+'", "'+IWBSTextToJsParamText(LHtmlTag)+'");', True, True);
DWApplication.CallbackResp.AddScriptToUpdate('AsyncRenderControl("' + AHTMLName + '", "' +
LParentSl + '", "' + DWTextToJsParamText(LHtmlTag) + '");', True);
// Call OnAfterRender event
DoAfterRender(AControl);
end;
end;
class function TDWBSCommon.RenderHTMLTag(AControl: Tcontrol): string;
var
LContainer: TDWContainer;
LTag: TDWElementTag;
begin
// Create and render control Tag
if AControl.InheritsFrom(TDWControl) then
LTag := TDWControl(AControl).RenderHTML
else if AControl.InheritsFrom(TDWContainer) then
LTag := TDWContainer(AControl).RenderHTML;
if LTag = nil then
raise Exception.Create('LTag Not Created in TDWCommon.RenderHTMLTag');
try
if not AControl.Visible then
TDWBSCommon.SetNotVisible(LTag.Params);
(* ---> Moved to RenderHTML
// render child components
if AControl.InheritsFrom(TDWContainer) then
begin
LContainer := TDWContainer(AControl);
if LContainer <> nil then
TDWRegionCommon.RenderComponents(LContainer, LTag);
end;
*)
Result := LTag.Render;
DoHtmlTag(AControl, LTag);
finally
LTag.Free;
end;
end;
class function TDWBSCommon.RenderStyle(AComponent: Tcontrol): string;
var
xStyle: TstringList;
i: integer;
begin
Result := '';
xStyle := TstringList.Create;
try
// assign user style
if AComponent.InheritsFrom(TDWControl) then
xStyle.Assign(TDWControl(AComponent).Style)
else if AComponent.InheritsFrom(TDWContainer) then
xStyle.Assign(TDWContainer(AComponent).Style);
// z-index
if AComponent.InheritsFrom(TDWControl) then
begin
if TDWControl(AComponent).ZIndex <> 0 then
xStyle.Values['z-index'] := IntToStr(TDWControl(AComponent).ZIndex)
end
else if AComponent.InheritsFrom(TDWContainer) then
begin
if TDWContainer(AComponent).ZIndex <> 0 then
xStyle.Values['z-index'] := IntToStr(TDWContainer(AComponent).ZIndex);
end;
// render cursor
if AComponent.Cursor <> crDefault then
begin
if AComponent.InheritsFrom(TDWControl) then
xStyle.Values['cursor'] := Copy(TDWControl(AComponent).RenderCursorStyle, 9, MaxInt)
else if AComponent.InheritsFrom(TDWContainer) then
xStyle.Values['cursor'] := Copy(TDWContainer(AComponent).RenderCursorStyle, 9, MaxInt);
end;
if AComponent.InheritsFrom(TDWControl) then
TDWControl(AComponent).InternalRenderStyle(xStyle)
else if AComponent.InheritsFrom(TDWContainer) then
TDWContainer(AComponent).InternalRenderStyle(xStyle);
for i := 0 to xStyle.Count - 1 do
begin
if Result <> '' then
Result := Result + ';';
Result := Result + xStyle[i];
end;
finally
xStyle.Free;
end;
end;
class function TDWBSCommon.ReplaceParams(AComponent: Tcontrol; const AScript: string;
AFrom: integer = 1): string;
var
LF, LT, i: integer;
LParam, LParNm: string;
LFound: Boolean;
LCompo: TComponent;
L_IComp: IDWControl;
begin
Result := AScript;
if (not Supports(AComponent, IDWControl, L_IComp)) or (L_IComp = nil) then
Exit;
LF := PosEx('{%', Result, AFrom);
if LF > 0 then
begin
LFound := False;
LT := PosEx('%}', Result, LF);
if LT > LF then
LParam := Copy(Result, LF, LT - LF + 2);
LParNm := Copy(Result, LF + 2, LT - LF - 2);
i := L_IComp.ScriptParams.IndexOfName(LParNm);
if i >= 0 then
begin
Result := ReplaceStr(Result, LParam, L_IComp.ScriptParams.ValueFromIndex[i]);
LFound := True;
end;
i := L_IComp.ScriptParams.IndexOf(LParNm);
if i >= 0 then
begin
if L_IComp.ScriptParams.Objects[i] is TJsonObject then
Result := ReplaceText(Result, LParam,
TJsonObject(L_IComp.ScriptParams.Objects[i]).ToJSON)
else
Result := ReplaceText(Result, LParam, '');
end;
if not LFound and AnsiSameText('htmlname', LParNm) then
begin
Result := ReplaceStr(Result, LParam, L_IComp.HTMLName);
LFound := True;
end;
if not LFound and (AComponent.Owner <> nil) then
begin
LCompo := AComponent.Owner.FindComponent(LParNm);
if (LCompo <> nil) then
begin
if (not Supports(LCompo, IDWControl, L_IComp) or (L_IComp = nil)) then
Exit;
Result := ReplaceStr(Result, LParam, L_IComp.HTMLName);
LFound := True;
end;
end;
if LFound then
Result := ReplaceParams(AComponent, Result)
else
Result := ReplaceParams(AComponent, Result, LF + 1);
end;
end;
class procedure TDWBSCommon.ValidateParamName(const AName: string);
var
i: integer;
begin
for i := 1 to Length(AName) do
if not CharInSet(AName[i], ['-', '.', '0' .. '9', 'A' .. 'Z', 'a' .. 'z']) then
raise Exception.Create('Invalid character in param name ' + AName);
end;
class procedure TDWBSCommon.ValidateTagName(const AName: string);
var
i: integer;
begin
if AName = '' then
Exception.Create('Tag name could not be empty');
for i := 1 to Length(AName) do
if ((i = 1) and not CharInSet(AName[i], ['A' .. 'Z', 'a' .. 'z'])) or
((i > 1) and not CharInSet(AName[i], ['0' .. '9', 'A' .. 'Z', 'a' .. 'z'])) then
raise Exception.Create('Invalid character in tag name ' + AName);
end;
class procedure TDWBSCommon.SetNotVisible(AParams: TStrings);
var
LStyle: string;
begin
LStyle := AParams.Values['style'];
LStyle := Trim(LStyle);
if (LStyle <> '') and not AnsiEndsStr(';', LStyle) then
LStyle := LStyle + ';';
if not AnsiContainsStr(LStyle, 'visibility:') then
LStyle := LStyle + 'visibility: hidden;';
if not AnsiContainsStr(LStyle, 'display:') then
LStyle := LStyle + 'display: none;';
if LStyle <> '' then
AParams.Values['style'] := LStyle;
end;
class procedure TDWBSCommon.SetAsyncDisabled(const HTMLName: string; Value: Boolean;
var OldValue: Boolean);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").prop("disabled",' +
iif(Value, 'true', 'false') + ');', False);
// IWBSExecuteAsyncJScript(AApplication, '$("#' + HTMLName + '").prop("disabled",' + iif(Value,
// 'true', 'false') + ');', False, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncReadOnly(const HTMLName: string; Value: Boolean;
var OldValue: Boolean);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").prop("readonly",' +
iif(Value, 'true', 'false') + ');', False);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").prop("readonly",'+iif(Value,'true','false')+');', False, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncVisible(const HTMLName: string; Value: Boolean;
var OldValue: Boolean);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").css("visibility","'
+ iif(Value, '', 'hidden') + '");', False);
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").css("display","' +
iif(Value, '', 'none') + '");', False);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").css("visibility","'+iif(Value,'','hidden')+'");', False, True);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").css("display","'+iif(Value,'','none')+'");', False, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncText(const HTMLName: string; const Value: string;
var OldValue: string);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").val("' +
DWTextToJsParamText(Value) + '");', True);
// IWBSExecuteAsyncJScript(AApplication, '$("#' + HTMLName + '").val("' + TIWBaseHTMLControl.TextToJSStringLiteral(Value) + '");', True, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncHtml(const HTMLName: string; const Value: string;
var OldValue: string);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").html("' +
DWTextToJsParamText(Value) + '");', True);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").html("'+IWBSTextToJsParamText(Value)+'");', True, True);
OldValue := Value;
end;
end;
class function TDWBSCommon.TextToHTML(const AText: string; ReplaceEOLs: Boolean = True;
ReplaceSpaces: Boolean = False): string;
var
POrig, PDest: PChar;
L_IsCallBack: Boolean;
begin
L_IsCallBack := DWApplication.IsCallBack;
SetLength(Result, Length(AText) * 10);
POrig := PChar(AText);
PDest := PChar(Result);
while POrig^ <> #0 do
begin
case POrig^ of
'&':
begin
FormatBuf(PDest^, 10 { 5*2 } , '&', 10 { 5*2 } , []);
Inc(PDest, 4);
end;
'<', '>':
begin
if POrig^ = '<' then
FormatBuf(PDest^, 8 { 4*2 } , '<', 8 { 4*2 } , [])
else
FormatBuf(PDest^, 8 { 4*2 } , '>', 8 { 4*2 } , []);
Inc(PDest, 3);
end;
'"':
begin
FormatBuf(PDest^, 12 { 6*2 } , '"', 12 { 6*2 } , []);
Inc(PDest, 5);
end;
'''':
begin
FormatBuf(PDest^, 10 { 5*2 } , ''', 10 { 5*2 } , []);
Inc(PDest, 4);
end;
'\':
begin
FormatBuf(PDest^, 10 { 5*2 } , '\', 10 { 5*2 } , []);
Inc(PDest, 4);
end;
#10:
if ReplaceEOLs then
begin
FormatBuf(PDest^, 8 { 4*2 } , '<br>', 8 { 4*2 } , []);
Inc(PDest, 3);
end
else
PDest^ := POrig^;
#13:
if ReplaceEOLs then
begin
Dec(PDest);
end
else
PDest^ := POrig^;
#32:
if ReplaceSpaces then
begin
if L_IsCallBack then
begin
FormatBuf(PDest^, 20, '&nbsp;', 20, []);
Inc(PDest, 9);
end
else
begin
FormatBuf(PDest^, 12, ' ', 12, []);
Inc(PDest, 5);
end;
end
else
PDest^ := POrig^;
else
PDest^ := POrig^
end;
Inc(PDest);
Inc(POrig);
end;
SetLength(Result, PDest - PChar(Result));
end;
class procedure TDWBSCommon.SetAsyncClass(const HTMLName: string; const Value: string;
var OldValue: string);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst
('$("#' + HTMLName + '").removeClass().addClass("' + Value + '");', False);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").removeClass().addClass("'+Value+'");', False, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncStyle(const HTMLName: string; const Value: string;
var OldValue: string);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").prop("style","' +
Value + '");', False);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").prop("style","'+Value+'");', False, True);
OldValue := Value;
end;
end;
class procedure TDWBSCommon.SetAsyncChecked(const HTMLName: string; const Value: Boolean;
var OldValue: Boolean);
begin
if OldValue <> Value then
begin
DWApplication.CallbackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '").prop("checked",' +
iif(Value, 'true', 'false') + ');', False);
// IWBSExecuteAsyncJScript(AApplication,'$("#'+HTMLName+'").prop("checked",'+iif(Value,'true','false')+');', False, True);
OldValue := Value;
end;
end;
class procedure TDWRegionCommon.DisableRenderOptions(StyleRenderOptions: TDWRenderOptions);
begin
StyleRenderOptions.RenderAbsolute := False;
StyleRenderOptions.RenderBorder := False;
StyleRenderOptions.RenderFont := False;
StyleRenderOptions.RenderPadding := False;
StyleRenderOptions.RenderPosition := False;
StyleRenderOptions.RenderSize := False;
StyleRenderOptions.RenderZIndex := False;
end;
class procedure TDWRegionCommon.PrepareChildComponentsForRender(AContainer: Tcontrol);
var
i: integer;
LComponent: TComponent;
LControl: TDWControl;
begin
for i := 0 to AContainer.ComponentCount - 1 do
begin
LComponent := AContainer.Components[i];
// Ignore non DWComponents
if (not LComponent.InheritsFrom(TDWControl)) and (not LComponent.InheritsFrom(TDWContainer))
then
Continue;
// TFrame
if LComponent.InheritsFrom(TDWFrame) then
begin
raise Exception.Create
('Need to implement TDWRegionCommon.PrepareChildComponentsForRender for TDWFrame');
end
else if LComponent.ClassName = 'TDWTabPage' then
begin
raise Exception.Create
('Need to implement TDWRegionCommon.PrepareChildComponentsForRender for TDWTabPage');
end;
// disable child StyleRenderOptions
if LComponent.InheritsFrom(TDWControl) then
begin
LControl := TDWControl(LComponent);
if Assigned(LControl) then
begin
DisableRenderOptions(LControl.StyleRenderOptions);