-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackmanchester.js
514 lines (451 loc) · 13.5 KB
/
hackmanchester.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
var hacks = new Mongo.Collection("hacks");
var challenges = new Mongo.Collection("challenges");
var teams = new Mongo.Collection("teams");
var tech = new Mongo.Collection("tech");
Router.configure({
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
layoutTemplate: 'layout'
});
Router.map(function(){
this.route('home',{
path: '/'
});
this.route('hacks',{
template:'allhacks',
data(){
return {hacks: hacks.find({})};
}
});
this.route('myhacks',{
path: '/hacks/my',
template: 'myhacks',
data() {
return {hacks: hacks.find({team:Meteor.user().profile.team})};
}
});
this.route('teams',{
data() {
var mapped = teams.find({}).map(function(t){
var members = _.map(t.members, function(m){
return Meteor.users.findOne({_id:m});
});
return {_id: t._id,name: t.name, members:members,hacks:hacks.find({team: t._id}),table: t.table};
});
return {teams:mapped};
}
});
this.route('team', {
path:'/team/:_id',
data(){
var myteam = teams.findOne({_id:this.params._id});
var members = _.map(myteam.members, function(m){
return Meteor.users.findOne({_id:m});
});
return {_id:myteam._id,name:myteam.name, members:members, hacks:hacks.find({team: myteam._id}),table: myteam.table};
}
});
this.route('myteam',{
template:'team',
data(){
var myteam = teams.findOne({_id:Meteor.user().profile.team});
var members = _.map(myteam.members, function(m){
return Meteor.users.findOne({_id:m});
});
return {_id:myteam._id,name:myteam.name, members:members, hacks:hacks.find({team: myteam._id}),table: myteam.table};
}
});
this.route('administration',{
path:'/admin',
data(){
return { users: Meteor.users.find({})};
}
});
this.route('judging');
this.route('challenge',{
path: 'challenge/:_id',
data(){
return {
challenge: challenges.findOne({_id: this.params._id}),
validEntries: hacks.find({ challenges : this.params._id, youtube:{$ne:""}}),
entries: hacks.find({ challenges : this.params._id, youtube:{$eq:""}})
};
}
});
this.route('hack', {
path: '/hack/:_id',
data(){
return hacks.findOne({ _id : this.params._id});
}
});
});
if (Meteor.isClient) {
// counter starts at 0
Session.setDefault('counter', 0);
Accounts.ui.config({
passwordSignupFields: 'USERNAME_AND_EMAIL'
});
UI.registerHelper('trim250', function(context){
if(context){
return context.toString().substring(0, 250) + "...";
}
});
UI.registerHelper('isJudge', function(){
return Meteor.user().profile.isJudge;
});
UI.registerHelper('isAdmin', function(){
return Meteor.user().profile.isAdmin;
});
UI.registerHelper('challenges', function(){
return challenges.find();
});
UI.registerHelper('technologies', function(){
return tech.find();
});
UI.registerHelper('teamname', function(context){
return teams.findOne({_id:context}).name;
});
UI.registerHelper('techname', function(context){
return tech.findOne({_id:context}).description;
});
UI.registerHelper('ismyteam', function(context){
return Meteor.user().profile.team === context;
});
UI.registerHelper('countitems', function(context){
return context.length;
});
Template.navigation.helpers({
isActive(value){
return Router.current().route.getName() == value ? 'active' : '';
}
});
Template.home.helpers({
hacks(){
return hacks.find({},{$sort:{created:-1},limit:3})
},
techs() {
return tech.find().map(function(t){
var count = hacks.find({techchoices: t._id}).count();
return {name: t.description, count:count};
});
},
statistics(){
return {hacks:hacks.find().count(),hackers:Meteor.users.find({$or:[{"profile.isAdmin":{$ne:true}, "profile.isJudge":{$ne:true}}]}).count(), teams:teams.find().count(), challenges:challenges.find().count()}
}
});
Template.showhacks.events({
"submit .new-hack": function(event, template){
event.preventDefault();
var target = event.target;
if(target.name.value === '' && target.team.value === '' && target.description.value === '') {
Meteor.error("Values cannot be null!")
}
var selected = template.findAll( "input[type=checkbox]:checked");
var challenges = _.map(selected, function(item) {
return item.defaultValue;
});
var selectedtech = template.findAll("div.menu div.active");
var techchoices = _.map(selectedtech, function(item){
return item.attributes["data-value"].value;
});
hacks.insert({
name:target.name.value,
team:Meteor.user().profile.team,
description:target.description.value,
youtube:target.youtube.value,
hackurl:target.hackurl.value,
created:new Date(),
judgements: [],
challenges:challenges,
techchoices: techchoices
});
target.name.value = '';
target.description.value = '';
target.youtube.value = '';
target.table.value = '';
}
});
Template.hack.events({
"submit .edit-hack": function(event, template){
event.preventDefault();
var target = event.target;
if(target.name.value === '' && target.team.value === '' && target.description.value === '') {
Meteor.error("Values cannot be null!")
}
var selected = template.findAll( "input[type=checkbox]:checked");
var challenges = _.map(selected, function(item) {
return item.defaultValue;
});
var selectedtech = template.findAll("div.menu div.active");
var techchoices = _.map(selectedtech, function(item){
return item.attributes["data-value"].value;
});
hacks.update(this._id,{
$set: {
name: target.name.value,
description: target.description.value,
youtube:target.youtube.value,
hackurl:target.hackurl.value,
challenges:challenges,
techchoices: techchoices
}
});
Router.go('myhacks');
},
"submit .new-judgement": function(){
event.preventDefault();
var target = event.target;
if(target.judgement.value === '' && !Meteor.user().isJudge) {
Meteor.error("Values cannot be null!")
}
hacks.update(this._id,{
$push: {
judgements: {
judgement: target.judgement.value,
created: new Date(),
judge: Meteor.userId()
}
}
});
target.judgement.value = '';
}
});
Template.hackoverview.helpers({
myHack: function(){
return this.team === Meteor.user().profile.team;
},
enteredChallenges: function(){
if(this.challenges === undefined){
return null;
}
return challenges.find({_id:{$in:this.challenges}});
}
});
Template.judgingEntry.helpers({
judgesComments: function(){
return this.judgements.map(function(j){
var judge = Meteor.users.findOne({_id: j.judge});
return {
judgement: j.judgement,
created: j.created.toLocaleString(),
judge: judge.username
}
});
},
enteredChallenges: function(){
var chals = this.challenges || [];
return challenges.find().map(function(c){
var entered = chals.indexOf(c._id) > -1;
return {
description: c.description,
_id: c._id,
entered:entered
}
});
},
});
Template.hack.helpers({
myHack: function(){
return this.team === Meteor.user().profile.team;
},
judgesComments: function(){
return this.judgements.map(function(j){
var judge = Meteor.users.findOne({_id: j.judge});
return {
judgement: j.judgement,
created: j.created.toLocaleString(),
judge: judge.username
}
});
},
enteredChallenges: function(){
var chals = this.challenges || [];
return challenges.find().map(function(c){
var entered = chals.indexOf(c._id) > -1;
return {
description: c.description,
_id: c._id,
entered:entered
}
});
},
});
Template.hack.events({
"submit .delete-hack": function(){
event.preventDefault();
hacks.remove({_id:this._id});
Router.go('hacks');
}
})
Template.entry.helpers({
myChallenges: function(){
var chals = this.challenges || [];
return challenges.find().map(function(c){
var entered = chals.indexOf(c._id) > -1;
return {
description: c.description,
_id: c._id,
entered:entered
}
});
},
myTechnologies: function(){
var techs = this.techchoices || [];
return tech.find().map(function(c){
var selected = techs.indexOf(c._id) > -1;
return {
description: c.description,
_id: c._id,
selected:selected
}
});
}
});
Template.entry.events({
"keyup input.search": function(event, instance){
event.preventDefault();
if (event.which === 13) {
var tag = event.target.value;
if(tag.length === 0) return;
Meteor.call("saveTag", tag);
event.target.value = '';
Meteor.setTimeout(function() {
$('div.item:contains("'+tag+'")').click();
},200);
}
}
});
Template.entry.onRendered(
function(){
$('.dropdown').dropdown({ transition: 'drop' });
}
);
Template.teamoverview.events({
"submit .join-team": function(){
event.preventDefault();
teams.update({_id:this._id},{$push:{members:Meteor.userId()}});
Meteor.users.update(Meteor.userId(), { $set: {"profile.team": this._id} });
}
});
Template.team.events({
"submit .add-team": function(){
event.preventDefault();
var target = event.target;
if(target.name.value === '') {
Meteor.error("Values cannot be null!")
}
var team = teams.insert({
name:target.name.value,
members:[Meteor.userId()],
table:target.table.value
});
target.name.value = '';
target.table.value = '';
Meteor.users.update(Meteor.userId(), { $set: {"profile.team": team} });
},
"submit .edit-team": function(){
event.preventDefault();
var target = event.target;
if(target.name.value === '') {
Meteor.error("Values cannot be null!")
}
teams.update({_id:this._id},{$set:{
name:target.name.value,
table:target.table.value
}});
Router.go('teams');
},
"submit .leave-team": function(){
event.preventDefault();
teams.update({_id:this._id},{$pull:{members:Meteor.userId()}});
Meteor.users.update(Meteor.userId(), { $unset: {"profile.team": ""} });
Router.go('teams');
},
"submit .delete-team": function(){
event.preventDefault();
Meteor.call('deleteTeam', this._id);
Router.go('teams');
}
});
Template.administration.events({
"click .toggle-isjudge": function () {
event.preventDefault();
Meteor.call('setJudge', this._id, event.target.checked);
},
"click .toggle-isadmin": function () {
event.preventDefault();
Meteor.call('setAdmin', this._id, event.target.checked);
},
"submit .new-challenge": function(){
event.preventDefault();
var target = event.target;
if(target.challenge.value === '') {
Meteor.error("Values cannot be null!")
}
challenges.insert({description:target.challenge.value});
target.challenge.value = '';
},
"submit .edit-challenge": function(){
event.preventDefault();
var target = event.target;
if(target.description.value === '') {
Meteor.error("Values cannot be null!")
}
challenges.update({_id:this._id},{description:target.description.value});
},
"click .delete-challenge": function(){
event.preventDefault();
challenges.remove({_id:this._id});
}
,
"submit .new-tech": function(){
event.preventDefault();
var target = event.target;
if(target.tech.value === '') {
Meteor.error("Values cannot be null!")
}
tech.insert({description:target.tech.value});
target.tech.value = '';
},
"submit .edit-tech": function(){
event.preventDefault();
var target = event.target;
if(target.description.value === '') {
Meteor.error("Values cannot be null!")
}
tech.update({_id:this._id},{description:target.description.value});
},
"click .delete-tech": function(){
event.preventDefault();
tech.remove({_id:this._id});
}
});
Template.judging.helpers({
hacks(){
var challengeId = this._id;
return {entries: hacks.find({challenges:{$in:[challengeId]}}).count(),
validEntries:hacks.find({challenges:{$in:[challengeId]},youtube:{$ne:""}}).count()};
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.methods({
setJudge: function(userId, isJudge) {
Meteor.users.update(userId, {$set: {"profile.isJudge": isJudge}});
},
setAdmin: function(userId, isAdmin) {
Meteor.users.update(userId, {$set: {"profile.isAdmin": isAdmin}});
},
saveTag: function(tag) {
tech.upsert({description:tag},{description:tag});
},
deleteTeam: function(team){
hacks.remove({team:team});
Meteor.users.update({"profile.team":team}, { $unset: {"profile.team": ""} },{multi:true});
teams.remove({_id:team});
}
});
}