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 | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame^] | 21 | /* |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 22 | url += '/' + match.corpusID; |
| 23 | url += '/' + match.docID; |
| 24 | url += '/' + match.textID; |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame^] | 25 | */ |
| 26 | url += '/' + match.textSigle; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 27 | url += '/' + match.matchID; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 28 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 29 | // { spans: true, layer:x, foundry : y} |
| 30 | if (param['spans'] == true) { |
| 31 | url += '?spans=true'; |
| 32 | if (param['foundry'] !== undefined) |
| 33 | url += '&foundry=' + param['foundry']; |
| 34 | if (param['layer'] !== undefined) |
| 35 | url += '&layer=' + param['layer']; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 36 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 37 | |
| 38 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 39 | else { |
| 40 | // TODO |
| 41 | url += '?spans=false'; |
| 42 | } |
| 43 | |
| 44 | KorAP.API.getJSON(url, cb); |
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 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 47 | /** |
| 48 | * Retrieve information about collections |
| 49 | */ |
| 50 | KorAP.API.getCollections = function (cb) { |
| 51 | KorAP.API.getJSON(KorAP.URL + '/collection', cb); |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * General method to retrieve JSON information |
| 56 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 57 | KorAP.API.getJSON = function (url, onload) { |
| 58 | var req = new XMLHttpRequest(); |
| 59 | |
| 60 | req.open("GET", url, true); |
| 61 | req.setRequestHeader("Accept", "application/json"); |
| 62 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 63 | req.onreadystatechange = function () { |
| 64 | /* |
| 65 | States: |
| 66 | 0 - unsent (prior to open) |
| 67 | 1 - opened (prior to send) |
| 68 | 2 - headers received |
| 69 | 3 - loading (responseText has partial data) |
| 70 | 4 - done |
| 71 | */ |
| 72 | if (this.readyState == 4) { |
Akron | 9f64fd0 | 2016-02-13 05:51:38 +0100 | [diff] [blame] | 73 | if (this.status === 200) { |
| 74 | var json = JSON.parse(this.responseText); |
| 75 | if (json["errors"] !== null) { |
| 76 | for (var i in json["errors"]) { |
| 77 | KorAP.log(json["errors"][i][0], json["errors"][i][1]); |
| 78 | }; |
| 79 | }; |
| 80 | onload(json); |
| 81 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 82 | else |
| 83 | KorAP.log(this.status, this.statusText); |
| 84 | } |
| 85 | }; |
| 86 | req.ontimeout = function () { |
| 87 | KorAP.log(0, 'Request Timeout'); |
| 88 | }; |
| 89 | req.send(); |
| 90 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | }); |