-
Notifications
You must be signed in to change notification settings - Fork 2
/
test9.pas
180 lines (158 loc) · 5.04 KB
/
test9.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
program webTest;
uses
SysUtils, sharedMem, webUtils;
var
webData: TSharedMem;
// menyisipkan javascript ke dokumen html
procedure writeScript(const aScript: string);
begin
writeln('<script>',LE,aScript,'</script>',LE);
end;
// mengubah isian masukan dgn kode javascript [string]
function jsFillValue(const aID: string; const aValue: string): string;
begin
result := 'document.getElementById("'+aID+'").value = "'+aValue+'";'+LE;
end;
// mengubah isian masukan dgn kode javascript [integer]
function jsFillValue(const aID: string; const aValue: integer): string;
begin
result := 'document.getElementById("'+aID+'").value = '+intToStr(aValue)+';'+LE;
end;
// mengubah isian masukan dgn kode javascript [boolean]
function jsFillValue(const aID: string; const aValue: boolean): string;
begin
result := 'document.getElementById("'+aID+'").checked = '+
lowerCase(boolToStr(aValue,true))+';'+LE;
end;
// mengubah isian masukan dgn kode javascript [memo]
function jsFillMemo(const aID: string; const aValue: string): string;
begin
result := 'document.getElementById("'+aID+'").innerHTML = "'+aValue+'";'+LE;
end;
// membuat garis pemisah vertical
function spacer(const aClass: string = 'spacer'; const aID: string = ''): string;
begin
result := '<span'+switch(aClass='','',' class="'+aClass+'"')+
switch(aID='','',' id="'+aID+'"')+'> │ </span>';
end;
// membuat masukan tombol
function inputButton(const aCaption: string; const action: string = '';
const isReset: boolean = false): string;
begin
inputIndex := inputIndex+1;
result := '<button'+
' type="'+switch(isReset,'reset','submit')+'"'+
' id="input_'+intToStr(inputIndex)+'"'+
' name="input_'+intToStr(inputIndex)+'"'+
switch(action='','',' formaction="'+action+'"')+
' value="clicked">'+aCaption+
'</button>';
end;
// gabungkan antarmuka masukan dan pembacaan data masukan [button]
procedure webReadButton(var clicked: boolean; const aCaption: string; const newLine: boolean = false;
const action: string = ''; const isReset: boolean = false);
var
s: string;
begin
s := getValue('input_'+intToStr(inputIndex+1),allWebInput);
clicked := (s = 'clicked'); // baca klik dari nilai
write(inputButton(aCaption,action,isReset));
if newLine then writeln(BR) else writeln;
end;
// cek ketersediaan data tersimpan
function hasWebData: boolean;
var
s: string;
begin
memRead(webData,s);
result := (s <> '');
end;
// simpan data ke memori
procedure saveWebData;
begin
memWrite(webData,allWebInput);
end;
// baca data dari memori
function readWebData: string;
begin
memRead(webData,result);
end;
// mengisi form dengan data tersimpan
procedure showWebData(const isReset: boolean = false);
var
i: integer = 0;
d,s,web: string;
function getInput: string;
begin
i += 1;
result := getValue('input_'+intToStr(i),web);
end;
begin
web := readWebData;
log.debug('#'+{$I %LINE%}+': @showWebData saved = '+web);
// baca dan tampilkan data masukan sesuai urutannya
d := switch(isReset,'',htmlEncode(httpDecode(getInput)));
s := jsFillValue('input_'+intToStr(i),d);
d := switch(isReset,'',getInput);
s += jsFillValue('input_'+intToStr(i),d);
d := switch(isReset,'',getInput);
s += jsFillValue('input_'+intToStr(i),d='true');
d := switch(isReset,'',htmlEncode(httpDecode(getInput)));
s += jsFillMemo('input_'+intToStr(i),d);
writeScript(s);
end;
// hapus data tersimpan
procedure clearWebData;
begin
memDestroy(webData);
showWebData(true);
end;
// menulis isi laman
procedure writeContent;
var
d: string = '';
s: string = '';
i: integer = -1;
b: boolean = true;
m: string = '';
btnSubmit, btnClear: boolean;
begin
// buat penyimpan data dgn kapasitas 1 kb
memCreate(webData,1024);
// tampilkan antarmuka masukan
writeln(Po,formOpen(ExtractFilename(ParamStr(0)),true));
write(putLabel('String: ')); webReadln(s);
write(putLabel('Number: ')); webReadln(i);
write(putLabel('Boolean: ','')); webReadln(b,'Yes, I agree');
write(putLabel('Memo: ')); webReadMemo(m);
writeln(separator);
write(putSpace); webReadButton(btnSubmit,' SUBMIT ');
write(spacer); webReadButton(btnClear ,' CLEAR ');
writeln(FORMc,Pc,HR);
// tampilkan masukan, jika ada
if webHasInput then
begin
if btnClear then clearWebData;
if btnSubmit then saveWebData;
if hasWebData then d := readWebData;
// tampilkan data tersedia
writeln(Po,span('Available data:','bold'),BR);
writeln('Input: ' ,switch(webInputs ='','[empty]',webInputs),BR);
writeln('Query: ' ,switch(webQueries='','[empty]',webQueries),BR);
writeln('Saved: ' ,switch(d='','[empty]',d),BR);
writeln(Pc,HR);
end
// tampilkan data tersimpan
else begin
if hasWebData then showWebData;
end;
log.debug('#'+{$I %LINE%}+': [main.writeContent] done.');
end;
(*** program utama ***)
begin
writeHeader('Read Input','test.css');
writeTitle('SAVE INPUT',3);
allWebInput := readAllWebInput;
writeContent;
writeFooter;
end.