Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | define(['util'], function () { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 2 | "use strict"; |
| 3 | |
| 4 | // TODO: |
| 5 | // - https://github.com/honza/140medley/blob/master/140medley.js |
| 6 | // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest |
| 7 | // - https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest |
| 8 | // - r.addEventListener("progress", updateProgress, false); |
| 9 | // - http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml |
| 10 | // - http://stackoverflow.com/questions/6112744/load-javascript-on-demand |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 11 | |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 12 | KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : ''; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 13 | KorAP.API = KorAP.API || {}; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 14 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 15 | const legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$'); |
| 16 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 17 | /** |
| 18 | * Retrieve information about a match |
| 19 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 20 | KorAP.API.getMatchInfo = function (match, param, cb) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 21 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 22 | // match is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 23 | let url = KorAP.URL + '/corpus'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 24 | /* |
| 25 | url += '/' + match.corpusID; |
| 26 | url += '/' + match.docID; |
| 27 | url += '/' + match.textID; |
| 28 | */ |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 29 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 30 | // This is for legacy support |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 31 | const legacy = legacySigle.exec(match.textSigle); |
| 32 | let docFragment = ""; |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 33 | if (legacy !== null && legacy[0]) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 34 | docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3]; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 35 | } |
| 36 | else { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 37 | docFragment = match.textSigle; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 38 | } |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 39 | |
| 40 | docFragment += '/' + match.matchID; |
| 41 | url += '/' + docFragment; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 42 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 43 | // { spans: true, layer:x, foundry : y} |
| 44 | if (param['spans'] == true) { |
| 45 | url += '?spans=true'; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 46 | docFragment += ' +spans '; |
| 47 | if (param['foundry'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 48 | url += '&foundry=' + param['foundry']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 49 | docFragment += param['foundry']; |
| 50 | }; |
| 51 | if (param['layer'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 52 | url += '&layer=' + param['layer']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 53 | docFragment += '/'+param['layer']; |
| 54 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 55 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 56 | |
| 57 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 58 | else { |
| 59 | // TODO |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 60 | docFragment += ' -spans'; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 61 | url += '?spans=false'; |
| 62 | } |
| 63 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 64 | KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 65 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 66 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 67 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 68 | /** |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 69 | * Retrieve information about a document. |
| 70 | */ |
| 71 | KorAP.API.getTextInfo = function (doc, param, cb) { |
| 72 | |
| 73 | // doc is a KorAP.Match object |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 74 | let url = KorAP.URL + '/corpus' + '/' + doc.textSigle; |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 75 | |
| 76 | if (param['fields'] !== undefined) { |
| 77 | url += '?fields='; // TODO! |
| 78 | } |
| 79 | else { |
Akron | 4bbd8b3 | 2018-03-06 19:19:44 +0100 | [diff] [blame] | 80 | url += '?fields=@all'; // TODO: Maybe '*'? |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 81 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 82 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 83 | KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle); |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | |
| 87 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 88 | * Retrieve information about virtual corpora |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 89 | */ |
| 90 | KorAP.API.getCollections = function (cb) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 91 | KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo"); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Retrieve information about corpus statistic |
| 97 | * |
| 98 | * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22 |
| 99 | * |
| 100 | * cq = corpus query (formerly collectionQuery) |
| 101 | * |
| 102 | * Adress the MOJO-Endpoint for example with |
| 103 | * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22 |
| 104 | */ |
| 105 | KorAP.API.getCorpStat = function (cq, cb){ |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 106 | let url = KorAP.URL + "/corpus?cq=" + encodeURIComponent(cq); |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 107 | KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq); |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 108 | }; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 109 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 110 | |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 111 | /** |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 112 | * General method to retrieve JSON information |
| 113 | */ |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 114 | KorAP.API.getJSON = function (url, onload, title) { |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 115 | const req = new XMLHttpRequest(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 116 | req.open("GET", url, true); |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 117 | |
| 118 | // Dispatch global "window" event |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 119 | const reqE = new CustomEvent('korapRequest', { |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 120 | bubbles : false, |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 121 | detail: { |
| 122 | "url" : url, |
| 123 | "title" : title |
| 124 | } |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 125 | }); |
| 126 | window.dispatchEvent(reqE); |
| 127 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 128 | req.setRequestHeader("Accept", "application/json"); |
| 129 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 130 | req.onreadystatechange = function () { |
| 131 | /* |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 132 | States: |
| 133 | 0 - unsent (prior to open) |
| 134 | 1 - opened (prior to send) |
| 135 | 2 - headers received |
| 136 | 3 - loading (responseText has partial data) |
| 137 | 4 - done |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 138 | */ |
| 139 | if (this.readyState == 4) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 140 | |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 141 | let json; |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 142 | try { |
| 143 | json = JSON.parse(this.responseText); |
| 144 | } |
| 145 | catch (e) { |
| 146 | KorAP.log(0, e); |
| 147 | console.log(e); |
| 148 | onload(undefined); |
| 149 | return; |
| 150 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 151 | |
Akron | 962fff9 | 2020-10-12 14:25:49 +0200 | [diff] [blame] | 152 | if (json !== undefined && json["errors"] !== undefined) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 153 | json["errors"].forEach( |
| 154 | e => KorAP.log(e[0], e[1] || "Unknown") |
| 155 | ); |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 156 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 157 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 158 | else if (this.status !== 200) { |
| 159 | KorAP.log(this.status, this.statusText); |
| 160 | }; |
| 161 | |
| 162 | if (this.status === 200) { |
| 163 | onload(json); |
| 164 | } |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 165 | |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 166 | else { |
| 167 | onload(undefined); |
| 168 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 169 | } |
| 170 | }; |
Akron | bf713fc | 2020-10-13 10:44:35 +0200 | [diff] [blame^] | 171 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 172 | req.ontimeout = function () { |
| 173 | KorAP.log(0, 'Request Timeout'); |
| 174 | }; |
| 175 | req.send(); |
| 176 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 177 | }); |