tra-analysis/website/public/team/scripts.js

181 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-02-06 23:51:58 +00:00
/* Set the width of the side navigation to 250px and the left margin of the page content to 250px and add a black background color to body */
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
2019-02-13 02:20:30 +00:00
for (var i = 0; i < document.getElementsByClassName("btn").length; i++) {
2019-02-13 02:34:05 +00:00
document.getElementsByClassName("btn")[i].style.backgroundColor = "rgba(0,0,0,.2)"
2019-02-13 02:20:30 +00:00
}
2019-02-06 23:51:58 +00:00
}
/* Set the width of the side navigation to 0 and the left margin of the page content to 0, and the background color of body to white */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
2019-02-13 02:20:30 +00:00
for (var i = 0; i < document.getElementsByClassName("btn").length; i++) {
2019-02-13 02:34:05 +00:00
document.getElementsByClassName("btn")[i].style.backgroundColor = "buttonface"
2019-02-13 02:20:30 +00:00
}
2019-02-06 23:51:58 +00:00
}
2019-02-07 00:19:18 +00:00
window.onload = function() {
2019-02-07 00:19:18 +00:00
document.getElementById('sideload').style.display = 'block';
var config = {
apiKey: "(insert the TitanScout Api Key Here)",
authDomain: "titanscoutandroid.firebaseapp.com",
databaseURL: "https://titanscoutandroid.firebaseio.com",
projectId: "titanscoutandroid",
storageBucket: "titanscoutandroid.appspot.com",
messagingSenderId: "1097635313476"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
2019-02-13 02:37:50 +00:00
if (user != null) {
if (user.displayName != null) {
document.getElementById('status').innerHTML = "You are signed in as: " + user.displayName;
document.getElementById('newDN').innerHTML = user.displayName;
} else if (user.email != null) {
document.getElementById('status').innerHTML = "You are signed in as: " + user.email;
} else if (user.phoneNumber != null) {
document.getElementById('status').innerHTML = "You are signed in as: " + user.phoneNumber;
} else {
2019-02-13 02:37:50 +00:00
document.getElementById('status').innerHTML = "You are signed in.";
}
if (user.email != null) {
document.getElementById('newEM').innerHTML = user.email;
}
2019-02-13 02:37:50 +00:00
} else {
window.location.replace('../');
}
2019-02-13 02:44:21 +00:00
teamAssoc = firebase.firestore().collection('UserAssociations').doc(user.uid);
2019-02-13 02:37:50 +00:00
teamAssoc.get().then(function(doc) {
if (doc.exists) {
list = doc.data()
teamNums = Object.keys(list)
document.getElementById('teammem').innerHTML = ""
for (var i = 0; i < teamNums.length; i++) {
document.getElementById('teammem').innerHTML += "<tr><td>" + teamNums[i] + "</td><td>" + list[teamNums[i]] + "</td></tr>"
2019-02-13 02:20:30 +00:00
}
2019-02-13 02:37:50 +00:00
} else {
document.getElementById('teammem').innerHTML = "<tr><td>You are not part of any teams</td></tr>"
2019-02-13 02:37:50 +00:00
}
})
});
2019-03-08 02:13:32 +00:00
firebase.firestore.settings({
timestampsInSnapshots: true
})
2019-02-07 00:19:18 +00:00
}
2019-02-13 02:34:05 +00:00
2019-02-13 02:20:30 +00:00
function cnt(tn) {
2019-03-08 02:13:32 +00:00
user = firebase.auth().currentUser;
push = {}
push[tn] = 'captian'
2019-02-13 03:00:00 +00:00
firebase.firestore().collection("UserAssociations").doc(user.uid).set(push, {
2019-02-13 02:34:05 +00:00
merge: true
2019-02-13 02:46:23 +00:00
}).then(function() {
teamAssoc = firebase.firestore().collection('UserAssociations').doc(user.uid)
2019-02-13 02:42:49 +00:00
teamAssoc.get().then(function(doc) {
2019-02-13 02:34:05 +00:00
if (doc.exists) {
list = doc.data()
teamNums = Object.keys(list)
2019-02-13 03:00:43 +00:00
document.getElementById('teammem').innerHTML = ""
for (var i = 0; i < teamNums.length; i++) {
2019-03-08 02:13:32 +00:00
document.getElementById('teammem').innerHTML += "<tr><td>" + teamNums[i] + "</td><td>" + list[teamNums[i]] + "</td>" + "<td><input type='button' class='btn' value='leave' onclick='leaveTeam(" + teamNums[i] + ")'></td></tr>"
2019-02-13 02:20:30 +00:00
}
2019-02-13 02:34:05 +00:00
} else {
document.getElementById('teammem').innerHTML = "<tr><td>You are not part of any teams</td></tr>"
2019-02-13 02:20:30 +00:00
}
})
2019-02-13 02:46:23 +00:00
})
2019-02-13 02:20:30 +00:00
}
2019-03-08 02:13:32 +00:00
function checkKeyMatch(dt, tn, key) {
for (i = 0; i < Object.keys(dt).length; i++) {
if (Object.keys(dt)[i] == "code-" + key) {
if (dt[Object.keys(dt)[i]] == "team-" + tn) {
return true
}
2019-03-08 01:31:34 +00:00
}
2019-03-08 02:13:32 +00:00
}
return false
2019-03-08 01:31:34 +00:00
}
2019-03-08 02:13:32 +00:00
function reqjt(tn, tc) {
user = firebase.auth().currentUser;
firebase.firestore().collection('teamData').doc('joinCodes').get().then(function(doc) {
2019-03-08 01:43:50 +00:00
if (doc.exists) {
2019-03-08 02:13:32 +00:00
dict = doc.data();
if (checkKeyMatch(dict, tn, tc)) {
push = {};
push[tn] = 'scout';
firebase.firestore().collection("UserAssociations").doc(user.uid).set(push, {
merge: true
}).then(function() {
teamAssoc = firebase.firestore().collection('UserAssociations').doc(user.uid)
teamAssoc.get().then(function(doc) {
if (doc.exists) {
list = doc.data()
teamNums = Object.keys(list)
document.getElementById('teammem').innerHTML = ""
for (var i = 0; i < teamNums.length; i++) {
document.getElementById('teammem').innerHTML += "<tr><td>" + teamNums[i] + "</td><td>" + list[teamNums[i]] + "</td>" + "<td><input type='button' class='btn' value='leave' onclick='leaveTeam(" + teamNums[i] + ")'></td></tr>"
}
} else {
document.getElementById('teammem').innerHTML = "<tr><td>You are not part of any teams</td></tr>"
}
})
})
} else {
alert("You don't have a correct join key. Please check it and try again.")
console.log(dict)
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
2019-03-08 01:43:50 +00:00
});
2019-03-08 01:47:35 +00:00
2019-03-08 01:31:34 +00:00
}
2019-02-13 02:34:05 +00:00
2019-03-08 02:13:32 +00:00
function leaveTeam(tn) {
teamAssoc = firebase.firestore().collection('UserAssociations').doc(user.uid)
teamAssoc.get().then(function(doc) {
2019-03-08 02:16:14 +00:00
if (doc.exists) {
list = doc.data()
delete list[tn]
firebase.firestore().collection("UserAssociations").doc(user.uid).set(list).then(function() {
teamAssoc = firebase.firestore().collection('UserAssociations').doc(user.uid)
teamAssoc.get().then(function(doc) {
if (doc.exists) {
list = doc.data()
teamNums = Object.keys(list)
document.getElementById('teammem').innerHTML = ""
for (var i = 0; i < teamNums.length; i++) {
document.getElementById('teammem').innerHTML += "<tr><td>" + teamNums[i] + "</td><td>" + list[teamNums[i]] + "</td>" + "<td><input type='button' class='btn' value='leave' onclick='leaveTeam(" + teamNums[i] + ")'></td></tr>"
2019-03-08 02:13:32 +00:00
}
2019-03-08 02:16:14 +00:00
} else {
document.getElementById('teammem').innerHTML = "<tr><td>You are not part of any teams</td></tr>"
}
2019-03-08 02:13:32 +00:00
})
2019-03-08 02:16:14 +00:00
})
2019-03-08 02:13:32 +00:00
}
2019-03-08 02:16:14 +00:00
})
2019-02-13 02:20:30 +00:00
2019-03-08 02:13:32 +00:00
function signout() {
var user = firebase.auth().currentUser;
firebase.auth().signOut().then(
2019-02-13 02:34:05 +00:00
window.location.href = '../');
2019-03-08 02:13:32 +00:00
}
function deleteAccount() {
try {
firebase.auth().currentUser.delete().then(
window.location.href = '../');
} catch (error) {
if (error.code == 'auth/requires-recent-login') {
alert("Please sign in again to delete your account.")
window.location.href = '../';
}
2019-02-13 02:20:30 +00:00
}
}