This repository has been archived by the owner on Oct 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsc.api.js
333 lines (242 loc) · 10.5 KB
/
tsc.api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Service
var api = angular
.module('tsc')
.service('tscApi', tscApi);
// service implementation
tscApi.$inject = ['$http','$q', '$filter','$sce'];
function tscApi ($http, $q, $filter,$sce) {
console.log('tscApi instanciated.');
var mainFile = {};
var imgurl = 'https://causa.tagesspiegel.de';
var jsonFile = 'data/all_debates.json';
// load the data
getMainFile();
function getMainFile(){
console.log("getMainFile called");
return $q(function(resolve, reject){
$http.get(jsonFile)
.then(function success(response){
console.log('mainFile read ' + jsonFile + ' done');
mainFile = response.data;
resolve(mainFile);
}, function fail(response){
console.log('mainFile error ' + JSON.stringify(response));
reject(response);
});
});
}
function filterListToArray(objlist, key, value){
var array = [];
for(var item in objlist){
// skip loop if the property is from prototype
if(!objlist.hasOwnProperty(item)) continue;
//console.log('filterListToArray for ' + key + ':' + value + '@' + JSON.stringify(item));
if (objlist[item][key] === value){
var o = objlist[item];
o["id"] = item;
array.push(o);
}
}
return array;
}
this.getRandomArticle = function _randomArticle(){
return $q(function(resolve, reject){
console.log("getRandomArticle");
do {
setTimeout(function(){
var randomId = Object.keys(mainFile.articles)[Math.floor(Math.random() * Object.keys(mainFile.articles).length)];
console.log("articles length " + Object.keys(mainFile.articles).length + " > random id " + randomId );
resolve(mainFile.articles[randomId]);
}, 1000);
} while ( Object.keys(mainFile).lenght === 0 );
});
};
// we assume that this is called first all time!
this.getTopics = function _topics() {
console.log("tscApi.getTopics");
return $q(function(resolve, reject){
do {
setTimeout(function(){
resolve(mainFile.topics);
}, 1000);
} while ( Object.keys(mainFile).lenght === 0 );
});
};
this.getDebates = function _debates(topicId) {
console.log("tscApi.getDebates for " + topicId + " (undefined means all)");
return $q(function(resolve, reject){
do {
setTimeout(function(){
var da = mainFile.debates;
for (var d in da){
da[d]["topic_title"] = mainFile.topics[da[d].topic]["title"];
//console.log(JSON.stringify(da[d]));
}
if (topicId === undefined){
console.log("tscApi.getDebates all");
resolve(da);
} else {
console.log("tscApi.getDebates for topic: " + topicId);
resolve(filterListToArray(da, "topic", topicId));
}
}, 1000);
} while ( Object.keys(mainFile).lenght === 0 );
});
};
/* not used - was useful previously
this.getDebate = function _debate(debateId) {
console.log("tscApi.getDebate for " + debateId);
return $q(function(resolve, reject){
resolve(this.getDebates()[debateId]);
});
};*/
this.getArticlesOfDebate = function _articlesOfDebate(debateId) {
console.log("tscApi.getArticlesOfDebate");
return $q(function(resolve, reject){
var vA = filterListToArray(mainFile.articles, "debate", debateId);
/* replace data-id by id for anchor scrolling and add mouse over
- be aware that a modified ngSanitize is used, bcz not all attributes are allowed and filtered by default
- id and ng.mouseenter was manually added to angular-sanitize.min.js
there should be a better way by avoiding embedded html
*/
for(var k in vA){
vA[k].text = vA[k].text.replace(/data-id/gi, "id");
vA[k].text = vA[k].text.replace(/class=\"/gi, "ng-click=vm.paintArticleIndicator($event) ng-mouseenter=vm.paintArticleIndicator($event) class=\"anchor ");
vA[k]["text_trusted"] = $sce.trustAsHtml(vA[k].text);
vA[k]["theses"] = filterListToArray(mainFile.articles_theses, "article", k);
}
resolve(vA);
});
};
/*
{
bubbles:[[count,sum],[count,sum],[count,sum] ..],
theses: [{topic:"", author:"", pro:"", con:"", arrayIndex: 0}]
}
*/
this.getVotesDataOfDebate = function _voteDataOfDebate(debateId){
console.log("tscApi.getVotesDataOfDebate");
return $q(function(resolve, reject){
var votes = [];
var bubbles = [];
var theses = {};
// 1. get all votes of debate
var votesOfDebate = filterListToArray(mainFile.votes, "debate", debateId);
//console.log(JSON.stringify(votesOfDebate));
// 2. get unique id list of all thesis
var listOfTheses = [];
for(var thesis in votesOfDebate) {
if (listOfTheses.indexOf(votesOfDebate[thesis].thesis)<0){
//console.log("tscApi.getVotesDataOfDebate.listOfTheses new " + votesOfDebate[thesis]["thesis"]);
listOfTheses.push(votesOfDebate[thesis].thesis);
}
}
// 3. build the bubble array and build display info in same sequence
for(var t in listOfTheses){
var v = filterListToArray(votesOfDebate, "thesis", listOfTheses[t]);
var sum = 0;
var pro = 0;
var con = 0;
var neutral = 0;
var proAuthors = [];
var conAuthors = [];
for(var v2 in v){
var vote = parseInt(v[v2]["vote"])
sum = sum+ vote;
switch(vote) {
case 0:
neutral=neutral+1;
break;
case -1:
con=con+1;
conAuthors.push(mainFile.authors[v[v2]["author"]]);
break;
case 1:
pro=pro+1;
proAuthors.push(mainFile.authors[v[v2]["author"]]);
break;
default:
console.log(vote);
}
}
bubbles.push([v.length, sum]);
var thesis = mainFile.theses[listOfTheses[t]];
thesis['author'] = mainFile.authors[thesis['created_by']];
thesis['arrayIndex'] = t;
thesis['vote_pro'] = pro;
thesis['vote_pro_authors'] = proAuthors;
thesis['vote_con'] = con;
thesis['vote_con_authors'] = conAuthors;
thesis['vote_neutral'] = neutral;
thesis['vote_sum'] = sum;
thesis['key'] = listOfTheses[t];
var obj = {};
//obj[listOfTheses[t]] = thesis;
//theses.push( obj );
theses[listOfTheses[t]] = thesis;
}
votes['bubbles'] = bubbles;
votes['theses'] = theses;
//console.log(votes);
//console.log(bubbles);
//console.log(theses);
resolve(votes);
});
};
/* this.getArticlesOfAuthor = function _articlesOfAuthor(authorId) {
console.log("tscApi.getArticles");
return $q(function(resolve, reject){
var articles = filterListToArray(mainFile.articles, "author", authorId);
resolve(articles);
});
};*/
/*this.getArticle = function _article(articleId) {
console.log("tscApi.getArticle for " + articleId);
return $q(function(resolve, reject){
var article = mainFile.articles[articleId]
resolve(article);
});
};*/
this.getAuthors = function _authors(){
console.log("tscApi.getAuthors");
return $q(function(resolve, reject){
if (mainFile.length === 0){
getMainFile().then(function success(response){
var authors = mainFile.authors;
for(var a in authors){
// update pic
authors[a].images.portrait = 'https://causa.tagesspiegel.de'+authors[a].images.portrait;
// org
if (authors[a].organisation !== undefined){
console.log("org id: " + authors[a].organisation);
authors[a].organisation = mainFile.organisations[authors[a].organisation].name;
}
authors[a]["theses"] = filterListToArray(mainFile.theses, "created_by", a);
authors[a]["articles"] = filterListToArray(mainFile.articles, "author", a);
authors[a]["votes"] = filterListToArray(mainFile.votes, "author", a);
for (var i = 0; i < authors[a].votes.length; i++) {
authors[a].votes[i]["thesis_text"] = mainFile.theses[authors[a].votes[i].thesis].text;
}
}
resolve(authors);
});
} else {
resolve(mainFile.authors);
}
});
};
this.getAuthor = function _author(authorId){
return $q(function(resolve, reject){
getAuthors().then(function success(response){
resolve(response.authorId);
});
});
};
this.getAuthorsProVote = function _proVoteAuthors(thesisId){
};
this.getAuthorsConVote = function _conVoteAuthors(thesisId){
};
this.dummycall = function _dummy(){
console.log("api function dummycall. well. done.")
}
}