-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.js
128 lines (122 loc) · 4.65 KB
/
viewer.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
var database, data, pairingsTable, standingsTable;
const select = document.getElementById('tournament');
$(document).ready(() => {
// Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyDHF0Y3_9jGK36heIMdtjU1y2Mf2oou1Fw",
authDomain: "bracketeer-22018.firebaseapp.com",
databaseURL: "https://bracketeer-22018-default-rtdb.firebaseio.com",
projectId: "bracketeer-22018",
storageBucket: "bracketeer-22018.appspot.com",
messagingSenderId: "633046274851",
appId: "1:633046274851:web:f42e6fb356e6ea94a7b296",
measurementId: "G-RMXF948GSQ"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
database = firebase.database();
// Get initial data
database.ref('viewer').get().then(snapshot => {
data = snapshot.val();
// Fill dropdown with tournaments
for (const prop in data) {
const opt = document.createElement('option');
opt.value = prop;
opt.text = data[prop].name;
select.add(opt);
}
// Load table if search params present
let url = new URL(window.location.href);
if (url.searchParams.has('data')) select.value = url.searchParams.get('data');
var ref = database.ref('viewer');
ref.on('value', snap => {
const tableData = snap.val()[select.value];
if (select.value.length === 0) {
return;
} else if (tableData !== undefined && (pairingsTable === undefined || standingsTable === undefined)) {
pairingsTable = $('#pairings').DataTable({
data: tableData.pairings,
scrollY: '290px',
scrollCollapse: true,
paging: false,
responsive: true,
columns: tableData.columns.pairings
});
standingsTable = $('#standings').DataTable({
data: tableData.standings,
scrollY: '290px',
scrollCollapse: true,
paging: false,
responsive: true,
columns: tableData.columns.standings
});
} else {
if (tableData === undefined) {
alert('The tournament is now over.');
pairingsTable.clear().draw();
standingsTable.clear().draw();
return;
}
try {
pairingsTable.clear().rows.add(tableData.pairings).draw();
} catch (err) {
pairingsTable.clear().draw();
}
try {
standingsTable.clear().rows.add(tableData.standings).draw();
} catch (err) {
standingsTable.clear().draw();
}
}
});
});
});
const loadTournament = () => {
database.ref('viewer/' + select.value).get().then(snapshot => {
const newEvent = snapshot.val();
if (newEvent === null) {
alert('The tournament is now over.');
pairingsTable.clear().draw();
standingsTable.clear().draw();
return;
}
if (pairingsTable === undefined || standingsTable === undefined) {
pairingsTable = $('#pairings').DataTable({
data: newEvent.pairings,
scrollY: '290px',
scrollCollapse: true,
paging: false,
responsive: true,
columns: newEvent.columns.pairings
});
standingsTable = $('#standings').DataTable({
data: newEvent.standings,
scrollY: '290px',
scrollCollapse: true,
paging: false,
responsive: true,
columns: newEvent.columns.standings
});
return;
}
try {
pairingsTable.clear().rows.add(newEvent.pairings).draw();
} catch (err) {
pairingsTable.clear().draw();
}
try {
standingsTable.destroy();
$('#standings').empty();
standingsTable = $('#standings').DataTable({
data: newEvent !== undefined ? newEvent.standings : {},
scrollY: '290px',
scrollCollapse: true,
paging: false,
columns: newEvent !== undefined ? newEvent.columns.standings : {}
});
standingsTable.clear().rows.add(newEvent.standings).draw();
} catch (err) {
standingsTable.clear().draw();
}
});
}