proposal-box.directive.js
6.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
(function() {
'use strict';
angular
.module('dialoga')
.directive('proposalBox', proposalBox);
/** @ngInject */
function proposalBox() {
/** @ngInject */
function ProposalBoxController($scope, $rootScope, $state, $timeout, $interval, $window, VOTE_STATUS, VOTE_OPTIONS, AuthService, DialogaService, $log) {
$log.debug('ProposalBoxController');
var vm = this;
vm.$scope = $scope;
vm.$rootScope = $rootScope;
vm.$state = $state;
vm.$timeout = $timeout;
vm.$interval = $interval;
vm.$window = $window;
vm.VOTE_STATUS = VOTE_STATUS;
vm.VOTE_OPTIONS = VOTE_OPTIONS;
vm.AuthService = AuthService;
vm.$log = $log;
vm.init();
vm.addListeners();
}
ProposalBoxController.prototype.init = function() {
var vm = this;
vm.showVote = vm.showVote || false;
vm.focus = vm.focus || false;
vm.STATE = null;
vm.errorOnSkip = false;
vm.showCaptchaForm = null;
vm.voteProposalRedirectURI = 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;
};
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;
});
// Load captcha
var stop = null;
stop = vm.$interval(function() {
var $el = angular.element('#serpro_captcha');
if ($el && $el.length > 0) {
vm.$window.initCaptcha($el[0]);
vm.$interval.cancel(stop);
stop = undefined;
}else {
vm.$log.debug('captcha element not found.');
}
}, 10);
};
ProposalBoxController.prototype.showContent = function(slug) {
var vm = this;
vm.$state.go('programa', {
slug: slug,
proposal_id: vm.proposal.id
}, {
location: true
});
};
ProposalBoxController.prototype.canVote = function() {
var vm = this;
return !!vm.$rootScope.temporaryToken;
};
ProposalBoxController.prototype.submitCaptcha = function($event, captchaForm) {
var vm = this;
var target = $event.target;
var $target = angular.element(target);
var $captcha = $target.find('[name="txtToken_captcha_serpro_gov_br"]');
vm.sendingCaptcha = true;
vm.AuthService.loginCaptcha({
captcha_text: captchaForm.captcha_text.$modelValue,
txtToken_captcha_serpro_gov_br: $captcha.val()
}).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;
}, function(data) {
// ERROR
vm.$log.debug('register error.data', data);
vm.sendingCaptchaError = {
code: data.status,
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.';
}
}, function(data) {
// UPDATE
vm.$log.debug('register update.data', data);
}).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
var $el = angular.element('#serpro_captcha');
vm.$window.reloadCaptcha($el[0]);
// focus on input
angular.element('#captcha_text').val('').focus();
};
ProposalBoxController.prototype.vote = function(value) {
var vm = this;
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;
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,
});
};
var directive = {
restrict: 'E',
templateUrl: 'app/components/proposal-box/proposal-box.html',
scope: {
proposal: '=',
topic: '=',
category: '=',
showVote: '=',
focus: '@',
doVote: '&'
// @ -> Text binding / one-way binding
// = -> Direct model binding / two-way binding
// & -> Behaviour binding / Method binding
},
controller: ProposalBoxController,
controllerAs: 'vm',
bindToController: true
};
return directive;
}
})();