frmdemo.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. unit frmDemo;
  2. {$mode objfpc}
  3. interface
  4. uses
  5. Classes, SysUtils, web, webwidget, htmlwidgets, widgetdemo, propertygridwidget, webrouter;
  6. Type
  7. { THelpDemoContainer }
  8. THelpDemoContainer = class(TDemoContainer)
  9. Protected
  10. Function HTMLTag: String; override;
  11. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  12. public
  13. class function WebWidgetClass: TCustomWebWidgetClass; override;
  14. end;
  15. { TDemoForm }
  16. TDemoForm = Class(TComponent)
  17. Private
  18. Fcontainer : TDemoContainer;
  19. FSelectedClass : TDemoContainerClass;
  20. FDemoParent : TJSHTMLElement;
  21. FPropertyGrid : TSimplePropertyGridWidget;
  22. FCBshowconsole: TJSHTMLInputelement;
  23. FConsole: TJSHTMLElement;
  24. Flist : TSimpleLoopTemplateWidget;
  25. procedure DoGetValue(Sender: TObject; aValue: TLoopTemplateValue);
  26. procedure DoRoute(URl: String; aRoute: TRoute; Params: TStrings);
  27. procedure DoSelectDemo(Sender: TObject; Event: TJSEvent);
  28. function DoShowConsole(Event: TEventListenerEvent): boolean;
  29. procedure ShowSelected;
  30. Public
  31. Constructor Create(aOwner : TComponent) ; override;
  32. Procedure Show;
  33. property SelectedClass : TDemoContainerClass Read FSelectedClass;
  34. end;
  35. implementation
  36. Const
  37. SDemoContainerID = 'democontainer';
  38. SDemoListID = 'demolist';
  39. SPropertyGridID = 'propertygrid';
  40. SShowconsoleID = 'showconsole';
  41. SpasjsconsoleID = 'pasjsconsole';
  42. { THelpDemoContainer }
  43. function THelpDemoContainer.HTMLTag: String;
  44. begin
  45. Result:='p';
  46. end;
  47. procedure THelpDemoContainer.ApplyWidgetSettings(aElement: TJSHTMLElement);
  48. begin
  49. inherited ApplyWidgetSettings(aElement);
  50. aElement.innerHTML:='Please select an element in the list located at the left of the screen';
  51. end;
  52. class function THelpDemoContainer.WebWidgetClass: TCustomWebWidgetClass;
  53. begin
  54. Result:=Nil;
  55. end;
  56. { TDemoForm }
  57. procedure TDemoForm.ShowSelected;
  58. Var
  59. C: TDemoContainerClass;
  60. begin
  61. C:=FSelectedClass;
  62. if C=Nil then
  63. C:=THelpDemoContainer;
  64. FDemoParent.InnerHTML:='';
  65. if Assigned(FContainer) then
  66. FreeAndNil(FContainer);
  67. FDemoParent.InnerHTML:=''; // To be sure
  68. FContainer:=C.Create(Self);
  69. FContainer.Name:='CurrentContainer';
  70. FContainer.ParentID:=SDemoContainerID;
  71. FContainer.Refresh;
  72. FContainer.ShowDemo;
  73. FPropertyGrid.Subject:=FContainer.InspectorInstance;
  74. FPropertyGrid.LookupRoot:=FContainer;
  75. FPropertyGrid.Refresh;
  76. end;
  77. procedure TDemoForm.DoSelectDemo(Sender: TObject; Event: TJSEvent);
  78. begin
  79. { Writeln('Changed : ',Flist.SelectedIndex);
  80. if Flist.SelectedIndex>=0 then
  81. Writeln('Value : ',Flist.Values[Flist.SelectedIndex]);
  82. FSelectedClass:=TDemoFactory.Instance.FindDemoClass(Flist.Values[Flist.SelectedIndex]);}
  83. ShowSelected;
  84. end;
  85. function TDemoForm.DoShowConsole(Event: TEventListenerEvent): boolean;
  86. begin
  87. if FCBShowConsole.checked then
  88. FConsole.style.cssText:=''
  89. else
  90. FConsole.style.cssText:='display: none;'
  91. end;
  92. constructor TDemoForm.Create(aOwner: TComponent);
  93. begin
  94. inherited Create(aOwner);
  95. Flist:=TSimpleLoopTemplateWidget.Create(Self);
  96. Flist.OnGetValue:=@DoGetValue;
  97. FList.HeaderTemplate:='<div class="list-group">';
  98. FList.FooterTemplate:='</div>';
  99. FList.ItemTemplate:='<a href="#/{{DemoName}}/" class="list-group-item list-group-item-action">{{DemoDescription}}</a>';
  100. FDemoParent:=TJSHTMLelement(Document.getElementById(SdemocontainerID));
  101. FPropertyGrid:=TSimplePropertyGridWidget.Create(Self);
  102. FCBshowconsole:=TJSHTMLInputelement(Document.getElementById(SShowconsoleID));
  103. FCBshowconsole.OnChange:=@DoShowConsole;
  104. FConsole:=TJSHTMLElement(Document.getElementById(SpasjsconsoleID));
  105. Router.RegisterRoute(':Demo',@DoRoute,True);
  106. end;
  107. procedure TDemoForm.DoRoute(URl : String; aRoute : TRoute; Params: TStrings);
  108. begin
  109. writeln('In route: ', Params.Values['Demo']);
  110. FSelectedClass:=TDemoFactory.Instance.FindDemoClass(Params.Values['Demo']);
  111. ShowSelected;
  112. end;
  113. procedure TDemoForm.DoGetValue(Sender: TObject; aValue: TLoopTemplateValue);
  114. Var
  115. F : TDemoFactory;
  116. begin
  117. F:=TDemoFactory.Instance;
  118. if aValue.Name='DemoName' then
  119. aValue.Value:=F.Demos[aValue.Index].DemoClass.ClassName
  120. else if aValue.Name='DemoDescription' then
  121. aValue.Value:=F.Demos[aValue.Index].DemoClass.Description
  122. end;
  123. procedure TDemoForm.Show;
  124. Var
  125. F : TDemoFactory;
  126. I : Integer;
  127. begin
  128. F:=TDemoFactory.Instance;
  129. Flist.ParentID:=SDemoListID;
  130. { for I:=0 to F.DemoCount-1 do
  131. begin
  132. Flist.Items.Add(F.Demos[i].DemoClass.Description);
  133. Flist.Values.Add(F.Demos[i].DemoClass.ClassName);
  134. end;
  135. Flist.OnChange:=@DoSelectDemo;
  136. }
  137. FList.ItemCount:=DemoFactory.DemoCount;
  138. Writeln('Demo count: ',FList.ItemCount);
  139. Flist.Refresh;
  140. FPropertyGrid.ParentID:=SPropertyGridID;
  141. ShowSelected;
  142. end;
  143. end.