blob: 75a4e6841c685cdad2dee309a7ed02d6e37ba5c6 [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 };
hebasta4ba496a2018-06-05 15:56:01 +0200110
Akronbf713fc2020-10-13 10:44:35 +0200111
hebasta4ba496a2018-06-05 15:56:01 +0200112 /**
Akron48b1e4d2015-06-17 18:47:01 +0200113 * General method to retrieve JSON information
114 */
Akronb89863a2018-11-13 16:43:59 +0100115 KorAP.API.getJSON = function (url, onload, title) {
Akronbf713fc2020-10-13 10:44:35 +0200116 const req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +0000117 req.open("GET", url, true);
Akron4c33c622018-11-12 13:43:27 +0100118
119 // Dispatch global "window" event
Akronbf713fc2020-10-13 10:44:35 +0200120 const reqE = new CustomEvent('korapRequest', {
Akron4c33c622018-11-12 13:43:27 +0100121 bubbles : false,
Akronb89863a2018-11-13 16:43:59 +0100122 detail: {
123 "url" : url,
124 "title" : title
125 }
Akron4c33c622018-11-12 13:43:27 +0100126 });
127 window.dispatchEvent(reqE);
128
Nils Diewald4347ee92015-05-04 20:32:48 +0000129 req.setRequestHeader("Accept", "application/json");
130 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
131 req.onreadystatechange = function () {
132 /*
Akron0b489ad2018-02-02 16:49:32 +0100133 States:
134 0 - unsent (prior to open)
135 1 - opened (prior to send)
136 2 - headers received
137 3 - loading (responseText has partial data)
138 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +0000139 */
140 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +0200141
Akronbf713fc2020-10-13 10:44:35 +0200142 let json;
Akronb5d05d72018-02-12 15:09:12 +0100143 try {
144 json = JSON.parse(this.responseText);
145 }
146 catch (e) {
147 KorAP.log(0, e);
148 console.log(e);
149 onload(undefined);
150 return;
151 };
Akronbf713fc2020-10-13 10:44:35 +0200152
Akron962fff92020-10-12 14:25:49 +0200153 if (json !== undefined && json["errors"] !== undefined) {
Akron678c26f2020-10-09 08:52:50 +0200154 json["errors"].forEach(
155 e => KorAP.log(e[0], e[1] || "Unknown")
156 );
Akron515851a2017-05-02 12:53:17 +0200157 }
Akronbf713fc2020-10-13 10:44:35 +0200158
Akron515851a2017-05-02 12:53:17 +0200159 else if (this.status !== 200) {
160 KorAP.log(this.status, this.statusText);
161 };
162
163 if (this.status === 200) {
164 onload(json);
165 }
Akronbf713fc2020-10-13 10:44:35 +0200166
Akron515851a2017-05-02 12:53:17 +0200167 else {
168 onload(undefined);
169 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000170 }
171 };
Akronbf713fc2020-10-13 10:44:35 +0200172
Nils Diewald4347ee92015-05-04 20:32:48 +0000173 req.ontimeout = function () {
174 KorAP.log(0, 'Request Timeout');
175 };
176 req.send();
177 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000178});