-
Notifications
You must be signed in to change notification settings - Fork 10
/
DW.VCL.Accordion.pas
226 lines (194 loc) · 5.83 KB
/
DW.VCL.Accordion.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
unit DW.VCL.Accordion;
interface
uses System.Classes, Controls, System.StrUtils, DW.VCL.Region, DWElementTag;
type
TDWAccordion = class;
TDWAccordionItem = class(TDWRegion)
private
FAccordion: TDWAccordion;
FCaption: string;
FID: Integer;
// FHTMLResult:TDWElementTag;
procedure SetAccordion(const Value: TDWAccordion);
procedure SetCaption(const Value: string);
procedure SetID(const Value: Integer);
protected
procedure RenderComponents(aTagParent: TDWElementTag); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Accordion: TDWAccordion read FAccordion write SetAccordion;
property Caption: string read FCaption write SetCaption;
property ID: Integer read FID write SetID;
end;
TDWAccordionItems = class(TCollection)
end;
TDWAccordion = class(TDWRegion)
private
FItems: TList;
FItemsPadding: string;
procedure SetItemsPadding(const Value: string);
protected
function RenderHTML: TDWElementTag; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CreateItem(AOwner: TComponent): TDWAccordionItem;
procedure InsertItem(Item: TDWAccordionItem);
procedure RemoveItem(Item: TDWAccordionItem);
published
property ItemsPadding: string read FItemsPadding write SetItemsPadding;
end;
implementation
uses
System.SysUtils;
{ TDWAccordion }
constructor TDWAccordion.Create(AOwner: TComponent);
begin
inherited;
FItems := TList.Create;
end;
function TDWAccordion.CreateItem(AOwner: TComponent): TDWAccordionItem;
begin
Result := TDWAccordionItem.Create(AOwner);
Result.Parent := Self;
Result.Accordion := Self;
end;
destructor TDWAccordion.Destroy;
begin
inherited;
end;
procedure TDWAccordion.InsertItem(Item: TDWAccordionItem);
begin
FItems.Add(Item);
Item.FAccordion := Self;
Item.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;
{ if not (csLoading in ComponentState) then
AutoScrollInView(Item); }
end;
procedure TDWAccordion.RemoveItem(Item: TDWAccordionItem);
begin
Item.FAccordion := nil;
FItems.Remove(Item);
RemoveControl(Item);
end;
function TDWAccordion.RenderHTML: TDWElementTag;
begin
Result := inherited;
Result.AddClassParam('panel-group');
end;
procedure TDWAccordion.SetItemsPadding(const Value: string);
begin
if FItemsPadding <> Value then
begin
if (Value <> '') and ((Copy(Value, Length(Value) - 1, 2) <> 'px') and
(Copy(Value, Length(Value), 1) <> '%')) then
raise Exception.Create('ItemsPadding must be of the format: 1px or 1%');
FItemsPadding := Value;
end;
end;
{ TIBSAccordionItem }
constructor TDWAccordionItem.Create(AOwner: TComponent);
begin
inherited;
Height := 30;
end;
destructor TDWAccordionItem.Destroy;
begin
inherited;
end;
procedure TDWAccordionItem.RenderComponents(aTagParent: TDWElementTag);
var
aComponents: string;
i: Integer;
aBody: TDWElementTag;
aHeader: TDWElementTag;
begin
// Render children componentes inside Item
inherited;
// Extract children components of Item
for i := 0 to aTagParent.Contents.Count - 1 do
begin
aComponents := aComponents + aTagParent.Contents.Items[i].Render;
end;
// Clear Item
aTagParent.Contents.Clear;
// Add class css to Item
aTagParent.AddClassParam('panel-default');
// Create the Header
aHeader := TDWElementTag.CreateHTMLTag('div');
aHeader.AddClassParam('panel-heading');
aHeader.Contents.AddText('<h4 class="panel-title">' +
'<a style="display:block;width:100%;" data-toggle="collapse" data-parent="#' +
FAccordion.HTMLName + '" href="#collapse' + HTMLName + '">' + FCaption + '</a>' + '</h4>');
// Add the Header to Item
aTagParent.Contents.AddElemetAsObject(aHeader);
// Create the Body
aBody := TDWElementTag.CreateHTMLTag('div');
aBody.AddClassParam('panel-collapse');
aBody.AddClassParam('collapse');
aBody.AddStringParam('id', 'collapse' + HTMLName);
if FAccordion.ItemsPadding <> '' then
aBody.AddStringParam('padding', FAccordion.ItemsPadding);
if FAccordion = nil then
raise Exception.Create('Accordion Property for Item ' + Self.Name + ' is not set.');
aBody.Contents.AddText
( { '<div id="collapse' + HTMLName + '" class="panel-collapse collapse">' + }
'<div class="panel-body"' + ifthen(FAccordion.ItemsPadding <> '',
' style="padding:' + FAccordion.ItemsPadding + ';"', '') + '>' + aComponents +
{ <-- Add Components to Body }
'</div>' { +
'</div>' } );
// Add Body(and components) to Item
aTagParent.Contents.AddElemetAsObject(aBody);
end;
procedure TDWAccordionItem.SetAccordion(const Value: TDWAccordion);
var
LRecreating: Boolean;
begin
if FAccordion <> 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 FAccordion <> nil then
FAccordion.RemoveItem(Self);
Parent := Value;
if Value <> nil then
begin
Value.InsertItem(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 TDWAccordionItem.SetCaption(const Value: string);
begin
FCaption := Value;
end;
procedure TDWAccordionItem.SetID(const Value: Integer);
begin
FID := Value;
end;
initialization
RegisterClass(TDWAccordionItem);
end.