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 | |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 13 | KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : ''; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 14 | KorAP.API = KorAP.API || {}; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 15 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 16 | const legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$'); |
| 17 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 18 | /** |
| 19 | * Retrieve information about a match |
| 20 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 21 | KorAP.API.getMatchInfo = function (match, param, cb) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 22 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 23 | // match is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 24 | let url = KorAP.URL + '/corpus'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 25 | /* |
| 26 | url += '/' + match.corpusID; |
| 27 | url += '/' + match.docID; |
| 28 | url += '/' + match.textID; |
| 29 | */ |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 30 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 31 | // This is for legacy support |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 32 | const legacy = legacySigle.exec(match.textSigle); |
| 33 | let docFragment = ""; |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 34 | if (legacy !== null && legacy[0]) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 35 | docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3]; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 36 | } |
| 37 | else { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 38 | docFragment = match.textSigle; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 39 | } |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 40 | |
| 41 | docFragment += '/' + match.matchID; |
| 42 | url += '/' + docFragment; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 43 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 44 | // { spans: true, layer:x, foundry : y} |
| 45 | if (param['spans'] == true) { |
| 46 | url += '?spans=true'; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 47 | docFragment += ' +spans '; |
| 48 | if (param['foundry'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 49 | url += '&foundry=' + param['foundry']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 50 | docFragment += param['foundry']; |
| 51 | }; |
| 52 | if (param['layer'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 53 | url += '&layer=' + param['layer']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 54 | docFragment += '/'+param['layer']; |
| 55 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 56 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 57 | |
| 58 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 59 | else { |
| 60 | // TODO |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 61 | docFragment += ' -spans'; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 62 | url += '?spans=false'; |
| 63 | } |
| 64 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 65 | KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 66 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 67 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 68 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 69 | /** |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 70 | * Retrieve information about a document. |
| 71 | */ |
| 72 | KorAP.API.getTextInfo = function (doc, param, cb) { |
| 73 | |
| 74 | // doc is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 75 | let url = KorAP.URL + '/corpus' + '/' + doc.textSigle; |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 76 | |
| 77 | if (param['fields'] !== undefined) { |
| 78 | url += '?fields='; // TODO! |
| 79 | } |
| 80 | else { |
Akron | 4bbd8b3 | 2018-03-06 19:19:44 +0100 | [diff] [blame] | 81 | url += '?fields=@all'; // TODO: Maybe '*'? |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 82 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 83 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 84 | KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle); |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | |
| 88 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 89 | * Retrieve information about virtual corpora |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 90 | */ |
| 91 | KorAP.API.getCollections = function (cb) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 92 | KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo"); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 95 | |
| 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){ |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 107 | let url = KorAP.URL + "/corpus?cq=" + encodeURIComponent(cq); |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 108 | KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq); |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 109 | }; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 110 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 111 | |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 112 | /** |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 113 | * General method to retrieve JSON information |
| 114 | */ |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 115 | KorAP.API.getJSON = function (url, onload, title) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 116 | const req = new XMLHttpRequest(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 117 | req.open("GET", url, true); |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 118 | |
| 119 | // Dispatch global "window" event |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 120 | const reqE = new CustomEvent('korapRequest', { |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 121 | bubbles : false, |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 122 | detail: { |
| 123 | "url" : url, |
| 124 | "title" : title |
| 125 | } |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 126 | }); |
| 127 | window.dispatchEvent(reqE); |
| 128 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 129 | req.setRequestHeader("Accept", "application/json"); |
| 130 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 131 | req.onreadystatechange = function () { |
| 132 | /* |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 133 | 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 Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 139 | */ |
| 140 | if (this.readyState == 4) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 141 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 142 | let json; |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 143 | 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 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 152 | |
Akron | 962fff9 | 2020-10-12 14:25:49 +0200 | [diff] [blame] | 153 | if (json !== undefined && json["errors"] !== undefined) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 154 | json["errors"].forEach( |
| 155 | e => KorAP.log(e[0], e[1] || "Unknown") |
| 156 | ); |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 157 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 158 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 159 | else if (this.status !== 200) { |
| 160 | KorAP.log(this.status, this.statusText); |
| 161 | }; |
| 162 | |
| 163 | if (this.status === 200) { |
| 164 | onload(json); |
| 165 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 166 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 167 | else { |
| 168 | onload(undefined); |
| 169 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 170 | } |
| 171 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame] | 172 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 173 | req.ontimeout = function () { |
| 174 | KorAP.log(0, 'Request Timeout'); |
| 175 | }; |
| 176 | req.send(); |
| 177 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 178 | }); |