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 = "8061"
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:8061" ;
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<=100;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:8061") ; % 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<=100;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