2
0

treedemo.lpr 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. program BrowserDom10;
  2. {$mode objfpc}
  3. uses
  4. BrowserConsole, JS, Classes, SysUtils, Web, BrowserApp, libzenfs, libzenfsdom, wasizenfs,
  5. zenfsutils;
  6. Type
  7. { TMyApplication }
  8. TMyApplication = class(TBrowserApplication)
  9. Private
  10. BtnDownload : TJSHTMLButtonElement;
  11. EdtFileName : TJSHTMLInputElement;
  12. DivDownloads : TJSHTMLElement;
  13. FTreeBuilder : THTMLZenFSTree;
  14. procedure CreateFiles;
  15. procedure DoReset(Event: TJSEvent); async;
  16. procedure DoSelectFile(Sender: TObject; aFileName: String; aType: TFileType);
  17. procedure MaybeCreateFiles;
  18. procedure SetupFS; async;
  19. procedure DoDownload(Event : TJSEvent);
  20. Public
  21. constructor Create(aOwner : TComponent); override;
  22. procedure DoRun; override;
  23. end;
  24. { TMyApplication }
  25. constructor TMyApplication.Create(aOwner: TComponent);
  26. begin
  27. inherited Create(aOwner);
  28. // Allow to load file specified in hash: index.html#mywasmfile.wasm
  29. BtnDownload:=TJSHTMLButtonElement(GetHTMLElement('btnDownload'));
  30. BtnDownload.AddEVentListener('click',@DoDownload);
  31. BtnDownload:=TJSHTMLButtonElement(GetHTMLElement('btnReset'));
  32. BtnDownload.AddEVentListener('click',@DoReset);
  33. EdtFileName:=TJSHTMLInputElement(GetHTMLElement('edtFilename'));
  34. DivDownloads:=GetHTMLElement('divDownloads');
  35. FTreeBuilder:=THTMLZenFSTree.Create(Self);
  36. FTreeBuilder.MaxHeight:='300px';
  37. FTreeBuilder.ParentElementID:='treeFiles';
  38. FTreeBuilder.OnFileSelected:=@DoSelectFile;
  39. end;
  40. procedure TMyApplication.DoRun;
  41. begin
  42. SetupFS;
  43. end;
  44. procedure TMyApplication.CreateFiles;
  45. Procedure ForceDir(const aDir: string);
  46. var
  47. Stat : TZenFSStats;
  48. begin
  49. try
  50. Stat:=ZenFS.statSync(aDir);
  51. except
  52. Writeln('Directory "',aDir,'" does not exist, creating it.')
  53. end;
  54. if Not assigned(Stat) then
  55. begin
  56. try
  57. ZenFS.mkdirSync(aDir,&775)
  58. except
  59. Writeln('Failed to create directory "',aDir,'"');
  60. Raise;
  61. end;
  62. end
  63. else if Stat.isDirectory then
  64. Raise Exception.Create(aDir+' is not a directory');
  65. end;
  66. Procedure ForceFile(aFile : String);
  67. var
  68. S : String;
  69. I : Integer;
  70. begin
  71. Writeln('Creating file: ',aFile);
  72. S:='This is the content of file "'+aFile+'". Some random numbers:';
  73. For I:=1 to 10+Random(90) do
  74. S:=S+'Line '+IntToStr(i)+': '+IntToStr(1+Random(100))+sLineBreak;
  75. try
  76. ZenFS.writeFileSync(aFile,S);
  77. except
  78. Writeln('Failed to create file: ',aFile);
  79. end;
  80. end;
  81. var
  82. FN : Integer;
  83. begin
  84. ForceDir('/tmp');
  85. ForceFile('/tmp/file1.txt');
  86. ForceDir('/tmp/logs');
  87. For FN:=2 to 5+Random(5) do
  88. ForceFile(Format('/tmp/file_%d.txt',[FN]));
  89. For FN:=1 to 5+Random(5) do
  90. ForceFile(Format('/tmp/logs/file_%.6d.log',[FN]));
  91. ForceDir('/home');
  92. ForceDir('/home/user');
  93. For FN:=1 to 5+Random(5) do
  94. ForceFile(Format('/home/user/diary%d.log',[FN]));
  95. ForceDir('/home/user2');
  96. For FN:=1 to 1+Random(5) do
  97. ForceFile(Format('/home/user2/diary%d.log',[FN]));
  98. end;
  99. procedure TMyApplication.DoSelectFile(Sender: TObject; aFileName: String; aType: TFileType);
  100. const
  101. filetypes : Array[TFileType] of string = ('Unknown','File','Directory','SymLink');
  102. begin
  103. Writeln('You selected '+FileTypes[aType]+': '+aFileName);
  104. if aType=ftFile then
  105. EdtFileName.Value:=aFileName;
  106. end;
  107. procedure TMyApplication.MaybeCreateFiles;
  108. var
  109. Stat : TZenFSStats;
  110. begin
  111. try
  112. Stat:=ZenFS.statSync('/tmp/file1.txt');
  113. except
  114. Writeln('Directory structure does not exist, creating one');
  115. end;
  116. if Not assigned(Stat) then
  117. CreateFiles
  118. else
  119. Writeln('Directory structure already exists.');
  120. end;
  121. procedure TMyApplication.SetupFS;
  122. begin
  123. Terminate;
  124. aWait(TJSObject,ZenFS.configure(
  125. New([
  126. 'mounts', New([
  127. '/',DomBackends.WebStorage
  128. ])
  129. ])));
  130. MaybeCreateFiles;
  131. FTreeBuilder.ShowDir('/');
  132. end;
  133. procedure TMyApplication.DoReset(Event : TJSEvent);
  134. begin
  135. window.localStorage.removeItem('0');
  136. FTreeBuilder.Clear;
  137. SetupFS;
  138. end;
  139. procedure TMyApplication.DoDownload(Event : TJSEvent);
  140. var
  141. a : TJSHTMLAnchorElement;
  142. begin
  143. a:=CreateDownLoadFromFile(edtFileName.value,'application/octet-stream',divDownloads,'file '+edtFileName.value);
  144. a.click;
  145. end;
  146. var
  147. Application : TMyApplication;
  148. begin
  149. Application:=TMyApplication.Create(nil);
  150. Application.Initialize;
  151. Application.Run;
  152. end.