webcoreformgen.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. {
  2. This file is part of the Pas2JS tool chain
  3. Copyright (c) 2020 by Michael Van Canneyt
  4. This unit implements a HTML to DFM/LFM generator
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit webcoreformgen;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, formgen;
  16. type
  17. TWebCoreFormFileCodeGen = Class;
  18. { TWebCoreFormCodeGen }
  19. TWebCoreOption = (wcoUseActionList);
  20. TWebCoreOptions = set of TWebCoreOption;
  21. TWebCoreFormCodeGen = Class(TFormCodeGen)
  22. private
  23. FListClassName: String;
  24. FListInstanceName: String;
  25. FWebCoreOptions: TWebCoreOptions;
  26. function GetWFF: TWebCoreFormFileCodeGen;
  27. procedure SetListClassName(AValue: String);
  28. procedure SetListInstanceName(AValue: String);
  29. procedure SetWebCoreOptions(AValue: TWebCoreOptions);
  30. Protected
  31. procedure EmitPublishedSection; override;
  32. function CreateFormFileGen : TFormFileCodeGen; override;
  33. Property WebFormFile : TWebCoreFormFileCodeGen Read GetWFF;
  34. Published
  35. Property WebCoreOptions : TWebCoreOptions Read FWebCoreOptions Write SetWebCoreOptions;
  36. Property ListClassName : String Read FListClassName Write SetListClassName;
  37. Property ListInstanceName : String Read FListInstanceName Write SetListInstanceName;
  38. end;
  39. { TWebCoreFormFileCodeGen }
  40. TWebCoreFormFileCodeGen = Class(TFormFileCodeGen)
  41. private
  42. FListClassName: String;
  43. FListInstanceName: String;
  44. FWebCoreOptions: TWebCoreOptions;
  45. procedure EmitItem(el: TFormElement; aEvent, aHandler: String);
  46. procedure GenerateListItems;
  47. Protected
  48. Procedure GenerateElements; override;
  49. Public
  50. Constructor create(aOwner : TComponent); override;
  51. Published
  52. Property WebCoreOptions : TWebCoreOptions Read FWebCoreOptions Write FWebCoreOptions;
  53. Property ListClassName : String Read FListClassName Write FListClassName;
  54. Property ListInstanceName : String Read FListInstanceName Write FListInstanceName;
  55. end;
  56. implementation
  57. { TWebCoreFormFileCodeGen }
  58. procedure TWebCoreFormFileCodeGen.EmitItem(el : TFormElement; aEvent,aHandler : String);
  59. begin
  60. AddLn('item');
  61. Indent;
  62. AddLn('ID = ''%s''',[el.HTMLID]);
  63. if (aEvent<>'') then
  64. begin
  65. Addln('Event = %s',[aEvent]);
  66. if (aHandler<>'') then
  67. AddLn('OnExecute = %s',[aHandler]);
  68. end;
  69. AddLn('TargetAction = actNone');
  70. Undent;
  71. AddLn('end');
  72. end;
  73. procedure TWebCoreFormFileCodeGen.GenerateListItems;
  74. Var
  75. I : Integer;
  76. El : TFormElement;
  77. S,EN,EH : String;
  78. begin
  79. Addln('Actions = <');
  80. Indent;
  81. For I:=0 to FormElements.Count-1 do
  82. begin
  83. el:=FormElements[i];
  84. // Web Core does not support multiple events on 1 webaction,
  85. // So, we must generate 1 item per event.
  86. if DoEvents and (El.Events.Count>0) then
  87. begin
  88. For S in El.Events do
  89. begin
  90. TFormCodeGen.GetEventNameAndHandler(S,El.Name,EN,EH);
  91. EmitItem(El,EN,EH);
  92. end;
  93. end
  94. else
  95. EmitItem(El,'','');
  96. end;
  97. Undent;
  98. Addln('>');
  99. end;
  100. procedure TWebCoreFormFileCodeGen.GenerateElements;
  101. begin
  102. if wcoUseActionList in WebcoreOptions then
  103. begin
  104. AddLn('object %s : %s',[ListInstanceName,ListClassName]);
  105. Indent;
  106. GenerateListItems;
  107. Undent;
  108. AddLn('end');
  109. end
  110. else
  111. inherited GenerateElements;
  112. end;
  113. constructor TWebCoreFormFileCodeGen.create(aOwner: TComponent);
  114. begin
  115. inherited create(aOwner);
  116. ListInstanceName:='Elements';
  117. ListClassName:='TWebElementActionList';
  118. end;
  119. { TWebCoreFormCodeGen }
  120. procedure TWebCoreFormCodeGen.SetWebCoreOptions(AValue: TWebCoreOptions);
  121. begin
  122. if FWebCoreOptions=AValue then Exit;
  123. FWebCoreOptions:=AValue;
  124. WebFormFile.WebCoreOptions:=aValue;
  125. if wcoUseActionList in aValue then
  126. begin
  127. EventSignature:='Sender: TObject; Element: TJSHTMLElementRecord; Event: TJSEventParameter';
  128. EventModifiers:='';
  129. end;
  130. end;
  131. function TWebCoreFormCodeGen.GetWFF: TWebCoreFormFileCodeGen;
  132. begin
  133. Result:=Self.FormFileGenerator as TWebCoreFormFileCodeGen;
  134. end;
  135. procedure TWebCoreFormCodeGen.SetListClassName(AValue: String);
  136. begin
  137. if FListClassName=AValue then Exit;
  138. FListClassName:=AValue;
  139. WebFormFile.ListClassName:=aValue;
  140. end;
  141. procedure TWebCoreFormCodeGen.SetListInstanceName(AValue: String);
  142. begin
  143. if FListInstanceName=AValue then Exit;
  144. FListInstanceName:=AValue;
  145. WebFormFile.ListInstanceName:=aValue;
  146. end;
  147. procedure TWebCoreFormCodeGen.EmitPublishedSection;
  148. begin
  149. if wcoUseActionList in WebcoreOptions then
  150. begin
  151. AddLn('%s : %s;',[ListInstanceName,ListClassName]);
  152. end
  153. else
  154. inherited EmitPublishedSection;
  155. end;
  156. function TWebCoreFormCodeGen.CreateFormFileGen: TFormFileCodeGen;
  157. begin
  158. Result:=TWebCoreFormFileCodeGen.Create(Nil);
  159. end;
  160. end.