forked from andryw/quemMeRepresenta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.js
463 lines (338 loc) · 12.7 KB
/
controllers.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
'use strict';
/* Controllers */
var houseOfCunhaApp = angular.module('houseOfCunhaApp', ['ui.bootstrap']);
houseOfCunhaApp.controller('ModalInstanceCtrl', function ($scope, $modalInstance, temaToModel) {
$scope.temaToModel = temaToModel;
$scope.ok = function () {
$modalInstance.close();
};
});
houseOfCunhaApp.controller('ModalDeputadoCtrl', function ($scope, $modalInstance, deputado, eleitor,indexesTemas,titulo) {
$scope.deputado = deputado;
$scope.titulo = titulo;
$scope.eleitor = eleitor;
$scope.indexesTemas = indexesTemas;
var nomesVotacoes = {"1":"sim","0":"não","-1":"não sei"}
$scope.ok = function () {
$modalInstance.close();
};
$scope.getIndex = function(index){
return $scope.indexesTemas[index];
};
$scope.getValueEleitor = function(index){
var value = $scope.eleitor[$scope.getIndex(index)].value.toString();
if (_.has(nomesVotacoes,value)){
return nomesVotacoes[value];
}else{
return "";
}
};
$scope.getRoWColour = function(voto,index){
var value = $scope.eleitor[$scope.getIndex(index)].value;
//var votosOK = ["não","sim"];
//var indexVotoDeputado = votosOK.indexOf(voto.value);
if ((value >= 0) && voto.value > -1){
if (value == voto.value){
return "green";
}else{
return "red";
}
}
};
});
houseOfCunhaApp.filter('startFrom', function() {
return function(input, start,scope) {
scope.totalItems = input.length;
start = +start;
return input.slice(start * scope.pageSize);
}
});
houseOfCunhaApp.filter('startFromP', function() {
return function(input, start,scope) {
scope.totalItemsP = input.length;
start = +start;
return input.slice(start * scope.pageSize);
}
});
houseOfCunhaApp.filter('startFromMidia', function() {
return function(input, start,scope) {
if (typeof input === 'undefined'){
return []
}
scope.totalItemsMidia = input.length;
start = +start;
return input.slice(start * scope.pageSizeMidia);
}
});
houseOfCunhaApp.filter('minimoVotacoes', function() {
return function(input,scope) {
var votacoesDoCara = _.filter(scope.temas, function(tema){
return tema.value > -1
}).length;
if (votacoesDoCara > 0 && !scope.mostrarPoucos){
var a = _.filter(input,function (deputado,index){
return deputado.total > votacoesDoCara / 2;
});
return a;
}else{
return input;
}
}
});
houseOfCunhaApp.filter('minimoVotacoesP', function() {
return function(input,scope) {
var votacoesDoCara = _.filter(scope.temas, function(tema){
return tema.value > -1
}).length;
if (votacoesDoCara > 0 && !scope.mostrarPoucosP){
var a = _.filter(input,function (partido,index){
return partido.total > votacoesDoCara / 2;
});
return a;
}else{
return input;
}
}
});
houseOfCunhaApp.controller('VotacoesCtrl', ['$scope', '$modal','$http', '$filter', function($scope, $modal, $http,$filter) {
//Temas
$scope.collapsed = false;
$scope.temas = [];
$scope.idParse = null;
//Deputados
$scope.estadoSelecionado = "";
$scope.estadoSelecionado2 = "";
$scope.partidoSelecionado = null;
$scope.estados = [];
$scope.partidos = [];
$scope.deputados = [];
$scope.temaToModel = "";
$scope.totalItems = 10;
$scope.currentPage = 1;
$scope.pageSize =15;
$scope.indexesTemas = [];
$scope.mostrarPoucos = false;
$scope.tooltip_semvalor = "Deputado não votou em nenhum dos temas selecionados. Continue votando!"
$scope.tooltip_semvalorP = "Partido não deu orientação de voto a nenhum dos temas selecionados. Continue votando!"
$scope.tooltip_art17 = "Eduardo Cunha, por ser presdiente da câmara, não vota (Artigo 17) ."
//Partidos
$scope.partidos_votos = [];
$scope.totalItemsP = 10;
$scope.currentPageP = 1;
$scope.mostrarPoucosP = false;
//$scope.pageSizeP =15;
//Midia
$scope.totalItemsMidia = 10;
$scope.currentPageMidia = 1;
$scope.pageSizeMidia = 5;
if ($(window).width() < 768){
$scope.pageSizeMidia = 3;
}
$http.get('dados/estados.json').success(function(data) {
$scope.estados = data;
});
$http.get('dados/midia.json').success(function(data) {
$scope.sites = data;
$scope.totalItemsMidia = $scope.sites.length;
});
$http.get('dados/deputados_votos.json').success(function(data) {
$scope.deputados = data;
$scope.partidos =_.uniq(_.pluck($scope.deputados, 'partido'));
$scope.partidos = _.map($scope.partidos,function(partido){
return {"partido": partido};
})
_.map($scope.deputados,function (deputado){
_.extend(deputado,{"score":-1,"sum":0,"total":0})
})
$scope.totalItems = $scope.deputados.length;
});
$http.get('dados/partidos_votos.json').success(function(data) {
$scope.partidos_votos = _.filter(data,function(partido){
return partido.nome != "s.part.";
});
_.map($scope.partidos_votos,function (partido){
_.extend(partido,{"score":-1,"sum":0,"total":0})
})
$scope.totalItemsP = $scope.partidos_votos.length;
});
$http.get('dados/temas.json').success(function(data) {
$scope.temas = data;
var paradas =_.clone(data);
_.map(paradas,function (tema,index){
_.extend(tema,{"index":index})
});
var parada2 = _.sortBy(paradas,"Tema");
$scope.indexesTemas = _.pluck(parada2,"index");
_.map($scope.temas,function (tema){
_.extend(tema,{"value":-1})
})
});
$scope.tema_descricao = function(tema) {
$scope.temaToModel = tema;
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
temaToModel: function () {
return $scope.temaToModel;
}
}
});
};
$scope.votos_deputados = function(deputado,titulo) {
$scope.deputado = deputado;
$scope.titulo = titulo;
var modalInstance = $modal.open({
templateUrl: 'modal_deputados.html',
controller: 'ModalDeputadoCtrl',
resolve: {
deputado: function () {
return $scope.deputado;
},
eleitor: function () {
return $scope.temas;
},
indexesTemas: function () {
return $scope.indexesTemas;
},
titulo: function () {
return $scope.titulo;
}
}
});
};
$scope.typeProgessBar = function(value) {
if(value >= 75){
return "success";
}else if (value >= 25){
return "warning";
}else{
return "danger";
}
};
$scope.getTooltipProgressBar = function(deputado) {
return "Similaridade: " + $filter('number')(deputado.score,2) + '% <br />' +
$scope.iguais(deputado) + '<br /><br />' + $scope.diferentes(deputado) + '<br /><br />' +
"Total de temas votados por você e pelo deputado: " + deputado.total +"";
};
$scope.tooltipBadgeText = function(deputado,nome) {
var primeiraParta = "";
var segundaParte = "";
if(deputado.sum == 1){
primeiraParta = "1 opinião semelhante de ";
}else {
primeiraParta = deputado.sum + " opiniões semelhantes de";
}
if(deputado.total == 1){
segundaParte = "1 opinião dada tanto por <br> você quanto pelo "+ nome + ".";
}else {
segundaParte = deputado.total + " opiniões dadas tanto por <br> você quanto pelo "+nome+".";
}
return primeiraParta + "<br>" + segundaParte;
};
$scope.getTooltipPlacement = function () {
return ($(window).width() < 768) ? 'left' : 'top';
};
$scope.getTooltipPlacementRight = function () {
return ($(window).width() < 768) ? 'right' : 'top';
};
$scope.getIndex = function(index){
return $scope.indexesTemas[index];
};
$scope.iguais = function(deputado) {
var temas_deputado = deputado.temas;
var iguais =
_.filter(temas_deputado,function(tema,index){
return (tema.value >= 0 && ($scope.temas[$scope.getIndex(index)].value >= 0) && (tema.value == $scope.temas[$scope.getIndex(index)].value));
})
return "Temas iguais (" + deputado.sum + '/' + deputado.total +"): " + _.pluck(iguais,'tema').join(", ");
};
$scope.diferentes = function(deputado) {
var temas_deputado = deputado.temas;
var diferentes =
_.filter(temas_deputado,function(tema,index){
return (tema.value >= 0 && ($scope.temas[$scope.getIndex(index)].value >= 0) && (tema.value != $scope.temas[$scope.getIndex(index)].value));
})
return "Temas diferentes: " + _.pluck(diferentes,'tema').join(", ");
};
$scope.click = function(data,value) {
if(value == "s"){
data.value = 1;
}else if (value == "n"){
data.value = 0;
}else{
data.value = -1;
}
sendData();
$scope.refresh_values($scope.deputados,data,value);
$scope.refresh_values($scope.partidos_votos,data,value);
};
var sendData = function () {
if (_.filter($scope.temas, function(tema){
return tema.value > -1
}).length > 4){
console.log(1)
connectToParse();
// Quando for testar localmente descomente essa linha e comente a linha abaixo, para usar uma tabela diferente no banco de dados
// var Resposta = Parse.Object.extend("RespostaTeste");
var Resposta = Parse.Object.extend("Resposta");
var resposta = new Resposta();
if(!$scope.posicao && navigator.geolocation){
navigator.geolocation.getCurrentPosition(function (pos){
$scope.posicao = {"latitude": pos.coords.latitude, "longitude": pos.coords.longitude, "altitude": pos.coords.altitude,
"accuracy": pos.coords.accuracy, "altitudeAccuracy": pos.coords.altitudeAccuracy}
})}
resposta.save({
"id": $scope.idParse,
"posicao": $scope.posicao,
"temas": _.map($scope.temas, function(tema){
delete tema.$$hashKey;
return tema;
})
}, {
success: function(obj) {
$scope.idParse = obj.id;
},
error: function(obj, error) {
console.log("houve um erro ao enviar dados para o parse")
console.log(error)
}
});
}
}
var connectToParse = function() {
if(!$scope.isConnected){
Parse.initialize("OU8n5rFt09C0jNvFVLxe7vZK8dcxeq9pmrcVXMK9", "PobCwq2u50lrLcx4wDu2CRNhFd92YNN4sviqgkVG");
$scope.isConnected = true;
}
}
$scope.refresh_values = function(lista,data,value) {
_.map(lista,function (deputado){
var temas_deputado = deputado.temas;
var sumEquals =
_.reduce(temas_deputado,function (memo,tema_deputado,index) {
if (tema_deputado.value >= 0 && ($scope.temas[$scope.getIndex(index)].value >= 0)){
if (tema_deputado.value == $scope.temas[$scope.getIndex(index)].value){
memo['sum'] = memo['sum'] + 1;
memo['total'] = memo['total'] + 1;
return memo;
}else{
memo['total'] = memo['total'] + 1;
return memo;
}
}
else{
return memo;
}
},{sum:0,total:0})
if (! sumEquals["total"] == 0){
var score = sumEquals["sum"] / sumEquals["total"];
deputado.score = score * 100;
}else{
deputado.score = -1;
}
deputado.sum = sumEquals["sum"];
deputado.total = sumEquals["total"];
})
};
}]);