-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
executable file
·420 lines (397 loc) · 14.1 KB
/
app.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
/**
* This file is part of election-portal.
* Copyright (C) 2015-2016 Sequent Tech Inc <[email protected]>
* election-portal 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.
* election-portal 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 election-portal. If not, see <http://www.gnu.org/licenses/>.
**/
window.SequentConfigData.base = '/election';
angular.module(
'election-portal',
['ui.bootstrap',
'ui.utils',
'ui.router',
'ngAria',
'ngAnimate',
'ngResource',
'ngCookies',
'ipCookie',
'ngSanitize',
'infinite-scroll',
'angularMoment',
'SequentConfig',
'SequentPluginsConfig',
'jm.i18next',
'avUi',
'avRegistration',
'avTest',
'avElection',
'angularFileUpload',
'dndLists',
'angularLoad',
'angular-date-picker-polyfill',
'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: '/election/__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('election-portal').config(function($sceDelegateProvider, ConfigServiceProvider) {
$sceDelegateProvider.resourceUrlWhitelist(ConfigServiceProvider.resourceUrlWhitelist);
});
angular.module('election-portal').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: '/election',
template: '<div ui-view></div>'
})
.state('election.login_openid_connect_redirect', {
url: '/login-openid-connect-redirect',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController",
params: {
isOpenId: true
}
})
.state('election.public', {
url: '/{id:[0-9]{1,15}}/public',
templateUrl: 'avElection/public-controller/public-controller.html',
controller: "PublicController"
})
.state('election.public.loading', {
templateUrl: 'avElection/public-controller/loading.html'
})
.state('election.public.error', {
templateUrl: 'avElection/public-controller/error.html'
})
.state('election.public.show', {
templateUrl: 'avElection/public-controller/show.html'
})
.state('election.public.show.home', {
url: '/home',
templateUrl: 'avElection/public-controller/home.html'
})
.state('election.public.show.view', {
url: '/view/:name',
templateUrl: 'avElection/public-controller/view_page.html'
})
.state('election.public.show.auths', {
url: '/authorities',
templateUrl: 'avElection/public-controller/authorities.html'
})
.state('election.public.show.censusQuery', {
url: '/census-query',
templateUrl: 'avElection/public-controller/census_query.html'
})
.state('election.public.show.verify-results', {
url: '/verify-results',
templateUrl: 'avElection/public-controller/verify_results.html'
})
.state('election.public.show.ballot-locator', {
url: '/ballot-locator',
templateUrl: 'avElection/ballot-locator-controller/ballot-locator-controller.html',
controller: "BallotLocatorController"
})
.state('election.public.show.ballot-locator-included', {
url: '/ballot-locator/:locator',
templateUrl: 'avElection/ballot-locator-controller/ballot-locator-controller.html',
controller: "BallotLocatorController"
})
.state('election.public.show.home.simple', {
template: '<div ave-simple-question></div>'
})
.state('election.public.show.home.unknown', {
templateUrl: 'avElection/question-results-directive/unknown.html'
})
.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.register', {
url: '/register',
templateUrl: 'avRegistration/register-controller/register-controller.html',
controller: "RegisterController"
})
.state('election.public.show.login', {
url: '/login',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController"
})
.state('election.public.show.login_email', {
url: '/login/:email',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController"
})
.state('election.public.show.login_email_code', {
url: '/login/:email/:code',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController"
})
.state('election.public.show.login_with_code', {
url: '/login-with-code/:username',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController",
params: {
withCode: true
}
})
.state('election.public.show.login_alt', {
url: '/login-alt/:altmethod',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController",
params: {
withAltMethod: true
}
})
.state('election.public.show.otl', {
url: '/otl/:otlSecret',
templateUrl: 'avRegistration/login-controller/login-controller.html',
controller: "LoginController",
params: {
isOtl: true
}
})
.state('election.public.show.logout', {
url: '/logout',
controller: "LogoutController"
})
.state('election.public.show.legal', {
url: '/legal',
templateUrl: 'avElection/public-controller/legal.html',
controller: "PublicController"
})
.state('election.public.show.documentation', {
url: '/documentation',
template: '<div class="col-xs-12 top-section" documentation-directive></div>'
})
.state('election.results', {
url: '/:id/results',
templateUrl: 'avElection/results-controller/results-controller.html',
controller: "ResultsController"
})
.state('election.results.loading', {
templateUrl: 'avElection/results-controller/loading.html'
})
.state('election.results.error', {
templateUrl: 'avElection/results-controller/error.html'
})
.state('election.results.show', {
templateUrl: 'avElection/results-controller/show.html'
})
.state('election.results.show.unknown', {
templateUrl: 'avElection/question-results-directive/unknown.html'
})
.state('election.results.show.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('election-portal').run(function($http, $rootScope, $window, ConfigService) {
$rootScope.electionsTitle = 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('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams) {
console.log("change start from " + fromState.name + " to " + toState.name);
// redirect to /admin/login if this login link is invalid
if (_.contains (['election.public.show.login',
'election.public.show.login_email_code',
'election.public.show.login_email'],
toState.name) &&
ConfigService.freeAuthId+"" === toParams.id)
{
$window.location.href = "/admin/login";
}
$("#angular-preloading").show();
});
$rootScope.$on('$stateChangeSuccess',
function(event, toState, toParams, fromState, fromParams) {
console.log("change success");
$("#angular-preloading").hide();
});
});
/*
This directive allows us to pass a function in on an enter key to do what we want.
*/
angular.module('election-portal').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('election-portal').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;
}
};
});