blob: 5407e8f1a4d5374e3e85f83133c80111ca66fd74 [file] [log] [blame]
hebasta10e0c212018-06-19 17:09:08 +02001/**
2 *
3 * Creates and displays corpus statistic
4 *
5 * @author Helge Stallkamp
6 *
7 */
8
9
10define(['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 };
hebasta4c92eec2018-06-29 10:11:15 +020045
46 //create HTML Description List Element
47 var statDL = document.createElement('dl');
48 var statistic = this._statistic;
hebasta10e0c212018-06-19 17:09:08 +020049
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
hebasta86f03652018-06-20 15:31:43 +020065
66 /**
67 *
68 * Receive Corpus statistic from the server
69 */
70 getStatistic : function(vc, cb){
71 //cq = corpusQuery
hebastacf5f9b62018-06-29 14:27:50 +020072
73 //console.log("HK ld-Type corpus = " + vc.getLdType());
74
75 var cq;
76
77 cq = encodeURI(vc.toQuery());
hebasta86f03652018-06-20 15:31:43 +020078
79 try{
80 KorAP.API.getCorpStat(cq, function(statResponse){
81 if(statResponse === null){
82 cb(null);
83 return;
84 }
85 if(statResponse === undefined){
86 cb(null);
87 return;
88 }
hebasta4c92eec2018-06-29 10:11:15 +020089
90 //catches notifications
91 if(statResponse["notifications"] !== null && statResponse["notifications"] !== undefined ){
92 notif = statResponse["notifications"];
93 KorAP.log(0, notif[0][1]);
hebastacf5f9b62018-06-29 14:27:50 +020094 cb(null);
hebasta4c92eec2018-06-29 10:11:15 +020095 return;
96 }
97
hebasta86f03652018-06-20 15:31:43 +020098 cb(statResponse);
99 });
100 }
101
102 catch(e){
103 KorAP.log(0, e);
104 cb(null);
105 }
106
107 },
108
109
110 /**
111 * Shows corpus statistic
112 */
113 showCorpStat : function(appendEl, vc){
114
115 /*
116 * If corpus statistic is already visible,
117 * there is no need to show the statistic again.
118 */
119 if(this._visibleStat)
120 return;
121
122 var statTable = document.createElement('div');
123 statTable.classList.add('stattable', 'loading');
124 appendEl.appendChild(statTable);
125
126 var that = this;
127
128
129 /*
130 * Get corpus statistic, remove "loading"-image and
131 * append result to statTable
132 */
133 this.getStatistic(vc, function (statistic) {
hebastacf5f9b62018-06-29 14:27:50 +0200134
135 statTable.classList.remove('loading');
136 statisticobj = that.create(statistic);
hebasta86f03652018-06-20 15:31:43 +0200137
138 if (statistic === null)
139 return;
hebastacf5f9b62018-06-29 14:27:50 +0200140
hebasta86f03652018-06-20 15:31:43 +0200141 statTable.appendChild(statisticobj.element());
142
143 // Add Close Button
144 var actions = document.createElement('ul');
145 actions.classList.add('action', 'image');
146 var b = actions.addE('li');
147 b.className = 'close';
148 b.addE('span').addT('close');
149 statTable.appendChild(actions);
150 b.addEventListener('click', function (e){
151 statTable.parentNode.removeChild(statTable);
152 that._visibleStat = false;
hebasta86f03652018-06-20 15:31:43 +0200153 e.halt();
154 });
155 });
156
157 //corpus statistic is displayed
158 this._visibleStat = true;
159 // Do not load any longer
160 statTable.classList.remove('loading');
161 },
162
hebasta10e0c212018-06-19 17:09:08 +0200163 }
164
165});