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

  1. unit QueryComps;
  2.  
  3. interface
  4.  
  5. uses Classes, HTTPApp, Db, DbClient, Midas,
  6.   WebComp, MidItems;
  7.  
  8. procedure Register;
  9.  
  10. type
  11.   TQueryPassword = class(TWebTextInput, IQueryField)
  12.   protected
  13.     function ControlContent(Options: TWebContentOptions): string; override;
  14.     function GetText: string;
  15.     procedure SetText(const Value: string);
  16.   public
  17.     class function IsQueryField: Boolean; override;
  18.   published
  19.     property ParamName;
  20.     property DisplayWidth;
  21.     property Caption;
  22.     property CaptionAttributes;
  23.     property CaptionPosition;
  24.     property FieldName;
  25.     property TabIndex;
  26.     property Style;
  27.     property Custom;
  28.     property StyleRule;
  29.   end;
  30.  
  31.   TQueryHiddenText = class(TWebTextInput, IQueryField)
  32.   private
  33.     FText: string;
  34.   protected
  35.     function ControlContent(Options: TWebContentOptions): string; override;
  36.     function GetText: string;
  37.     procedure SetText(const Value: string);
  38.     procedure AddAttributes(var Attrs: string); override;
  39.     function ImplContent(Options: TWebContentOptions;
  40.       ParentLayout: TLayout): string; override;
  41.   public
  42.     class function IsQueryField: Boolean; override;
  43.   published
  44.     property ParamName;
  45.     property FieldName;
  46.     property Text: string read GetText write SetText;
  47.   end;
  48.  
  49.   TSubmitValueButton = class(TQueryButton)
  50.   private
  51.     FValueName: string;
  52.     FValue: string;
  53.     procedure SetValue(const Value: string);
  54.     procedure SetValueName(const Value: string);
  55.   protected
  56.     function EventContent(Options: TWebContentOptions): string; override;
  57.     function GetHTMLControlName: string; override;
  58.     function GetValue: string;
  59.     function GetValueName: string;
  60.     function GetInputType: string; override;
  61.     function IsSubmitType: Boolean;
  62.     function ImplContent(Options: TWebContentOptions;
  63.       ParentLayout: TLayout): string; override;
  64.   public
  65.     constructor Create(AOwner: TComponent); override;
  66.   published
  67.     property Value: string read GetValue write SetValue;
  68.     property ValueName: string read GetValueName write SetValueName;
  69.   end;
  70.  
  71.   TPromptQueryButton = class(TQueryButton, IScriptComponent)
  72.   private
  73.     FLines: TStrings;
  74.     FHiddenInputName: string;
  75.   protected
  76.     procedure SetLines(const Value: TStrings);
  77.     function GetPromptMessage: string;
  78.     function ImplContent(Options: TWebContentOptions;
  79.       ParentLayout: TLayout): string; override;
  80.     function EventContent(Options: TWebContentOptions): string; override;
  81.     function GetHiddenInputName: string;
  82.     { IScriptComponent }
  83.     procedure AddElements(AddIntf: IAddScriptElements); virtual;
  84.     function GetSubComponents: TObject;
  85.   public
  86.     constructor Create(AOwner: TComponent); override;
  87.     destructor Destroy; override;
  88.   published
  89.     property HiddenInputName: string read GetHiddenInputName write FHiddenInputName;
  90.     property Lines: TStrings read FLines write SetLines;
  91.   end;
  92.  
  93. implementation
  94.  
  95. uses sysutils;
  96.  
  97. resourcestring 
  98.   sPromptQuery = 'Prompt...';
  99.   sSubmitValue = 'Submit Value';
  100.  
  101. const   
  102.   sPromptFunctionName = 'PromptSetField';
  103.   sPromptFunction =
  104.    'function %0:s(input, msg)'     + #13#10 +
  105.    '{'                             + #13#10 +
  106.    '  var v = prompt(msg);'        + #13#10 +
  107.    '  if (v == null || v == "")'              + #13#10 +
  108.    '    return false;'             + #13#10 +
  109.    '  input.value = v'             + #13#10 +
  110.    '  return true;'               + #13#10 +
  111.    '}'                             + #13#10;
  112.  
  113.  
  114. constructor TPromptQueryButton.Create(AOwner: TComponent);
  115. begin
  116.   inherited;
  117.   DefaultCaption := sPromptQuery;
  118.   InputType := 'BUTTON';
  119.   FLines := TStringList.Create;
  120. end;
  121.  
  122. function TPromptQueryButton.EventContent(
  123.   Options: TWebContentOptions): string;
  124. var
  125.   HTMLForm: IHTMLForm;
  126.   HTMLFormVarName: string;
  127. begin
  128.   HTMLForm := GetHTMLForm;
  129.   if Assigned(HTMLForm) then
  130.     HTMLFormVarName := HTMLForm.HTMLFormVarName;
  131.   Result :=
  132.     Format(' onclick=''if (%0:s(%1:s, %2:s)) %3:s.submit();''',
  133.       [sPromptFunctionName, HiddenInputName, GetPromptMessage,
  134.         HTMLFormVarName]);
  135. end;
  136.  
  137. function TPromptQueryButton.GetPromptMessage: string;
  138. var
  139.   I: Integer;
  140. begin
  141.   Result := '';
  142.   for I := 0 to Lines.Count - 1 do
  143.   begin
  144.     Result := Format('%s"%s\n"', [Result, Lines[I]]);
  145.     if I < Lines.Count - 1 then Result := Result + ' +';
  146.     Result := Result + #13#10;
  147.   end;
  148. end;
  149.  
  150. procedure TPromptQueryButton.SetLines(const Value: TStrings);
  151. begin
  152.   FLines.Assign(Value);
  153. end;
  154.  
  155. function TPromptQueryButton.ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string;
  156. begin
  157.   Result :=
  158.       Format('<INPUT TYPE=HIDDEN NAME=%0:s>'#13#10,
  159.         [HiddenInputName]);
  160.   Result := Result + inherited ImplContent(Options, ParentLayout);
  161. end;
  162.  
  163. destructor TPromptQueryButton.Destroy;
  164. begin
  165.   FLines.Free;
  166.   inherited;
  167. end;
  168.  
  169. procedure TPromptQueryButton.AddElements(AddIntf: IAddScriptElements);
  170. begin
  171.   inherited;
  172.   AddIntf.AddFunction(sPromptFunctionName, Format(sPromptFunction, [sPromptFunctionName]));
  173. end;
  174.  
  175. function TPromptQueryButton.GetHiddenInputName: string;
  176. begin
  177.   if FHiddenInputName = '' then
  178.     Result := Format('_%s', [Self.Name])
  179.   else
  180.     Result := FHiddenInputName;
  181. end;
  182.  
  183. { TSubmitValueButton }
  184.  
  185. constructor TSubmitValueButton.Create(AOwner: TComponent);
  186. begin
  187.   inherited;
  188.   DefaultCaption := sSubmitValue;
  189.   InputType := 'BUTTON';
  190. end;
  191.  
  192. function TSubmitValueButton.EventContent(
  193.   Options: TWebContentOptions): string;
  194. var
  195.   HTMLForm: IHTMLForm;
  196.   HTMLFormVarName: string;
  197. begin
  198.   if IsSubmitType then
  199.     // No code necessary.  Use default submit button behavior
  200.     Result := ''
  201.   else
  202.   begin
  203.     HTMLForm := GetHTMLForm;
  204.     if Assigned(HTMLForm) then
  205.       HTMLFormVarName := HTMLForm.HTMLFormVarName;
  206.     Result :=
  207.       Format(' onclick=''%0:s.value="%1:s";%2:s.submit();''',
  208.         [ValueName, Value,
  209.           HTMLFormVarName]);
  210.   end;
  211. end;
  212.  
  213. function TSubmitValueButton.GetHTMLControlName: string;
  214. begin
  215.   Result := Name;
  216. end;
  217.  
  218. function TSubmitValueButton.ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string;
  219. begin
  220.   Result := '';
  221.   if not IsSubmitType then
  222.     Result :=
  223.       Format('<INPUT TYPE=HIDDEN NAME=%0:s>'#13#10,
  224.         [ValueName]);
  225.   Result := Result + inherited ImplContent(Options, ParentLayout);
  226. end;
  227.  
  228. function TSubmitValueButton.IsSubmitType: Boolean;
  229. begin
  230.   // Can use submit button to send value.  The value
  231.   // with be the VALUE attributes and the value name will by the
  232.   // NAME attribute.
  233.   Result := (FValue <> '') and (FValueName <> '');
  234. end;
  235.  
  236. function TSubmitValueButton.GetInputType: string;
  237. begin
  238.   if IsSubmitType then
  239.     Result := 'SUBMIT'
  240.   else
  241.     Result := 'BUTTON';
  242. end;
  243.  
  244. function TSubmitValueButton.GetValue: string;
  245. begin
  246.   Result := FValue;
  247.   if Result = '' then
  248.     Result := Caption;
  249. end;
  250.  
  251. function TSubmitValueButton.GetValueName: string;
  252. begin
  253.   Result := FValueName;
  254.   if Result = '' then
  255.     Result := GetHTMLControlName;
  256. end;
  257.  
  258. procedure TSubmitValueButton.SetValue(const Value: string);
  259. begin
  260.   if Value = Caption then
  261.     FValue := ''
  262.   else
  263.     FValue := Value;
  264. end;
  265.  
  266. procedure TSubmitValueButton.SetValueName(const Value: string);
  267. begin
  268.   if Value = GetHTMLControlName then
  269.     FValueName := ''
  270.   else
  271.     FValueName := Value;
  272. end;
  273.  
  274. { Register procedure }
  275.  
  276. procedure Register;
  277. begin
  278.   RegisterWebComponents([
  279.     TPromptQueryButton, TSubmitValueButton]);
  280. end;
  281.  
  282. { TQueryPassword }
  283.  
  284. function TQueryPassword.ControlContent(
  285.   Options: TWebContentOptions): string;
  286. var
  287.   Attrs: string;
  288.   Events: string;
  289. begin
  290.   AddAttributes(Attrs);
  291.   if (not (coNoScript in Options.Flags)) then
  292.     Events := EventContent(Options);
  293.   Result := Format('<INPUT TYPE=PASSWORD %0:s%1:s>', [Attrs, Events]);
  294. end;
  295.  
  296. function TQueryPassword.GetText: string;
  297. begin
  298.   Result := '';  // Not supported
  299. end;
  300.  
  301. class function TQueryPassword.IsQueryField: Boolean;
  302. begin
  303.   Result := True;
  304. end;
  305.  
  306. procedure TQueryPassword.SetText(const Value: string);
  307. begin
  308. end;
  309.  
  310. { TQueryHiddenText }
  311.  
  312. function TQueryHiddenText.ControlContent(
  313.   Options: TWebContentOptions): string;
  314. var
  315.   Attrs: string;
  316. begin
  317.   AddAttributes(Attrs);
  318.   Result := Format('<INPUT TYPE=HIDDEN %0:s>', [Attrs]);
  319. end;
  320.  
  321. function TQueryHiddenText.GetText: string;
  322. begin
  323.   Result := FText;
  324. end;
  325.  
  326. class function TQueryHiddenText.IsQueryField: Boolean;
  327. begin
  328.   Result := True;
  329. end;
  330.  
  331. procedure TQueryHiddenText.SetText(const Value: string);
  332. begin
  333.   FText := Value;
  334. end;
  335.  
  336. procedure TQueryHiddenText.AddAttributes(var Attrs: string);
  337. begin
  338.   Inherited;
  339.   AddQuotedAttrib(Attrs, 'VALUE', Text);
  340. end;
  341.  
  342. function TQueryHiddenText.ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string;
  343. begin
  344.   Result := ControlContent(Options);
  345. end;
  346.  
  347. function TPromptQueryButton.GetSubComponents: TObject;
  348. begin
  349.   Result := nil;
  350. end;
  351.  
  352. end.
  353.