-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmunmod.js
2758 lines (2317 loc) · 98.6 KB
/
munmod.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//// munmod.js /////
//// This file is the main JS file for the KMUN MunModerator WebApp. /////
//// /////
//// /////
//// /////
//// /////
//// /////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Pure CSS Code (For navigation panel)
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
(function (window, document) {
var layout = document.getElementById('layout'),
menu = document.getElementById('menu'),
menuLink = document.getElementById('menuLink');
function toggleClass(element, className) {
var classes = element.className.split(/\s+/),
length = classes.length,
i = 0;
for(; i < length; i++) {
if (classes[i] === className) {
classes.splice(i, 1);
break;
}
}
// The className is not found
if (length === classes.length) {
classes.push(className);
}
element.className = classes.join(' ');
}
menuLink.onclick = function (e) {
var active = 'active';
e.preventDefault();
toggleClass(layout, active);
toggleClass(menu, active);
toggleClass(menuLink, active);
};
}(this, this.document));
// Initialize Materialize 'select' element
$(document).ready(function() {
$('select').material_select();
});
// Initialize the GSL List to be sortable using jQueryUI
$(function() {
$( "#GSLList" ).sortable({
delay: 100,
update: function (event, ui) {
gslListSelection();
},
handle: '.GSLListItem-Button2-Text',
cursor: 'move'
});
$( "#GSLList" ).disableSelection();
});
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Random Functions
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
// Fade in when page loads
$(window).load(function(){
setTimeout(function(){
$('#loading').fadeOut("slow");
}, 200);
});
// Show confirmation alert when window is closed
// window.onbeforeunload = function(e) {
// return 'Are you sure you want to close MUN Moderator?';
// };
// Stores the current session
var currentSession = "session0";
// Modifies the session
function sessionSelect(){
// Retrieve the session from the select element
currentSession = document.getElementById("sessionSelector").value;
// Insert value into the DB
// db.transaction("Variables", "readwrite").objectStore("Variables").put(currentSession, "currentSession").onerror = function(event){
// console.log("Couldn't change current session in DB.");
// currentSession = "session0";
// }
}
/*--------------------------------------------------
Connectivity Status
--------------------------------------------------*/
var connectivityErrors = 0; // Ensures that just a single error on a good connection is not reported as 'No Connectivty'; Requires two consecutive errors
$(document).ready(function(){
setInterval(function(){
var myData="0";
request = $.ajax({
url: "connectivity.php",
type: "post",
data: myData
});
// Internet connected, server accessible
request.done(function (response, textStatus, jqXHR){
connectivityErrors = 0; // Reset counter
if (response == "1"){ // Everything OK
$("#status-icon").css("color", "#006600"); // Green
$("#status-text").html(" OK");
}
else if (response == "0"){ // SQL Error
$("#status-icon").css("color", "#FF0000"); // Red
$("#status-text").html(" SQL Error");
}
else { // PHP Error
$("#status-icon").css("color", "#FF0000"); // Red
$("#status-text").html(" PHP Error");
}
});
// Not connected to internet or server down
request.fail(function (jqXHR, textStatus, errorThrown){
if (connectivityErrors > 1){
$("#status-icon").css("color", "#FFCC00"); // Yellow
$("#status-text").html(" No Internet");
}
connectivityErrors++; // Increment counter
});
}, 7000); // Check every 7 seconds
});
/*--------------------------------------------------
For fields: Numbers Only Validation
--------------------------------------------------*/
// Allows only numbers to be typed into fields
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Autocomplete configuration and initialisation
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
// Binds the sources for the autocomplete function
// Needs to be called once the array is populated
function AutocompleteSources(){
$(function(){
$('#unmodAutocomplete-start').autocomplete({
lookup: currentList,
minChars: 0,
maxHeight: 250
});
});
$(function(){
$('#modAutocomplete-start').autocomplete({
lookup: currentList,
minChars: 0,
maxHeight: 250
});
});
$(function(){
$('#modAutocomplete-speaker').autocomplete({
lookup: currentList,
minChars: 0,
maxHeight: 250,
onSelect: function() {
//modSpeakerReset();
}
});
});
$(function(){
$('#gslAutocomplete-speaker').autocomplete({
lookup: currentList,
minChars: 0,
maxHeight: 250,
onselect: function() {
//GSLAddSpeaker();
},
});
});
}
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Navigation pane control
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
$(document).ready(function(){
$("#headerArea").click(function(){
if ($("#welcome").hasClass("hidden")) {
$('#welcome').removeClass("hidden");
$('#welcome').fadeTo(0, 0);
$('#welcome').fadeTo(500, 1);
// Hide other sections
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$("#agenda").addClass("hidden"); // hide agenda for panels which are not main-navigation
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
}
$("#layout").removeClass("active"); // auto-close the navigation panel
});
// Click on RollCall button
$("#button-RollCall").click(function(){
if($('#tab-RollCall').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-RollCall').removeClass("hidden");
$('#tab-RollCall').fadeTo(0, 0);
$('#tab-RollCall').fadeTo(500, 1);
}
});
// Click on voting button
$("#button-voting").click(function(){
if($('#tab-voting').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-voting').removeClass("hidden");
$('#tab-voting').fadeTo(0, 0);
$('#tab-voting').fadeTo(500, 1);
}
});
// Click on GSL button
$("#button-gsl").click(function(){
if($('#tab-gsl').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-gsl').removeClass("hidden");
$('#tab-gsl').fadeTo(0, 0);
$('#tab-gsl').fadeTo(500, 1);
}
});
// Click on SSL button
$("#button-ssl").click(function(){
if($('#tab-ssl').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-ssl').removeClass("hidden");
$('#tab-ssl').fadeTo(0, 0);
$('#tab-ssl').fadeTo(500, 1);
}
});
// Click on Mod button
$("#button-mod").click(function(){
if($('#tab-mod').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-mod').removeClass("hidden");
$('#tab-mod').fadeTo(0, 0);
$('#tab-mod').fadeTo(500, 1);
}
});
// Click on Unmod button
$("#button-unmod").click(function(){
if($('#tab-unmod').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-resolution").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-unmod').removeClass("hidden");
$('#tab-unmod').fadeTo(0, 0);
$('#tab-unmod').fadeTo(500, 1);
}
});
// Click on Resolution button
$("#button-resolution").click(function(){
if($('#tab-resolution').hasClass('hidden')){ // Only if the panel is not visible / already selected
$(".main-navigation").removeClass("pure-menu-selected"); // de-highlight all options
// Hide other sections
$("#welcome").addClass("hidden");
$("#tab-RollCall").addClass("hidden");
$("#tab-voting").addClass("hidden");
$("#tab-gsl").addClass("hidden");
$("#tab-ssl").addClass("hidden");
$("#tab-mod").addClass("hidden");
$("#tab-unmod").addClass("hidden");
$(this).toggleClass("pure-menu-selected"); // Select clicked option
$("#layout").removeClass("active"); // auto-close the navigation panel
$("#agenda").removeClass("hidden"); // show current agenda in-case it was hidden
$('#tab-resolution').removeClass("hidden");
$('#tab-resolution').fadeTo(0, 0);
$('#tab-resolution').fadeTo(500, 1);
}
});
});
// Is populated by Ajax request once page loads; Contains list of all delegates
var masterList = [];
// List of delegates who are present
var currentList = [];
/////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Roll Call Section
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
// Bind event handlers for buttons after they are added to the page dynamically
function bindRollCallButtons() {
// Click on button 1 (absent)
$('.rollCallListItem-Button1-Text').click(function(){
if ($(this).hasClass('rollCallListItem-Button2-selected')) {
// Do nothing because the button is already selected
}
else {
// Remove formatting of the other buttons
var parent = $(this).parent();
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-2');
parent.find('.rollCallListItem-Button2-Text').removeClass('rollCallListItem-Button2-selected');
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-3');
parent.find('.rollCallListItem-Button3-Text').removeClass('rollCallListItem-Button3-selected');
// Add formatting for this button
parent.find('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-1');
$(this).addClass('rollCallListItem-Button1-selected');
// Modify the indexedDB
var delegate = parent.find('.rollCallListItem-Text').html();
db.transaction("Delegates").objectStore("Delegates").get(delegate).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 0; // Mark as absent
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
// If the delegate name exists in the currentList, remove the name
for (var i=0; i<currentList.length; i++) {
if (currentList[i] == delegate) { // If we have found the item (it exists)
currentList.splice(i, 1);
break;
}
}
// Bind the modified array for the autocomplete
AutocompleteSources();
}
});
// Click on button 2 (present)
$('.rollCallListItem-Button2-Text').click(function(){
if ($(this).hasClass('rollCallListItem-Button2-selected')) {
// Do nothing because the button is already selected
}
else {
// Remove formatting of the other buttons
var parent = $(this).parent();
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-3');
parent.find('.rollCallListItem-Button3-Text').removeClass('rollCallListItem-Button3-selected');
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-1');
parent.find('.rollCallListItem-Button1-Text').removeClass('rollCallListItem-Button1-selected');
// Add formatting for this button
parent.find('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-2');
$(this).addClass('rollCallListItem-Button2-selected');
// Modify the indexedDB
var delegate = parent.find('.rollCallListItem-Text').html();
db.transaction("Delegates").objectStore("Delegates").get(delegate).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 1; // Mark as present
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
// If the delegate name exists in the currentList, remove the name
for (var i=0; i<currentList.length; i++) {
if (currentList[i] == delegate) { // If we have found the item (it exists)
currentList.splice(i, 1);
break;
}
}
// Add the delegate name exists to the currentList
currentList.push(delegate);
// Bind the modified array for the autocomplete
AutocompleteSources();
}
});
// Click on button 3 (present & voting)
$('.rollCallListItem-Button3-Text').click(function(){
if ($(this).hasClass('rollCallListItem-Button3-selected')) {
// Do nothing because the button is already selected
}
else {
// Remove formatting of the other buttons
var parent = $(this).parent();
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-2');
parent.find('.rollCallListItem-Button2-Text').removeClass('rollCallListItem-Button2-selected');
parent.find('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-1');
parent.find('.rollCallListItem-Button1-Text').removeClass('rollCallListItem-Button1-selected');
// Add formatting for this button
parent.find('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-3');
$(this).addClass('rollCallListItem-Button3-selected');
// Modify the indexedDB
var delegate = parent.find('.rollCallListItem-Text').html();
db.transaction("Delegates").objectStore("Delegates").get(delegate).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 2; // Mark as present & voting
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
// If the delegate name exists in the currentList, remove the name
for (var i=0; i<currentList.length; i++) {
if (currentList[i] == delegate) { // If we have found the item (it exists)
currentList.splice(i, 1);
break;
}
}
// Add the delegate name exists to the currentList
currentList.push(delegate);
// Bind the modified array for the autocomplete
AutocompleteSources();
}
});
}
function CalcRollCallStatus() {
var total = masterList.length;
document.getElementById('rollCallStatus-total').innerHTML = total;
var absent=0, present = 0, presentAndVoting = 0;
db.transaction("Delegates").objectStore("Delegates").openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) { // If object exists in DB
var statement = "cursor.value." + currentSession + ".attendance;";
var result = eval(statement);
if (result == 1) present++;
else if (result == 2) presentAndVoting++;
else absent++;
cursor.continue();
}
else {
// After counting, display status
var absentPercent = (absent/total)*100;
var presentPercent = (present/total)*100;
var presentAndVotingPercent = (presentAndVoting/total)*100;
document.getElementById('rollCallStatus-absent').innerHTML = absent + " (" + Math.round(absentPercent*10)/10 + "%)";
document.getElementById('rollCallStatus-present').innerHTML = present + " (" + Math.round(presentPercent*10)/10 + "%)";
document.getElementById('rollCallStatus-presentAndVoting').innerHTML = presentAndVoting + " (" + Math.round(presentAndVotingPercent*10)/10 + "%)";
}
}
}
// Mark all absent
function RollCallAbsent() {
// Remove formatting of the other button
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-2');
$('.rollCallListItem-Button2-Text').removeClass('rollCallListItem-Button2-selected');
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-3');
$('.rollCallListItem-Button3-Text').removeClass('rollCallListItem-Button3-selected');
// Add formatting for this button
$('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-1');
$('.rollCallListItem-Button1-Text').addClass('rollCallListItem-Button1-selected');
// Mark absent in indexedDB
for (var delegate in masterList) {
db.transaction("Delegates").objectStore("Delegates").get(masterList[delegate]).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 0; // Mark as absent
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
}
// Clear currentList
currentList.length = 0;
// Bind the modified array for the autocomplete
AutocompleteSources();
}
// Mark all present
function RollCallPresent() {
// Remove formatting of the other button
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-1');
$('.rollCallListItem-Button1-Text').removeClass('rollCallListItem-Button1-selected');
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-3');
$('.rollCallListItem-Button3-Text').removeClass('rollCallListItem-Button3-selected');
// Add formatting for this button
$('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-2');
$('.rollCallListItem-Button2-Text').addClass('rollCallListItem-Button2-selected');
// Mark present in indexedDB
for (var delegate in masterList) {
db.transaction("Delegates").objectStore("Delegates").get(masterList[delegate]).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 1; // Mark as present
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
}
// Clear currentList
currentList.length = 0;
// Add all delegates to currentList
for (var i=0; i<masterList.length; i++) {
currentList.push(masterList[i]);
}
// Bind the modified array for the autocomplete
AutocompleteSources();
}
// Mark all present
function RollCallPresentAndVoting() {
// Remove formatting of the other button
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-1');
$('.rollCallListItem-Button1-Text').removeClass('rollCallListItem-Button1-selected');
$('.rollCallListItem-Text').removeClass('rollCallListItem-Text-selected-2');
$('.rollCallListItem-Button2-Text').removeClass('rollCallListItem-Button2-selected');
// Add formatting for this button
$('.rollCallListItem-Text').addClass('rollCallListItem-Text-selected-3');
$('.rollCallListItem-Button3-Text').addClass('rollCallListItem-Button3-selected');
// Mark present and voting in indexedDB
for (var delegate in masterList) {
db.transaction("Delegates").objectStore("Delegates").get(masterList[delegate]).onsuccess = function(event) {
tempDelegate = event.target.result;
tempDelegate.session0.attendance = 2; // Mark as present and voting
db.transaction("Delegates" ,"readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) { // Update the database
CalcRollCallStatus();
}
};
}
// Clear currentList
currentList.length = 0;
// Add all delegates to currentList
for (var i=0; i<masterList.length; i++) {
currentList.push(masterList[i]);
}
// Bind the modified array for the autocomplete
AutocompleteSources();
}
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
Voting Section
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
var votingYes, votingNo, votingAbstain; // for counting
function startVoting() {
// Start the voting
if (document.getElementById('votingButton-start').innerHTML == 'Start Vote'){
//Calculate_votingSerialNumber();
var count = 0;
// Open Delegates objectstore with cursor
db.transaction("Delegates").objectStore("Delegates").openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) { // if object exists in DB
tempDelegate = cursor.value; // store the object from the DB in temp variable
if (tempDelegate.session0.attendance == 0) { //absent
tempDelegate.votingSerialNumber = 0;
}
else {
tempDelegate.votingSerialNumber = ++count;
}
tempDelegate.tempVoting = 0; // reset
db.transaction("Delegates", "readwrite").objectStore("Delegates").put(tempDelegate);
cursor.continue();
}
else {
db.transaction("Variables", "readwrite").objectStore("Variables").put(count, "presentDelegates").onsuccess = function(event) {
db.transaction("Variables").objectStore("Variables").get("presentDelegates").onsuccess = function(event) {
var presentDelegates = event.target.result;
if (presentDelegates < 1) return;
var index = db.transaction("Delegates").objectStore("Delegates").index("votingSerialNumber");
index.get(1).onsuccess = function(event) {
// Display the first portfolio
document.getElementById("votingLabel-delegate").innerHTML = event.target.result.portfolio;
// disable or enable the abstain button acoording to the first delegate
if (event.target.result.session0.attendance == 1) {
document.getElementById('votingButton-3').disabled = false; // Can abstain if delegate is only present
}
else {
document.getElementById('votingButton-3').disabled = true; // Cannot abstain if delegate is present & voting
}
document.getElementById('votingButton-start').innerHTML = 'Stop Vote'; // Change the button text
$('#votingButton-4').removeClass('hidden'); // Show pass button if it was hidden
document.getElementById("votingLabel-current").innerHTML = 1; // Set the counter
document.getElementById("votingLabel-max").innerHTML = presentDelegates; // Set the max count
$('#voting-results').addClass('hidden'); // hide results panel
$('#voting-main').removeClass('hidden'); // show voting panel
}
// To track number of yes and no
db.transaction("Variables", "readwrite").objectStore("Variables").put(0, "votingYes");
db.transaction("Variables", "readwrite").objectStore("Variables").put(0, "votingNo");
db.transaction("Variables", "readwrite").objectStore("Variables").put(0, "votingAbstain");
votingYes = 0, votingNo = 0, votingAbstain = 0;
}
}
}
}
}
// Stop the voting
else {
$('#voting-main').addClass('hidden'); // hide voting panel
document.getElementById('votingButton-start').innerHTML = 'Start Vote'; // Change the button text
}
}
var votingResultsChart;
window.onload = function(){
votingResultsChart = new CanvasJS.Chart("votingResults-chart", {
width: 200,
height: 200,
title:{
//text: "My First Chart in CanvasJS"
},
data: [
{
// Change type to "doughnut", "line", "splineArea", etc.
type: "pie",
dataPoints: [
{ label: "Yes", y: 0 },
{ label: "No", y: 0 },
{ label: "Abstain", y: 0 }
]
}
]
});
}
function votingControlButton(button) {
// Yes button
if (button.id == 'votingButton-1') {
db.transaction("Variables").objectStore("Variables").get("votingYes").onsuccess = function(event) {
var yes = event.target.result;
yes++;
db.transaction("Variables", "readwrite").objectStore("Variables").put(yes, "votingYes");
}
votingYes++;
}
// No button
else if (button.id == 'votingButton-2') {
db.transaction("Variables").objectStore("Variables").get("votingNo").onsuccess = function(event) {
var no = event.target.result;
no++;
db.transaction("Variables", "readwrite").objectStore("Variables").put(no, "votingNo");
}
votingNo++;
}
// Abstain button
else if (button.id == 'votingButton-3') {
db.transaction("Variables").objectStore("Variables").get("votingAbstain").onsuccess = function(event) {
var abstain = event.target.result;
abstain++;
db.transaction("Variables", "readwrite").objectStore("Variables").put(abstain, "votingAbstain");
}
votingAbstain++;
}
// Pass button
else {
// Do nothing
}
// Save whether the delgate has finished voting
var current = document.getElementById("votingLabel-current").innerHTML;
var currentNumber = Number(current); // Convert string to number
db.transaction("Delegates").objectStore("Delegates").index("votingSerialNumber").get(currentNumber).onsuccess = function(event) {
tempDelegate = event.target.result;
if (button.id == 'votingButton-4') {
tempDelegate.tempVoting = 0; // delegate has passed
}
else {
tempDelegate.tempVoting = 1; // delegate has finished voting
}
db.transaction("Delegates", "readwrite").objectStore("Delegates").put(tempDelegate).onsuccess = function(event) {
// Check if voting has been completed
if (votingYes + votingNo + votingAbstain == document.getElementById('votingLabel-max').innerHTML) {
// Show the results
document.getElementById('votingLabel-yes').innerHTML = votingYes;
document.getElementById('votingLabel-no').innerHTML = votingNo;
document.getElementById('votingLabel-abstain').innerHTML = votingAbstain;
var percent = (votingYes/(votingYes+votingNo+votingAbstain))*100;
document.getElementById('votingLabel-percent').innerHTML = Math.round(percent*100)/100;
if (document.getElementById('votingLabel-percent').innerHTML > 50) {
document.getElementById('votingLabel-statement').innerHTML = "Vote passes.";
}
else {
document.getElementById('votingLabel-statement').innerHTML = "Vote fails.";
}
$('#voting-results').removeClass('hidden');
$('#voting-main').addClass('hidden');
document.getElementById('votingButton-start').innerHTML = 'Start Vote';
votingResultsChart.options.data[0].dataPoints[0].y = votingYes;
votingResultsChart.options.data[0].dataPoints[1].y = votingNo;
votingResultsChart.options.data[0].dataPoints[2].y = votingAbstain;
votingResultsChart.render();
return;
}
// Check if it is the last delegate
if (current == document.getElementById("votingLabel-max").innerHTML) {
// Start the second round for those who passed
currentNumber = 0;
// Disable the pass button
$('#votingButton-4').addClass('hidden');
}
// Go to next delegate
currentNumber++;
var index = db.transaction("Delegates").objectStore("Delegates").index("votingSerialNumber");
index.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
if (cursor.value.votingSerialNumber != currentNumber) {
cursor.continue();
}
else {
if (cursor.value.tempVoting == 1) {
cursor.continue();
currentNumber++;
}
else {
// Display the next portfolio
document.getElementById("votingLabel-delegate").innerHTML = cursor.value.portfolio;
// disable or enable the abstain button acoording to the delegate
if (cursor.value.session0.attendance == 1) {
document.getElementById('votingButton-3').disabled = false; // Can abstain if delegate is only present
}
else {
document.getElementById('votingButton-3').disabled = true; // Cannot abstain if delegate is present& voting
}
document.getElementById("votingLabel-current").innerHTML = currentNumber; // Set the counter
}
}
}
}
}
}
}
function Calculate_votingSerialNumber() {
var count = 0;
var objectStore = db.transaction("Delegates").objectStore("Delegates");
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
tempDelegate = cursor.value;
if (tempDelegate.session0.attendance == 0) { //absent
tempDelegate.votingSerialNumber = 0;
}
else {
count++;
tempDelegate.votingSerialNumber = count;
}
db.transaction("Delegates", "readwrite").objectStore("Delegates").put(tempDelegate).onerror = function(event) {
console.log("Couldn't update the DB while calculating votingSerialNumber!");
}
cursor.continue();
}
else {
db.transaction("Variables", "readwrite").objectStore("Variables").put(count, "presentDelegates");
}
}
}
////////////////////////////////////////////////////////////////////////////////
/*------------------------------------------------------------------------------
General Speakers List (GSL) Section
------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////
var gslArray = [], gslSelectionArray = [], previousSpeaker;
var gslSpeakerMins = 0, gslSpeakerSecs = 0, gslSpeakerSetMins = 0, gslSpeakerSetSecs = 0, gslSpeakerTimeTotal = 0;
var gslSpeakerControl, yieldToDelegate = false;
// Add the speaker to GSL
function GSLAddSpeaker() {
// Once autocomplete enters the value into the field, we add the speaker to the GSL
var delegate = document.getElementById('gslAutocomplete-speaker').value;
var i=0;
// we need to keep trying, since autocomplete plugin takes some time to enter the value in the textfield and speed varies on the device
var retry = setInterval(function(){
if (i==12) clearInterval(retry); // Stop retrying after 12 times
i++;
// Get the portfolio in the field
db.transaction("Delegates").objectStore("Delegates").get(delegate).onsuccess = function (event) {
// The result is not undefined, which means that the value in the field is a valid portfolio
if (event.target.result != undefined) {
// Stop trying because we have found the portfolio
clearInterval(retry);
// Clear the field
document.getElementById('gslAutocomplete-speaker').value = "";
// A click anywhere on the page (in this case on main).
//This solves an issue with the autocomplete suggestions disappearing when clicking on the field
document.getElementById('main').click();
// Check if the delegate already exists in the list
// for (var j=0; j<gslArray.length; j++) {
// if (gslArray[j] == delegate) {
// // The delegate is already in the list, so do nothing
// return;
// }
// }
// Add the delegate to the list of speakers
if (yieldToDelegate == false) {
$('#GSLList').append('<div class="GSLListItem ui-sortable-handle">' +
'<div class="GSLListItem-Text">' + delegate + '</div>' +
'<div class="GSLListItem-Button2-Text"><i class="fa fa-bars" aria-hidden="true"></i></div>' +
'<div class="GSLListItem-Button1-Text"><i class="fa fa-times" aria-hidden="true"></i></div>' +
'</div>');
}
else {
// If yeilding to delegate, that delegate should be added to the beginning
$('#GSLList').prepend('<div class="GSLListItem ui-sortable-handle">' +
'<div class="GSLListItem-Text">' + delegate + '</div>' +
'<div class="GSLListItem-Button2-Text"><i class="fa fa-bars" aria-hidden="true"></i></div>' +
'<div class="GSLListItem-Button1-Text"><i class="fa fa-times" aria-hidden="true"></i></div>' +
'</div>');
// Set yield to delegate as false
yieldToDelegate = false;
// Modify the autocomplete field label
document.getElementById('gslAutocompleteLabel-speaker').innerHTML = "Add speaker";
document.getElementById('gslAutocompleteLabel-speaker').style.color = '#000';
// Set the speaker
db.transaction("Variables", "readwrite").objectStore("Variables").put(delegate, "gslSpeaker");
// Add this speaker as the yielded to delegate of the previous speaker
db.transaction("Delegates").objectStore("Delegates").get(previousSpeaker).onsuccess = function(event) {
var tempDelegate = event.target.result;
var list;
var statement = "list = tempDelegate." + currentSession + ".gslYieldToDelegateList.push(delegate)"; // Add this speaker to yielded to list of previous speaker
eval(statement);
// Update the DB by replacing the delegate object
db.transaction("Delegates", "readwrite").objectStore("Delegates").put(tempDelegate);
}
// Load flag
gslFlagLoad(delegate);
// Resume the countdown
gslPause();
}
// Add the delegate to the array
gslArray.push(delegate);
// Bind the event handler so that the remove button works for this new item
bindGSLRemoveButton();
// Set the first delegate as the selected delegate
gslListSelection();
}