hebasta | 10e0c21 | 2018-06-19 17:09:08 +0200 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Creates and displays corpus statistic |
| 4 | * |
| 5 | * @author Helge Stallkamp |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | define(['util'], function (){ |
| 11 | |
| 12 | return{ |
| 13 | |
| 14 | /** |
| 15 | * Create new statistic object |
| 16 | */ |
| 17 | create: function(statistic){ |
| 18 | return Object.create(this)._init(statistic); |
| 19 | }, |
| 20 | |
| 21 | /** |
| 22 | * Initialize statistic object |
| 23 | */ |
| 24 | _init: function(statistic){ |
| 25 | if(statistic === undefined){ |
| 26 | throw new Error("Missing parameter"); |
| 27 | } |
| 28 | else{ |
| 29 | this._statistic = statistic; |
| 30 | this._visibleStat = false; |
| 31 | return this; |
| 32 | } |
| 33 | }, |
| 34 | |
| 35 | /** |
| 36 | * Display statistic object |
| 37 | * as HTML Description List Element |
| 38 | */ |
| 39 | element : function(){ |
| 40 | |
| 41 | //if this._element already exists return without doing something |
| 42 | if (this._element !== undefined) { |
| 43 | return this._element; |
| 44 | }; |
| 45 | |
| 46 | //create HTML Description List Element |
| 47 | var statDL = document.createElement('dl'); |
| 48 | var statistic = this._statistic; |
| 49 | |
| 50 | var keys = Object.keys(statistic); |
| 51 | for(i = 0; i < keys.length; i++){ |
| 52 | statSp = statDL.addE('div') |
| 53 | statDT = statSp.addE('dt'); |
| 54 | var k = keys[i]; |
| 55 | statDT.addT(k); |
| 56 | statDT.setAttribute('title' , k); |
| 57 | statDD = statSp.addE('dd'); |
| 58 | statDD.addT(statistic[k]); |
| 59 | } |
| 60 | |
| 61 | this._element = statDL; |
| 62 | return this._element; |
| 63 | }, |
| 64 | |
hebasta | 86f0365 | 2018-06-20 15:31:43 +0200 | [diff] [blame^] | 65 | |
| 66 | /** |
| 67 | * |
| 68 | * Receive Corpus statistic from the server |
| 69 | */ |
| 70 | getStatistic : function(vc, cb){ |
| 71 | //cq = corpusQuery |
| 72 | var cq = encodeURI(vc.toQuery()); |
| 73 | |
| 74 | try{ |
| 75 | KorAP.API.getCorpStat(cq, function(statResponse){ |
| 76 | if(statResponse === null){ |
| 77 | cb(null); |
| 78 | return; |
| 79 | } |
| 80 | if(statResponse === undefined){ |
| 81 | cb(null); |
| 82 | return; |
| 83 | } |
| 84 | cb(statResponse); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | catch(e){ |
| 89 | KorAP.log(0, e); |
| 90 | cb(null); |
| 91 | } |
| 92 | |
| 93 | }, |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Shows corpus statistic |
| 98 | */ |
| 99 | showCorpStat : function(appendEl, vc){ |
| 100 | |
| 101 | /* |
| 102 | * If corpus statistic is already visible, |
| 103 | * there is no need to show the statistic again. |
| 104 | */ |
| 105 | if(this._visibleStat) |
| 106 | return; |
| 107 | |
| 108 | var statTable = document.createElement('div'); |
| 109 | statTable.classList.add('stattable', 'loading'); |
| 110 | appendEl.appendChild(statTable); |
| 111 | |
| 112 | var that = this; |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * Get corpus statistic, remove "loading"-image and |
| 117 | * append result to statTable |
| 118 | */ |
| 119 | this.getStatistic(vc, function (statistic) { |
| 120 | |
| 121 | if (statistic === null) |
| 122 | return; |
| 123 | |
| 124 | statTable.classList.remove('loading'); |
| 125 | statisticobj = that.create(statistic); |
| 126 | |
| 127 | //Removes statistic button when statistic is displayed |
| 128 | var divStatButton = document.getElementById('dCorpStat'); |
| 129 | divStatButton.parentNode.removeChild(divStatButton); |
| 130 | |
| 131 | statTable.appendChild(statisticobj.element()); |
| 132 | |
| 133 | // Add Close Button |
| 134 | var actions = document.createElement('ul'); |
| 135 | actions.classList.add('action', 'image'); |
| 136 | var b = actions.addE('li'); |
| 137 | b.className = 'close'; |
| 138 | b.addE('span').addT('close'); |
| 139 | statTable.appendChild(actions); |
| 140 | b.addEventListener('click', function (e){ |
| 141 | statTable.parentNode.removeChild(statTable); |
| 142 | that._visibleStat = false; |
| 143 | vc.addStatBut(); |
| 144 | e.halt(); |
| 145 | }); |
| 146 | }); |
| 147 | |
| 148 | //corpus statistic is displayed |
| 149 | this._visibleStat = true; |
| 150 | // Do not load any longer |
| 151 | statTable.classList.remove('loading'); |
| 152 | }, |
| 153 | |
hebasta | 10e0c21 | 2018-06-19 17:09:08 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | }); |