| 1 | unit WebObjects;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, HtmlClasses, XmlClasses, Generics.Collections;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 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 |
|
|---|
| 76 | implementation
|
|---|
| 77 |
|
|---|
| 78 | { TQueryFormItemList }
|
|---|
| 79 |
|
|---|
| 80 | function TQueryFormItemList.FindByName(AValue: string): TQueryFormItem;
|
|---|
| 81 | var
|
|---|
| 82 | I: Integer;
|
|---|
| 83 | begin
|
|---|
| 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;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | { TQueryFormItem }
|
|---|
| 91 |
|
|---|
| 92 | procedure TQueryFormItem.SetItemType(const AValue: TQueryFormItemType);
|
|---|
| 93 | begin
|
|---|
| 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;
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | function TQueryFormItem.GetName: string;
|
|---|
| 109 | begin
|
|---|
| 110 | Result := Value.ItemName;
|
|---|
| 111 | end;
|
|---|
| 112 |
|
|---|
| 113 | procedure TQueryFormItem.SetName(AValue: string);
|
|---|
| 114 | begin
|
|---|
| 115 | Value.ItemName := AValue;
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 | constructor TQueryFormItem.Create;
|
|---|
| 119 | begin
|
|---|
| 120 | Value := THtmlInput.Create;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | destructor TQueryFormItem.Destroy;
|
|---|
| 124 | begin
|
|---|
| 125 | FreeAndNil(Value);
|
|---|
| 126 | inherited;
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | { TQueryForm }
|
|---|
| 130 |
|
|---|
| 131 | function TQueryForm.GetAsXmlElement: TXmlElement;
|
|---|
| 132 | var
|
|---|
| 133 | I: Integer;
|
|---|
| 134 | G: Integer;
|
|---|
| 135 | Table: THtmlTable;
|
|---|
| 136 | Row: THtmlRow;
|
|---|
| 137 | begin
|
|---|
| 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;
|
|---|
| 188 | end;
|
|---|
| 189 |
|
|---|
| 190 | function TQueryForm.AddNewGroup: TQueryFormGroup;
|
|---|
| 191 | begin
|
|---|
| 192 | Result := TQueryFormGroup.Create;
|
|---|
| 193 | Groups.Add(Result);
|
|---|
| 194 | end;
|
|---|
| 195 |
|
|---|
| 196 | function TQueryForm.AddNewAction(Caption, Action: string): TQueryAction;
|
|---|
| 197 | begin
|
|---|
| 198 | Result := TQueryAction.Create;
|
|---|
| 199 | Result.Caption := Caption;
|
|---|
| 200 | Result.Action := Action;
|
|---|
| 201 | Actions.Add(Result);
|
|---|
| 202 | end;
|
|---|
| 203 |
|
|---|
| 204 | procedure TQueryForm.Load(Items: TDictionary<string, string>);
|
|---|
| 205 | var
|
|---|
| 206 | I: Integer;
|
|---|
| 207 | begin
|
|---|
| 208 | for I := 0 to Groups.Count - 1 do
|
|---|
| 209 | Groups[I].Load(Items);
|
|---|
| 210 | end;
|
|---|
| 211 |
|
|---|
| 212 | constructor TQueryForm.Create;
|
|---|
| 213 | begin
|
|---|
| 214 | inherited;
|
|---|
| 215 | Actions := TQueryActions.Create;
|
|---|
| 216 | Groups := TQueryFormGroups.Create;
|
|---|
| 217 | Method := 'post';
|
|---|
| 218 | Action.AsString := '';
|
|---|
| 219 | end;
|
|---|
| 220 |
|
|---|
| 221 | destructor TQueryForm.Destroy;
|
|---|
| 222 | begin
|
|---|
| 223 | FreeAndNil(Actions);
|
|---|
| 224 | FreeAndNil(Groups);
|
|---|
| 225 | inherited;
|
|---|
| 226 | end;
|
|---|
| 227 |
|
|---|
| 228 | { TQueryFormGroup }
|
|---|
| 229 |
|
|---|
| 230 | procedure TQueryFormGroup.Load(Items: TDictionary<string, string>);
|
|---|
| 231 | var
|
|---|
| 232 | I: Integer;
|
|---|
| 233 | Item: string;
|
|---|
| 234 | begin
|
|---|
| 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;
|
|---|
| 240 | end;
|
|---|
| 241 |
|
|---|
| 242 | function TQueryFormGroup.AddNewItem: TQueryFormItem;
|
|---|
| 243 | begin
|
|---|
| 244 | Result := TQueryFormItem.Create;
|
|---|
| 245 | Rows.Add(Result);
|
|---|
| 246 | end;
|
|---|
| 247 |
|
|---|
| 248 | constructor TQueryFormGroup.Create;
|
|---|
| 249 | begin
|
|---|
| 250 | Rows := TQueryFormItemList.Create;
|
|---|
| 251 | end;
|
|---|
| 252 |
|
|---|
| 253 | destructor TQueryFormGroup.Destroy;
|
|---|
| 254 | begin
|
|---|
| 255 | FreeAndNil(Rows);
|
|---|
| 256 | inherited;
|
|---|
| 257 | end;
|
|---|
| 258 |
|
|---|
| 259 | end.
|
|---|
| 260 |
|
|---|