democlasstopas.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. program democlasstopas;
  2. {$IFDEF FPC_DOTTEDUNITS}
  3. uses System.SysUtils, System.Types, BrowserApi.Web, System.Classes, JSApi.JS, Browser.Console, System.Class2Pas;
  4. {$ELSE FPC_DOTTEDUNITS}
  5. uses Sysutils, Types, Web, Classes, JS, browserconsole, class2pas;
  6. {$ENDIF FPC_DOTTEDUNITS}
  7. Type
  8. { TGenCodeApp }
  9. TGenCodeApp = Class
  10. elHead : TJSHTMLElement;
  11. btnGo : TJSHTMLElement;
  12. btnLoad : TJSHTMLElement;
  13. edtJSObject : TJSHTMLInputElement;
  14. edtScript : TJSHTMLInputElement;
  15. edtPascalClass : TJSHTMLInputElement;
  16. edtPascalParentClass : TJSHTMLInputElement;
  17. edtExternalName : TJSHTMLInputElement;
  18. edtClassDefinition : TJSHTMLTextAreaElement;
  19. Procedure Execute;
  20. procedure ShowRTLProps(aClassName,aParentClassName,aJSClassName : String; O : TJSObject);
  21. private
  22. function DoGenCode(aEvent: TJSMouseEvent): boolean;
  23. function DoLoad(aEvent: TJSMouseEvent): boolean;
  24. function FindObject(aPath: String): TJSObject;
  25. end;
  26. procedure TGenCodeApp.ShowRTLProps(aClassName,aParentClassName,aJSClassName : String; O : TJSObject);
  27. Var
  28. S : TStrings;
  29. begin
  30. S:=TStringList.Create;
  31. try
  32. ClassToPas(aJSClassName,aClassName,aParentClassName,O,S,True);
  33. edtClassDefinition.value:=S.Text;
  34. finally
  35. S.Free;
  36. end;
  37. end;
  38. function TGenCodeApp.FindObject(aPath : String): TJSObject;
  39. Var
  40. p : JSValue;
  41. O : TJSObject;
  42. Path : TStringDynArray;
  43. Done,S : String;
  44. begin
  45. Path:=aPath.Split('.');
  46. Result:=nil;
  47. O:=Window;
  48. Done:='';
  49. for S in Path do
  50. begin
  51. if Done<>'' then
  52. Done:=Done+'.';
  53. Done:=Done+S;
  54. p:=O.Properties[S];
  55. if Not Assigned(P) then
  56. begin
  57. Window.Alert('No object found at : '+Done);
  58. exit;
  59. end;
  60. if Not isObject(P) then
  61. begin
  62. Window.Alert('Value at : '+Done+' is not an object');
  63. exit;
  64. end;
  65. O:=TJSObject(P);
  66. end;
  67. Result:=O;
  68. end;
  69. function TGenCodeApp.DoGenCode(aEvent: TJSMouseEvent): boolean;
  70. var
  71. O : TJSObject;
  72. begin
  73. Result:=False;
  74. if (edtPascalClass.value='') or (edtJSObject.Value='') or (edtExternalName.Value='') then
  75. begin
  76. Window.Alert('Please fill in all fields');
  77. exit;
  78. end;
  79. O:=FindObject(edtJSObject.Value);
  80. if Assigned(O) then
  81. ShowRTLProps(edtPascalClass.value,edtPascalParentClass.Value,edtExternalName.Value,O);
  82. end;
  83. function TGenCodeApp.DoLoad(aEvent: TJSMouseEvent): boolean;
  84. Var
  85. El : TJSElement;
  86. begin
  87. if (edtScript.Value='') then
  88. begin
  89. Window.Alert('Please fill in URL');
  90. exit;
  91. end;
  92. El:=Document.createElement('script');
  93. EL.Properties['src']:=edtScript.Value;
  94. elHead.appendChild(El);
  95. end;
  96. Procedure TGEncodeApp.Execute;
  97. begin
  98. elHead:=TJSHTMLElement(Document.GetElementByID('head'));
  99. btnGo:=TJSHTMLButtonElement(Document.GetElementByID('go'));
  100. btnLoad:=TJSHTMLButtonElement(Document.GetElementByID('load'));
  101. edtJSObject:=TJSHTMLInputElement(Document.GetElementByID('edtJSObject'));
  102. edtScript:=TJSHTMLInputElement(Document.GetElementByID('edtScript'));
  103. edtPascalClass:=TJSHTMLInputElement(Document.GetElementByID('edtPascalClass'));
  104. edtPascalParentClass:=TJSHTMLInputElement(Document.GetElementByID('edtPascalClassAncestor'));
  105. edtExternalName:=TJSHTMLInputElement(Document.GetElementByID('edtExternalName'));
  106. edtClassDefinition:=TJSHTMLTextAreaElement(Document.GetElementByID('edtClassDefinition'));
  107. btnGo.onclick:=@DoGenCode;
  108. btnLoad.onclick:=@DoLoad;
  109. end;
  110. begin
  111. With TGenCodeApp.Create do
  112. Execute;
  113. end.