source: trunk/Application/WebObjects.pas

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 5.9 KB
Line 
1unit WebObjects;
2
3interface
4
5uses
6 Classes, SysUtils, HtmlClasses, XmlClasses, Generics.Collections;
7
8type
9 TQueryFormItemType = (fitText, fitComboBox, fitRadioButton, fitSelect, fitPassword,
10 fitSubmit, fitReset, fitHidden, fitFileSelect, fitButton, fitCheckBox, fitTextArea);
11
12 { TQueryFormItem }
13
14 TQueryFormItem = class
15 private
16 FInputType: TQueryFormItemType;
17 FItemType: TQueryFormItemType;
18 function GetName: string;
19 procedure SetItemType(const AValue: TQueryFormItemType);
20 procedure SetName(AValue: string);
21 public
22 Caption: string;
23 Required: Boolean;
24 Hint: string;
25 Value: THTMLInput;
26 property Name: string read GetName write SetName;
27 property ItemType: TQueryFormItemType read FItemType write SetItemType;
28 constructor Create;
29 destructor Destroy; override;
30 end;
31
32 { TQueryFormItemList }
33
34 TQueryFormItemList = class(TObjectList<TQueryFormItem>)
35 function FindByName(AValue: string): TQueryFormItem;
36 end;
37
38 { TQueryFormGroup }
39
40 TQueryFormGroup = class
41 Title: string;
42 Rows: TQueryFormItemList;
43 procedure Load(Items: TDictionary<string, string>);
44 function AddNewItem: TQueryFormItem;
45 constructor Create;
46 destructor Destroy; override;
47 end;
48
49 TQueryFormGroups = class(TObjectList<TQueryFormGroup>)
50 end;
51
52 TQueryAction = class
53 Caption: string;
54 Action: string;
55 end;
56
57 TQueryActions = class(TObjectList<TQueryAction>)
58 end;
59
60 TQueryForm = class(THtmlForm)
61 protected
62 function GetAsXmlElement: TXmlElement; override;
63 public
64 Title: string;
65 Groups: TQueryFormGroups;
66 Actions: TQueryActions;
67 ClassId: string;
68 function AddNewGroup: TQueryFormGroup;
69 function AddNewAction(Caption, Action: string): TQueryAction;
70 procedure Load(Items: TDictionary<string, string>);
71 constructor Create;
72 destructor Destroy; override;
73 end;
74
75
76implementation
77
78{ TQueryFormItemList }
79
80function TQueryFormItemList.FindByName(AValue: string): TQueryFormItem;
81var
82 I: Integer;
83begin
84 I := 0;
85 while (I < Count) and (Items[I].Value.ItemName <> AValue) do Inc(I);
86 if I < Count then Result := Items[I]
87 else Result := nil;
88end;
89
90{ TQueryFormItem }
91
92procedure TQueryFormItem.SetItemType(const AValue: TQueryFormItemType);
93begin
94 if FItemType = AValue then Exit;
95 FItemType := AValue;
96 case AValue of
97 fitButton: Value.InputType := itButton;
98 fitSubmit: Value.InputType := itSubmit;
99 fitReset: Value.InputType := itReset;
100 fitHidden: Value.InputType := itHidden;
101 fitCheckBox: Value.InputType := itCheckBox;
102 fitPassword: Value.InputType := itPassword;
103 fitText: Value.InputType := itText;
104 fitFileSelect: Value.InputType := itFileSelect;
105 end;
106end;
107
108function TQueryFormItem.GetName: string;
109begin
110 Result := Value.ItemName;
111end;
112
113procedure TQueryFormItem.SetName(AValue: string);
114begin
115 Value.ItemName := AValue;
116end;
117
118constructor TQueryFormItem.Create;
119begin
120 Value := THtmlInput.Create;
121end;
122
123destructor TQueryFormItem.Destroy;
124begin
125 FreeAndNil(Value);
126 inherited;
127end;
128
129{ TQueryForm }
130
131function TQueryForm.GetAsXmlElement: TXmlElement;
132var
133 I: Integer;
134 G: Integer;
135 Table: THtmlTable;
136 Row: THtmlRow;
137begin
138 Result := inherited GetAsXmlElement;
139 with TXmlTag(Result).SubElements do begin
140 Table := THtmlTable.Create;
141 Table.ClassId := ClassId;
142 Row := Table.Rows.AddRow;
143 with Row.Cells.AddCell do begin
144 ColSpan := 2;
145 Value := THtmlString.Create;
146 THtmlString(Value).Text := Title;
147 end;
148 with Table do
149 for G := 0 to Groups.Count - 1 do
150 with Groups[G] do begin
151 if Title <> '' then begin
152 Row := Table.Rows.AddRow;
153 with Row.Cells.AddCell do begin
154 ColSpan := 2;
155 Value := THtmlString.Create;
156 THtmlString(Value).Text := Title;
157 end;
158 end;
159 for I := 0 to Rows.Count - 1 do
160 with Rows[I] do begin
161 Row := Table.Rows.AddRow;
162 with Row.Cells.AddCell do begin
163 Value := THtmlString.Create;
164 THtmlString(Value).Text := Caption;
165 if Required then THtmlString(Value).Text := THtmlString(Value).Text + '*';
166 THtmlString(Value).Text := THtmlString(Value).Text + ': ';
167 end;
168 with Row.Cells.AddCell do begin
169 Value := THtmlInput.Create;
170 THtmlInput(Value).Assign(Rows[I].Value);
171 end;
172 end;
173 end;
174 Row := Table.Rows.AddRow;
175 with Row.Cells.AddCell do begin
176 ColSpan := 2;
177 Value := THtmlBlock.Create;
178 for I := 0 to Actions.Count - 1 do
179 with THtmlBlock(Value).SubItems.AddInput do begin
180 Value := Actions[I].Caption;
181 InputType := itSubmit;
182 ItemName := Actions[I].Action;
183 end;
184 end;
185 Add(Table.AsXmlElement);
186 Table.Free;
187 end;
188end;
189
190function TQueryForm.AddNewGroup: TQueryFormGroup;
191begin
192 Result := TQueryFormGroup.Create;
193 Groups.Add(Result);
194end;
195
196function TQueryForm.AddNewAction(Caption, Action: string): TQueryAction;
197begin
198 Result := TQueryAction.Create;
199 Result.Caption := Caption;
200 Result.Action := Action;
201 Actions.Add(Result);
202end;
203
204procedure TQueryForm.Load(Items: TDictionary<string, string>);
205var
206 I: Integer;
207begin
208 for I := 0 to Groups.Count - 1 do
209 Groups[I].Load(Items);
210end;
211
212constructor TQueryForm.Create;
213begin
214 inherited;
215 Actions := TQueryActions.Create;
216 Groups := TQueryFormGroups.Create;
217 Method := 'post';
218 Action.AsString := '';
219end;
220
221destructor TQueryForm.Destroy;
222begin
223 FreeAndNil(Actions);
224 FreeAndNil(Groups);
225 inherited;
226end;
227
228{ TQueryFormGroup }
229
230procedure TQueryFormGroup.Load(Items: TDictionary<string, string>);
231var
232 I: Integer;
233 Item: string;
234begin
235 for I := 0 to Rows.Count - 1 do
236 with Rows[I] do begin
237 if Items.TryGetValue(Value.ItemName, Item) then
238 Value.Value := Item;
239 end;
240end;
241
242function TQueryFormGroup.AddNewItem: TQueryFormItem;
243begin
244 Result := TQueryFormItem.Create;
245 Rows.Add(Result);
246end;
247
248constructor TQueryFormGroup.Create;
249begin
250 Rows := TQueryFormItemList.Create;
251end;
252
253destructor TQueryFormGroup.Destroy;
254begin
255 FreeAndNil(Rows);
256 inherited;
257end;
258
259end.
260
Note: See TracBrowser for help on using the repository browser.