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 | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 9 | KorAP.URL = KorAP.URL || 'http://korap.ids-mannheim.de/kalamar'; |
| 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 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 13 | KorAP.API.getMatchInfo = function (match, param, cb) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 14 | |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 15 | // match is a KorAP.Match object |
| 16 | var url = KorAP.URL; |
| 17 | url += '/corpus'; |
| 18 | url += '/' + match.corpusID; |
| 19 | url += '/' + match.docID; |
| 20 | url += '/' + match.textID; |
| 21 | url += '/' + match.matchID; |
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 | // { spans: true, layer:x, foundry : y} |
| 24 | if (param['spans'] == true) { |
| 25 | url += '?spans=true'; |
| 26 | if (param['foundry'] !== undefined) |
| 27 | url += '&foundry=' + param['foundry']; |
| 28 | if (param['layer'] !== undefined) |
| 29 | url += '&layer=' + param['layer']; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 30 | } |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 31 | |
| 32 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 33 | else { |
| 34 | // TODO |
| 35 | url += '?spans=false'; |
| 36 | } |
| 37 | |
| 38 | KorAP.API.getJSON(url, cb); |
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 | |
| 41 | KorAP.API.getJSON = function (url, onload) { |
| 42 | var req = new XMLHttpRequest(); |
| 43 | |
| 44 | req.open("GET", url, true); |
| 45 | req.setRequestHeader("Accept", "application/json"); |
| 46 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 47 | req.onreadystatechange = function () { |
| 48 | /* |
| 49 | States: |
| 50 | 0 - unsent (prior to open) |
| 51 | 1 - opened (prior to send) |
| 52 | 2 - headers received |
| 53 | 3 - loading (responseText has partial data) |
| 54 | 4 - done |
| 55 | */ |
| 56 | if (this.readyState == 4) { |
| 57 | if (this.status === 200) |
| 58 | onload(JSON.parse(this.responseText)); |
| 59 | else |
| 60 | KorAP.log(this.status, this.statusText); |
| 61 | } |
| 62 | }; |
| 63 | req.ontimeout = function () { |
| 64 | KorAP.log(0, 'Request Timeout'); |
| 65 | }; |
| 66 | req.send(); |
| 67 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 68 | }); |