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