demorest.lpr 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. program demorest;
  2. uses sysutils, classes, js, web, db, extjsdataset, jsondataset, restconnection;
  3. Type
  4. { TRestDataset }
  5. TRestDataset = Class(TExtJSJSONObjectDataSet)
  6. private
  7. FConnection: TRestConnection;
  8. Protected
  9. Function DoGetDataProxy: TDataProxy; override;
  10. Public
  11. Property Connection: TRestConnection Read FConnection Write FConnection;
  12. end;
  13. { TForm }
  14. TForm = Class
  15. private
  16. Table,
  17. TBody : TJSElement;
  18. Panel,
  19. PanelContent,
  20. ButtonDelete,
  21. ButtonAdd,
  22. ButtonApplyUpdates,
  23. ButtonChange,
  24. ButtonFetch : TJSElement;
  25. EName,
  26. EPopulation: TJSHTMLInputElement;
  27. DS : TRestDataset;
  28. Conn : TRestConnection;
  29. Public
  30. Constructor create;
  31. function CreateTable: TJSElement;
  32. procedure AddRecords;
  33. function ApplyUpdatesClick(aEvent{%H-}: TJSMouseEvent): boolean;
  34. function ButtonAddClick(aEvent: TJSMouseEvent): boolean;
  35. function ButtonDeleteClick(aEvent{%H-}: TJSMouseEvent): boolean;
  36. function ButtonFetchClick(Event{%H-}: TJSMouseEvent): boolean;
  37. function CreateInput(aParent: TJSElement; Atype, AName, ALabel, aID: String): TJSHTMLInputElement;
  38. function CreateRow(AID : Integer; AName: String; APopulation: NativeInt): TJSElement;
  39. function DoAddRecord(aEvent{%H-}: TJSMouseEvent): boolean;
  40. procedure DoAfterLoad(DataSet: TDataSet);
  41. function DoEditRecord(aEvent{%H-}: TJSMouseEvent): boolean;
  42. procedure DoGetURL(Sender: TComponent; aRequest: TDataRequest; Var aURL: String);
  43. procedure DoLoadFail(DataSet{%H-}: TDataSet; ID{%H-}: Integer; const ErrorMsg: String);
  44. procedure DSAfterApplyUpdates(DataSet{%H-}: TDataSet; Updates{%H-}: TResolveResults);
  45. procedure DSOpen(DataSet{%H-}: TDataSet);
  46. procedure ResetButtons(Sender: TDataset);
  47. function SelectRecord(aEvent: TJSMouseEvent): boolean;
  48. end;
  49. function TRestDataset.DoGetDataProxy: TDataProxy;
  50. begin
  51. Result:=Connection.DataProxy;
  52. end;
  53. function TForm.CreateRow(AID : Integer; AName : String; APopulation : NativeInt) : TJSElement;
  54. Var
  55. C : TJSElement;
  56. S : String;
  57. begin
  58. Result:=document.createElement('TR');
  59. S:=IntToStr(AID);
  60. Result.id:=S;
  61. C:=document.createElement('TD');
  62. C.id:=S+'_name';
  63. Result.Append(C);
  64. C.appendChild(Document.createTextNode(AName));
  65. Result.id:=IntToStr(AID);
  66. C:=document.createElement('TD');
  67. C.id:=S+'_population';
  68. Result.Append(C);
  69. C.AppendChild(document.createTextNode(IntToStr(APopulation)));
  70. TJSHTMLElement(Result).onclick:=@SelectRecord;
  71. end;
  72. function TForm.DoAddRecord(aEvent: TJSMouseEvent): boolean;
  73. begin
  74. DS.Append;
  75. DS.FieldByName('name').AsString:=EName.value;
  76. DS.FieldByName('population').AsString:=EPopulation.value;
  77. DS.Post;
  78. Result:=True;
  79. end;
  80. function TForm.DoEditRecord(aEvent: TJSMouseEvent): boolean;
  81. Var
  82. E : TJSElement;
  83. begin
  84. Writeln('Updating record: ',DS.RecNo);
  85. DS.Edit;
  86. DS.FieldByName('name').AsString:=EName.value;
  87. DS.FieldByName('population').AsString:=EPopulation.value;
  88. DS.Post;
  89. E:=Document.getElementById(IntToStr(DS.RecNo)+'_name');
  90. if Assigned(E) then
  91. E.innerText:=EName.value;
  92. E:=Document.getElementById(IntToStr(DS.RecNo)+'_population');
  93. if Assigned(E) then
  94. E.innerText:=EPopulation.value;
  95. Result:=True;
  96. end;
  97. procedure TForm.DoAfterLoad(DataSet: TDataSet);
  98. begin
  99. if Dataset.Active then
  100. begin
  101. Writeln('Loading additional records to table');
  102. // We're on the last record of the last batch, so move forward 1 record
  103. Dataset.Next;
  104. AddRecords;
  105. end;
  106. end;
  107. procedure TForm.DoGetURL(Sender: TComponent; aRequest: TDataRequest; Var aURL: String);
  108. begin
  109. aURL:='http://localhost:3000/countries/?limit=20&offset='+IntToStr((aRequest.RequestID-1)*20);
  110. end;
  111. procedure TForm.DoLoadFail(DataSet: TDataSet; ID: Integer; const ErrorMsg: String);
  112. Var
  113. N : TJSElement;
  114. begin
  115. N:=Document.CreateElement('div');
  116. N.appendChild(Document.createTextNode('Failed to load countries...'+ErrorMsg));
  117. PanelContent.append(N);
  118. end;
  119. procedure TForm.DSAfterApplyUpdates(DataSet: TDataSet; Updates : TResolveResults);
  120. begin
  121. Window.Alert('Updates applied on server!');
  122. EName.value:='';
  123. EPopulation.value:='0';
  124. end;
  125. function TForm.CreateTable : TJSElement;
  126. Var
  127. TH,R,H : TJSElement;
  128. begin
  129. Result:=document.createElement('TABLE');
  130. Result.className:='table table-striped table-bordered table-hover table-condensed';
  131. TH:=document.createElement('THEAD');
  132. Result.Append(TH);
  133. R:=document.createElement('TR');
  134. TH.Append(R);
  135. H:=document.createElement('TH');
  136. R.Append(H);
  137. H.AppendChild(document.createTextNode('Name'));
  138. H:=document.createElement('TH');
  139. R.Append(H);
  140. H.AppendChild(document.createTextNode('Population'));
  141. end;
  142. procedure TForm.DSOpen(DataSet: TDataSet);
  143. begin
  144. console.log('Dataset opened');
  145. if not Assigned(Table) then
  146. begin
  147. Table:=CreateTable;
  148. Document.Body.append(Table);
  149. TBody:=document.createElement('TBODY');
  150. Table.Append(TBody);
  151. end;
  152. DS.First;
  153. AddRecords;
  154. end;
  155. function TForm.SelectRecord(aEvent: TJSMouseEvent): boolean;
  156. Var
  157. aID : integer;
  158. e : TJSElement;
  159. begin
  160. e:=aEvent.target;
  161. While Assigned(e) and Not SameText(e.nodeName,'tr') do
  162. e:=e.parentElement;
  163. if Not Assigned(E) then exit;
  164. aId:=StrToIntDef(e.ID,-1);
  165. Writeln('Jumping to record : ',aID);
  166. if (AID<>-1) then
  167. begin
  168. DS.RecNo:=AID;
  169. EName.value:=DS.FieldByName('Name').AsString;
  170. EPopulation.value:=DS.FieldByName('Population').AsString;
  171. TJSHTMLInputElement(ButtonChange).disabled:=false;
  172. TJSHTMLInputElement(ButtonDelete).disabled:=false;
  173. end;
  174. Result:=true;
  175. end;
  176. procedure TForm.AddRecords;
  177. begin
  178. While not DS.EOF do
  179. begin
  180. // Writeln(DS.RecNo, ' - ',DS.FieldByName('Name').AsString,'-',DS.FieldByName('Population').AsInteger);
  181. TBody.Append(CreateRow(DS.RecNo, DS.FieldByName('Name').AsString,DS.FieldByName('Population').AsInteger));
  182. DS.Next;
  183. end;
  184. TJSHTMLInputElement(ButtonChange).disabled:=false;
  185. end;
  186. function TForm.ApplyUpdatesClick(aEvent: TJSMouseEvent): boolean;
  187. begin
  188. DS.ApplyUpdates;
  189. Result:=true;
  190. end;
  191. function TForm.ButtonAddClick(aEvent: TJSMouseEvent): boolean;
  192. begin
  193. DS.Append;
  194. DS.FieldByname('Name').AsString:=EName.value;
  195. DS.FieldByname('Population').AsLargeInt:=StrToInt(EPopulation.Value);
  196. DS.Post;
  197. AddRecords;
  198. end;
  199. function TForm.ButtonDeleteClick(aEvent: TJSMouseEvent): boolean;
  200. Var
  201. ID : Integer;
  202. E : TJSElement;
  203. begin
  204. ID:=DS.RecNo;
  205. if ID=0 then ;
  206. DS.Delete;
  207. EName.value:='';
  208. EPopulation.Value:='';
  209. E:=Document.getElementById(IntToStr(DS.RecNo));
  210. if E<>Nil then
  211. E.parentElement.removeChild(e);
  212. Result:=true;
  213. end;
  214. function TForm.ButtonFetchClick(Event: TJSMouseEvent): boolean;
  215. begin
  216. if Assigned(TBody) then
  217. TBody.innerHTML:='';
  218. DS.Load([],Nil);
  219. Result:=true;
  220. end;
  221. Function TForm.CreateInput(aParent : TJSElement; Atype,AName,ALabel,aID : String) : TJSHTMLInputElement;
  222. Var
  223. aDiv,aLabelDiv : TJSElement;
  224. begin
  225. adiv:=document.createElement('div');
  226. adiv.className:='form-group';
  227. aLabelDiv:=document.createElement('label');
  228. aLabelDiv['for']:=aID;
  229. aLabelDiv.innerText:=ALabel;
  230. aDiv.appendChild(aLabelDiv);
  231. Result:=TJSHTMLInputElement(document.createElement('input'));
  232. Result.id:=AID;
  233. Result.name:=AName;
  234. Result['type']:=AType;
  235. aDiv.appendChild(Result);
  236. aParent.appendChild(aDiv);
  237. end;
  238. Procedure TForm.ResetButtons (Sender : TDataset);
  239. begin
  240. TJSHTMLInputElement(ButtonChange).disabled:=True;
  241. TJSHTMLInputElement(ButtonDelete).disabled:=True;
  242. end;
  243. constructor TForm.create;
  244. Var
  245. F : TJSElement;
  246. begin
  247. Panel:=document.createElement('div');
  248. // attrs are default array property...
  249. Panel['class']:='panel panel-default';
  250. PanelContent:=document.createElement('div');
  251. PanelContent['class']:='panel-body';
  252. document.body.appendChild(panel);
  253. Panel.appendChild(PanelContent);
  254. // fetch button
  255. ButtonFetch:=document.createElement('button');
  256. ButtonFetch['id']:='ButtonFetch';
  257. ButtonFetch.className:='btn btn-default';
  258. ButtonFetch.InnerText:='Fetch countries';
  259. TJSHTMLElement(ButtonFetch).onclick:=@ButtonFetchClick;
  260. PanelContent.AppendChild(ButtonFetch);
  261. // Input form
  262. F:=document.createElement('form');
  263. F.ClassName:='form-inline';
  264. PanelContent.appendChild(F);
  265. EName:=CreateInput(F, 'text','EName','Name','IDName');
  266. EPopulation:=CreateInput(F, 'number','EPopulation','Population','IDPopulation');
  267. // Add/Apply updates button
  268. // Add
  269. ButtonAdd:=document.createElement('button');
  270. ButtonAdd['id']:='ButtonAdd';
  271. ButtonAdd.className:='btn btn-default';
  272. ButtonAdd.InnerText:='Add record';
  273. TJSHTMLElement(ButtonAdd).OnClick:=@DoAddRecord;
  274. PanelContent.AppendChild(ButtonAdd);
  275. // Edit
  276. ButtonChange:=document.createElement('button');
  277. ButtonChange['id']:='ButtonChange';
  278. ButtonChange.className:='btn btn-default';
  279. ButtonChange.InnerText:='Update record';
  280. TJSHTMLInputElement(ButtonChange).disabled:=true;
  281. TJSHTMLElement(ButtonChange).OnClick:=@DoEditRecord;
  282. PanelContent.AppendChild(ButtonChange);
  283. // Delete
  284. ButtonDelete:=document.createElement('button');
  285. ButtonDelete['id']:='ButtonDelete';
  286. ButtonDelete.className:='btn btn-default';
  287. ButtonDelete.InnerText:='Delete record';
  288. TJSHTMLInputElement(ButtonDelete).disabled:=true;
  289. TJSHTMLElement(ButtonDelete).OnClick:=@ButtonDeleteClick;
  290. PanelContent.AppendChild(ButtonDelete);
  291. // Apply updates
  292. ButtonApplyUpdates:=document.createElement('button');
  293. ButtonApplyUpdates['id']:='ButtonAdd';
  294. ButtonApplyUpdates.className:='btn btn-default';
  295. ButtonApplyUpdates.InnerText:='Apply updates';
  296. TJSHTMLElement(ButtonApplyUpdates).onclick:=@ApplyUpdatesClick;
  297. PanelContent.AppendChild(ButtonApplyUpdates);
  298. DS:=TRestDataset.Create(Nil);
  299. Conn:=TRestConnection.Create(nil);
  300. Conn.BaseURL:='/countries';
  301. Conn.OnGetURL:=@DoGetURL;
  302. DS.Connection:=Conn;
  303. DS.OnLoadFail:=@DoLoadFail;
  304. DS.AfterLoad:=@DoAfterLoad;
  305. DS.AfterOpen:=@DSOpen;
  306. DS.AfterApplyUpdates:=@DSAfterApplyUpdates;
  307. DS.AfterPost:=@ResetButtons;
  308. DS.AfterDelete:=@ResetButtons;
  309. end;
  310. begin
  311. TForm.Create;
  312. end.