blob: e536ea2eae24b49b0564906bb7fb3784653ca0b9 [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
Akron4c33c622018-11-12 13:43:27 +01009 KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : '';
Nils Diewald0e6992a2015-04-14 20:13:52 +000010
Nils Diewald4347ee92015-05-04 20:32:48 +000011 KorAP.API = KorAP.API || {};
Akronb89863a2018-11-13 16:43:59 +010012
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);
Akronb89863a2018-11-13 16:43:59 +010031 var docFragment = "";
Akron7f613e02016-11-07 02:50:44 +010032 if (legacy !== null && legacy[0]) {
Akronb89863a2018-11-13 16:43:59 +010033 docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3];
Akron19d97fe2016-09-06 20:47:05 +020034 }
35 else {
Akronb89863a2018-11-13 16:43:59 +010036 docFragment = match.textSigle;
Akron19d97fe2016-09-06 20:47:05 +020037 }
Akronb89863a2018-11-13 16:43:59 +010038
39 docFragment += '/' + match.matchID;
40 url += '/' + docFragment;
Nils Diewald58141332015-04-07 16:18:45 +000041
Nils Diewald4347ee92015-05-04 20:32:48 +000042 // { spans: true, layer:x, foundry : y}
43 if (param['spans'] == true) {
44 url += '?spans=true';
Akronb89863a2018-11-13 16:43:59 +010045 docFragment += ' +spans ';
46 if (param['foundry'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020047 url += '&foundry=' + param['foundry'];
Akronb89863a2018-11-13 16:43:59 +010048 docFragment += param['foundry'];
49 };
50 if (param['layer'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020051 url += '&layer=' + param['layer'];
Akronb89863a2018-11-13 16:43:59 +010052 docFragment += '/'+param['layer'];
53 }
Nils Diewald58141332015-04-07 16:18:45 +000054 }
Nils Diewald4347ee92015-05-04 20:32:48 +000055
56 // { spans : false, layer: [Array of KorAP.InfoLayer] }
57 else {
58 // TODO
Akronb89863a2018-11-13 16:43:59 +010059 docFragment += ' -spans';
Nils Diewald4347ee92015-05-04 20:32:48 +000060 url += '?spans=false';
61 }
62
Akronb89863a2018-11-13 16:43:59 +010063 KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment);
Nils Diewald58141332015-04-07 16:18:45 +000064 };
Nils Diewald4347ee92015-05-04 20:32:48 +000065
Akron0b489ad2018-02-02 16:49:32 +010066
Akron48b1e4d2015-06-17 18:47:01 +020067 /**
Akron0ad7cd22018-02-08 18:03:06 +010068 * Retrieve information about a document.
69 */
70 KorAP.API.getTextInfo = function (doc, param, cb) {
71
72 // doc is a KorAP.Match object
73 var url = KorAP.URL;
Akronb5d05d72018-02-12 15:09:12 +010074 url += '/corpus';
75 url += '/' + doc.textSigle;
Akron0ad7cd22018-02-08 18:03:06 +010076
77 if (param['fields'] !== undefined) {
78 url += '?fields='; // TODO!
79 }
80 else {
Akron4bbd8b32018-03-06 19:19:44 +010081 url += '?fields=@all'; // TODO: Maybe '*'?
Akron0ad7cd22018-02-08 18:03:06 +010082 }
Akronb89863a2018-11-13 16:43:59 +010083 KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle);
Akron0ad7cd22018-02-08 18:03:06 +010084 };
85
86
87 /**
Akron48b1e4d2015-06-17 18:47:01 +020088 * Retrieve information about collections
89 */
90 KorAP.API.getCollections = function (cb) {
Akronb89863a2018-11-13 16:43:59 +010091 KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo");
Akron48b1e4d2015-06-17 18:47:01 +020092 };
93
Akron0b489ad2018-02-02 16:49:32 +010094
Akron48b1e4d2015-06-17 18:47:01 +020095 /**
hebasta4ba496a2018-06-05 15:56:01 +020096 * Retrieve information about corpus statistic
97 *
98 * Development mode:
99 * URL = http://localhost:8089/api
hebasta0ee50802018-06-20 10:24:45 +0200100 *
101 * collectionQuery has be changed to corpusQuery
hebasta4ba496a2018-06-05 15:56:01 +0200102 */
hebasta0ee50802018-06-20 10:24:45 +0200103 /* KorAP.API.getCorpStat = function (collQu, cb){
hebasta4ba496a2018-06-05 15:56:01 +0200104 //development mode:
105 var url = "http://localhost:8089/api/";
106 url = url + "statistics";
hebasta0ee50802018-06-20 10:24:45 +0200107 url = url + "?corpusQuery=";
hebasta4ba496a2018-06-05 15:56:01 +0200108 url = url + collQu;
109 KorAP.API.getJSON(url, cb);
110 };*/
hebasta0ee50802018-06-20 10:24:45 +0200111
112
113 /**
114 * Retrieve information about corpus statistic
115 *
116 * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
117 *
118 * cq = corpus query (formerly collectionQuery)
119 *
120 * Adress the MOJO-Endpoint for example with
121 * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
122 */
123 KorAP.API.getCorpStat = function (cq, cb){
Akron8f0de5e2018-08-27 16:31:12 +0200124 var url = KorAP.URL;
Akronb89863a2018-11-13 16:43:59 +0100125 url += "/corpus?cq=" + encodeURIComponent(cq);
126 KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq);
hebasta0ee50802018-06-20 10:24:45 +0200127 };
hebasta4ba496a2018-06-05 15:56:01 +0200128
129 /**
Akron48b1e4d2015-06-17 18:47:01 +0200130 * General method to retrieve JSON information
131 */
Akronb89863a2018-11-13 16:43:59 +0100132 KorAP.API.getJSON = function (url, onload, title) {
Nils Diewald4347ee92015-05-04 20:32:48 +0000133 var req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +0000134 req.open("GET", url, true);
Akron4c33c622018-11-12 13:43:27 +0100135
136 // Dispatch global "window" event
137 var reqE = new CustomEvent('korapRequest', {
138 bubbles : false,
Akronb89863a2018-11-13 16:43:59 +0100139 detail: {
140 "url" : url,
141 "title" : title
142 }
Akron4c33c622018-11-12 13:43:27 +0100143 });
144 window.dispatchEvent(reqE);
145
Nils Diewald4347ee92015-05-04 20:32:48 +0000146 req.setRequestHeader("Accept", "application/json");
147 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
148 req.onreadystatechange = function () {
149 /*
Akron0b489ad2018-02-02 16:49:32 +0100150 States:
151 0 - unsent (prior to open)
152 1 - opened (prior to send)
153 2 - headers received
154 3 - loading (responseText has partial data)
155 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +0000156 */
157 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +0200158
Akronb5d05d72018-02-12 15:09:12 +0100159 var json;
160 try {
161 json = JSON.parse(this.responseText);
162 }
163 catch (e) {
164 KorAP.log(0, e);
165 console.log(e);
166 onload(undefined);
167 return;
168 };
Akron515851a2017-05-02 12:53:17 +0200169 if (json !== null && json["errors"] !== null) {
170 for (var i in json["errors"]) {
171 KorAP.log(json["errors"][i][0], json["errors"][i][1] || "Unknown");
172 };
173 }
174 else if (this.status !== 200) {
175 KorAP.log(this.status, this.statusText);
176 };
177
178 if (this.status === 200) {
179 onload(json);
180 }
181 else {
182 onload(undefined);
183 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000184 }
185 };
186 req.ontimeout = function () {
187 KorAP.log(0, 'Request Timeout');
188 };
189 req.send();
190 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000191});