Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | define(['util'], function () { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 2 | // 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 | |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 9 | KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : ''; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 10 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 11 | KorAP.API = KorAP.API || {}; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 12 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 13 | /** |
| 14 | * Retrieve information about a match |
| 15 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 16 | KorAP.API.getMatchInfo = function (match, param, cb) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 17 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 18 | // match is a KorAP.Match object |
| 19 | var url = KorAP.URL; |
| 20 | url += '/corpus'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 21 | /* |
| 22 | url += '/' + match.corpusID; |
| 23 | url += '/' + match.docID; |
| 24 | url += '/' + match.textID; |
| 25 | */ |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 26 | |
Akron | 311ca65 | 2017-06-26 20:39:56 +0200 | [diff] [blame] | 27 | var legacySigle = new RegExp('^([^_]+)_([^\\.]+)\\.(.+?)$'); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 28 | |
| 29 | // This is for legacy support |
| 30 | var legacy = legacySigle.exec(match.textSigle); |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 31 | var docFragment = ""; |
Akron | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 32 | if (legacy !== null && legacy[0]) { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 33 | docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3]; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 34 | } |
| 35 | else { |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 36 | docFragment = match.textSigle; |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 37 | } |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 38 | |
| 39 | docFragment += '/' + match.matchID; |
| 40 | url += '/' + docFragment; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 41 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 42 | // { spans: true, layer:x, foundry : y} |
| 43 | if (param['spans'] == true) { |
| 44 | url += '?spans=true'; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 45 | docFragment += ' +spans '; |
| 46 | if (param['foundry'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 47 | url += '&foundry=' + param['foundry']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 48 | docFragment += param['foundry']; |
| 49 | }; |
| 50 | if (param['layer'] !== undefined) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 51 | url += '&layer=' + param['layer']; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 52 | docFragment += '/'+param['layer']; |
| 53 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 54 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 55 | |
| 56 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 57 | else { |
| 58 | // TODO |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 59 | docFragment += ' -spans'; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 60 | url += '?spans=false'; |
| 61 | } |
| 62 | |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 63 | KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 64 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 65 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 66 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 67 | /** |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 68 | * Retrieve information about a document. |
| 69 | */ |
| 70 | KorAP.API.getTextInfo = function (doc, param, cb) { |
| 71 | |
| 72 | // doc is a KorAP.Match object |
| 73 | var url = KorAP.URL; |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 74 | url += '/corpus'; |
| 75 | url += '/' + 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 | 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 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 94 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 95 | /** |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 96 | * Retrieve information about corpus statistic |
| 97 | * |
| 98 | * Development mode: |
| 99 | * URL = http://localhost:8089/api |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 100 | * |
| 101 | * collectionQuery has be changed to corpusQuery |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 102 | */ |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 103 | /* KorAP.API.getCorpStat = function (collQu, cb){ |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 104 | //development mode: |
| 105 | var url = "http://localhost:8089/api/"; |
| 106 | url = url + "statistics"; |
Akron | 5fa61e9 | 2019-07-15 11:56:11 +0200 | [diff] [blame^] | 107 | url = url + "?cq="; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 108 | url = url + collQu; |
| 109 | KorAP.API.getJSON(url, cb); |
| 110 | };*/ |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 111 | |
| 112 | |
| 113 | /** |
| 114 | * Retrieve information about corpus statistic |
| 115 | * |
| 116 | * Example URL: /corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22 |
| 117 | * |
| 118 | * cq = corpus query (formerly collectionQuery) |
| 119 | * |
| 120 | * Adress the MOJO-Endpoint for example with |
| 121 | * http://localhost:3000/corpus?cq=availability+%3D+%2FCC-BY.*%2F+%26+textClass+%3D+%22kultur%22 |
| 122 | */ |
| 123 | KorAP.API.getCorpStat = function (cq, cb){ |
Akron | 8f0de5e | 2018-08-27 16:31:12 +0200 | [diff] [blame] | 124 | var url = KorAP.URL; |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 125 | url += "/corpus?cq=" + encodeURIComponent(cq); |
| 126 | KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq); |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame] | 127 | }; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 130 | * General method to retrieve JSON information |
| 131 | */ |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 132 | KorAP.API.getJSON = function (url, onload, title) { |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 133 | var req = new XMLHttpRequest(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 134 | req.open("GET", url, true); |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 135 | |
| 136 | // Dispatch global "window" event |
| 137 | var reqE = new CustomEvent('korapRequest', { |
| 138 | bubbles : false, |
Akron | b89863a | 2018-11-13 16:43:59 +0100 | [diff] [blame] | 139 | detail: { |
| 140 | "url" : url, |
| 141 | "title" : title |
| 142 | } |
Akron | 4c33c62 | 2018-11-12 13:43:27 +0100 | [diff] [blame] | 143 | }); |
| 144 | window.dispatchEvent(reqE); |
| 145 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 146 | req.setRequestHeader("Accept", "application/json"); |
| 147 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 148 | req.onreadystatechange = function () { |
| 149 | /* |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 150 | States: |
| 151 | 0 - unsent (prior to open) |
| 152 | 1 - opened (prior to send) |
| 153 | 2 - headers received |
| 154 | 3 - loading (responseText has partial data) |
| 155 | 4 - done |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 156 | */ |
| 157 | if (this.readyState == 4) { |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 158 | |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 159 | var json; |
| 160 | try { |
| 161 | json = JSON.parse(this.responseText); |
| 162 | } |
| 163 | catch (e) { |
| 164 | KorAP.log(0, e); |
| 165 | console.log(e); |
| 166 | onload(undefined); |
| 167 | return; |
| 168 | }; |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 169 | if (json !== null && json["errors"] !== null) { |
| 170 | for (var i in json["errors"]) { |
| 171 | KorAP.log(json["errors"][i][0], json["errors"][i][1] || "Unknown"); |
| 172 | }; |
| 173 | } |
| 174 | else if (this.status !== 200) { |
| 175 | KorAP.log(this.status, this.statusText); |
| 176 | }; |
| 177 | |
| 178 | if (this.status === 200) { |
| 179 | onload(json); |
| 180 | } |
| 181 | else { |
| 182 | onload(undefined); |
| 183 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | req.ontimeout = function () { |
| 187 | KorAP.log(0, 'Request Timeout'); |
| 188 | }; |
| 189 | req.send(); |
| 190 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 191 | }); |