blob: 1d715500df25c2930236e616a726a4b28532b4ea [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 || {};
Akron4c33c622018-11-12 13:43:27 +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);
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 /**
Akron0ad7cd22018-02-08 18:03:06 +010060 * Retrieve information about a document.
61 */
62 KorAP.API.getTextInfo = function (doc, param, cb) {
63
64 // doc is a KorAP.Match object
65 var url = KorAP.URL;
Akronb5d05d72018-02-12 15:09:12 +010066 url += '/corpus';
67 url += '/' + doc.textSigle;
Akron0ad7cd22018-02-08 18:03:06 +010068
69 if (param['fields'] !== undefined) {
70 url += '?fields='; // TODO!
71 }
72 else {
Akron4bbd8b32018-03-06 19:19:44 +010073 url += '?fields=@all'; // TODO: Maybe '*'?
Akron0ad7cd22018-02-08 18:03:06 +010074 }
75 KorAP.API.getJSON(url, cb);
76 };
77
78
79 /**
Akron48b1e4d2015-06-17 18:47:01 +020080 * Retrieve information about collections
81 */
82 KorAP.API.getCollections = function (cb) {
83 KorAP.API.getJSON(KorAP.URL + '/collection', cb);
84 };
85
Akron0b489ad2018-02-02 16:49:32 +010086
Akron48b1e4d2015-06-17 18:47:01 +020087 /**
hebasta4ba496a2018-06-05 15:56:01 +020088 * Retrieve information about corpus statistic
89 *
90 * Development mode:
91 * URL = http://localhost:8089/api
hebasta0ee50802018-06-20 10:24:45 +020092 *
93 * collectionQuery has be changed to corpusQuery
hebasta4ba496a2018-06-05 15:56:01 +020094 */
hebasta0ee50802018-06-20 10:24:45 +020095 /* KorAP.API.getCorpStat = function (collQu, cb){
hebasta4ba496a2018-06-05 15:56:01 +020096 //development mode:
97 var url = "http://localhost:8089/api/";
98 url = url + "statistics";
hebasta0ee50802018-06-20 10:24:45 +020099 url = url + "?corpusQuery=";
hebasta4ba496a2018-06-05 15:56:01 +0200100 url = url + collQu;
101 KorAP.API.getJSON(url, cb);
102 };*/
hebasta0ee50802018-06-20 10:24:45 +0200103
104
105 /**
106 * Retrieve information about corpus statistic
107 *
108 * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
109 *
110 * cq = corpus query (formerly collectionQuery)
111 *
112 * Adress the MOJO-Endpoint for example with
113 * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22
114 */
115 KorAP.API.getCorpStat = function (cq, cb){
Akron8f0de5e2018-08-27 16:31:12 +0200116 var url = KorAP.URL;
117 url += "/corpus?cq=" + cq;
hebasta0ee50802018-06-20 10:24:45 +0200118 KorAP.API.getJSON(url, cb);
119 };
hebasta4ba496a2018-06-05 15:56:01 +0200120
121 /**
Akron48b1e4d2015-06-17 18:47:01 +0200122 * General method to retrieve JSON information
123 */
Nils Diewald4347ee92015-05-04 20:32:48 +0000124 KorAP.API.getJSON = function (url, onload) {
125 var req = new XMLHttpRequest();
Nils Diewald4347ee92015-05-04 20:32:48 +0000126 req.open("GET", url, true);
Akron4c33c622018-11-12 13:43:27 +0100127
128 // Dispatch global "window" event
129 var reqE = new CustomEvent('korapRequest', {
130 bubbles : false,
131 detail: { "url" : url }
132 });
133 window.dispatchEvent(reqE);
134
Nils Diewald4347ee92015-05-04 20:32:48 +0000135 req.setRequestHeader("Accept", "application/json");
136 req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
137 req.onreadystatechange = function () {
138 /*
Akron0b489ad2018-02-02 16:49:32 +0100139 States:
140 0 - unsent (prior to open)
141 1 - opened (prior to send)
142 2 - headers received
143 3 - loading (responseText has partial data)
144 4 - done
Nils Diewald4347ee92015-05-04 20:32:48 +0000145 */
146 if (this.readyState == 4) {
Akron515851a2017-05-02 12:53:17 +0200147
Akronb5d05d72018-02-12 15:09:12 +0100148 var json;
149 try {
150 json = JSON.parse(this.responseText);
151 }
152 catch (e) {
153 KorAP.log(0, e);
154 console.log(e);
155 onload(undefined);
156 return;
157 };
Akron515851a2017-05-02 12:53:17 +0200158 if (json !== null && json["errors"] !== null) {
159 for (var i in json["errors"]) {
160 KorAP.log(json["errors"][i][0], json["errors"][i][1] || "Unknown");
161 };
162 }
163 else if (this.status !== 200) {
164 KorAP.log(this.status, this.statusText);
165 };
166
167 if (this.status === 200) {
168 onload(json);
169 }
170 else {
171 onload(undefined);
172 };
Nils Diewald4347ee92015-05-04 20:32:48 +0000173 }
174 };
175 req.ontimeout = function () {
176 KorAP.log(0, 'Request Timeout');
177 };
178 req.send();
179 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180});