blob: 4a40ea5f9b9012d7bb68b50caa3777d84d7ba455 [file] [log] [blame]
hebastaa303a792018-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 };
hebasta0f0fd332018-06-29 10:11:15 +020045
46 //create HTML Description List Element
47 var statDL = document.createElement('dl');
48 var statistic = this._statistic;
hebastaa303a792018-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
hebasta3510e792018-06-20 15:31:43 +020065
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 }
hebasta0f0fd332018-06-29 10:11:15 +020084
85 //catches notifications
86 if(statResponse["notifications"] !== null && statResponse["notifications"] !== undefined ){
87 notif = statResponse["notifications"];
88 KorAP.log(0, notif[0][1]);
89 return;
90 }
91
hebasta3510e792018-06-20 15:31:43 +020092 cb(statResponse);
93 });
94 }
95
96 catch(e){
97 KorAP.log(0, e);
98 cb(null);
99 }
100
101 },
102
103
104 /**
105 * Shows corpus statistic
106 */
107 showCorpStat : function(appendEl, vc){
108
109 /*
110 * If corpus statistic is already visible,
111 * there is no need to show the statistic again.
112 */
113 if(this._visibleStat)
114 return;
115
116 var statTable = document.createElement('div');
117 statTable.classList.add('stattable', 'loading');
118 appendEl.appendChild(statTable);
119
120 var that = this;
121
122
123 /*
124 * Get corpus statistic, remove "loading"-image and
125 * append result to statTable
126 */
127 this.getStatistic(vc, function (statistic) {
128
129 if (statistic === null)
130 return;
131
132 statTable.classList.remove('loading');
133 statisticobj = that.create(statistic);
hebasta3510e792018-06-20 15:31:43 +0200134
135 statTable.appendChild(statisticobj.element());
136
137 // Add Close Button
138 var actions = document.createElement('ul');
139 actions.classList.add('action', 'image');
140 var b = actions.addE('li');
141 b.className = 'close';
142 b.addE('span').addT('close');
143 statTable.appendChild(actions);
144 b.addEventListener('click', function (e){
145 statTable.parentNode.removeChild(statTable);
146 that._visibleStat = false;
hebasta3510e792018-06-20 15:31:43 +0200147 e.halt();
148 });
149 });
150
151 //corpus statistic is displayed
152 this._visibleStat = true;
153 // Do not load any longer
154 statTable.classList.remove('loading');
155 },
156
hebastaa303a792018-06-19 17:09:08 +0200157 }
158
159});