proposal-box.directive.js
7.36 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
(function() {
'use strict';
angular
.module('dialoga')
.directive('proposalBox', proposalBox);
/** @ngInject */
function proposalBox() {
/** @ngInject */
function ProposalBoxController($scope, $location, $rootScope, $state, $timeout, $interval, $window, APP, VOTE_STATUS, VOTE_OPTIONS, AuthService, DialogaService, vcRecaptchaService, $log) {
$log.debug('ProposalBoxController');
var vm = this;
vm.$scope = $scope;
vm.$location = $location;
vm.$rootScope = $rootScope;
vm.$state = $state;
vm.$timeout = $timeout;
vm.$interval = $interval;
vm.$window = $window;
vm.APP = APP;
vm.VOTE_STATUS = VOTE_STATUS;
vm.VOTE_OPTIONS = VOTE_OPTIONS;
vm.AuthService = AuthService;
vm.vcRecaptchaService = vcRecaptchaService;
vm.$log = $log;
vm.init();
vm.addListeners();
}
ProposalBoxController.prototype.init = function() {
var vm = this;
vm.showVote = vm.showVote || false;
vm.archived = vm.archived || false;
vm.focus = vm.focus || false;
vm.STATE = null;
vm.errorOnSkip = false;
vm.showCaptchaForm = null;
vm.recaptchaWidgetId = null;
vm.recaptchaResponse = null;
vm.voteProposalRedirectURI = null;
vm.proposalsImg = null;
var slug = vm.topic.slug;
var proposal_id = vm.proposal.id;
vm.voteProposalRedirectURI = 'state=programa&task=vote-proposal&slug=' + slug + '&proposal_id=' + proposal_id;
// Take the coming proposal image of the body
// Pegar a imagem da proposta vinda do body
vm.proposalsImg = String(vm.proposal.body).replace(/<[^>]+>/gm, '');
if(vm.proposalsImg !== "undefined"){
vm.ProposalBody = false;
}else{
vm.ProposalBody = true;
}
};
ProposalBoxController.prototype.addListeners = function() {
var vm = this;
vm.$scope.$on('proposal-box:proposal-loaded', function(event, data) {
if (data.success) {
vm.STATE = null;
}
if (data.error) {
vm.errorOnSkip = data.error;
}
});
vm.$scope.$on('proposal-box:vote-response', function(event, data) {
if (data.success) {
vm.STATE = vm.VOTE_STATUS.SUCCESS;
}
if (data.error) {
vm.STATE = vm.VOTE_STATUS.ERROR;
}
if (data.code === 401) {
vm.message = 'Não autorizado.';
}
vm.messageCode = data.code;
});
// reCaptcha Listeners
vm.setWidgetId = function(widgetId) {
// store the `widgetId` for future usage.
// For example for getting the response with
// `recaptcha.getResponse(widgetId)`.
vm.$log.info('Created widget ID:', widgetId);
vm.recaptchaWidgetId = widgetId;
};
vm.setResponse = function(response) {
// Update local captcha response
vm.$log.debug('Response available', response);
vm.recaptchaResponse = response;
};
vm.cbExpiration = function() {
// reset the 'response' object that is on scope
vm.$log.debug('cbExpiration');
};
};
ProposalBoxController.prototype.canVote = function() {
var vm = this;
return !!vm.$rootScope.temporaryToken || (vm.$rootScope.currentUser && !!vm.$rootScope.currentUser.private_token);
};
ProposalBoxController.prototype.submitCaptcha = function($event, captchaForm) {
var vm = this;
var target = $event.target;
var $target = angular.element(target);
vm.sendingCaptcha = true;
vm.AuthService.loginCaptcha({
recaptcha_response: vm.recaptchaResponse
}).then(function(data) {
// SUCCESS
vm.$log.debug('register success.data', data);
// SEND VOTE
if (vm._oldVoteValue) {
vm.vote(vm._oldVoteValue);
vm._oldVoteValue = null;
}
// hide captcha form
vm.showCaptchaForm = false;
})
.catch(function(data) {
// ERROR
vm.$log.debug('register error.data', data);
// In case of a failed validation you need to reload the captcha
// because each response can be checked just once
vm.vcRecaptchaService.reload(vm.recaptchaWidgetId);
vm.sendingCaptchaError = {};
vm.sendingCaptchaError.code = data.status;
vm.sendingCaptchaError.message = data.message || ('Erro (' + data.status + '). Já estamos trabalhando para resolver o problema.<br/>Por favor, tente novamente mais tarde');
if (angular.equals(vm.sendingCaptchaError.message, 'Internal captcha validation error')) {
vm.sendingCaptchaError.message = 'Erro interno ao tentar validar captcha.<br/><br/>Já estamos trabalhando para resolver o problema.<br/>Por favor, tente novamente mais tarde.';
}
}).finally(function() {
vm.sendingCaptcha = false;
});
};
ProposalBoxController.prototype.captchaTryAgain = function() {
var vm = this;
vm.showCaptchaForm = true;
vm.sendingCaptcha = false;
vm.sendingCaptchaError = false;
vm.message = null;
// reload new captcha
vm.vcRecaptchaService.reload(vm.recaptchaWidgetId);
// focus on input
// angular.element('#captcha_text').val('').focus();
};
ProposalBoxController.prototype.vote = function(value) {
var vm = this;
if(vm.archived === true){
vm.$log.info('Article archived. Abort.');
return;
}
vm._oldVoteValue = value;
if (vm.canVote()) {
if (vm.doVote) {
vm.doVote({
proposal_id: vm.proposal.id,
value: value
});
}else {
vm.$log.error('No vote function to handler votes');
}
}else {
vm.$log.debug('You cannot vote.');
vm.showCaptchaForm = true;
angular.element('#captcha_text').focus();
}
};
ProposalBoxController.prototype.skip = function() {
var vm = this;
if(vm.archived === true){
vm.$log.info('Article archived. Abort.');
return;
}
vm.errorOnSkip = false;
vm.STATE = vm.VOTE_STATUS.LOADING;
vm.doVote({
proposal_id: vm.proposal.id,
value: vm.VOTE_OPTIONS.SKIP
});
vm.$log.debug('Sending vote');
};
ProposalBoxController.prototype.getSocialUrl = function() {
var vm = this;
return vm.$state.href('programa', {
slug: vm.topic.slug,
proposal_id: vm.proposal.id,
}, {
absolute: true
});
};
ProposalBoxController.prototype.getSocialText = function() {
var vm = this;
return vm.proposal.abstract;
};
ProposalBoxController.prototype.getSocialImage = function() {
var vm = this;
return vm.$rootScope.basePath + vm.topic.image.url;
};
var directive = {
restrict: 'E',
templateUrl: 'app/components/proposal-box/proposal-box.html',
scope: {
// @ -> Text binding / one-way binding
// = -> Direct model binding / two-way binding
// & -> Behaviour binding / Method binding
archived: '=',
category: '=',
doVote: '&',
focus: '@',
proposal: '=',
showVote: '=',
topic: '=',
location: '=',
},
controller: ProposalBoxController,
controllerAs: 'vm',
bindToController: true
};
return directive;
}
})();