blob: 82f10621ca63a5c3c6aa185ef97c51951b9799da [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
Nils Diewald58141332015-04-07 16:18:45 +000014 var url = KorAP.URL;
15 url += '/corpus';
16 url += '/' + match.corpusID;
17 url += '/' + match.docID + '.' + match.textID; // TODO
18 url += '/' + match.matchID;
19
20 // { spans: true, layer:x, foundry : y}
21 if (param['spans'] == true) {
22 url += '?spans=true';
23 if (param['foundry'] !== undefined)
24 url += '&foundry=' + param['foundry'];
25 if (param['layer'] !== undefined)
26 url += '&layer=' + param['layer'];
27 }
28
29 // { spans : false, layer: [Array of KorAP.InfoLayer] }
30 else {
31 // TODO
32 url += '?spans=false';
33 }
34
35 this.getJSON(url, cb);
36 },
Nils Diewald0e6992a2015-04-14 20:13:52 +000037
Nils Diewald58141332015-04-07 16:18:45 +000038 getJSON : function (url, onload) {
39 var req = new XMLHttpRequest();
40
Nils Diewald58141332015-04-07 16:18:45 +000041 req.open("GET", url, true);
Nils Diewald58141332015-04-07 16:18:45 +000042 req.setRequestHeader("Accept", "application/json");
43 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
44 req.onreadystatechange = function () {
45 /*
46 States:
47 0 - unsent (prior to open)
48 1 - opened (prior to send)
49 2 - headers received
50 3 - loading (responseText has partial data)
51 4 - done
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 */
Nils Diewald58141332015-04-07 16:18:45 +000053 if (this.readyState == 4) {
54 if (this.status === 200)
55 onload(JSON.parse(this.responseText));
56 else
57 KorAP.log(this.status, this.statusText);
58 }
59 };
60 req.ontimeout = function () {
61 KorAP.log(0, 'Request Timeout');
62 };
63 req.send();
64 }
65 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000066});