blob: 89534bee75e0e0b7963fac3408600e55eb84f476 [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';
Akron0b489ad2018-02-02 16:49:32 +010021 /*
22 url += '/' + match.corpusID;
23 url += '/' + match.docID;
24 url += '/' + match.textID;
25 */
Akron19d97fe2016-09-06 20:47:05 +020026
Akron311ca652017-06-26 20:39:56 +020027 var legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$');
Akron19d97fe2016-09-06 20:47:05 +020028
29 // This is for legacy support
30 var legacy = legacySigle.exec(match.textSigle);
Akron7f613e02016-11-07 02:50:44 +010031 if (legacy !== null && legacy[0]) {
Akron19d97fe2016-09-06 20:47:05 +020032 url += '/' + legacy[1] + '/' + legacy[2] + '/' + legacy[3];
33 }
34 else {
35 url += '/' + match.textSigle;
36 }
37
Nils Diewald4347ee92015-05-04 20:32:48 +000038 url += '/' + match.matchID;
Nils Diewald58141332015-04-07 16:18:45 +000039
Nils Diewald4347ee92015-05-04 20:32:48 +000040 // { spans: true, layer:x, foundry : y}
41 if (param['spans'] == true) {
42 url += '?spans=true';
43 if (param['foundry'] !== undefined)
Akron515851a2017-05-02 12:53:17 +020044 url += '&foundry=' + param['foundry'];
Nils Diewald4347ee92015-05-04 20:32:48 +000045 if (param['layer'] !== undefined)
Akron515851a2017-05-02 12:53:17 +020046 url += '&layer=' + param['layer'];
Nils Diewald58141332015-04-07 16:18:45 +000047 }
Nils Diewald4347ee92015-05-04 20:32:48 +000048
49 // { spans : false, layer: [Array of KorAP.InfoLayer] }
50 else {
51 // TODO
52 url += '?spans=false';
53 }
54
55 KorAP.API.getJSON(url, cb);
Nils Diewald58141332015-04-07 16:18:45 +000056 };
Nils Diewald4347ee92015-05-04 20:32:48 +000057
Akron0b489ad2018-02-02 16:49:32 +010058
Akron48b1e4d2015-06-17 18:47:01 +020059 /**
60 * Retrieve information about collections
61 */
62 KorAP.API.getCollections = function (cb) {
63 KorAP.API.getJSON(KorAP.URL + '/collection', cb);
64 };
65
Akron0b489ad2018-02-02 16:49:32 +010066
Akron48b1e4d2015-06-17 18:47:01 +020067 /**
68 * General method to retrieve JSON information
69 */
Nils Diewald4347ee92015-05-04 20:32:48 +000070 KorAP.API.getJSON = function (url, onload) {
71 var req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +000072 req.open("GET", url, true);
73 req.setRequestHeader("Accept", "application/json");
74 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
75 req.onreadystatechange = function () {
76 /*
Akron0b489ad2018-02-02 16:49:32 +010077 States:
78 0 - unsent (prior to open)
79 1 - opened (prior to send)
80 2 - headers received
81 3 - loading (responseText has partial data)
82 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +000083 */
84 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +020085
86 var json = JSON.parse(this.responseText);
87 if (json !== null && json["errors"] !== null) {
88 for (var i in json["errors"]) {
89 KorAP.log(json["errors"][i][0], json["errors"][i][1] || "Unknown");
90 };
91 }
92 else if (this.status !== 200) {
93 KorAP.log(this.status, this.statusText);
94 };
95
96 if (this.status === 200) {
97 onload(json);
98 }
99 else {
100 onload(undefined);
101 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000102 }
103 };
104 req.ontimeout = function () {
105 KorAP.log(0, 'Request Timeout');
106 };
107 req.send();
108 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109});