-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaaMain.js
462 lines (373 loc) · 13.9 KB
/
aaMain.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
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
zones = new Mongo.Collection("zones");
fields = new Mongo.Collection("fields");
comments = new Mongo.Collection("comments");
users = Meteor.users;
zone_map = true;
counties = new Mongo.Collection("counties");
//routing code
Router.configure({
layoutTemplate: 'main'
});
Router.route('/', {
name: 'home',
template: 'accountLogin'
});
Router.map(function(){
this.route('results', {
path: '/results',
onBeforeAction: function(){
if(!Meteor.user()){
this.render('login')
}else{
this.next();
}
}
});
});
Router.map(function(){
this.route('admin', {
path: '/admin',
onBeforeAction: function(){
if(!Meteor.user()){
this.render('login')
}else{
var user = Meteor.user();
if(Roles.userIsInRole(user, ['admin'])){
this.next();
}
else{
alert('Not authorized to access this page');
this.render('results');
throw new Meteor.Error(403, "Not authorized to access this page");
}
}
}
});
});
//client side code
if(Meteor.isClient) {
Accounts.config({forbidClientAccountCreation:true});
Session.set('addNewUser', true);
Session.set('drawZones', true);
Meteor.subscribe("fields");
Meteor.subscribe("userData");
//events section
Template.register.events({
'submit form': function(event){
event.preventDefault();
var email= $('[name=email]').val();
var password = $('[name=password]').val();
id = Accounts.createUser({
email: email,
password: password
});
Router.go('home');
}
});
Template.login.events({
'submit form': function(event){
event.preventDefault();
var email = $('[name=email]').val();
var password = $('[name=password]').val();
Meteor.loginWithPassword(email, password, function(error){
if(error){
console.log(error);
}
else{
Router.go('results')
}
});
}
});
Template.accountLogin.rendered = function(){
Accounts._loginButtonsSession.set('dropdownVisible',true)
};
Template.main.events({
'click .logout': function(event){
event.preventDefault();
Meteor.logout();
Router.go('home');
},
'click #adminLink': function(event){
event.preventDefault();
Router.go('admin');
},
'click #resultLink': function(event){
event.preventDefault();
Router.go('results');
}
});
//variable to check if there is an existing subscription
var yearHandler = false;
var countyYearHandler = false;
Template.results.events({
'click #queryButton': function (event, template) {
//var selectedYear = parseInt(template.find('#yearSelect').value);
//var selectedField = template.find("#fieldSelect").value;
var selectedYear = parseInt($('#yearSelect').val());
var selectedField = $('#fieldSelect').val();
//var parameters = [selectedField, selectedYear];
var newYearHandler = Meteor.subscribe("zones", selectedField, selectedYear);
//var newYearHandler = Meteor.subscribe("zones", parameters);
if (yearHandler) {
yearHandler.stop();
}
yearHandler = newYearHandler;
$('#resultTable').css("display", "none");
$('#downloadLink').css("display", "inline");
},
'click #queryButtonCounty': function(event, template){
event.preventDefault();
var selectedYear = parseInt($('#yearSelectCounty').val());
var selectedField = $('#fieldSelectCounty').val();
var newYearHandler = Meteor.subscribe("counties", selectedField, selectedYear);
if (countyYearHandler){
countyYearHandler.stop();
}
countyYearHandler = newYearHandler;
$('#resultTable').css("display", "none");
}
});
Template.resultTable.events({
'click #addComment': function(event, target){
var field;
var year;
var zone = $('#zoneIdData').text();
var text = $('#comment').val();
if(Session.get('drawZones')){
field = $('#fieldSelect option:selected').text();
year = $('#yearSelect option:selected').text();
}
else{
field = $('#fieldSelectCounty option:selected').text();
var zone = $('#zoneIdData').text();
var text = $('#comment').val();
year = $('#yearSelectCounty option:selected').text();
}
Meteor.call("addComment", zone, field, year, text);
$('#comment').val('');
alert('Comment has been added to the database.');
}
});
Template.admin.events({
'click #changePassword': function(event, template){
Session.set('changePassword', true);
Session.set('addNewUser', false);
Session.set('editRole', false);
Session.set('deleteUser', false);
$('li').removeClass("active");
$(event.currentTarget).addClass('active');
},
'click #addNewUser': function(event){
Session.set('addNewUser', true);
Session.set('changePassword', false);
Session.set('editRole', false);
Session.set('deleteUser', false);
$('li').removeClass("active");
$(event.currentTarget).addClass('active');
},
'click #editRole': function(event){
Session.set('editRole', true);
Session.set('addNewUser', false);
Session.set('changePassword', false);
Session.set('deleteUser', false);
$('li').removeClass("active");
$(event.currentTarget).addClass('active');
},
'click #deleteUser': function(event){
Session.set('editRole', false);
Session.set('addNewUser', false);
Session.set('changePassword', false);
Session.set('deleteUser', true);
$('li').removeClass("active");
$(event.currentTarget).addClass('active');
}
});
Template.addNewUserTemplate.events({
'click #btnAddUser': function(event, template){
event.preventDefault();
var email = template.firstNode.lastElementChild[0].value;
var pass1 = template.firstNode.lastElementChild[1].value;
var pass2 = template.firstNode.lastElementChild[2].value;
var role = template.firstNode.lastElementChild[3].value;
if(pass1 === pass2){
Meteor.call("addNewUser", email, pass1, role)
template.firstNode.lastElementChild[0].value = "";
template.firstNode.lastElementChild[1].value = "";
template.firstNode.lastElementChild[2].value = "";
template.firstNode.lastElementChild[3].value = "";
alert('New user added to database');
}
else{
alert("Passwords do not match");
}
},
'click #btnAddUserWithoutPass': function(event){
event.preventDefault();
var email = $('#newUserEmail').val();
var role = $('#userRole').val()
Meteor.call("sendEmail", email, role);
}
});
Template.editRoleTemplate.events({
'click #btnDeleteRole': function(event){
event.preventDefault();
var role = $('#roleList option:selected').text();
var email = $('#userDropDownSelect option:selected').text();
Meteor.call("deleteRole", email, role);
Meteor.call("getRoles", email, function(error,result){
Session.set('selectedUserRoles', result);
});
},
'change #userDropDownSelect': function(event){
var email = event.currentTarget.value;
Meteor.call("getRoles", email, function(error,result){
Session.set('selectedUserRoles', result);
});
},
'click #addNewRole': function(event, template){
event.preventDefault();
var role = $('#newRole').val();
var email = $('#userDropDownSelect option:selected').text();
Meteor.call("addRole", email, role);
Meteor.call("getRoles", email, function(error,result){
Session.set('selectedUserRoles', result);
});
$('#newRole').val("");
}
});
Template.changePasswordTemplate.events({
"click #btnChangePass": function(event){
event.preventDefault();
var email = $('#userDropDownSelectPass option:selected').text();
var old_password = $('#newPassword1').val();
var new_password = $('#newPassword2').val();
if(old_password.length > 0){
if(old_password === new_password){
Meteor.call("newPass", email, new_password);
alert('Password has been changed.');
$('#newPassword1').val("");
$('#newPassword1').val("");
}
else{
alert('Passwords do not match.')
}
}
else{
alert('Password must be at least 1 character long.')
}
}
});
Template.deleteUserTemplate.events({
"click #btnDelete": function(event){
event.preventDefault();
var email = $('#userDropDownSelectDelete option:selected').text();
Meteor.call("deleteUser", email);
alert('User removed from database.')
}
});
//helpers section
Template.adminMenu.helpers({
changePassword: function(){return Session.get('changePassword');},
addNewUser: function(){return Session.get('addNewUser');},
editRole: function(){return Session.get('editRole');},
deleteUser: function(){return Session.get('deleteUser');}
});
Template.results.helpers({
field: function(){return fields.find({});},
year: function(){
var years = [{sim_year:2015}, {sim_year:2020}, {sim_year:2025}, {sim_year:2030}, {sim_year:2035}, {sim_year:2040}];
//for(var i=2011; i<2041; i++){
// var year = {sim_year: i};
// years.push(year);
//}
return years;
},
link: function(){
var data = zones.find({}).fetch();
data = Papa.unparse(data);
return encodeURIComponent(data);
}
});
Template.editRoleTemplate.helpers({
userEmail: function(){
var data = users.find({}).fetch();
var emails = [];
data.forEach(function(cv, index, arr){
var email_dict = {email:cv.emails[0].address};
emails.push(email_dict);
});
return emails;
},
userRoles: function(){return Session.get('selectedUserRoles');}
});
Template.changePasswordTemplate.helpers({
userEmail: function() {
var data = users.find({}).fetch();
var emails = [];
data.forEach(function (cv, index, arr) {
var email_dict = {email: cv.emails[0].address};
emails.push(email_dict);
});
return emails;
}
});
Template.deleteUserTemplate.helpers({
userEmail: function() {
var data = users.find({}).fetch();
var emails = [];
data.forEach(function (cv, index, arr) {
var email_dict = {email: cv.emails[0].address};
emails.push(email_dict);
});
return emails;
}
})
}
//server code for publishing
if (Meteor.isServer){
Meteor.publish("fields", function(){
if(Roles.userIsInRole(this.userId, ['admin'])){
return fields.find({});
}
else if(Roles.userIsInRole(this.userId, ['drcog'])){
var fieldList = ['pop_base', 'hh_base', 'emp_base','emp1_base','emp2_base','emp3_base','emp4_base','emp5_base','emp6_base','pop_sim','hh_sim','emp_sim','emp1_sim','emp2_sim','emp3_sim','emp4_sim','emp5_sim','emp6_sim','pop_diff','hh_diff','emp_diff','emp1_diff','emp2_diff','emp3_diff','emp4_diff','emp5_diff','emp6_diff'];
return fields.find({field:{$in:fieldList}});
}
else{
var fieldList = ['hh_base', 'emp_base','hh_sim','emp_sim','hh_diff','emp_diff'];
return fields.find({field:{$in:fieldList}});
}
});
Meteor.publish("zones", function(selectedField, selectedYear){
var dict = {};
var newField = selectedField;
var user = users.findOne(this.userId);
var userRoles = user.roles;
dict["zone_id"] = 1;
dict["county_name"] = 1;
dict[newField] = 1;
if(userRoles[0] === "admin" || userRoles[0] === "drcog"){
return zones.find({sim_year:selectedYear}, {fields:dict});
}
else{
return zones.find({sim_year:selectedYear, county_name:{$in:userRoles}}, {fields:dict});
}
});
Meteor.publish("counties", function(selectedField, selectedYear){
var dict = {};
var newField = selectedField;
dict["county_name"] = 1;
dict[newField] = 1;
return counties.find({sim_year:selectedYear}, {fields:dict});
});
Meteor.publish("userData", function(){
//var selectedFields = {emails: 1, roles:1, services:1};
if(this.userId){
return users.find({});
}
});
}
/**
* Created by jmartinez on 7/9/2015.
*/