Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 1 | var KorAP = KorAP || {}; |
| 2 | |
| 3 | (function (KorAP) { |
| 4 | "use strict"; |
| 5 | |
| 6 | // Default log message |
| 7 | KorAP.log = KorAP.log || function (type, msg) { |
| 8 | console.log(type + ": " + msg); |
| 9 | }; |
| 10 | |
| 11 | KorAP.URL = KorAP.URL || 'http://korap.ids-mannheim.de/kalamar'; |
| 12 | |
| 13 | // TODO: https://github.com/honza/140medley/blob/master/140medley.js |
| 14 | // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest |
| 15 | // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest |
| 16 | // r.addEventListener("progress", updateProgress, false); |
| 17 | // http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml |
| 18 | // http://stackoverflow.com/questions/6112744/load-javascript-on-demand |
| 19 | |
| 20 | KorAP.API = { |
| 21 | getMatchInfo : function (match, param, cb) { |
| 22 | // match is a KorAP.Match object |
| 23 | |
| 24 | var url = KorAP.URL; |
| 25 | url += '/corpus'; |
| 26 | url += '/' + match.corpusID; |
| 27 | url += '/' + match.docID + '.' + match.textID; // TODO |
| 28 | url += '/' + match.matchID; |
| 29 | |
| 30 | // { spans: true, layer:x, foundry : y} |
| 31 | if (param['spans'] == true) { |
| 32 | url += '?spans=true'; |
| 33 | if (param['foundry'] !== undefined) |
| 34 | url += '&foundry=' + param['foundry']; |
| 35 | if (param['layer'] !== undefined) |
| 36 | url += '&layer=' + param['layer']; |
| 37 | } |
| 38 | |
| 39 | // { spans : false, layer: [Array of KorAP.InfoLayer] } |
| 40 | else { |
| 41 | // TODO |
| 42 | url += '?spans=false'; |
| 43 | } |
| 44 | |
| 45 | this.getJSON(url, cb); |
| 46 | }, |
| 47 | getJSON : function (url, onload) { |
| 48 | var req = new XMLHttpRequest(); |
| 49 | |
| 50 | console.log('Request url: ' + url); |
| 51 | |
| 52 | req.open("GET", url, true); |
| 53 | |
| 54 | |
| 55 | req.setRequestHeader("Accept", "application/json"); |
| 56 | req.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 57 | req.onreadystatechange = function () { |
| 58 | /* |
| 59 | States: |
| 60 | 0 - unsent (prior to open) |
| 61 | 1 - opened (prior to send) |
| 62 | 2 - headers received |
| 63 | 3 - loading (responseText has partial data) |
| 64 | 4 - done |
| 65 | */ |
| 66 | if (this.readyState == 4) { |
| 67 | if (this.status === 200) |
| 68 | onload(JSON.parse(this.responseText)); |
| 69 | else |
| 70 | KorAP.log(this.status, this.statusText); |
| 71 | } |
| 72 | }; |
| 73 | req.ontimeout = function () { |
| 74 | KorAP.log(0, 'Request Timeout'); |
| 75 | }; |
| 76 | req.send(); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | }(this.KorAP)); |