home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Demos / Midas / InternetExpress / InetXCustom / linkflds.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  6KB  |  193 lines

  1. unit LinkFlds;
  2.  
  3. interface
  4.  
  5. uses Classes, HTTPApp, Db, DbClient, Midas,
  6.   XMLBrokr, WebComp, PagItems, MidItems;
  7.  
  8. type
  9.   TWebLink = class(TWebDataDisplay, IScriptComponent, IValidateField)
  10.   private
  11.     FText: string;
  12.     FAction: string;
  13.     FOpenWindow: Boolean;
  14.     FKeyFieldName: string;
  15.   protected
  16.     function ControlContent(Options: TWebContentOptions): string; override;
  17.     function GetText: string;
  18.     procedure SetKeyFieldName(const Value: string);
  19.     { IScriptComponent }
  20.     procedure AddElements(AddIntf: IAddScriptElements); virtual;
  21.     function GetSubComponents: TObject;
  22.     { IValidateField }
  23.     function ValidateField(DataSet: IMidasWebDataSet; AddIntf: IAddScriptElements): Boolean;
  24.   public
  25.     constructor Create(AOwner: TComponent); override;
  26.     property Action: string read FAction write FAction;
  27.     property Caption;
  28.     property CaptionAttributes;
  29.     property CaptionPosition;
  30.     property Text: string read GetText write FText;
  31.     property KeyFieldName: string read FKeyFieldName write SetKeyFieldName;
  32.     property OpenWindow: Boolean read FOpenWindow write FOpenWindow default True;
  33.   end;
  34.  
  35.   TLinkColumn = class(TWebLink)
  36.   public
  37.     class function IsColumn: Boolean; override;
  38.   published
  39.     property Text;
  40.     property Caption;
  41.     property CaptionAttributes;
  42.     property Action;
  43.     property OpenWindow;
  44.     property KeyFieldName;
  45.   end;
  46.  
  47.   TFieldLink = class(TWebLink)
  48.   published
  49.     property Caption;
  50.     property CaptionAttributes;
  51.     property CaptionPosition;
  52.     property Text;
  53.     property Action;
  54.     property OpenWindow;
  55.   end;
  56.  
  57. implementation
  58.  
  59. uses sysutils, MidProd;
  60.  
  61. const 
  62.   sLinkFunctionName = 'GoLink';
  63.   // TODO:  This function does not set the current row of the rowset.  Clicking
  64.   // on a link in a grid will use values from the focus row which may not be the
  65.   // focus row.
  66.   sLinkFunction =
  67.    'function %0:s(rs,fname,action,open)'     + #13#10 +
  68.    '{'                             + #13#10 +
  69.    '  var s;'                   + #13#10 +
  70.    '  var i;'                    + #13#10 +
  71.    '  if (rs==null) exit;'        + #13#10 +
  72.    '  if (action.indexOf("?") == -1)' + #13#10 +
  73.    '    s = action + "?";'           + #13#10 +
  74.    '  else'           + #13#10 +
  75.    '    s = action + "&";'           + #13#10 +
  76.    '  for (i=0; i<rs.FieldCnt; i++)' + #13#10 +
  77.    '  {'                             + #13#10 +
  78.    '    var f;'                      + #13#10 +
  79.    '    f = rs.Fields.Fieldx[i];'    + #13#10 +
  80.    '    if (f.name == fname)'        + #13#10 +
  81.    '    {'                           + #13#10 +
  82.    '      s = s + f.name + "=" + f.Value();' + #13#10 +
  83.    '      break;'                    + #13#10 +
  84.    '    }'                           + #13#10 +
  85.    '  }'                             + #13#10 +
  86.    '  s = s.replace(/ /g, "+");'         + #13#10 +
  87.    '  if (open)'                     + #13#10 +
  88.    '    window.open(s);'       + #13#10 +
  89.    '  else'       + #13#10 +
  90.    '    location = s;'       + #13#10 +
  91.    '}'                             + #13#10;
  92.  
  93.  
  94. { TWebLink }
  95.  
  96. resourcestring 
  97.   sLinkCaption = 'Link';
  98.  
  99. function TWebLink.ControlContent(Options: TWebContentOptions): string;
  100. const
  101.   truefalse: array[Boolean] of string =('false', 'true');
  102. var
  103.   Attrs: string;
  104.   Events: string;
  105. begin
  106.   AddQuotedAttrib(Attrs, 'STYLE', Style);
  107.   AddQuotedAttrib(Attrs, 'CLASS', StyleRule);
  108.   AddCustomAttrib(Attrs, Custom);
  109.   if (not (coNoScript in Options.Flags)) then
  110.     Events := Format(' onclick="%s;return false;"',
  111.       [Format('if(%s)%s(%s,''%s'',''%s'',%s)', [sXMLReadyVar, sLinkFunctionName,
  112.         GetXMLRowSetName, KeyFieldName, Action, truefalse[OpenWindow]])])
  113.   else
  114.     Events := ' onclick="return false;"';
  115.   // Note that HREF is ignored because onclick always returns false.
  116.   Result := Format('<A HREF="%0:s"%1:s%2:s>%3:s</A>', [Action, Attrs, Events, Text]);
  117. end;
  118.  
  119. constructor TWebLink.Create(AOwner: TComponent);
  120. begin
  121.   inherited;
  122.   Caption := sLinkCaption;
  123.   OpenWindow := True;
  124. end;
  125.  
  126. function TWebLink.GetText: string;
  127. begin
  128.   if FText = '' then
  129.     Result := ClassName
  130.   else
  131.     Result := FText;
  132. end;
  133.  
  134. procedure TWebLink.AddElements(AddIntf: IAddScriptElements);
  135. begin
  136.   inherited;
  137.   AddIntf.AddFunction(sLinkFunctionName, Format(sLinkFunction, [sLinkFunctionName]));
  138. end;
  139.  
  140. function TWebLink.GetSubComponents: TObject;
  141. begin
  142.   Result := nil;
  143. end;
  144.  
  145. procedure TWebLink.SetKeyFieldName(const Value: string);
  146. var
  147.   Intf: IValidateFields;
  148.   Component: TComponent;
  149. begin
  150.   if (AnsiCompareText(Value, FKeyFieldName) <> 0) then
  151.   begin
  152.     FKeyFieldName := Value;
  153.     if [csLoading, csDesigning] * ComponentState <> [] then
  154.     begin
  155.       Component := GetParentComponent;
  156.       while Assigned(Component) and
  157.        (not Component.GetInterface(IValidateFields, Intf)) do
  158.        Component := Component.GetParentComponent;
  159.       if Assigned(Component) then
  160.         Intf.EnableValidateFields := True;
  161.     end;
  162.   end;
  163. end;
  164.  
  165. resourcestring
  166.   sKeyFieldNameBlank = '%s.KeyFieldName = ''''';
  167.   sKeyFieldNotFound = '%0:s: KeyField "%1:s" not found';
  168.  
  169. function TWebLink.ValidateField(DataSet: IMidasWebDataSet;
  170.   AddIntf: IAddScriptElements): Boolean;
  171.   procedure AddError(Value: string);
  172.   begin
  173.     AddIntf.AddError(Value);
  174.     Result := False;
  175.   end;
  176. begin
  177.   Result := True;
  178.   if KeyFieldName = '' then
  179.     AddError(Format(sKeyFieldNameBlank, [Name]))
  180.   else if Assigned(DataSet) then
  181.     if DataSet.Fields.FindField(KeyFieldName) = nil then
  182.       AddError(Format(sKeyFieldNotFound, [Name, KeyFieldName]));
  183. end;
  184.  
  185. { TLinkColumn }
  186.  
  187. class function TLinkColumn.IsColumn: Boolean;
  188. begin
  189.   Result := True;
  190. end;
  191.  
  192. end.
  193.