hebasta | 6965f86 | 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 | b94dca3 | 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 |
hebasta | f3f91a7 | 2018-06-25 10:31:58 +0200 | [diff] [blame^] | 128 | if(document.getElementById('dCorpStat') !== null){ |
| 129 | var divStatButton = document.getElementById('dCorpStat'); |
| 130 | divStatButton.parentNode.removeChild(divStatButton); |
| 131 | } |
hebasta | b94dca3 | 2018-06-20 15:31:43 +0200 | [diff] [blame] | 132 | |
| 133 | statTable.appendChild(statisticobj.element()); |
| 134 | |
| 135 | // Add Close Button |
| 136 | var actions = document.createElement('ul'); |
| 137 | actions.classList.add('action', 'image'); |
| 138 | var b = actions.addE('li'); |
| 139 | b.className = 'close'; |
| 140 | b.addE('span').addT('close'); |
| 141 | statTable.appendChild(actions); |
| 142 | b.addEventListener('click', function (e){ |
| 143 | statTable.parentNode.removeChild(statTable); |
| 144 | that._visibleStat = false; |
| 145 | vc.addStatBut(); |
| 146 | e.halt(); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | //corpus statistic is displayed |
| 151 | this._visibleStat = true; |
| 152 | // Do not load any longer |
| 153 | statTable.classList.remove('loading'); |
| 154 | }, |
| 155 | |
hebasta | 6965f86 | 2018-06-19 17:09:08 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | }); |