blob: 8eeedf45612f0fe928fd5b6c7f668132bba2aab0 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001define(['util'], function () {
Akronbf713fc2020-10-13 10:44:35 +02002 "use strict";
3
4 // TODO:
5 // - https://github.com/honza/140medley/blob/master/140medley.js
6 // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
7 // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
8 // - r.addEventListener("progress", updateProgress, false);
9 // - http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
10 // - http://stackoverflow.com/questions/6112744/load-javascript-on-demand
Nils Diewald58141332015-04-07 16:18:45 +000011
Akron4c33c622018-11-12 13:43:27 +010012 KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : '';
Nils Diewald4347ee92015-05-04 20:32:48 +000013 KorAP.API = KorAP.API || {};
Akronb89863a2018-11-13 16:43:59 +010014
Akronbf713fc2020-10-13 10:44:35 +020015 const legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$');
16
Akron48b1e4d2015-06-17 18:47:01 +020017 /**
18 * Retrieve information about a match
19 */
Nils Diewald4347ee92015-05-04 20:32:48 +000020 KorAP.API.getMatchInfo = function (match, param, cb) {
Nils Diewald58141332015-04-07 16:18:45 +000021
Nils Diewald4347ee92015-05-04 20:32:48 +000022 // match is a KorAP.Match object
Akronbf713fc2020-10-13 10:44:35 +020023 let url = KorAP.URL + '/corpus';
Akron0b489ad2018-02-02 16:49:32 +010024 /*
25 url += '/' + match.corpusID;
26 url += '/' + match.docID;
27 url += '/' + match.textID;
28 */
Akron19d97fe2016-09-06 20:47:05 +020029
Akron19d97fe2016-09-06 20:47:05 +020030 // This is for legacy support
Akronbf713fc2020-10-13 10:44:35 +020031 const legacy = legacySigle.exec(match.textSigle);
32 let docFragment = "";
Akron7f613e02016-11-07 02:50:44 +010033 if (legacy !== null && legacy[0]) {
Akronb89863a2018-11-13 16:43:59 +010034 docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3];
Akron19d97fe2016-09-06 20:47:05 +020035 }
36 else {
Akronb89863a2018-11-13 16:43:59 +010037 docFragment = match.textSigle;
Akron19d97fe2016-09-06 20:47:05 +020038 }
Akronb89863a2018-11-13 16:43:59 +010039
40 docFragment += '/' + match.matchID;
41 url += '/' + docFragment;
Nils Diewald58141332015-04-07 16:18:45 +000042
Nils Diewald4347ee92015-05-04 20:32:48 +000043 // { spans: true, layer:x, foundry : y}
44 if (param['spans'] == true) {
45 url += '?spans=true';
Akronb89863a2018-11-13 16:43:59 +010046 docFragment += ' +spans ';
47 if (param['foundry'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020048 url += '&foundry=' + param['foundry'];
Akronb89863a2018-11-13 16:43:59 +010049 docFragment += param['foundry'];
50 };
51 if (param['layer'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020052 url += '&layer=' + param['layer'];
Akronb89863a2018-11-13 16:43:59 +010053 docFragment += '/'+param['layer'];
54 }
Nils Diewald58141332015-04-07 16:18:45 +000055 }
Nils Diewald4347ee92015-05-04 20:32:48 +000056
57 // { spans : false, layer: [Array of KorAP.InfoLayer] }
58 else {
59 // TODO
Akronb89863a2018-11-13 16:43:59 +010060 docFragment += ' -spans';
Nils Diewald4347ee92015-05-04 20:32:48 +000061 url += '?spans=false';
62 }
63
Akronb89863a2018-11-13 16:43:59 +010064 KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment);
Nils Diewald58141332015-04-07 16:18:45 +000065 };
Nils Diewald4347ee92015-05-04 20:32:48 +000066
Akron0b489ad2018-02-02 16:49:32 +010067
Akron48b1e4d2015-06-17 18:47:01 +020068 /**
Akron0ad7cd22018-02-08 18:03:06 +010069 * Retrieve information about a document.
70 */
71 KorAP.API.getTextInfo = function (doc, param, cb) {
72
73 // doc is a KorAP.Match object
Akronbf713fc2020-10-13 10:44:35 +020074 let url = KorAP.URL + '/corpus' + '/' + doc.textSigle;
Akron0ad7cd22018-02-08 18:03:06 +010075
76 if (param['fields'] !== undefined) {
77 url += '?fields='; // TODO!
78 }
79 else {
Akron4bbd8b32018-03-06 19:19:44 +010080 url += '?fields=@all'; // TODO: Maybe '*'?
Akron0ad7cd22018-02-08 18:03:06 +010081 }
Akronbf713fc2020-10-13 10:44:35 +020082
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 /**
Akroncd42a142019-07-12 18:55:37 +020088 * Retrieve information about virtual corpora
Akron48b1e4d2015-06-17 18:47:01 +020089 */
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
hebasta0ee50802018-06-20 10:24:45 +020094
95 /**
96 * Retrieve information about corpus statistic
97 *
98 * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
99 *
100 * cq = corpus query (formerly collectionQuery)
101 *
102 * Adress the MOJO-Endpoint for example with
103 * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
104 */
105 KorAP.API.getCorpStat = function (cq, cb){
Akronbf713fc2020-10-13 10:44:35 +0200106 let url = KorAP.URL + "/corpus?cq=" + encodeURIComponent(cq);
Akronb89863a2018-11-13 16:43:59 +0100107 KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq);
hebasta0ee50802018-06-20 10:24:45 +0200108 };
hebasta4ba496a2018-06-05 15:56:01 +0200109
Akronbf713fc2020-10-13 10:44:35 +0200110
hebasta4ba496a2018-06-05 15:56:01 +0200111 /**
Akron48b1e4d2015-06-17 18:47:01 +0200112 * General method to retrieve JSON information
113 */
Akronb89863a2018-11-13 16:43:59 +0100114 KorAP.API.getJSON = function (url, onload, title) {
Akronbf713fc2020-10-13 10:44:35 +0200115 const req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +0000116 req.open("GET", url, true);
Akron4c33c622018-11-12 13:43:27 +0100117
118 // Dispatch global "window" event
Akronbf713fc2020-10-13 10:44:35 +0200119 const reqE = new CustomEvent('korapRequest', {
Akron4c33c622018-11-12 13:43:27 +0100120 bubbles : false,
Akronb89863a2018-11-13 16:43:59 +0100121 detail: {
122 "url" : url,
123 "title" : title
124 }
Akron4c33c622018-11-12 13:43:27 +0100125 });
126 window.dispatchEvent(reqE);
127
Nils Diewald4347ee92015-05-04 20:32:48 +0000128 req.setRequestHeader("Accept", "application/json");
129 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
130 req.onreadystatechange = function () {
131 /*
Akron0b489ad2018-02-02 16:49:32 +0100132 States:
133 0 - unsent (prior to open)
134 1 - opened (prior to send)
135 2 - headers received
136 3 - loading (responseText has partial data)
137 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +0000138 */
139 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +0200140
Akronbf713fc2020-10-13 10:44:35 +0200141 let json;
Akronb5d05d72018-02-12 15:09:12 +0100142 try {
143 json = JSON.parse(this.responseText);
144 }
145 catch (e) {
146 KorAP.log(0, e);
147 console.log(e);
148 onload(undefined);
149 return;
150 };
Akronbf713fc2020-10-13 10:44:35 +0200151
Akron962fff92020-10-12 14:25:49 +0200152 if (json !== undefined && json["errors"] !== undefined) {
Akron678c26f2020-10-09 08:52:50 +0200153 json["errors"].forEach(
154 e => KorAP.log(e[0], e[1] || "Unknown")
155 );
Akron515851a2017-05-02 12:53:17 +0200156 }
Akronbf713fc2020-10-13 10:44:35 +0200157
Akron515851a2017-05-02 12:53:17 +0200158 else if (this.status !== 200) {
159 KorAP.log(this.status, this.statusText);
160 };
161
162 if (this.status === 200) {
163 onload(json);
164 }
Akronbf713fc2020-10-13 10:44:35 +0200165
Akron515851a2017-05-02 12:53:17 +0200166 else {
167 onload(undefined);
168 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000169 }
170 };
Akronbf713fc2020-10-13 10:44:35 +0200171
Nils Diewald4347ee92015-05-04 20:32:48 +0000172 req.ontimeout = function () {
173 KorAP.log(0, 'Request Timeout');
174 };
175 req.send();
176 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177});