blob: cfc06251e0246ed3ae8e0f2532904d6c0da60681 [file] [log] [blame]
Akrone51eaa32020-11-10 09:35:53 +01001"use strict";
2
Nils Diewald0e6992a2015-04-14 20:13:52 +00003define(['util'], function () {
Akronbf713fc2020-10-13 10:44:35 +02004
5 // TODO:
6 // - https://github.com/honza/140medley/blob/master/140medley.js
7 // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
8 // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
9 // - r.addEventListener("progress", updateProgress, false);
10 // - http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
11 // - http://stackoverflow.com/questions/6112744/load-javascript-on-demand
Nils Diewald58141332015-04-07 16:18:45 +000012
Akron4c33c622018-11-12 13:43:27 +010013 KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : '';
Nils Diewald4347ee92015-05-04 20:32:48 +000014 KorAP.API = KorAP.API || {};
Akronb89863a2018-11-13 16:43:59 +010015
Akronbf713fc2020-10-13 10:44:35 +020016 const legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$');
17
Akron48b1e4d2015-06-17 18:47:01 +020018 /**
19 * Retrieve information about a match
20 */
Nils Diewald4347ee92015-05-04 20:32:48 +000021 KorAP.API.getMatchInfo = function (match, param, cb) {
Nils Diewald58141332015-04-07 16:18:45 +000022
Nils Diewald4347ee92015-05-04 20:32:48 +000023 // match is a KorAP.Match object
Akronbf713fc2020-10-13 10:44:35 +020024 let url = KorAP.URL + '/corpus';
Akron0b489ad2018-02-02 16:49:32 +010025 /*
26 url += '/' + match.corpusID;
27 url += '/' + match.docID;
28 url += '/' + match.textID;
29 */
Akron19d97fe2016-09-06 20:47:05 +020030
Akron19d97fe2016-09-06 20:47:05 +020031 // This is for legacy support
Akronbf713fc2020-10-13 10:44:35 +020032 const legacy = legacySigle.exec(match.textSigle);
33 let docFragment = "";
Akron7f613e02016-11-07 02:50:44 +010034 if (legacy !== null && legacy[0]) {
Akronb89863a2018-11-13 16:43:59 +010035 docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3];
Akron19d97fe2016-09-06 20:47:05 +020036 }
37 else {
Akronb89863a2018-11-13 16:43:59 +010038 docFragment = match.textSigle;
Akron19d97fe2016-09-06 20:47:05 +020039 }
Akronb89863a2018-11-13 16:43:59 +010040
41 docFragment += '/' + match.matchID;
42 url += '/' + docFragment;
Nils Diewald58141332015-04-07 16:18:45 +000043
Nils Diewald4347ee92015-05-04 20:32:48 +000044 // { spans: true, layer:x, foundry : y}
45 if (param['spans'] == true) {
46 url += '?spans=true';
Akronb89863a2018-11-13 16:43:59 +010047 docFragment += ' +spans ';
48 if (param['foundry'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020049 url += '&foundry=' + param['foundry'];
Akronb89863a2018-11-13 16:43:59 +010050 docFragment += param['foundry'];
51 };
52 if (param['layer'] !== undefined) {
Akron515851a2017-05-02 12:53:17 +020053 url += '&layer=' + param['layer'];
Akronb89863a2018-11-13 16:43:59 +010054 docFragment += '/'+param['layer'];
55 }
Nils Diewald58141332015-04-07 16:18:45 +000056 }
Nils Diewald4347ee92015-05-04 20:32:48 +000057
58 // { spans : false, layer: [Array of KorAP.InfoLayer] }
59 else {
60 // TODO
Akronb89863a2018-11-13 16:43:59 +010061 docFragment += ' -spans';
Nils Diewald4347ee92015-05-04 20:32:48 +000062 url += '?spans=false';
63 }
64
Akronb89863a2018-11-13 16:43:59 +010065 KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment);
Nils Diewald58141332015-04-07 16:18:45 +000066 };
Nils Diewald4347ee92015-05-04 20:32:48 +000067
Akron0b489ad2018-02-02 16:49:32 +010068
Akron48b1e4d2015-06-17 18:47:01 +020069 /**
Akron0ad7cd22018-02-08 18:03:06 +010070 * Retrieve information about a document.
71 */
72 KorAP.API.getTextInfo = function (doc, param, cb) {
73
74 // doc is a KorAP.Match object
Akronbf713fc2020-10-13 10:44:35 +020075 let url = KorAP.URL + '/corpus' + '/' + 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 }
Akronbf713fc2020-10-13 10:44:35 +020083
Akronb89863a2018-11-13 16:43:59 +010084 KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle);
Akron0ad7cd22018-02-08 18:03:06 +010085 };
86
87
88 /**
Akroncd42a142019-07-12 18:55:37 +020089 * Retrieve information about virtual corpora
Akron48b1e4d2015-06-17 18:47:01 +020090 */
91 KorAP.API.getCollections = function (cb) {
Akronb89863a2018-11-13 16:43:59 +010092 KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo");
Akron48b1e4d2015-06-17 18:47:01 +020093 };
94
hebasta0ee50802018-06-20 10:24:45 +020095
96 /**
97 * Retrieve information about corpus statistic
98 *
99 * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
100 *
101 * cq = corpus query (formerly collectionQuery)
102 *
103 * Adress the MOJO-Endpoint for example with
104 * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
105 */
106 KorAP.API.getCorpStat = function (cq, cb){
Akronbf713fc2020-10-13 10:44:35 +0200107 let url = KorAP.URL + "/corpus?cq=" + encodeURIComponent(cq);
Akronb89863a2018-11-13 16:43:59 +0100108 KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq);
hebasta0ee50802018-06-20 10:24:45 +0200109 };
Akron8dda1c62021-01-20 10:27:32 +0100110
111
112 /**
113 * Retrieve a list of all plugin objects to
114 * establish in the frontend.
115 */
116 KorAP.API.getPluginList = function (url, cb) {
117 KorAP.API.getJSON(url, cb, "Plugin-List")
118 };
Akronbf713fc2020-10-13 10:44:35 +0200119
hebasta4ba496a2018-06-05 15:56:01 +0200120 /**
Akron48b1e4d2015-06-17 18:47:01 +0200121 * General method to retrieve JSON information
122 */
Akronb89863a2018-11-13 16:43:59 +0100123 KorAP.API.getJSON = function (url, onload, title) {
Akronbf713fc2020-10-13 10:44:35 +0200124 const req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +0000125 req.open("GET", url, true);
Akron4c33c622018-11-12 13:43:27 +0100126
127 // Dispatch global "window" event
Akronbf713fc2020-10-13 10:44:35 +0200128 const reqE = new CustomEvent('korapRequest', {
Akron4c33c622018-11-12 13:43:27 +0100129 bubbles : false,
Akronb89863a2018-11-13 16:43:59 +0100130 detail: {
131 "url" : url,
132 "title" : title
133 }
Akron4c33c622018-11-12 13:43:27 +0100134 });
135 window.dispatchEvent(reqE);
136
Nils Diewald4347ee92015-05-04 20:32:48 +0000137 req.setRequestHeader("Accept", "application/json");
138 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
139 req.onreadystatechange = function () {
140 /*
Akron0b489ad2018-02-02 16:49:32 +0100141 States:
142 0 - unsent (prior to open)
143 1 - opened (prior to send)
144 2 - headers received
145 3 - loading (responseText has partial data)
146 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +0000147 */
148 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +0200149
Akronbf713fc2020-10-13 10:44:35 +0200150 let json;
Akronb5d05d72018-02-12 15:09:12 +0100151 try {
152 json = JSON.parse(this.responseText);
153 }
154 catch (e) {
155 KorAP.log(0, e);
156 console.log(e);
157 onload(undefined);
158 return;
159 };
Akronbf713fc2020-10-13 10:44:35 +0200160
Akron962fff92020-10-12 14:25:49 +0200161 if (json !== undefined && json["errors"] !== undefined) {
Akron678c26f2020-10-09 08:52:50 +0200162 json["errors"].forEach(
163 e => KorAP.log(e[0], e[1] || "Unknown")
164 );
Akron515851a2017-05-02 12:53:17 +0200165 }
Akronbf713fc2020-10-13 10:44:35 +0200166
Akron515851a2017-05-02 12:53:17 +0200167 else if (this.status !== 200) {
168 KorAP.log(this.status, this.statusText);
169 };
170
171 if (this.status === 200) {
172 onload(json);
173 }
Akronbf713fc2020-10-13 10:44:35 +0200174
Akron515851a2017-05-02 12:53:17 +0200175 else {
176 onload(undefined);
177 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000178 }
179 };
Akronbf713fc2020-10-13 10:44:35 +0200180
Nils Diewald4347ee92015-05-04 20:32:48 +0000181 req.ontimeout = function () {
182 KorAP.log(0, 'Request Timeout');
183 };
184 req.send();
185 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186});