demowebcompiler.lpr 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. program demowebcompiler;
  2. {$mode objfpc}
  3. uses
  4. Classes, SysUtils, Web, webfilecache, pas2jswebcompiler;
  5. Type
  6. { TWebCompilerDemo }
  7. TWebCompilerDemo = Class(TComponent)
  8. Private
  9. BCompile : TJSHTMLElement;
  10. BDefaultUnits : TJSHTMLElement;
  11. BThisUnit : TJSHTMLElement;
  12. EUnitName : TJSHTMLInputElement;
  13. BRun : TJSHTMLElement;
  14. MSource : TJSHTMLInputElement;
  15. MLog: TJSHTMLInputElement;
  16. MUnits: TJSHTMLInputElement;
  17. RFrame : TJSHTMLIFrameElement;
  18. PResult : TJSHTMLElement;
  19. FCompiler : TPas2JSWebCompiler;
  20. procedure ActivateTab(aTab: String);
  21. procedure ClearResult;
  22. procedure DoLog(Sender: TObject; const Msg: String);
  23. function LoadDefaultsClick(aEvent: TJSMouseEvent): boolean;
  24. function LoadSingleUnitClick(aEvent: TJSMouseEvent): boolean;
  25. procedure OnUnitLoaded(Sender: TObject; aFileName: String; aError: string);
  26. function RunClick(aEvent: TJSMouseEvent): boolean;
  27. Protected
  28. function CompileClick(aEvent: TJSMouseEvent): boolean;
  29. Procedure LinkElements;
  30. Property Compiler : TPas2JSWebCompiler Read FCompiler;
  31. Public
  32. Constructor Create(aOwner : TComponent); override;
  33. Procedure Execute;
  34. end;
  35. Const
  36. // Default run HTML page, shown in IFrame.
  37. SHTMLHead =
  38. '<HTML>'+LineEnding+
  39. '<head>'+LineEnding+
  40. ' <meta charset="UTF-8">'+LineEnding+
  41. ' <Title>Pas2JS web compiler Program output</Title>'+LineEnding+
  42. ' <script type="application/javascript">'+LineEnding;
  43. SHTMLTail =
  44. ' </script>'+LineEnding+
  45. ' <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">'+LineEnding+
  46. '</head>'+LineEnding+
  47. '<body>'+LineEnding+
  48. ' <div class="container">'+LineEnding+
  49. ' <div class="panel panel-info">'+LineEnding+
  50. ' <div class="panel-heading">Run program output</div>'+LineEnding+
  51. ' <div class="panel-body">'+
  52. ' <div id="pasjsconsole" style="width: 640px; height: 200px;">'+LineEnding+
  53. ' </div>'+LineEnding+
  54. ' </div>'+LineEnding+
  55. ' </div>'+LineEnding+
  56. '</div>'+LineEnding+
  57. '<script>'+LineEnding+
  58. ' rtl.run();'+LineEnding+
  59. '</script>'+LineEnding+
  60. '</body>'+LineEnding+
  61. '</HTML>';
  62. { TWebCompilerDemo }
  63. procedure TWebCompilerDemo.OnUnitLoaded(Sender: TObject; aFileName: String; aError: string);
  64. begin
  65. if aError='' then
  66. MUnits.Value:=MUnits.Value+sLineBreak+'Loaded: '+aFileName
  67. else
  68. MUnits.Value:=MUnits.Value+sLineBreak+'Error Loading "'+aFileName+'": '+AError;
  69. if SameText(afilename,EUnitName.Value) then
  70. EUnitName.Value:='';
  71. end;
  72. procedure TWebCompilerDemo.LinkElements;
  73. begin
  74. BCompile:=TJSHTMLElement(Document.getElementById('btn-compile'));
  75. BCompile.onclick:=@CompileClick;
  76. BRun:=TJSHTMLElement(Document.getElementById('btn-run'));
  77. BRun.onClick:=@RunClick;
  78. MSource:=TJSHTMLInputElement(Document.getElementById('memo-program-src'));
  79. MLog:=TJSHTMLInputElement(Document.getElementById('memo-compiler-output'));
  80. MUnits:=TJSHTMLInputElement(Document.getElementById('memo-loaded-units'));
  81. RFrame:=TJSHTMLIFrameElement(Document.getElementById('runarea'));
  82. BDefaultUnits:=TJSHTMLElement(Document.getElementById('btn-load-defaults'));
  83. BDefaultUnits.Onclick:=@LoadDefaultsClick;
  84. BThisUnit:=TJSHTMLElement(Document.getElementById('btn-load-unit'));
  85. BThisUnit.Onclick:=@LoadSingleUnitClick;
  86. EUnitName:=TJSHTMLInputElement(Document.getElementById('edt-load-unit-name'));
  87. PResult:=TJSHTMLElement(Document.getElementById('compile-result'));
  88. end;
  89. constructor TWebCompilerDemo.Create(aOwner : TComponent);
  90. begin
  91. Inherited;
  92. FCompiler:=TPas2JSWebCompiler.Create;
  93. Compiler.Log.OnLog:=@DoLog;
  94. Compiler.WebFS.LoadBaseURL:='sources';
  95. end;
  96. function TWebCompilerDemo.RunClick(aEvent: TJSMouseEvent): boolean;
  97. Var
  98. Src : String;
  99. begin
  100. Result:=True;
  101. Src:=Compiler.WebFS.GetFileContent('main.js');
  102. if Src='' then
  103. begin
  104. Window.Alert('No source available');
  105. exit;
  106. end;
  107. Src:=SHTMLHead+Src+LineEnding+SHTMLTail;
  108. RFrame['srcdoc']:=Src;
  109. end;
  110. procedure TWebCompilerDemo.DoLog(Sender: TObject; const Msg: String);
  111. begin
  112. MLog.Value:=MLog.Value+sLineBreak+Msg;
  113. end;
  114. function TWebCompilerDemo.LoadDefaultsClick(aEvent: TJSMouseEvent): boolean;
  115. begin
  116. Result:=False;
  117. Compiler.WebFS.LoadFiles(['rtl.js','system.pas','sysutils.pas','types.pas','typinfo.pas','classes.pas','rtlconsts.pas','js.pas','web.pas','browserconsole.pas'],@OnUnitLoaded);
  118. end;
  119. function TWebCompilerDemo.LoadSingleUnitClick(aEvent: TJSMouseEvent): boolean;
  120. begin
  121. Result:=False;
  122. Compiler.WebFS.LoadFile(EUnitName.Value,@OnUnitLoaded);
  123. end;
  124. Procedure TWebCompilerDemo.ActivateTab(aTab : String);
  125. begin
  126. asm
  127. $("#act-"+aTab).tab('show');
  128. end;
  129. end;
  130. Procedure TWebCompilerDemo.ClearResult;
  131. begin
  132. While PResult.firstElementChild<>Nil do
  133. PResult.removeChild(PResult.firstElementChild);
  134. end;
  135. function TWebCompilerDemo.CompileClick(aEvent: TJSMouseEvent): boolean;
  136. Procedure ShowResult(success : boolean);
  137. Const
  138. CloseLink = '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
  139. Var
  140. E : TJSHTMLElement;
  141. begin
  142. ClearResult;
  143. E:=TJSHTMLElement(document.createElement('div'));
  144. if Success then
  145. begin
  146. E['class']:='alert alert-success alert-dismissible fade in';
  147. E.innerHTML:=CloseLink+'<strong>Succes!</strong> program compiled succesfully. You can run the program now.';
  148. end
  149. else
  150. begin
  151. E['class']:='alert alert-danger alert-dismissible fade in';
  152. E.innerHTML:=CloseLink+'<strong>Failure</strong> failed to compile program, please check error messages.';
  153. end;
  154. PResult.appendChild(E);
  155. end;
  156. Var
  157. args : TStrings;
  158. Res : Boolean;
  159. begin
  160. Result:=False;
  161. BRun['disabled']:='disabled';
  162. ClearResult;
  163. MLog.Value:='';
  164. Compiler.WebFS.SetFileContent('main.pp',MSource.value);
  165. args:=TStringList.Create;
  166. try
  167. Args.Add('-Tbrowser');
  168. Args.Add('-Jc');
  169. Args.Add('-Jirtl.js');
  170. Args.Add('main.pp');
  171. ActivateTab('output');
  172. RFrame.Src:='run.html';
  173. Compiler.Run('','',Args,True);
  174. Res:=Compiler.ExitCode=0;
  175. ShowResult(Res);
  176. if Res then
  177. BRun.removeAttribute('disabled');
  178. finally
  179. Args.Free;
  180. end;
  181. end;
  182. procedure TWebCompilerDemo.Execute;
  183. begin
  184. LinkElements;
  185. end;
  186. begin
  187. With TWebCompilerDemo.Create(Nil) do
  188. Execute;
  189. end.