democlasstopas.pas 3.2 KB

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