blob: eeddc58971cb01eda9ec559e0e927754fbfc7c0f [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 Diewald29f25ee2015-05-08 00:51:47 +00009 KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : 'http://korap.ids-mannheim.de/kalamar';
Nils Diewald0e6992a2015-04-14 20:13:52 +000010
Nils Diewald4347ee92015-05-04 20:32:48 +000011 KorAP.API = KorAP.API || {};
Nils Diewald58141332015-04-07 16:18:45 +000012
Akron48b1e4d2015-06-17 18:47:01 +020013 /**
14 * Retrieve information about a match
15 */
Nils Diewald4347ee92015-05-04 20:32:48 +000016 KorAP.API.getMatchInfo = function (match, param, cb) {
Nils Diewald58141332015-04-07 16:18:45 +000017
Nils Diewald4347ee92015-05-04 20:32:48 +000018 // match is a KorAP.Match object
19 var url = KorAP.URL;
20 url += '/corpus';
21 url += '/' + match.corpusID;
22 url += '/' + match.docID;
23 url += '/' + match.textID;
24 url += '/' + match.matchID;
Nils Diewald58141332015-04-07 16:18:45 +000025
Nils Diewald4347ee92015-05-04 20:32:48 +000026 // { spans: true, layer:x, foundry : y}
27 if (param['spans'] == true) {
28 url += '?spans=true';
29 if (param['foundry'] !== undefined)
30 url += '&foundry=' + param['foundry'];
31 if (param['layer'] !== undefined)
32 url += '&layer=' + param['layer'];
Nils Diewald58141332015-04-07 16:18:45 +000033 }
Nils Diewald4347ee92015-05-04 20:32:48 +000034
35 // { spans : false, layer: [Array of KorAP.InfoLayer] }
36 else {
37 // TODO
38 url += '?spans=false';
39 }
40
41 KorAP.API.getJSON(url, cb);
Nils Diewald58141332015-04-07 16:18:45 +000042 };
Nils Diewald4347ee92015-05-04 20:32:48 +000043
Akron48b1e4d2015-06-17 18:47:01 +020044 /**
45 * Retrieve information about collections
46 */
47 KorAP.API.getCollections = function (cb) {
48 KorAP.API.getJSON(KorAP.URL + '/collection', cb);
49 };
50
51 /**
52 * General method to retrieve JSON information
53 */
Nils Diewald4347ee92015-05-04 20:32:48 +000054 KorAP.API.getJSON = function (url, onload) {
55 var req = new XMLHttpRequest();
56
57 req.open("GET", url, true);
58 req.setRequestHeader("Accept", "application/json");
59 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
60 req.onreadystatechange = function () {
61 /*
62 States:
63 0 - unsent (prior to open)
64 1 - opened (prior to send)
65 2 - headers received
66 3 - loading (responseText has partial data)
67 4 - done
68 */
69 if (this.readyState == 4) {
Akron9f64fd02016-02-13 05:51:38 +010070 if (this.status === 200) {
71 var json = JSON.parse(this.responseText);
72 if (json["errors"] !== null) {
73 for (var i in json["errors"]) {
74 KorAP.log(json["errors"][i][0], json["errors"][i][1]);
75 };
76 };
77 onload(json);
78 }
Nils Diewald4347ee92015-05-04 20:32:48 +000079 else
80 KorAP.log(this.status, this.statusText);
81 }
82 };
83 req.ontimeout = function () {
84 KorAP.log(0, 'Request Timeout');
85 };
86 req.send();
87 }
Nils Diewald0e6992a2015-04-14 20:13:52 +000088});