-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDW.VCL.TabControl.pas
631 lines (557 loc) · 18.3 KB
/
DW.VCL.TabControl.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
unit DW.VCL.TabControl;
interface
uses
SysUtils, Classes, Controls, Dialogs, StrUtils, DW.VCL.Region, DWElementTag;
type
TIWBSCloseTabAction = (bstabFree, bstabHide, bstabNone);
TIWBSTabOptions = class(TPersistent)
private
FFade: boolean;
FPills: boolean;
FJustified: boolean;
FStacked: boolean;
FCloseButtons: boolean;
procedure SetCloseButtons(const Value: boolean);
public
constructor Create(AOwner: TComponent);
procedure Assign(Source: TPersistent); override;
published
property Fade: boolean read FFade write FFade default false;
property Pills: boolean read FPills write FPills default false;
property Justified: boolean read FJustified write FJustified default false;
property Stacked: boolean read FStacked write FStacked default false;
property CloseButtons: boolean read FCloseButtons write SetCloseButtons default false;
end;
TDWTabControl = class;
TDWTabPage = class(TDWRegion)
private
FTitle: string;
FTabIndex: Integer;
FTabControl: TDWTabControl;
procedure SetTitle(const Value: string);
procedure SetTabIndex(const Value: Integer);
procedure SetTabControl(const Value: TDWTabControl);
protected
procedure InternalRenderCss(var ACss: string); override;
published
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property TabIndex: Integer read FTabIndex write SetTabIndex;
property TabControl: TDWTabControl read FTabControl write SetTabControl;
property Title: string read FTitle write SetTitle;
end;
TIWBSTabCloseEvent = procedure(Sender: TObject; aTab: TDWTabPage;
var aCloseAction: TIWBSCloseTabAction) of object;
TDWTabControl = class(TDWRegion)
private
FPages: TList;
FOldActivePage: Integer;
FActivePage: Integer;
FTabOptions: TIWBSTabOptions;
FOnTabClose: TIWBSTabCloseEvent;
function TabIndexToIndex(ATabOrder: Integer): Integer;
procedure CheckActiveVisible;
procedure SetTabOptions(const Value: TIWBSTabOptions);
procedure SetActivePage(const Value: Integer);
procedure DoOnTabClose(aParams: TStringList);
procedure SetOnTabClose(const Value: TIWBSTabCloseEvent);
function GetActiveTabPage: TDWTabPage;
procedure SetActiveTabPage(const Value: TDWTabPage);
procedure SetPages(const Value: TList);
protected
procedure DoAsyncCHange(aParams: TStringList); override;
procedure SetValue(const AValue: string);
procedure InternalRenderScript(const AHTMLName: string; AScript: TStringList); override;
procedure InternalRenderStyle(AStyle: TStringList); override;
// procedure InternalBeforeRenderControls(var aRenderStream: TIWRenderStream); override;
// procedure InternalAfterRenderControls(var aRenderStream: TIWRenderStream); override;
function RenderAsync: TDWElementXHTMLTag; override;
procedure RenderComponents(aTagParent: TDWElementTag); override;
function RenderCSSClass: string; override;
function RenderHTML: TDWElementTag; override;
procedure RenderScripts; override;
function RenderStyle: string; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetTabPageCSSClass(ATabPage: TComponent): string;
procedure SetTabPageVisibility(ATabIndex: Integer; Visible: boolean); overload;
procedure SetTabPageVisibility(ATabPage: TDWTabPage; Visible: boolean); overload;
function AddNewTab: TDWTabPage;
property ActiveTabPage: TDWTabPage read GetActiveTabPage write SetActiveTabPage;
procedure InsertTab(aTab: TDWTabPage);
procedure RemoveTab(aTab: TDWTabPage);
published
property ActivePage: Integer read FActivePage write SetActivePage;
property BSTabOptions: TIWBSTabOptions read FTabOptions write SetTabOptions;
property Pages: TList read FPages write SetPages;
property OnTabClose: TIWBSTabCloseEvent read FOnTabClose write SetOnTabClose;
end;
implementation
uses DWGlobal, DWUtils, DWTypes, DW.VCL.Common, DW.CORE.MergeSort,
DW.CORE.MergeSortFunc, DW.VCL.CustomForm;
{$REGION 'TIWBSTabOptions'}
constructor TIWBSTabOptions.Create(AOwner: TComponent);
begin
FFade := false;
FPills := false;
FJustified := false;
FStacked := false;
FCloseButtons := false;
end;
procedure TIWBSTabOptions.SetCloseButtons(const Value: boolean);
begin
if FCloseButtons <> Value then
begin
FCloseButtons := Value;
{ TODO 1 -oDELCIO -cIMPROVEMENT : Implemet UPDATEOPTIONS }
end;
end;
procedure TIWBSTabOptions.Assign(Source: TPersistent);
begin
if Source is TIWBSTabOptions then
begin
Fade := TIWBSTabOptions(Source).Fade;
Pills := TIWBSTabOptions(Source).Pills;
Justified := TIWBSTabOptions(Source).Justified;
Stacked := TIWBSTabOptions(Source).Stacked;
end
else
inherited;
end;
{$ENDREGION}
{$REGION 'TIWBSTabControl'}
constructor TDWTabControl.Create(AOwner: TComponent);
begin
inherited;
FPages := TList.Create;
FTabOptions := TIWBSTabOptions.Create(Self);
Height := 112;
Width := 112;
end;
destructor TDWTabControl.Destroy;
begin
FreeAndNil(FTabOptions);
FreeAndNil(FPages);
inherited;
end;
procedure TDWTabControl.DoAsyncCHange(aParams: TStringList);
var
LPage: Integer;
begin
LPage := StrToIntDef(aParams.Values['page'], -1);
if (LPage > -1) then
begin
ActivePage := LPage;
end;
inherited;
end;
procedure TDWTabControl.DoOnTabClose(aParams: TStringList);
var
LTabIndex: Integer;
LCloseAction: TIWBSCloseTabAction;
LPage: TDWTabPage;
begin
LCloseAction := bstabHide;
LTabIndex := StrToIntDef(aParams.Values['page'], -1);
if (LTabIndex > -1) and (LTabIndex < FPages.Count) then
begin
LPage := TDWTabPage(FPages[LTabIndex]);
if Assigned(FOnTabClose) then
FOnTabClose(Self, LPage, LCloseAction);
case LCloseAction of
bstabFree:
LPage.Free;
bstabHide:
LPage.Hide;
// bstabNone: ;
end;
Self.AsyncRefreshControl;
end;
end;
function TDWTabControl.AddNewTab: TDWTabPage;
var
LTabOrder: Integer;
begin
Result := TDWTabPage.Create(Self);
try
Result.Title := 'Page' + IntToStr(FPages.Count);
Result.Name := Self.Name + 'Page' + IntToStr(FPages.Count);
LTabOrder := FPages.Count;
// Result.TabOrder:= FPages.Count;
// FPages.Add(Result);
Result.TabControl := Self;
Result.Parent := Self;
Result.TabOrder := LTabOrder;
AsyncRefreshControl;
except
FreeAndNil(Result);
end;
end;
procedure TDWTabControl.SetOnTabClose(const Value: TIWBSTabCloseEvent);
begin
FOnTabClose := Value;
end;
procedure TDWTabControl.SetPages(const Value: TList);
begin
if FPages <> Value then
begin
FPages.Assign(Value);
FAsyncRefreshControl := True;
end;
end;
procedure TDWTabControl.SetValue(const AValue: string);
var
LIndex: Integer;
begin
{ TODO 1 -oDELCIO -cVERIFY : Check this }
// if RequiresUpdateNotification(Parent) then
// UpdateNotifiedInterface(Parent).NotifyUpdate(Self,AValue);
LIndex := StrToIntDef(AValue, 0);
if (LIndex < 0) or (LIndex >= FPages.Count) then
begin
FActivePage := 0;
FOldActivePage := -1;
end
else
begin
FActivePage := TDWTabPage(FPages[LIndex]).TabOrder;
FOldActivePage := FActivePage;
end;
end;
procedure TDWTabControl.SetTabOptions(const Value: TIWBSTabOptions);
begin
FTabOptions.Assign(Value);
invalidate;
end;
function TDWTabControl.GetActiveTabPage: TDWTabPage;
begin
if FActivePage < FPages.Count then
Result := TDWTabPage(FPages[FActivePage]);
end;
procedure TDWTabControl.SetActivePage(const Value: Integer);
begin
FActivePage := Value;
invalidate;
end;
procedure TDWTabControl.SetActiveTabPage(const Value: TDWTabPage);
var
I: Integer;
begin
for I := 0 to FPages.Count - 1 do
begin
if TDWTabPage(FPages[I]) = Value then
begin
SetActivePage(I);
Break;
end;
end;
end;
// procedure TIWBSTabControl.InternalAfterRenderControls(
// var aRenderStream: TIWRenderStream);
// begin
// aRenderStream.WriteLine('</div>');
// end;
// procedure TIWBSTabControl.InternalBeforeRenderControls(
// var aRenderStream: TIWRenderStream);
// begin
// aRenderStream.WriteLine('<div class="tab-content">');
// end;
procedure TDWTabControl.InsertTab(aTab: TDWTabPage);
begin
FPages.Add(aTab);
aTab.FTabControl := Self;
aTab.Parent := Self;
// Item.Top := 0;
{ TODO 1 -oDELCIO -cVerify : verify insert item position in accordion }
{
if FItems.Count = 1 then
Item.Top := 0
else
Item.Top := VertScrollBar.Range; }
// Item.Left := 0;
// Item.Align := alTop;
end;
procedure TDWTabControl.InternalRenderScript(const AHTMLName: string; AScript: TStringList);
var
LCalbackName: string;
begin
if not FTabOptions.Justified and not FTabOptions.Stacked then
AScript.Add('$("#' + AHTMLName + '_tabs' + '").bootstrapDynamicTabs();');
// save seleted tab on change, manually trigger change event because val don't do it
AScript.Add('$("#' + AHTMLName + '_tabs").off("show.bs.tab").on("show.bs.tab", function(e){ $("#'
+ AHTMLName + '_input").val($(e.target).attr("tabindex")).change(); });');
// Add Close Button event
LCalbackName := DWApplication.RegisterCallBack(Self, ae_tabclose_dw, DoOnTabClose);
AScript.Add('$("#' + AHTMLName +
'_tabs span.tab-close-btn").off("click").on("click", function(e){ executeAjaxCallBack("&page=" + $(this).next().attr("tabindex"), '
+ JQSelector + '[0], "' + LCalbackName + '"); });');
// To update server Tab Index to avoid change browser active tab if refresh page
LCalbackName := DWApplication.RegisterCallBack(Self, ae_shown_bs_tab, DoAsyncCHange);
AScript.Add('$("#' + AHTMLName +
'_tabs").off("shown.bs.tab").on("shown.bs.tab", function(e){ executeAjaxCallBack("&page="+$(e.target).attr("tabindex"), '
+ JQSelector + '[0], "' + LCalbackName + '"); });');
(* // event async change
if Assigned(OnAsyncChange) then begin
AScript.Add('$("#'+AHTMLName+'_tabs").off("shown.bs.tab").on("shown.bs.tab", function(e){ executeAjaxEvent("&page="+$(e.target).attr("tabindex"), null, "'+AHTMLName+'.DoOnAsyncChange", true, null, true); });');
AContext.WebApplication.RegisterCallBack(AHTMLName+'.DoOnAsyncChange', DoOnAsyncChange);
end; *)
end;
procedure TDWTabControl.InternalRenderStyle(AStyle: TStringList);
begin
//
end;
procedure TDWTabControl.RemoveTab(aTab: TDWTabPage);
begin
aTab.FTabControl := nil;
FPages.Remove(aTab);
RemoveControl(aTab);
end;
function TDWTabControl.RenderAsync: TDWElementXHTMLTag;
var
xHTMLName: string;
begin
Result := nil;
xHTMLName := HTMLName;
if FAsyncRefreshControl or not FRendered then
begin
TDWRegionCommon.CancelChildAsyncRender(Self);
{ TODO 1 -oDELCIO -cVERIFY : Check this }
// DoRender;
TDWBSCommon.RenderAsync(xHTMLName, Self);
end
else
begin
TDWBSCommon.SetAsyncClass(xHTMLName, RenderCSSClass, FOldCss);
TDWBSCommon.SetAsyncStyle(xHTMLName, RenderStyle, FOldStyle);
TDWBSCommon.SetAsyncVisible(FMainID, Visible, FOldVisible);
if FOldActivePage <> FActivePage then
begin
DWApplication.CallBackResp.AddScriptToExecuteFirst('$("#' + HTMLName + '_tabs a[tabindex='
+ IntToStr(TabIndexToIndex(FActivePage)) + ']").tab("show");', false);
FOldActivePage := FActivePage;
end;
if Assigned(OnAfterAsyncChange) then
OnAfterAsyncChange(Self);
{ TODO 1 -oDELCIO -cIMPLEMENT : Global Event OnAfterAsyncChange }
{ if Assigned(gIWBSOnAfterAsyncChange) then
gIWBSOnAfterAsyncChange(Self, xHTMLName); }
end;
end;
function TDWTabControl.TabIndexToIndex(ATabOrder: Integer): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to FPages.Count - 1 do
if TDWTabPage(FPages[I]).TabOrder = ATabOrder then
begin
Result := I;
Break;
end;
end;
procedure TDWTabControl.CheckActiveVisible;
var
I, LIndex: Integer;
begin
LIndex := TabIndexToIndex(FActivePage);
if (LIndex = -1) or not TDWTabPage(FPages.Items[LIndex]).Visible then
for I := 0 to FPages.Count - 1 do
if TDWTabPage(FPages[I]).Visible then
begin
FActivePage := TDWTabPage(FPages[I]).TabOrder;
Break;
end;
end;
procedure TDWTabControl.RenderComponents(aTagParent: TDWElementTag);
var
LTabContent: TDWElementTag;
begin
LTabContent := aTagParent.Contents.AddElement('div');
LTabContent.AddClassParam('tab-content');
inherited RenderComponents(LTabContent);
end;
function TDWTabControl.RenderCSSClass: string;
begin
Result := 'iwbs-tabs ' + BSGridOptions.GetClassString;
end;
function TDWTabControl.RenderHTML: TDWElementTag;
var
xHTMLName: string;
xHTMLInput: string;
I, TabIndex: Integer;
tagTabs, tagLi, tagA, TagSpan: TDWElementTag;
TabPage: TDWTabPage;
begin
FOldCss := RenderCSSClass;
FOldStyle := RenderStyle;
FOldVisible := Visible;
FOldActivePage := FActivePage;
MergeSortList(FPages, TabIndexSort);
CheckActiveVisible;
// read only one time
xHTMLName := HTMLName;
xHTMLInput := xHTMLName + '_input';
// main div
FRegionDiv := TDWElementTag.CreateHTMLTag('div');
FRegionDiv.AddStringParam('id', xHTMLName);
FRegionDiv.AddClassParam(FOldCss);
FRegionDiv.AddStringParam('style', RenderStyle);
Result := FRegionDiv;
// tabs region
tagTabs := Result.Contents.AddElement('ul');
tagTabs.AddStringParam('id', xHTMLName + '_tabs');
tagTabs.AddClassParam('nav');
if FTabOptions.Pills then
tagTabs.AddClassParam('nav-pills')
else
tagTabs.AddClassParam('nav-tabs');
if FTabOptions.Justified then
tagTabs.AddClassParam('nav-justified');
if FTabOptions.Stacked then
tagTabs.AddClassParam('nav-stacked');
tagTabs.AddStringParam('role', 'tablist');
// build the tabs
TabIndex := -1;
for I := 0 to FPages.Count - 1 do
begin
TabPage := TDWTabPage(FPages.Items[I]);
TabPage.TabIndex := I;
if not TabPage.Visible and not RenderInvisibleControls then
Continue;
tagLi := tagTabs.Contents.AddElement('li');
if (TabIndex = -1) and (FActivePage = TabPage.TabIndex) and TabPage.Visible then
begin
tagLi.AddClassParam('active');
TabIndex := I;
end;
// Add icon Close
if FTabOptions.CloseButtons then
begin
TagSpan := tagLi.Contents.AddElement('span');
TagSpan.AddClassParam('tab-close-btn');
TagSpan.AddStringParam('role', 'presentation');
TagSpan.Contents.AddText('X');
if not TabPage.Visible then
TagSpan.AddStringParam('style', 'display:none');
end;
tagA := tagLi.Contents.AddElement('a');
tagA.AddStringParam('data-toggle', IfThen(FTabOptions.Pills, 'pill', 'tab'));
tagA.AddStringParam('href', '#' + TabPage.HTMLName);
tagA.AddIntegerParam('tabindex', I);
if not TabPage.Visible then
tagA.AddStringParam('style', 'display: none');
tagA.Contents.AddText(TabPage.Title);
end;
// this hidden input is for input seleted tab page
Result.Contents.AddHiddenField(xHTMLInput, xHTMLInput, IntToStr(TabIndex));
// Render Child Tabs
RenderComponents(Result);
// render scripts
TDWBSCommon.RenderScript(Self, Result);
FMainID := Result.Params.Values['id'];
// initialize hidden input (after render scripts)
if ParentContainer is TDWCustomForm then
(Form as TDWCustomForm).HTMLPage.HTMLTag.HeadTag.InitComponents.Add
('DWInitControl(DWAjaxForm, "' + xHTMLInput + '", false, true);')
else
(Form as TDWCustomForm).HTMLPage.HTMLTag.HeadTag.InitComponents.Add
('DWInitControl(' + ParentContainer.HTMLName + ', "' + xHTMLInput + '", false, true);');
FAsyncRefreshControl := false;
FRendered := True;
end;
procedure TDWTabControl.RenderScripts;
begin
//
end;
function TDWTabControl.RenderStyle: string;
begin
Result := TDWBSCommon.RenderStyle(Self);
end;
function TDWTabControl.GetTabPageCSSClass(ATabPage: TComponent): string;
begin
Result := 'tab-pane';
if BSTabOptions.Fade then
Result := Result + ' fade';
if TDWTabPage(ATabPage).TabIndex = FActivePage then
Result := Result + ' active in';
end;
procedure TDWTabControl.SetTabPageVisibility(ATabIndex: Integer; Visible: boolean);
var
LIndex: Integer;
begin
LIndex := TabIndexToIndex(ATabIndex);
if LIndex >= 0 then
begin
TDWTabPage(FPages.Items[LIndex]).Visible := Visible;
CheckActiveVisible;
DWApplication.CallBackResp.AddScriptToExecute('$("#' + HTMLName + '_tabs a[tabindex=' +
IntToStr(LIndex) + ']").css("display", "' + iif(Visible, '', 'none') + '");');
end;
end;
procedure TDWTabControl.SetTabPageVisibility(ATabPage: TDWTabPage; Visible: boolean);
begin
SetTabPageVisibility(FPages.IndexOf(ATabPage), Visible);
end;
{$ENDREGION}
{ TDWTabPage }
constructor TDWTabPage.Create(AOwner: TComponent);
begin
inherited;
if FTitle = '' then
FTitle := Name;
Align := alClient;
end;
destructor TDWTabPage.Destroy;
begin
inherited;
end;
procedure TDWTabPage.InternalRenderCss(var ACss: string);
begin
inherited InternalRenderCss(ACss);
// Add CSS Class for active/inactive Tab and fade effect
if FTabControl <> nil then
ACss := FTabControl.GetTabPageCSSClass(Self) + ' ' + ACss;
end;
procedure TDWTabPage.SetTabControl(const Value: TDWTabControl);
var
LRecreating: boolean;
begin
if FTabControl <> Value then
begin
LRecreating := false;
if not(csLoading in ComponentState) then
begin
LRecreating := csRecreating in ControlState;
if not LRecreating then
UpdateRecreatingFlag(True);
end;
try
if FTabControl <> nil then
FTabControl.RemoveTab(Self);
Parent := Value;
if Value <> nil then
begin
Value.InsertTab(Self);
if not(csLoading in ComponentState) and not LRecreating then
RecreateWnd;
end;
finally
if not(csLoading in ComponentState) and not LRecreating then
UpdateRecreatingFlag(false);
end;
end;
end;
procedure TDWTabPage.SetTabIndex(const Value: Integer);
begin
FTabIndex := Value;
end;
procedure TDWTabPage.SetTitle(const Value: string);
begin
FTitle := Value;
end;
initialization
RegisterClass(TDWTabPage);
IWBSAddGlobalLinkFile('/<dwlibpath>/dyntabs/bootstrap-dynamic-tabs.css');
IWBSAddGlobalLinkFile('/<dwlibpath>/dyntabs/bootstrap-dynamic-tabs.js');
end.