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 | |
Nils Diewald | 29f25ee | 2015-05-08 00:51:47 +0000 | [diff] [blame] | 9 | KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : 'http://korap.ids-mannheim.de/kalamar'; |
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 || {}; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [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 | 7f613e0 | 2016-11-07 02:50:44 +0100 | [diff] [blame] | 31 | if (legacy !== null && legacy[0]) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 32 | url += '/' + legacy[1] + '/' + legacy[2] + '/' + legacy[3]; |
| 33 | } |
| 34 | else { |
| 35 | url += '/' + match.textSigle; |
| 36 | } |
| 37 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 38 | url += '/' + match.matchID; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 39 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 40 | // { spans: true, layer:x, foundry : y} |
| 41 | if (param['spans'] == true) { |
| 42 | url += '?spans=true'; |
| 43 | if (param['foundry'] !== undefined) |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 44 | url += '&foundry=' + param['foundry']; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 45 | if (param['layer'] !== undefined) |
Akron | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 46 | url += '&layer=' + param['layer']; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 47 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 48 | |
| 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 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 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 58 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 59 | /** |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 60 | * 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; |
Akron | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 66 | url += '/corpus'; |
| 67 | url += '/' + doc.textSigle; |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 68 | |
| 69 | if (param['fields'] !== undefined) { |
| 70 | url += '?fields='; // TODO! |
| 71 | } |
| 72 | else { |
Akron | 4bbd8b3 | 2018-03-06 19:19:44 +0100 | [diff] [blame] | 73 | url += '?fields=@all'; // TODO: Maybe '*'? |
Akron | 0ad7cd2 | 2018-02-08 18:03:06 +0100 | [diff] [blame] | 74 | } |
| 75 | KorAP.API.getJSON(url, cb); |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | /** |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 80 | * Retrieve information about collections |
| 81 | */ |
| 82 | KorAP.API.getCollections = function (cb) { |
| 83 | KorAP.API.getJSON(KorAP.URL + '/collection', cb); |
| 84 | }; |
| 85 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 86 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 87 | /** |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 88 | * Retrieve information about corpus statistic |
| 89 | * |
| 90 | * Development mode: |
| 91 | * URL = http://localhost:8089/api |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame^] | 92 | * |
| 93 | * collectionQuery has be changed to corpusQuery |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 94 | */ |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame^] | 95 | /* KorAP.API.getCorpStat = function (collQu, cb){ |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 96 | //development mode: |
| 97 | var url = "http://localhost:8089/api/"; |
| 98 | url = url + "statistics"; |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame^] | 99 | url = url + "?corpusQuery="; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 100 | url = url + collQu; |
| 101 | KorAP.API.getJSON(url, cb); |
| 102 | };*/ |
hebasta | 0ee5080 | 2018-06-20 10:24:45 +0200 | [diff] [blame^] | 103 | |
| 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){ |
| 116 | var url = "/corpus"; |
| 117 | url = url + "?cq="; |
| 118 | url = url + cq; |
| 119 | KorAP.API.getJSON(url, cb); |
| 120 | }; |
hebasta | 4ba496a | 2018-06-05 15:56:01 +0200 | [diff] [blame] | 121 | |
| 122 | /** |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 123 | * General method to retrieve JSON information |
| 124 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 125 | KorAP.API.getJSON = function (url, onload) { |
| 126 | var req = new XMLHttpRequest(); |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 127 | req.open("GET", url, true); |
| 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 | b5d05d7 | 2018-02-12 15:09:12 +0100 | [diff] [blame] | 141 | var json; |
| 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 | 515851a | 2017-05-02 12:53:17 +0200 | [diff] [blame] | 151 | if (json !== null && json["errors"] !== null) { |
| 152 | for (var i in json["errors"]) { |
| 153 | KorAP.log(json["errors"][i][0], json["errors"][i][1] || "Unknown"); |
| 154 | }; |
| 155 | } |
| 156 | else if (this.status !== 200) { |
| 157 | KorAP.log(this.status, this.statusText); |
| 158 | }; |
| 159 | |
| 160 | if (this.status === 200) { |
| 161 | onload(json); |
| 162 | } |
| 163 | else { |
| 164 | onload(undefined); |
| 165 | }; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 166 | } |
| 167 | }; |
| 168 | req.ontimeout = function () { |
| 169 | KorAP.log(0, 'Request Timeout'); |
| 170 | }; |
| 171 | req.send(); |
| 172 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 173 | }); |