java-imp-example.mkiv /size: 12 Kb    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=java-imp-example, % was: java-exa
3
%D version=2002.??.??,
4
%D title=\CONTEXT\ JavaScript Macros,
5
%D subtitle=Example Support,
6
%D author=Hans Hagen,
7
%D date=\currentdate,
8
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
%C
10
%C This module is part of the \CONTEXT\ macro||package and is
11
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12
%C details.
13 14
%D This code is just kept as an example of dealign with forms and submitting data to
15
%D a server. We used this to create stepwise growing student test forms and such.
16 17
% XFDF versus HTML
18
% localhost versus remote versus set
19 20
% filename | filename-nr => name
21
% fakename | fakename-nr => file upload, unless localhost
22
%
23
% we erase the list because otherwise we end up in browser remembering
24
% problems; also, it is not possible to set upload fields 'manually'
25 26
\startJSpreamble
request
_
1
used
now
27 28
var
example
_
method
=
"
HTML
"
;
29
var
example
_
host
=
""
;
30
var
example
_
port
=
""
;
31
var
example
_
url
=
""
;
32
var
example
_
file
=
""
;
33 34
var
example
_
log
=
true
;
% false
35 36
function
stripped
_
exa
_
value
(
name
)
{
37
f
=
this
.
getField
(
name
)
;
38
if
(
f
)
{
39
str
=
f
.
value
;
40
if
(
str
.
indexOf
(
name
+'-'
)==
0
)
{
41
str
=
str
.
substr
(
name
.
length
+
1
)
;
42
}
43
return
str
;
44
}
else
{
45
return
""
;
46
}
47
}
48 49
function
identify
_
example
_
url
(
)
{
50
if
(
example
_
log
)
{
51
console
.
clear
;
52
console
.
println
("
base
url
:
"
+
this
.
baseURL
)
;
53
console
.
println
("
this
url
:
"
+
this
.
URL
)
;
54
}
55
if
(
this
.
baseURL
!
=
"")
{
56
example
_
url
=
this
.
baseURL
;
57
}
else
{
58
if
(
this
.
URL
!
=
"")
{
59
example
_
url
=
this
.
URL
60
}
61
}
62
if
(
example
_
url
.
indexOf
("
file
:
//
")>=
0
)
{
63
example
_
url
=
""
;
64
}
65
str
=
stripped
_
exa
_
value
("
examplehost
")
;
66
if
(
str
!
=
"
auto
")
{
67
example
_
host
=
str
;
68
if
(
str
==
"
localhost
")
{
69
example
_
port
=
"
8
0
6
1
"
70
}
71
}
72
str
=
stripped
_
exa
_
value
("
exampleport
")
;
73
if
(
str
!
=
"
auto
")
{
74
example
_
port
=
str
;
75
}
76
if
(
example
_
log
)
{
77
console
.
println
("
example
_
url
:
"
+
example
_
url
)
;
78
console
.
println
("
example
_
host
:
"
+
example
_
host
)
;
79
console
.
println
("
example
_
port
:
"
+
example
_
port
)
;
80
}
81
}
82 83
function
check
_
example
_
url
(
url
)
{
84
if
(
url
.
indexOf
("
file
:
//
")>=
0
)
{
85
url
=
"
http
:
//
localhost
:
8
0
6
1
"
;
86
console
.
println
("
file
url
replaced
by
:
"
+
url
)
;
87
}
else
{
88
if
(
url
.
indexOf
("
:
//
")<
0
)
{
89
url
=
"
http
:
//
"
+
url
;
90
}
91
if
(
example
_
log
)
{
92
console
.
println
("
url
before
check
:
"
+
url
)
;
93
}
94
% url = url.replace(/(http:\\\/\\\/.+)(\\\/.*$)/, "$1") ;
95
url
=
url
.
replace
(
/
(
https
*
:
\\
\/
\\
\/
.
+
?
)(
\\
\/
.
*
$
)
/
,
"
$
1
/
exarequest
")
;
96
if
(
example
_
log
)
{
97
console
.
println
("
url
after
check
:
"
+
url
)
;
98
}
99
}
100
return
url
;
101
}
102 103
function
set
_
example
_
xfdf
(
method
)
{
104
example
_
method
=
method
;
105
}
106 107
function
set
_
example
_
file
(
file
)
{
108
example
_
method
=
file
;
109
}
110 111
function
set
_
example
_
host
(
host
)
{
112
example
_
host
=
host
;
113
}
114 115
function
set
_
example
_
port
(
port
)
{
116
example
_
host
=
host
;
117
}
118 119
function
do
_
erase
_
example
_
file
(
tag
)
{
120
f
=
this
.
getField
(
tag
)
;
121
if
(
f
)
{
122
f
.
value
=
""
;
123
}
124
}
125 126
function
do
_
erase
_
example
_
list
(
tag
)
{
127
for
(
i
=
1
;
i
<=
1
0
0
;
i
++
)
{
128
f
=
this
.
getField
(
tag
+
"
-
"
+
i
)
;
129
if
(
f
)
{
130
f
.
value
=
""
;
131
}
else
{
132
return
133
}
134
}
135
}
136 137
function
do
_
submit
_
example
_
url
(
url
)
{
138
if
((
example
_
method
==
"
XFDF
"
)
||
(
url
.
indexOf
("
localhost
")>=
0
))
{
139
do
_
erase
_
example
_
file
("
fakename
")
;
140
do
_
erase
_
example
_
list
("
fakename
")
;
141
}
142
url
=
check
_
example
_
url
(
url
)
;
143
if
(
example
_
log
)
{
144
console
.
println
("
submitting
form
to
"
+
url
+
"
using
method
"
+
example
_
method
)
;
145
}
146
if
(
example
_
file
!
=
"")
{
147
url
=
url
+
"
/
"
+
example
_
file
;
148
}
149
% we need the bFDF for acrobat 5
150
this
.
submitForm
(
{
cURL
:
url
,
bFDF
:
false
,
cSubmitAs
:
example
_
method
}
)
;
151
}
152 153
function
submit
_
example
_
form
(
)
{
154
identify
_
example
_
url
()
;
155
if
(
example
_
host
!
=
"")
{
156
if
(
example
_
port
!
=
"")
{
157
do
_
submit
_
example
_
url
(
example
_
host
+
"
:
"
+
example
_
port
)
;
158
}
else
{
159
do
_
submit
_
example
_
url
(
example
_
host
)
;
160
}
161
}
else
{
162
if
(
example
_
url
!
=
"")
{
163
do
_
submit
_
example
_
url
(
example
_
url
)
;
164
}
else
{
165
if
(
example
_
port
!
=
"")
{
166
do
_
submit
_
example
_
url
("
localhost
"
+
"
:
"
+
example
_
port
)
;
167
}
else
{
168
do
_
submit
_
example
_
url
("
localhost
:
8
0
6
1
")
;
% local exampler
169
}
170
}
171
}
172
resetfilename
()
;
173
}
174 175
function
submit
_
form
(
host
,
port
)
{
176
set
_
example
_
host
(
host
)
;
177
set
_
example
_
post
(
post
)
;
178
submit
_
example
_
form
;
179
}
180 181
\stopJSpreamble
182 183
\startJSpreamble
request
_
2
used
now
184 185
var
exa
_
command
=
""
;
186
var
exa
_
option
=
""
;
187
var
exa
_
filename
=
""
;
188
var
exa
_
filelist
=
""
;
189
var
exa
_
registered
=
""
;
190 191
function
set
_
request
(
command
,
option
)
{
192
exa
_
command
=
command
;
193
if
(
exa
_
command
==
"")
{
194
exa
_
option
=
option
;
195
}
else
{
if
(
option
==
"")
{
196
exa
_
option
=
""
;
197
}
else
{
if
(
option
.
indexOf
("
--
")<
0
)
{
198
exa
_
option
=
"
--
action
="
+
option
;
199
}
else
{
200
exa
_
option
=
option
;
201
}
}
}
202
}
203 204
function
assemble
_
request
(
)
{
205
v
=
this
.
getField
(
'
filename
'
)
;
206
if
(
v
)
{
207
exa
_
filename
=
v
.
value
;
208
}
209
v
=
this
.
getField
(
'
filelist
'
)
;
210
if
(
v
)
{
211
exa
_
filelist
=
v
.
value
;
212
}
213
% exa_filename = exa_filename.replace(/\\\\/g,'/') ;
214
% exa_filelist = exa_filelist.replace(/\\\\/g,'/') ;
215
str
=
"<
?
xml
version
=
'
1
.
0
'
?
>
\\
n
"
;
216
str
=
str
+
"<
exa
:
request
xmlns
:
exa
=
'
http
:
//
www
.
pragma
-
ade
.
com
/
schemas
/
example
.
rng
'
><
exa
:
application
>"
;
217
if
(
exa
_
filelist
==
"")
{
218
exa
_
filelist
=
exa
_
registered
;
219
}
else
{
if
(
exa
_
registered
!
=
"")
{
220
exa
_
filelist
=
exa
_
filelist
+
"
\\
n
"
+
exa
_
registered
;
221
}
}
222
if
(
exa
_
command
!
=
"")
{
223
str
=
str
+
"<
exa
:
command
>"
+
exa
_
command
+
"<
/
exa
:
command
>"
;
224
}
225
if
(
exa
_
option
!
=
"")
{
226
str
=
str
+
"<
exa
:
option
>"
+
exa
_
option
+
"<
/
exa
:
option
>"
;
227
}
228
if
(
exa
_
filename
!
=
"")
{
229
str
=
str
+
"<
exa
:
filename
>"
+
exa
_
filename
+
"<
/
exa
:
filename
>"
;
230
}
231
var
fls
=
""
;
232
if
(
exa
_
filelist
!
=
"")
{
233
% old method, soon obsolete
234
lst
=
exa
_
filelist
.
split
(
/
\\
s
/
)
;
235
for
(
i
=
0
;
i
<
lst
.
length
;
i
++
)
{
236
fls
=
fls
+
"<
exa
:
entry
>"
+
lst
[
i
]
+
"<
/
exa
:
entry
>"
;
237
}
238
}
else
{
if
(
exa
_
multiple
)
{
239
for
(
i
=
1
;
i
<=
1
0
0
;
i
++
)
{
240
% console.println("file field "+i) ;
241
f
=
this
.
getField
("
filename
-
"
+
i
)
;
242
% if (f) { if (f.value != "") {
243
% if (g) { if (g.value == "") {
244
% fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
245
% } else {
246
% fls = fls+"<exa:entry label='" + g.value + "'>"+f.value+"</exa:entry>" ;
247
% } } else {
248
% fls = fls+"<exa:entry>"+f.value+"</exa:entry>" ;
249
% }
250
% } }
251
if
(
f
)
{
if
(
f
.
value
!
=
"")
{
252
fls
=
fls
+
"<
exa
:
entry
"
;
253
g
=
this
.
getField
("
dataname
-
"
+
i
)
;
254
if
(
g
)
{
if
(
g
.
value
==
"")
{
255
% console.println("a") ;
256
fls
=
fls
+
">"
;
257
}
else
{
258
% console.println("b") ;
259
fls
=
fls
+
"
label
=
'
"
+
g
.
value
+
"
'
>"
;
260
}
}
else
{
261
% console.println("c") ;
262
fls
=
fls
+
">"
;
263
}
264
fls
=
fls
+
f
.
value
+
"<
/
exa
:
entry
>"
;
265
}
}
266
}
267
}
}
268
if
(
fls
!
=
"")
{
269
str
=
str
+
"<
exa
:
filelist
>"
+
fls
+
"<
/
exa
:
filelist
>"
;
270
}
271
str
=
str
+
"<
/
exa
:
application
><
/
exa
:
request
>"
;
272
v
=
this
.
getField
(
'
exa
:
request
'
)
;
273
if
(
v
)
{
274
v
.
value
=
str
;
275
}
276
}
277 278
\stopJSpreamble
279 280
\startJSpreamble
request
_
3
used
now
281 282
var
exa
_
multiple
=
false
;
283 284
function
setfilename
(
suffixes
)
{
285
if
(
event
.
targetName
)
{
286
var
name
=
event
.
targetName
;
287
var
fake
=
name
.
replace
(
/
filename
/
,
"
fakename
")
;
288
}
else
{
289
var
name
=
'
filename
'
;
290
var
fake
=
'
fakename
'
;
291
}
292
f
=
this
.
getField
(
fake
)
;
293
if
(
f
)
{
294
f
.
browseForFileToSubmit
()
;
295
if
((
suffixes
!
=
"")
&&
(
f
.
value
!
=
""))
{
296
%
297
s
=
suffixes
.
replace
(
/
,
/
g
,
"
|
")
;
298
r
=
new
RegExp
()
;
299
s
=
"
\\
\\
.
("
+
s
+
")
$
"
;
300
r
.
compile
(
s
,
"
i
")
;
301
if
(
f
.
value
.
search
(
r
)<=
0
)
{
302
f
.
value
=
""
;
303
app
.
alert
(
'
This
filetype
is
not
permitted
.
'
)
;
304
%
305
% lst = suffixes.split(/,/) ;
306
% ok = false ;
307
% for (i=0;i<lst.length;i++) {
308
% if (f.value == lst[i]) {
309
% ok = true ;
310
% }
311
% }
312
% if (! ok) {
313
% f.value = "" ;
314
% app.alert('This filetype is not permitted.') ;
315
%
316
}
317
}
318
g
=
this
.
getField
(
name
)
;
319
if
(
g
)
{
320
g
.
value
=
f
.
value
;
321
}
322
% we need to set the value of the selector to empty else
323
% we get some funny preloading of multimeg files in spite
324
% of the specs that say that xml fields will not get sent
325
% if (example_method == "XFDF" ) {
326
% f.value = "" ;
327
% } else {
328
% identify_example_url ;
329
% if (example_url.indexOf("localhost")>=0) {
330
% f.value = "" ;
331
% }
332
% }
333
}
334
this
.
dirty
=
false
;
335
}
336 337
function
addfilename
()
{
338
if
(
exa
_
multiple
)
{
339
h
=
this
.
getField
("
filelist
")
;
340
g
=
this
.
getField
("
filename
")
;
341
if
((
g
)
&&
(
h
))
{
342
str
=
g
.
value
;
343
if
(
h
.
value
==
"")
{
344
h
.
value
=
str
;
345
}
else
{
346
h
.
value
=
h
.
value
+
"
\\
n
"
+
str
;
347
}
348
g
.
value
=
""
;
349
this
.
value
=
""
;
350
}
351
}
352
this
.
dirty
=
false
;
353
}
354 355
% this only works with client that assembles request
356 357
function
registerfilename
(
str
)
{
358
if
(
str
!
="")
{
359
h
=
this
.
getField
("
filelist
")
;
360
if
(
h
)
{
361
if
(
h
.
value
!
=
"")
{
362
h
.
value
=
h
.
value
+
"
\\
n
"
;
363
}
364
h
.
value
=
h
.
value
+
str
;
365
}
else
{
366
if
(
exa
_
registered
!
=
"")
{
367
exa
_
registered
=
exa
_
registered
+
"
\\
n
"
;
368
}
369
exa
_
registered
=
exa
_
registered
+
str
;
370
}
371
}
372
console
.
show
;
373
console
.
println
(
'
registered
files
'
)
;
374
console
.
println
("
file
:
"
+
str
)
;
375
console
.
println
("
list
:
"
+
exa
_
registered
)
;
376
this
.
dirty
=
false
;
377
}
378 379
function
checkfilename
()
{
380
}
381 382
function
getfilename
(
suffixes
)
{
383
setfilename
(
suffixes
)
;
384
checkfilename
()
;
385
addfilename
()
;
386
}
387 388
function
resetfilename
()
{
389
do
_
erase
_
example
_
file
("
fakename
")
;
390
do
_
erase
_
example
_
file
("
filename
")
;
391
do
_
erase
_
example
_
file
("
filelist
")
;
392
do
_
erase
_
example
_
list
("
filename
")
;
393
do
_
erase
_
example
_
list
("
fakename
")
;
394
}
395 396
\stopJSpreamble
397 398
\endinput
399