blob: f9058bc88199fed6b83a3a085363fd5674f9a88f [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001define(['util'], function () {
Nils Diewald58141332015-04-07 16:18:45 +00002 // TODO: https://github.com/honza/140medley/blob/master/140medley.js
3 // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
4 // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
5 // r.addEventListener("progress", updateProgress, false);
6 // http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
7 // http://stackoverflow.com/questions/6112744/load-javascript-on-demand
8
Nils Diewald0e6992a2015-04-14 20:13:52 +00009 KorAP.URL = KorAP.URL || 'http://korap.ids-mannheim.de/kalamar';
10
Nils Diewald58141332015-04-07 16:18:45 +000011 KorAP.API = {
12 getMatchInfo : function (match, param, cb) {
13 // match is a KorAP.Match object
14
15 var url = KorAP.URL;
16 url += '/corpus';
17 url += '/' + match.corpusID;
18 url += '/' + match.docID + '.' + match.textID; // TODO
19 url += '/' + match.matchID;
20
21 // { spans: true, layer:x, foundry : y}
22 if (param['spans'] == true) {
23 url += '?spans=true';
24 if (param['foundry'] !== undefined)
25 url += '&foundry=' + param['foundry'];
26 if (param['layer'] !== undefined)
27 url += '&layer=' + param['layer'];
28 }
29
30 // { spans : false, layer: [Array of KorAP.InfoLayer] }
31 else {
32 // TODO
33 url += '?spans=false';
34 }
35
36 this.getJSON(url, cb);
37 },
Nils Diewald0e6992a2015-04-14 20:13:52 +000038
Nils Diewald58141332015-04-07 16:18:45 +000039 getJSON : function (url, onload) {
40 var req = new XMLHttpRequest();
41
Nils Diewald58141332015-04-07 16:18:45 +000042 req.open("GET", url, true);
Nils Diewald58141332015-04-07 16:18:45 +000043 req.setRequestHeader("Accept", "application/json");
44 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
45 req.onreadystatechange = function () {
46 /*
47 States:
48 0 - unsent (prior to open)
49 1 - opened (prior to send)
50 2 - headers received
51 3 - loading (responseText has partial data)
52 4 - done
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 */
Nils Diewald58141332015-04-07 16:18:45 +000054 if (this.readyState == 4) {
55 if (this.status === 200)
56 onload(JSON.parse(this.responseText));
57 else
58 KorAP.log(this.status, this.statusText);
59 }
60 };
61 req.ontimeout = function () {
62 KorAP.log(0, 'Request Timeout');
63 };
64 req.send();
65 }
66 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000067});