Changeset 309 for Network/CoolWeb


Ignore:
Timestamp:
Jan 4, 2012, 2:21:31 PM (13 years ago)
Author:
chronos
Message:
  • Added: HTML class THtmlTable.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Network/CoolWeb/Common/UHtmlClasses.pas

    r104 r309  
    99
    1010type
     11
     12  { TDomainAddress }
     13
    1114  TDomainAddress = class(TPersistent)
    1215  private
     
    1417    procedure SetAsString(const Value: string);
    1518  public
    16     Levels: array of string;
     19    Levels: TListString;
     20    constructor Create;
     21    destructor Destroy; override;
    1722    property AsString: string read GetAsString write SetAsString;
    1823  end;
    1924
    2025  TAddrClass = (acA, acB, acC, acD, acE);
     26
     27  { TIpAddress }
    2128
    2229  TIpAddress = class(TPersistent)
     
    3239    Octets: array[0..3] of Byte;
    3340    procedure Assign(Source: TPersistent); override;
     41    function IsAddressString(Value: string): Boolean;
    3442    property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal;
    3543    property AsString: string read GetAsString write SetAsString;
     
    7078  end;
    7179
     80  { THtmlElement }
     81
    7282  THtmlElement = class
    73   private
     83  protected
    7484    function GetAsXmlElement: TXmlElement; virtual;
    7585  public
     
    7888    ClassId: string;
    7989    Style: string;
     90    procedure Assign(Source: THtmlElement); virtual;
    8091    property AsXmlElement: TXmlElement read GetAsXmlElement;
    8192  end;
     
    8394  TBlockType = (btNoTag, btBlockLevel, btInline);
    8495
     96  { THtmlString }
     97
    8598  THtmlString = class(THtmlElement)
    8699  private
     
    88101  public
    89102    Text: string;
     103    procedure Assign(Source: THtmlElement); override;
    90104  end;
    91105
     
    100114
    101115  THtmlBlock = class(THtmlElement)
    102   private
     116  protected
    103117    function GetAsXmlElement: TXmlElement; override;
    104118  public
     
    149163    InputType: THtmlInputType;
    150164    Value: Variant;
     165    ItemName: string;
     166    procedure Assign(Source: THtmlElement); override;
    151167    constructor Create;
    152168    destructor Destroy; override;
     
    156172
    157173  THtmlForm = class(THtmlBlock)
    158   private
     174  protected
     175    function GetAsXmlElement: TXmlElement; override;
    159176  public
    160177    Method: string;
    161178    Action: TURL;
    162     function GetAsXmlElement: TXmlElement; override;
    163179    constructor Create;
    164180    destructor Destroy; override;
     
    176192    Scripts: TStringList;
    177193    property AsXmlDocument: TXmlDocument read GetAsXmlDocument;
     194    constructor Create;
     195    destructor Destroy; override;
     196  end;
     197
     198  { THtmlCell }
     199
     200  THtmlCell = class(THtmlElement)
     201  private
     202    function GetAsXmlElement: TXmlElement; override;
     203  public
     204    RowSpan: Integer;
     205    ColSpan: Integer;
     206    Value: THtmlElement;
     207    constructor Create;
     208    destructor Destroy; override;
     209  end;
     210
     211  { THtmlRow }
     212
     213  THtmlRow = class(THtmlElement)
     214  private
     215    function GetAsXmlElement: TXmlElement; override;
     216  public
     217    Cells: TListObject; // TListObject<THtmlCell>
     218    constructor Create;
     219    destructor Destroy; override;
     220  end;
     221
     222  { THtmlTable }
     223
     224  THtmlTable = class(THtmlElement)
     225  protected
     226    function GetAsXmlElement: TXmlElement; override;
     227  public
     228    Rows: TListObject; // TListObject<THtmlRow>
    178229    constructor Create;
    179230    destructor Destroy; override;
     
    247298    Result := False;
    248299  end;
     300end;
     301
     302{ THtmlCell }
     303
     304function THtmlCell.GetAsXmlElement: TXmlElement;
     305begin
     306  Result := inherited GetAsXmlElement;
     307  TXmlTag(Result).Name := 'td';
     308  with TXmlTag(Result).Attributes do begin
     309    if ColSpan > 1 then Add('colspan', IntToStr(ColSpan));
     310    if RowSpan > 1 then Add('rowspan', IntToStr(RowSpan));
     311  end;
     312  TXmlTag(Result).SubElements.Add(Value.AsXmlElement);
     313end;
     314
     315constructor THtmlCell.Create;
     316begin
     317  ColSpan := 1;
     318  RowSpan := 1;
     319end;
     320
     321destructor THtmlCell.Destroy;
     322begin
     323  Value.Free;
     324  inherited Destroy;
     325end;
     326
     327{ THtmlRow }
     328
     329function THtmlRow.GetAsXmlElement: TXmlElement;
     330var
     331  Column: Integer;
     332begin
     333  Result := inherited GetAsXmlElement;
     334  TXmlTag(Result).Name := 'tr';
     335  for Column := 0 to Cells.Count - 1 do
     336    TXmlTag(Result).SubElements.AddNew(THtmlCell(Cells[Column]).AsXmlElement);
     337end;
     338
     339constructor THtmlRow.Create;
     340begin
     341  Cells := TListObject.Create;
     342end;
     343
     344destructor THtmlRow.Destroy;
     345begin
     346  Cells.Free;
     347  inherited Destroy;
     348end;
     349
     350{ THtmlTable }
     351
     352function THtmlTable.GetAsXmlElement: TXmlElement;
     353var
     354  Row, Column: Integer;
     355begin
     356  Result := inherited;
     357  with TXmlTag(Result) do begin
     358    Name := 'table';
     359    for Row := 0 to Rows.Count - 1 do
     360      SubElements.AddNew(THtmlRow(Rows[Row]).AsXmlElement);
     361  end;
     362end;
     363
     364constructor THtmlTable.Create;
     365begin
     366  Rows := TListObject.Create;
     367end;
     368
     369destructor THtmlTable.Destroy;
     370begin
     371  Rows.Free;
     372  inherited Destroy;
    249373end;
    250374
     
    282406    Attributes.Add('type', InputTypeString);
    283407    Attributes.Add('value', Value);
    284     Attributes.Add('name', Name);
    285   end;
     408    if Self.ItemName <> '' then
     409      Attributes.Add('name', Self.ItemName);
     410  end;
     411end;
     412
     413procedure THtmlInput.Assign(Source: THtmlElement);
     414begin
     415  inherited Assign(Source);
     416  InputType := THtmlInput(Source).InputType;
     417  Value := THtmlInput(Source).Value;
     418  ItemName := THtmlInput(Source).ItemName;
    286419end;
    287420
     
    442575end;
    443576
     577procedure THtmlElement.Assign(Source: THtmlElement);
     578begin
     579  Id := Source.Id;
     580  Name := Source.Name;
     581  ClassId := Source.ClassId;
     582  Style := Source.Style;
     583end;
     584
    444585{ TIpAddress }
    445586
     
    454595    end else inherited;
    455596  end else inherited;
     597end;
     598
     599function TIpAddress.IsAddressString(Value: string): Boolean;
     600var
     601  Parts: TListString;
     602begin
     603  Result := True;
     604  try
     605    Parts := TListString.Create;
     606    Parts.Explode(Value, '.', StrToStr);
     607    if Parts.Count = 4 then begin
     608      if (StrToInt(Parts[3]) < 0) or (StrToInt(Parts[3]) > 255) then Result := False;
     609      if (StrToInt(Parts[2]) < 0) or (StrToInt(Parts[2]) > 255) then Result := False;
     610      if (StrToInt(Parts[1]) < 0) or (StrToInt(Parts[1]) > 255) then Result := False;
     611      if (StrToInt(Parts[0]) < 0) or (StrToInt(Parts[0]) > 255) then Result := False;
     612    end else Result := False;
     613  finally
     614    Parts.Free;
     615  end;
    456616end;
    457617
     
    571731
    572732function TDomainAddress.GetAsString: string;
    573 var
    574   I: Integer;
    575 begin
    576   Result := '';
    577   for I := High(Levels) downto 0 do Result := Result + '.' + Levels[I];
    578   Delete(Result, 1, 1);
     733begin
     734  try
     735    Levels.Reverse;
     736    Result := Levels.Implode('.', StrToStr);
     737  finally
     738    Levels.Reverse;
     739  end;
    579740end;
    580741
    581742procedure TDomainAddress.SetAsString(const Value: string);
    582 var
    583   StrArray: TListString;
    584   I: Integer;
    585 begin
    586   try
    587     StrArray := TListString.Create;
    588     StrArray.Explode(Value, '.', StrToStr);
    589     SetLength(Levels, StrArray.Count);
    590     for I := 0 to StrArray.Count - 1 do
    591       Levels[StrArray.Count - 1 - I] := StrArray[I];
    592   finally
    593     StrArray.Free;
    594   end;
     743begin
     744  Levels.Explode(Value, '.', StrToStr);
     745  Levels.Reverse;
     746end;
     747
     748constructor TDomainAddress.Create;
     749begin
     750  Levels := TListString.Create;
     751end;
     752
     753destructor TDomainAddress.Destroy;
     754begin
     755  Levels.Free;
     756  inherited Destroy;
    595757end;
    596758
     
    626788end;
    627789
     790procedure THtmlString.Assign(Source: THtmlElement);
     791begin
     792  inherited Assign(Source);
     793  Text := THtmlString(Source).Text;
     794end;
     795
    628796{ THostAddress }
    629797
     
    653821procedure THostAddress.SetAsString(const Value: string);
    654822begin
    655   State := asIpAddress;
    656   try
     823  if IpAddress.IsAddressString(Value) then begin
     824    State := asIpAddress;
    657825    IpAddress.AsString := Value;
    658   except
    659     on EConvertError do State := asDomainName;
    660   end;
    661   if State = asDomainName then DomainName.AsString := Value;
     826  end else begin
     827    State := asDomainName;
     828    DomainName.AsString := Value;
     829  end;
    662830end;
    663831
Note: See TracChangeset for help on using the changeset viewer.