Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 3 | define(['util'], function () { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 4 | |
| 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 12 | |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 13 | // See https://flaviocopes.com/http-request-headers/ for a list of headers |
| 14 | |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 15 | KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : ''; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 16 | KorAP.API = KorAP.API || {}; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 17 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 18 | const legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$'); |
| 19 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 20 | /** |
| 21 | * Retrieve information about a match |
| 22 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 23 | KorAP.API.getMatchInfo = function (match, param, cb) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 24 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 25 | // match is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 26 | let url = KorAP.URL + '/corpus'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 27 | /* |
| 28 | url += '/' + match.corpusID; |
| 29 | url += '/' + match.docID; |
| 30 | url += '/' + match.textID; |
| 31 | */ |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 32 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 33 | // This is for legacy support |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 34 | const legacy = legacySigle.exec(match.textSigle); |
| 35 | let docFragment = ""; |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 36 | if (legacy !== null && legacy[0]) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 37 | docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3]; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 38 | } |
| 39 | else { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 40 | docFragment = match.textSigle; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 41 | } |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 42 | |
| 43 | docFragment += '/' + match.matchID; |
Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame^] | 44 | url += '/' + docFragment + '?'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 45 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 46 | // { spans: true, layer:x, foundry : y} |
| 47 | if (param['spans'] == true) { |
Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame^] | 48 | url += 'spans=true'; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 49 | docFragment += ' +spans '; |
| 50 | if (param['foundry'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 51 | url += '&foundry=' + param['foundry']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 52 | docFragment += param['foundry']; |
| 53 | }; |
| 54 | if (param['layer'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 55 | url += '&layer=' + param['layer']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 56 | docFragment += '/'+param['layer']; |
| 57 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 58 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 59 | |
| 60 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 61 | else { |
| 62 | // TODO |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 63 | docFragment += ' -spans'; |
Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame^] | 64 | url += 'spans=false'; |
| 65 | }; |
| 66 | |
| 67 | if (KorAP.ResponsePipe != null) |
| 68 | url += '&response-pipe=' + KorAP.ResponsePipe.toString(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 69 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 70 | KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 71 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 72 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 73 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 74 | /** |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 75 | * Retrieve information about a document. |
| 76 | */ |
| 77 | KorAP.API.getTextInfo = function (doc, param, cb) { |
| 78 | |
| 79 | // doc is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 80 | let url = KorAP.URL + '/corpus' + '/' + doc.textSigle; |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 81 | |
| 82 | if (param['fields'] !== undefined) { |
| 83 | url += '?fields='; // TODO! |
| 84 | } |
| 85 | else { |
Akron | 4bbd8b3 | 2018-03-06 19:19:44 +0100 | [diff] [blame] | 86 | url += '?fields=@all'; // TODO: Maybe '*'? |
Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame^] | 87 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 88 | |
Akron | 910828a | 2025-06-27 15:38:48 +0200 | [diff] [blame^] | 89 | if (KorAP.ResponsePipe != null) |
| 90 | url += '&response-pipe=' + KorAP.ResponsePipe.toString(); |
| 91 | |
| 92 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 93 | KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle); |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
| 97 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 98 | * Retrieve information about virtual corpora |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 99 | */ |
| 100 | KorAP.API.getCollections = function (cb) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 101 | KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo"); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 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 | * |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 110 | * @param cq corpus query (formerly collectionQuery) |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 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){ |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 116 | let url = KorAP.URL + "/corpus?cq=" + encodeURIComponent(cq); |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 117 | KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq); |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 118 | }; |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 119 | |
| 120 | |
| 121 | /** |
| 122 | * Retrieve a list of all plugin objects to |
| 123 | * establish in the frontend. |
| 124 | */ |
| 125 | KorAP.API.getPluginList = function (url, cb) { |
| 126 | KorAP.API.getJSON(url, cb, "Plugin-List") |
| 127 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 128 | |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 129 | /** |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 130 | * General function to communicate JS Objects with the server |
| 131 | * |
| 132 | * @param {HTTMLRequestType} requestType Should be "GET", "PUT", "POST" or "DELETE" |
| 133 | * @param {String} url The url that specifies where the JSON file is ("GET"), will be ("PUT" and "POST") or will have been ("DELETE") |
| 134 | * @param {String} title How to store this request in the logs |
| 135 | * @param {JSObj} jsObj For "PUT" and "POST". The JS Object that is getting transfered. This function stringifies it. |
| 136 | * @param {function} returnValueCB For "GET". The callback function that receives the retrieved JS object (already parsed) as a parameter, or undefined if none is eligible |
| 137 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 138 | */ |
| 139 | function _actionJSON (requestType, url, title, jsObj, returnValueCB, errorCB) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 140 | const req = new XMLHttpRequest(); |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 141 | req.open(requestType, url, true); |
| 142 | // Dispatch global "window" event. See Kalamar::Plugin::Piwik |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 143 | const reqE = new CustomEvent('korapRequest', { |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 144 | bubbles : false, |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 145 | detail: { |
| 146 | "url" : url, |
| 147 | "title" : title |
| 148 | } |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 149 | }); |
| 150 | window.dispatchEvent(reqE); |
| 151 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 152 | req.setRequestHeader("Accept", "application/json"); |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 153 | req.setRequestHeader("Content-Type", "application/json"); |
| 154 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
Leo Repp | 5799740 | 2021-08-18 16:37:52 +0200 | [diff] [blame] | 155 | //req.setRequestHeader('Origin',"API"); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 156 | req.onreadystatechange = function () { |
| 157 | /* |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 158 | States: |
| 159 | 0 - unsent (prior to open) |
| 160 | 1 - opened (prior to send) |
| 161 | 2 - headers received |
| 162 | 3 - loading (responseText has partial data) |
| 163 | 4 - done |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 164 | */ |
| 165 | if (this.readyState == 4) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 166 | |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 167 | if (requestType === "GET") { //GET |
| 168 | let retJSObj; |
| 169 | try { |
| 170 | retJSObj = JSON.parse(this.responseText); |
| 171 | } |
| 172 | catch (e) { |
| 173 | KorAP.log(0, e); |
| 174 | console.log(e); |
| 175 | returnValueCB(undefined); |
| 176 | return; |
| 177 | }; |
| 178 | |
Akron | e71bd6d | 2024-06-11 15:47:39 +0200 | [diff] [blame] | 179 | if (retJSObj !== undefined) { |
| 180 | if (retJSObj["errors"] !== undefined) { |
| 181 | retJSObj["errors"].forEach( |
| 182 | e => KorAP.log(e[0], e[1] || "Unknown") |
| 183 | ); |
| 184 | } else if (retJSObj["warnings"] !== undefined) { |
| 185 | retJSObj["warnings"].forEach( |
| 186 | e => KorAP.log(e[0], e[1] || "Unknown", null, 'warn') |
| 187 | ); |
| 188 | } |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | else if (this.status !== 200) { |
| 192 | KorAP.log(this.status, this.statusText, "Remote service error (XMLHttpRequest) under URL: " + url); |
| 193 | }; |
| 194 | |
| 195 | if (this.status === 200) { |
| 196 | returnValueCB(retJSObj); |
| 197 | } |
| 198 | |
| 199 | else { |
| 200 | returnValueCB(undefined); |
| 201 | }; |
| 202 | |
| 203 | } else { // PUT, POST, DELETE |
| 204 | if (this.status >= 300 || this.status < 200) { //Error |
| 205 | KorAP.log(this.status, this.statusText, "Remote service error (XMLHttpRequest) under URL: " + url); |
| 206 | }; |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 207 | }; |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 208 | // Call the callback function (no matter requestType) if one is given. |
| 209 | if (typeof(errorCB) === "function"){ |
| 210 | errorCB({ |
| 211 | "status" : this.status, |
| 212 | "statusText" : this.statusText |
| 213 | }); |
| 214 | }; |
| 215 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 216 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 217 | |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 218 | /*Set a value for .timeout to use this functionality */ |
| 219 | //req.ontimeout = function () { |
| 220 | // KorAP.log(0, 'Request Timeout'); |
| 221 | //}; |
| 222 | if (requestType === "POST" || requestType === "PUT") { |
| 223 | req.send(JSON.stringify(jsObj)); |
| 224 | } else { //GET, DELETE |
| 225 | req.send(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 226 | }; |
Leo Repp | a6aba9a | 2021-01-23 20:16:43 +0100 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | /** |
| 230 | * General method to get JSON information. |
| 231 | * |
| 232 | * @param {String} url The url at which the JSON File will be located |
| 233 | * @param {function} returnValueCB The callback function that receives the retrieved JS object (already parsed) as a parameter, or undefined if none is eligible |
| 234 | * @param {String} title How to store this request in the logs |
| 235 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 236 | */ |
| 237 | KorAP.API.getJSON = function (url, returnValueCB, title, errorCB) { |
| 238 | _actionJSON("GET", url, title, undefined, returnValueCB, errorCB); |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * General method to put JSON information. |
| 243 | * |
| 244 | * @param {String} url The url at which the JSON File will be located |
| 245 | * @param {JSObj} jsObj The JS object that is getting transfered. This will be stringified |
| 246 | * @param {String} title How to store this request in the logs |
| 247 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 248 | */ |
| 249 | KorAP.API.putJSON = function (url, jsObj, title, errorCB) { |
| 250 | _actionJSON("PUT", url, title, jsObj, undefined, errorCB); |
| 251 | }; |
| 252 | |
| 253 | /** |
| 254 | * General method to post JSON information. |
| 255 | * |
| 256 | * @param {String} url The url at which the JSON File will be located |
| 257 | * @param {JSObj} jsObj The JS object that is getting transfered. This will be stringified |
| 258 | * @param {String} title How to store this request in the logs |
| 259 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 260 | */ |
| 261 | KorAP.API.postJSON = function (url, jsObj, title, errorCB) { |
| 262 | _actionJSON("POST", url, title, jsObj, undefined, errorCB); |
| 263 | }; |
| 264 | |
| 265 | /** |
| 266 | * General method to delete a file at a specific URL |
| 267 | * |
| 268 | * @param {String} url The url at which the to be deleted file is located |
| 269 | * @param {String} title How to store this request in the logs |
| 270 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 271 | */ |
| 272 | KorAP.API.deleteJSON = function (url, title, errorCB) { |
| 273 | _actionJSON("DELETE", url, title, undefined, undefined, errorCB); |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | // Stored query related functions |
| 278 | |
| 279 | /** |
| 280 | * Retrieve saved list of queries |
| 281 | * |
| 282 | * @param {function} returnValueCB The callback function that receives the JS object Listof queries, already parsed |
| 283 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 284 | */ |
| 285 | KorAP.API.getQueryList = function (returnValueCB, errorCB){ |
| 286 | KorAP.API.getJSON(KorAP.URL + "/query/", returnValueCB, "getSavedQueryList", errorCB); |
| 287 | }; |
| 288 | |
| 289 | /** |
| 290 | * Retrieve specific saved query by query name |
| 291 | * |
| 292 | * @param {String} qn The name of the query to be retrieved. Must be a string |
| 293 | * @param {function} returnValueCB The callback function that receives the query JS object Object, already parsed |
| 294 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 295 | */ |
| 296 | KorAP.API.getQuery = function (qn, returnValueCB, errorCB){ |
| 297 | KorAP.API.getJSON(KorAP.URL + "/query/" + qn, returnValueCB, "getSavedQuery of name "+ qn, errorCB); |
| 298 | }; |
| 299 | |
| 300 | /** |
| 301 | * Put new query by query name |
| 302 | * |
| 303 | * @param {String} qn The name of the new query |
| 304 | * @param {JSObj} jsObj The query. This will be stringified |
| 305 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 306 | */ |
| 307 | KorAP.API.putQuery = function (qn, jsObj, errorCB){ |
| 308 | KorAP.API.putJSON(KorAP.URL + "/query/" + qn, jsObj, "putQuery of name "+ qn, errorCB); |
| 309 | }; |
| 310 | |
| 311 | /** |
| 312 | * Post new query by query name |
| 313 | * |
| 314 | * @param {String} qn The name of the new query |
| 315 | * @param {JSObj} jsObj The query. This will be stringified |
| 316 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 317 | */ |
| 318 | KorAP.API.postQuery = function (qn, jsObj, errorCB){ |
| 319 | KorAP.API.postJSON(KorAP.URL + "/query/" + qn, jsObj, "postQuery of name "+ qn, errorCB); |
| 320 | }; |
| 321 | |
| 322 | /** |
| 323 | * delete query by query name |
| 324 | * |
| 325 | * @param {String} qn The name of the to be deleted query |
| 326 | * @param {function} errorCB Optional. Callback function for error handling, receives JS object with status and statusText attribute |
| 327 | */ |
| 328 | KorAP.API.deleteQuery = function (qn, errorCB){ |
| 329 | KorAP.API.deleteJSON(KorAP.URL + "/query/" + qn, "deleteQuery of name "+ qn, errorCB); |
| 330 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 331 | }); |