-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·347 lines (324 loc) · 10.9 KB
/
app.js
File metadata and controls
executable file
·347 lines (324 loc) · 10.9 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
/**
* This file is part of voting-booth.
* Copyright (C) 2015-2016 Sequent Tech Inc <legal@sequentech.io>
* voting-booth is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License.
* voting-booth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with voting-booth. If not, see <http://www.gnu.org/licenses/>.
**/
window.SequentConfigData.base = '/booth';
window.localStorage.clear();
angular.module(
'voting-booth',
['ui.bootstrap',
'ui.utils',
'ui.router',
'ngAria',
'ngAnimate',
'ngResource',
'ngCookies',
'ipCookie',
'ngSanitize',
'infinite-scroll',
'angularMoment',
'SequentConfig',
'jm.i18next',
'avUi',
'avBooth',
'avTest',
'avCrypto',
'angularFileUpload',
'dndLists',
'angularLoad',
'ng-autofocus',
'common-ui'
]);
angular
.module('jm.i18next')
.config(
function ($i18nextProvider, ConfigServiceProvider)
{
function expandObject(obj)
{
var result = {};
// Helper function to handle the recursion
function assignValue(ref, keys, value) {
var key = keys.shift(); // Get the current key part
if (keys.length === 0) {
// If no more keys, assign the value directly
ref[key] = value;
} else {
// Prepare the next level sub-object if necessary
if (!ref[key]) {
ref[key] = {};
}
// Recurse with the next level of the key and the corresponding sub-object
assignValue(ref[key], keys, value);
}
}
// Iterate over each property in the input object
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
var keys = prop.split('.'); // Split the property by dots into parts
assignValue(result, keys, obj[prop]); // Use the helper to assign the value in the result object
}
}
return result;
}
// note that we do not send the language: by default, it will try the language
// supported by the web browser
$("#no-js").hide();
window.i18next
.use(window.i18nextChainedBackend)
.init(_.extend(
{
debug: true,
load: 'languageOnly',
useCookie: true,
// Preload is needed because the language selector shows an item for
// each element in lngWhitelist, and the translation for each language
// is contained at each language i18n file, so we either preload it
// or it wouldn't work.
preload: ConfigServiceProvider.i18nextInitOptions.lngWhitelist || [],
useLocalStorage: false,
fallbackLng: 'en',
cookieName: 'lang',
detectLngQS: 'lang',
lngWhitelist: ['en', 'es'],
// files to load
ns: ['override', 'locales'],
// default namespace (needs no prefix on calling t)
defaultNS: 'override',
// fallback, can be a string or an array of namespaces
fallbackNS: 'locales',
interpolation: {
prefix: '__',
suffix: '__',
},
// Define the backends to use in the chain
backend: {
backends: [
{
type: 'backend',
/* use services and options */
init: function(services, backendOptions, i18nextOptions) {},
/* return resources */
read: function(language, namespace, callback)
{
if (namespace === 'override') {
if (
window.i18nOverride &&
typeof window.i18nOverride === 'object' &&
window.i18nOverride[language] &&
typeof window.i18nOverride[language] === 'object'
) {
var override = expandObject(window.i18nOverride[language]);
callback(null, override);
} else {
callback(null, {noop: "noop"});
}
} else {
// not found
callback(true, null);
}
},
/* save the missing translation */
create: function(languages, namespace, key, fallbackValue) {}
},
window.i18nextHttpBackend, // Primary backend
],
backendOptions: [
// Configuration for custom backend
{},
// Configuration for http backend
{
loadPath: '/booth/__ns__/__lng__.json',
},
]
},
defaultLoadingValue: '' // ng-i18next option, *NOT* directly supported by i18next
},
ConfigServiceProvider.i18nextInitOptions
));
// Prevent site translation if configured
if (ConfigServiceProvider.preventSiteTranslation) {
$('html').attr('translate', 'no');
}
}
);
angular.module('voting-booth').config(function($sceDelegateProvider, ConfigServiceProvider) {
$sceDelegateProvider.resourceUrlWhitelist(ConfigServiceProvider.resourceUrlWhitelist);
});
angular.module('voting-booth').config(
function(
$stateProvider,
$urlRouterProvider,
$httpProvider,
$locationProvider,
ConfigServiceProvider)
{
// CSRF verification
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$urlRouterProvider.otherwise(ConfigServiceProvider.defaultRoute);
// use the HTML5 History API
$locationProvider.html5Mode(ConfigServiceProvider.locationHtml5mode);
/* App states and urls are defined here */
$stateProvider
.state('election', {
abstract: true,
url: '/booth',
template: '<div ui-view></div>'
})
.state('election.booth', {
url: '/:id/vote/:hmac/:message',
templateUrl: 'avBooth/booth.html',
controller: "BoothController"
})
.state('election.booth-nohmac', {
url: '/:id/vote',
templateUrl: 'avBooth/booth.html',
controller: "BoothController"
})
.state('election.booth-demo', {
url: '/:id/demo-vote',
templateUrl: 'avBooth/booth.html',
controller: "BoothController",
params: {
isDemo: true
}
})
.state('election.booth-eligibility', {
url: '/:id/eligibility',
templateUrl: 'avBooth/booth.html',
controller: "BoothController",
params: {
isEligibility: true
}
})
.state('election.booth-preview', {
url: '/:id/preview-vote',
templateUrl: 'avBooth/booth.html',
controller: "BoothController",
params: {
isPreview: true
}
})
.state('election.booth-uuid-preview', {
url: '/:id/uuid-preview-vote',
templateUrl: 'avBooth/booth.html',
controller: "BoothController",
params: {
isPreview: true,
isUuidPreview: true
}
})
.state('election.public.show.home.simple', {
template: '<div ave-simple-question></div>'
})
.state('election.public.show.home.plurality-at-large', {
template: '<div av-plurality-at-large-results></div>',
})
.state('election.public.show.home.borda', {
template: '<div av-borda-results></div>',
})
.state('election.public.show.logout', {
url: '/logout',
controller: "LogoutController"
})
.state('election.results.show.home.borda', {
template: '<div av-borda-results></div>',
})
.state('election.results.show.plurality-at-large', {
template: '<div av-plurality-at-large-results></div>',
});
$stateProvider
.state('unit-test-e2e', {
url: '/unit-test-e2e',
templateUrl: 'test/unit_test_e2e.html',
controller: "UnitTestE2EController"
});
});
angular.module('voting-booth').run(function($http, $rootScope, ConfigService, $window) {
document.title = ConfigService.webTitle;
$rootScope.safeApply = function(fn) {
var phase = $rootScope.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
$rootScope.$on(
'i18nextLanguageChange',
function () {
console.log("i18nextLanguageChange: lang-change, calling safeApply()");
$rootScope.safeApply();
}
);
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams) {
console.log("change start from " + fromState.name + " to " + toState.name);
$("#angular-preloading").show();
});
$rootScope.$on('$stateChangeSuccess',
function(event, toState, toParams, fromState, fromParams) {
console.log("change success");
$("#angular-preloading").hide();
});
/**
* Warns the user about leaving the page, using a standard browser
* prompt dialog (there's no way to use any custom one).
*/
$window.onbeforeunload = function(e) {
// Cancel the event
e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
// Chrome requires returnValue to be set
e.returnValue = '';
};
});
/*
This directive allows us to pass a function in on an enter key to do what we want.
*/
angular.module('voting-booth').directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
/**
* Truncate Filter
* @Param text
* @Param length, default is 10
* @Param end, default is "..."
* @return string
*/
angular.module('voting-booth').filter('truncate', function () {
return function (text, length, end) {
if (isNaN(length)) {
length = 10;
}
if (end === undefined) {
end = "...";
}
if (text.length <= length || text.length - end.length <= length) {
return text;
}
else {
return String(text).substring(0, length-end.length) + end;
}
};
});