controllers.js
17.1 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// FIXME: Split it into smaller files
angular.module('confjuvapp.controllers', [])
.controller('ProposalCtrl', function($scope, $ionicModal, $http, $ionicPopup, filterFilter) {
$scope.loading = false;
/******************************************************************************
L O G I N
******************************************************************************/
$scope.loginFormDisplayed = false;
$scope.displayLoginForm = function() {
$scope.loginFormDisplayed = true;
$scope.registerFormDisplayed = false;
};
// Function to open the modal
$scope.openModal = function() {
if ($scope.modal) {
$scope.modal.show();
}
else {
// Initiate the modal
$ionicModal.fromTemplateUrl('html/_login.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
});
}
};
// Function to close the modal
$scope.closeModal = function() {
$scope.modal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Function to login
$scope.Login = function(data) {
if (!data || !data.login || !data.password) {
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
$http.post(ConfJuvAppUtils.pathTo('login'), jQuery.param(data), config)
.then(function(resp) {
$scope.closeModal();
var popup = $ionicPopup.alert({ title: 'Login', template: 'Login efetuado com sucesso!' });
$scope.loggedIn = true;
$scope.user = resp.data.person;
popup.then(function() {
$scope.token = resp.data.private_token;
$scope.loadTopics(resp.data.private_token);
});
}, function(err) {
$scope.closeModal();
var popup = $ionicPopup.alert({ title: 'Login', template: 'Erro ao efetuar login. Verifique usuário e senha e conexão com a internet.' });
$scope.loggedIn = false;
$scope.loading = false;
popup.then(function() {
$scope.openModal();
});
});
};
// Function to retrieve password
$scope.forgotPassword = function(email) {
if (!email) {
var popup = $ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Digite seu e-mail no campo "Usuário" e clique novamente neste link' });
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
var data = { value: email };
$http.post(ConfJuvAppUtils.pathTo('account/forgot_password', true), jQuery.param(data), config)
.then(function(resp) {
$ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Um link para redefinição de senha foi enviado para o seu e-mail.' });
$scope.loading = false;
}, function(err) {
$ionicPopup.alert({ title: 'Esqueceu a senha?', template: 'Erro ao requisitar redefinição de senha.' });
$scope.loading = false;
});
};
// Register as a new user
$scope.registerFormDisplayed = false;
$scope.displayRegisterForm = function() {
$scope.registerFormDisplayed = true;
$scope.loginFormDisplayed = false;
};
// Function to register
$scope.Register = function(data) {
if (!data || !data.login || !data.email || !data.password || !data.password_confirmation) {
$ionicPopup.alert({ title: 'Registrar', template: 'Por favor preencha todos os campos' });
return;
}
else if (data.password != data.password_confirmation) {
$ionicPopup.alert({ title: 'Registrar', template: 'Senhas não conferem' });
return;
}
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
}
$http.post(ConfJuvAppUtils.pathTo('register'), jQuery.param(data), config)
.then(function(resp) {
var popup = $ionicPopup.alert({ title: 'Registrar', template: 'Usuário registrado com sucesso!' });
popup.then(function() {
$scope.registerFormDisplayed = false;
});
$scope.loading = false;
}, function(err) {
var msg = '';
try {
var errors = JSON.parse(err.data.message);
for (var field in errors) {
msg += 'Campo "' + field + '" ' + errors[field][0] + '. ';
}
} catch(e) {
msg = err.data.message;
}
$ionicPopup.alert({ title: 'Registrar', template: 'Erro ao registrar usuário. ' + msg });
$scope.loading = false;
});
};
$scope.backToLoginHome = function() {
$scope.registerFormDisplayed = false;
$scope.loginFormDisplayed = false;
};
/******************************************************************************
D I S C U S S I O N S > T O P I C S > P R O P O S A L S
******************************************************************************/
$scope.topics = [];
$scope.proposalList = [];
$scope.proposalsByTopic = {};
$scope.cards = [];
// Selected topics
$scope.selection = [];
// Helper method to get selected topics
$scope.selectedTopics = function selectedTopics() {
return filterFilter($scope.topics, { selected: true });
};
// Watch topics for changes
$scope.$watch('topics|filter:{selected:true}', function (nv) {
$scope.selection = nv.map(function (topic) {
return topic.title;
});
}, true);
// Load topics
$scope.loadTopics = function(token) {
$scope.loading = true;
// var path = '?private_token=' + token + '&fields=title,image,body,abstract&content_type=ProposalsDiscussionPlugin::Proposal';
var params = '?private_token=' + token + '&fields=title,id&content_type=ProposalsDiscussionPlugin::Topic';
var path = 'articles/' + ConfJuvAppConfig.noosferoDiscussion + '/children' + params;
$http.get(ConfJuvAppUtils.pathTo(path))
.then(function(resp) {
$scope.loading = false;
var topics = resp.data.articles;
for (var i = 0; i < topics.length; i++) {
var topic = topics[i];
topic.selected = true;
$scope.topics.push(topic);
$scope.proposalsByTopic[topic.id] = [];
$scope.loadProposals(token, topic);
}
}, function(err) {
$ionicPopup.alert({ title: 'Tópicos', template: 'Não foi possível carregar os tópicos' });
$scope.loading = false;
});
};
// Load proposals
$scope.loadProposals = function(token, topic) {
$scope.loading = true;
var params = '?private_token=' + token + '&fields=title,image,body,abstract,id&content_type=ProposalsDiscussionPlugin::Proposal';
var path = 'articles/' + topic.id + '/children' + params;
$http.get(ConfJuvAppUtils.pathTo(path))
.then(function(resp) {
var proposals = resp.data.articles;
for (var i = 0; i < proposals.length; i++) {
var proposal = proposals[i];
proposal.topic = topic;
$scope.proposalList.push(proposal);
$scope.proposalsByTopic[topic.id].push(proposal);
$scope.cards.push(proposal);
}
fillBackgroundWithColor('#FAFAFA');
$scope.loading = false;
}, function(err) {
$ionicPopup.alert({ title: 'Propostas', template: 'Não foi possível carregar as propostas do tópico ' + topic.title });
$scope.loading = false;
});
};
// Cards
$scope.cardDestroyed = function(index) {
$scope.cards.splice(index, 1);
if ($scope.cards.length === 0) {
for (var i = 0; i < $scope.proposalList.length; i++) {
var card = $scope.proposalList[i];
if (card.topic.selected) {
$scope.cards.push(card);
}
}
}
};
$scope.nextCard = function() {
var index = $scope.cards.length - 1;
if (index == -1) {
index = 0;
}
$scope.cardDestroyed(index);
};
// Swipe cards when filters are selected
$scope.$watch('selection', function() {
$scope.cards = [];
for (var i = 0; i < $scope.proposalList.length; i++) {
var card = $scope.proposalList[i];
if (card.topic.selected) {
$scope.cards.push(card);
}
}
});
/******************************************************************************
S I N G L E P R O P O S A L
******************************************************************************/
$scope.proposal = null;
// Function to open the modal
$scope.openProposal = function(proposal) {
$scope.proposal = proposal;
if (!$scope.proposal.comments || $scope.proposal.comments.length == 0) {
loadComments();
}
if ($scope.proposalModal) {
$scope.proposalModal.show();
}
else {
// Initiate the modal
$ionicModal.fromTemplateUrl('html/_proposal.html?1', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.proposalModal = modal;
$scope.proposalModal.show();
});
}
};
// Function to close the modal
$scope.closeProposal = function() {
$scope.proposalModal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.proposalModal.remove();
});
/******************************************************************************
C R E A T E P R O P O S A L
******************************************************************************/
// Function to open the modal
$scope.openCreateProposalForm = function() {
if ($scope.createProposalModal) {
$scope.createProposalModal.show();
}
else {
// Initiate the modal
$ionicModal.fromTemplateUrl('html/_create_proposal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.createProposalModal = modal;
$scope.createProposalModal.show();
});
}
};
// Function to close the modal
$scope.closeProposalModal = function() {
$scope.createProposalModal.hide();
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.createProposalModal.remove();
});
// Submit the proposal
$scope.createProposal = function(data) {
if (!data || !data.title || !data.description || !data.topic_id) {
$scope.closeProposalModal();
var popup = $ionicPopup.alert({ title: 'Criar proposta', template: 'Por favor preencha todos os campos!' });
popup.then(function() {
$scope.openCreateProposalForm();
});
}
else if (data.description.length > 1000) {
$scope.closeProposalModal();
var popup = $ionicPopup.alert({ title: 'Criar proposta', template: 'A descrição deve ter no máximo 1000 caracteres!' });
popup.then(function() {
$scope.openCreateProposalForm();
});
}
else {
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
};
var params = {
'private_token': $scope.token,
'article[body]': data.description,
'article[name]': data.title,
'article[abstract]': data.description.substring(0, 130) + '...',
'fields': 'id',
'article[type]': 'ProposalsDiscussionPlugin::Proposal',
'content_type': 'ProposalsDiscussionPlugin::Proposal'
};
$http.post(ConfJuvAppUtils.pathTo('articles/' + data.topic_id + '/children'), jQuery.param(params), config)
.then(function(resp) {
$scope.closeProposalModal();
var popup = $ionicPopup.alert({ title: 'Criar proposta', template: 'Proposta criada com sucesso!' });
popup.then(function() {
var topic = null;
for (var i = 0; i < $scope.topics.length; i++) {
if (data.topic_id == $scope.topics[i].id) {
topic = $scope.topics[i];
}
}
var proposal = {
title: data.title,
body: data.description,
topic: topic
};
$scope.proposalList.push(proposal);
$scope.cards.push(proposal);
$scope.proposalsByTopic[data.topic_id].push(proposal);
data.title = data.description = data.topic_id = null;
$scope.loading = false;
});
}, function(err) {
$scope.closeProposalModal();
var popup = $ionicPopup.alert({ title: 'Criar proposta', template: 'Erro ao criar proposta!' });
$scope.loading = false;
popup.then(function() {
$scope.openCreateProposalForm();
});
});
}
};
/******************************************************************************
C R E A T E C O M M E N T
******************************************************************************/
// Function to open the modal
$scope.openCommentForm = function() {
$scope.closeProposal();
if ($scope.commentModal) {
$scope.commentModal.show();
}
else {
// Initiate the modal
$ionicModal.fromTemplateUrl('html/_create_comment.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.commentModal = modal;
$scope.commentModal.show();
});
}
};
// Function to close the modal
$scope.closeCommentModal = function() {
$scope.commentModal.hide();
$scope.openProposal($scope.proposal);
};
// Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.commentModal.remove();
$scope.openProposal($scope.proposal);
});
// Submit the comment
$scope.createComment = function(data) {
if (!data || !data.comment) {
$scope.closeCommentModal();
var popup = $ionicPopup.alert({ title: 'Comentar', template: 'O seu comentário não pode ficar em branco!' });
popup.then(function() {
$scope.openCommentForm();
});
}
else if (data.comment.length > 1000) {
$scope.closeCommentModal();
var popup = $ionicPopup.alert({ title: 'Comentar', template: 'O comentário deve ter no máximo 1000 caracteres!' });
popup.then(function() {
$scope.openCommentForm();
});
}
else {
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
};
var params = {
'private_token': $scope.token,
'body': data.comment
};
$http.post(ConfJuvAppUtils.pathTo('articles/' + $scope.proposal.id + '/comments'), jQuery.param(params), config)
.then(function(resp) {
$scope.closeCommentModal();
var popup = $ionicPopup.alert({ title: 'Comentar', template: 'Comentário criado com sucesso!' });
if (!$scope.proposal.comments) {
$scope.proposal.comments = [];
}
$scope.proposal.comments.unshift({ body: params.body, author: { name: $scope.user.name }});
popup.then(function() {
$scope.loading = false;
});
}, function(err) {
$scope.closeCommentModal();
var popup = $ionicPopup.alert({ title: 'Comentar', template: 'Erro ao criar comentário!' });
popup.then(function() {
$scope.loading = false;
$scope.openCommentForm();
});
});
}
};
/******************************************************************************
L O A D C O M M E N T S
******************************************************************************/
var loadComments = function() {
$scope.loading = true;
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
timeout: 10000
};
$http.get(ConfJuvAppUtils.pathTo('articles/' + $scope.proposal.id + '/comments?private_token=' + $scope.token), config)
.then(function(resp) {
$scope.loading = false;
$scope.proposal.comments = resp.data.comments;
if ($scope.proposal.comments.length == 0) {
$scope.proposal.comments = [{ body: '', skip: true, author: { name: '' }}];
}
}, function(err) {
var popup = $ionicPopup.alert({ title: 'Comentários', template: 'Erro ao carregar comentários da proposta ' + $scope.proposal.id });
popup.then(function() {
$scope.loading = false;
});
});
};
}); // Ends controller