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
|
"use strict";
/**
Documented in style.c:style_emit_script_fetch(). Requires that
window.fossil have already been set up.
*/
window.fossil.fetch = function f(uri,opt){
if(!f.onerror){
f.onerror = function(e){
console.error("Ajax error:",e);
fossil.error("Ajax error!");
if(e.originalTarget && e.originalTarget.responseType==='text'){
const txt = e.originalTarget.responseText;
try{
/* The convention from the /filepage_xyz routes is to
** return error responses in JSON form if possible.
*/
const j = JSON.parse(txt);
console.error("Error JSON:",j);
if(j.error){ fossil.error(j.error) };
}catch(e){/*ignore: not JSON*/}
}
};
}
if('/'===uri[0]) uri = uri.substr(1);
if(!opt){
opt = {};
}else if('function'===typeof opt){
opt={onload:opt};
}
if(!opt.onload) opt.onload = (r)=>console.debug('ajax response:',r);
if(!opt.onerror) opt.onerror = f.onerror;
let payload = opt.payload, jsonResponse = false;
if(payload){
opt.method = 'POST';
if(!(payload instanceof FormData)
&& !(payload instanceof Document)
&& !(payload instanceof Blob)
&& !(payload instanceof File)
&& !(payload instanceof ArrayBuffer)){
if('object'===typeof payload || payload instanceof Array){
payload = JSON.stringify(payload);
opt.contentType = 'application/json';
}
}
}
const url=[window.fossil.rootPath+uri], x=new XMLHttpRequest();
if(opt.urlParams){
url.push('?');
if('string'===typeof opt.urlParams){
url.push(opt.urlParams);
}else{/*assume object*/
let k, i = 0;
for( k in opt.urlParams ){
if(i++) url.push('&');
url.push(k,'=',encodeURIComponent(opt.urlParams[k]));
}
}
}
if('POST'===opt.method && 'string'===typeof opt.contentType){
x.setRequestHeader('Content-Type',opt.contentType);
}
x.open(opt.method||'GET', url.join(''), true);
if('json'===opt.responseType){
jsonResponse = true;
x.responseType = 'text';
}else{
x.responseType = opt.responseType||'text';
}
if(opt.onload){
x.onload = function(e){
|
|
>
|
>
|
>
|
|
>
|
>
|
>
>
|
<
|
<
<
|
>
|
<
<
<
<
<
<
<
<
<
<
<
<
>
>
>
|
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
|
"use strict";
/**
Documented in style.c:style_emit_script_fetch(). Requires that
window.fossil has already been set up (which happens via the routine
which emits this code C-side).
*/
window.fossil.fetch = function f(uri,opt){
if(!f.onerror){
f.onerror = function(e/*event or exception*/){
console.error("Ajax error:",e);
if(e instanceof Error){
fossil.error('Exception:',e);
}
else if(e.originalTarget && e.originalTarget.responseType==='text'){
const txt = e.originalTarget.responseText;
try{
/* The convention from the /filepage_xyz routes is to
** return error responses in JSON form if possible:
** {error: "..."}
*/
const j = JSON.parse(txt);
console.error("Error JSON:",j);
if(j.error){ fossil.error(j.error) };
}catch(e){/* Try harder */
fossil.error(txt)
}
}
};
f.onload = (r)=>console.debug('ajax response:',r);
}
if('/'===uri[0]) uri = uri.substr(1);
if(!opt) opt = {};
else if('function'===typeof opt) opt={onload:opt};
if(!opt.onload) opt.onload = f.onload;
if(!opt.onerror) opt.onerror = f.onerror;
let payload = opt.payload, jsonResponse = false;
if(payload){
opt.method = 'POST';
if(!(payload instanceof FormData)
&& !(payload instanceof Document)
&& !(payload instanceof Blob)
&& !(payload instanceof File)
&& !(payload instanceof ArrayBuffer)){
if('object'===typeof payload || payload instanceof Array){
payload = JSON.stringify(payload);
opt.contentType = 'application/json';
}
}
}
const url=[window.fossil.repoUrl(uri,opt.urlParams)],
x=new XMLHttpRequest();
if('POST'===opt.method && 'string'===typeof opt.contentType){
x.setRequestHeader('Content-Type',opt.contentType);
}
x.open(opt.method||'GET', url.join(''), true);
if('json'===opt.responseType){
/* 'json' is an extension to the supported XHR.responseType
list. We use it as a flag to tell us to JSON.parse()
the response. */
jsonResponse = true;
x.responseType = 'text';
}else{
x.responseType = opt.responseType||'text';
}
if(opt.onload){
x.onload = function(e){
|