-
Notifications
You must be signed in to change notification settings - Fork 10
/
DW.HTML.Page.pas
193 lines (163 loc) · 4.83 KB
/
DW.HTML.Page.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
unit DW.HTML.Page;
interface
uses System.Classes, DWElementTag;
type
TDWHTMLTag = class;
TDWHeadTag = class(TDWElementTag)
private
FTitle: TDWElementTag;
FInitScript: TDWElementTag;
FFinScript: TDWElementTag;
FInitComponents: TStrings;
procedure Prepare;
procedure SetInitComponents(const Value: TStrings);
public
constructor Create(aHTMLTAG: TDWHTMLTag); reintroduce;
destructor Destroy; override;
function Render: String; overload; override;
procedure Clear;
property Title: TDWElementTag read FTitle;
property InitScript: TDWElementTag read FInitScript;
property InitComponents: TStrings read FInitComponents write SetInitComponents;
end;
TDWBodyTag = class(TDWElementTag)
private
procedure Prepare;
public
constructor Create(aHTMLTAG: TDWHTMLTag); reintroduce;
procedure Clear;
end;
TDWHTMLTag = class(TDWElementTag)
private
FDWHeadTag: TDWHeadTag;
FDWBodyTag: TDWBodyTag;
procedure SetBodyTag(const Value: TDWBodyTag);
procedure SetHeadTag(const Value: TDWHeadTag);
public
constructor Create(AOwner: TComponent); reintroduce;
procedure Clear;
property HeadTag: TDWHeadTag read FDWHeadTag write SetHeadTag;
property BodyTag: TDWBodyTag read FDWBodyTag write SetBodyTag;
end;
TDWHTMLPage = class(TComponent)
private
FDocType: string;
FHTMLTAG: TDWHTMLTag;
procedure SetHTMLTag(const Value: TDWHTMLTag);
public
constructor Create(AOwner: TComponent); override;
procedure Clear;
property DocType: string read FDocType write FDocType;
property HTMLTag: TDWHTMLTag read FHTMLTAG write SetHTMLTag;
end;
implementation
{ TDWHTMLPage }
procedure TDWHTMLPage.Clear;
begin
FHTMLTAG.Clear;
end;
constructor TDWHTMLPage.Create(AOwner: TComponent);
begin
inherited;
FComponentStyle := FComponentStyle + [csTransient];
FComponentStyle := FComponentStyle - [csSubComponent];
FDocType := '<!DOCTYPE HTML>';
FHTMLTAG := TDWHTMLTag.Create(Self);
end;
procedure TDWHTMLPage.SetHTMLTag(const Value: TDWHTMLTag);
begin
FHTMLTAG := Value;
end;
{ TDWHTMLTag }
procedure TDWHTMLTag.Clear;
begin
FDWHeadTag.Clear;
FDWBodyTag.Clear;
Self.Contents.AddElemetAsObject(FDWHeadTag);
Self.Contents.AddElemetAsObject(FDWBodyTag);
end;
constructor TDWHTMLTag.Create(AOwner: TComponent);
begin
inherited CreateHTMLTag('html');
FDWHeadTag := TDWHeadTag.Create(Self);
FDWBodyTag := TDWBodyTag.Create(Self);
Self.Contents.AddElemetAsObject(FDWHeadTag);
Self.Contents.AddElemetAsObject(FDWBodyTag);
end;
procedure TDWHTMLTag.SetBodyTag(const Value: TDWBodyTag);
begin
FDWBodyTag := Value;
end;
procedure TDWHTMLTag.SetHeadTag(const Value: TDWHeadTag);
begin
FDWHeadTag := Value;
end;
{ TDWBodyTag }
procedure TDWBodyTag.Clear;
begin
inherited Clear;
Prepare;
end;
constructor TDWBodyTag.Create(aHTMLTAG: TDWHTMLTag);
begin
inherited CreateHTMLTag('body', aHTMLTAG);
end;
procedure TDWBodyTag.Prepare;
begin
AddStringParam('onload', 'Initialize();');
AddStringParam('onunload', 'Finalize();');
end;
{ TDWHeadTag }
procedure TDWHeadTag.Clear;
begin
FTitle.Clear;
FInitScript.Clear;
FFinScript.Clear;
FInitComponents.Clear;
inherited Clear;
Prepare;
end;
constructor TDWHeadTag.Create(aHTMLTAG: TDWHTMLTag);
begin
inherited CreateHTMLTag('head', aHTMLTAG);
FTitle := TDWElementTag.CreateHTMLTag('title');
FInitScript := TDWElementTag.CreateHTMLTag('script');
FFinScript := TDWElementTag.CreateHTMLTag('script');
FInitComponents := TStringList.Create;
Clear;
end;
destructor TDWHeadTag.Destroy;
begin
FTitle.Free;
FInitScript.Free;
FInitComponents.Free;
inherited;
end;
procedure TDWHeadTag.Prepare;
begin
Contents.AddElemetAsObject(FTitle, true);
Contents.AddElemetAsObject(FInitScript, true);
Contents.AddElemetAsObject(FFinScript, true);
// Initialize
FInitScript.Contents.AddText('function Initialize() {', true);
FInitScript.Contents.AddText(' DW.initDW();', true);
FInitScript.Contents.AddText(' DWInitComponents();', true);
FInitScript.Contents.AddText('};', true);
// Finalize
FFinScript.Contents.AddText('function Finalize() {', true);
FFinScript.Contents.AddText('};', true);
end;
function TDWHeadTag.Render: String;
begin
// Add components to be initiated in function DWInitComponents on InitScript
FInitScript.Contents.AddText('function DWInitComponents() {' + #10#13);
FInitScript.Contents.AddText(FInitComponents.Text);
FInitScript.Contents.AddText(#10#13 + '};');
// Render the tag and childs
Result := inherited Render;
end;
procedure TDWHeadTag.SetInitComponents(const Value: TStrings);
begin
FInitComponents.Assign(Value);
end;
end.