blob: 00821decab933f50e953389183a1d94fc8c14ac1 [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 Diewald4347ee92015-05-04 20:32:48 +000011 KorAP.API = KorAP.API || {};
Nils Diewald58141332015-04-07 16:18:45 +000012
Nils Diewald4347ee92015-05-04 20:32:48 +000013 KorAP.API.getMatchInfo = function (match, param, cb) {
Nils Diewald58141332015-04-07 16:18:45 +000014
Nils Diewald4347ee92015-05-04 20:32:48 +000015 // match is a KorAP.Match object
16 var url = KorAP.URL;
17 url += '/corpus';
18 url += '/' + match.corpusID;
19 url += '/' + match.docID;
20 url += '/' + match.textID;
21 url += '/' + match.matchID;
Nils Diewald58141332015-04-07 16:18:45 +000022
Nils Diewald4347ee92015-05-04 20:32:48 +000023 // { spans: true, layer:x, foundry : y}
24 if (param['spans'] == true) {
25 url += '?spans=true';
26 if (param['foundry'] !== undefined)
27 url += '&foundry=' + param['foundry'];
28 if (param['layer'] !== undefined)
29 url += '&layer=' + param['layer'];
Nils Diewald58141332015-04-07 16:18:45 +000030 }
Nils Diewald4347ee92015-05-04 20:32:48 +000031
32 // { spans : false, layer: [Array of KorAP.InfoLayer] }
33 else {
34 // TODO
35 url += '?spans=false';
36 }
37
38 KorAP.API.getJSON(url, cb);
Nils Diewald58141332015-04-07 16:18:45 +000039 };
Nils Diewald4347ee92015-05-04 20:32:48 +000040
41 KorAP.API.getJSON = function (url, onload) {
42 var req = new XMLHttpRequest();
43
44 req.open("GET", url, true);
45 req.setRequestHeader("Accept", "application/json");
46 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
47 req.onreadystatechange = function () {
48 /*
49 States:
50 0 - unsent (prior to open)
51 1 - opened (prior to send)
52 2 - headers received
53 3 - loading (responseText has partial data)
54 4 - done
55 */
56 if (this.readyState == 4) {
57 if (this.status === 200)
58 onload(JSON.parse(this.responseText));
59 else
60 KorAP.log(this.status, this.statusText);
61 }
62 };
63 req.ontimeout = function () {
64 KorAP.log(0, 'Request Timeout');
65 };
66 req.send();
67 }
Nils Diewald0e6992a2015-04-14 20:13:52 +000068});