var config = { apiKey: "AIzaSyA4De5itV56yaOBfBW6Cnk3fS7skPmDCHM", authDomain: "punchlist-1561043639952.firebaseapp.com", databaseURL: "https://punchlist-1561043639952.firebaseio.com", projectId: "punchlist-1561043639952", storageBucket: "", messagingSenderId: "999467953896", appId: "1:999467953896:web:a4ded59b12ccb9ff" }; //firebase.initializeApp(firebaseConfig); if (!firebase.apps.length) { firebase.initializeApp(config); } function initApp() { // Auth state changes. // [START authstatelistener] firebase.auth().onAuthStateChanged(function(user){ console.log(`in onAuthStateChanged`); if (user) { console.log(`${user.displayName}`); // User is signed in. var displayName = user.displayName; var email = user.email; var emailVerified = user.emailVerified; var photoURL = user.photoURL; var isAnonymous = user.isAnonymous; var uid = user.uid; window.uid = uid; var providerData = user.providerData; writeUserData(uid, displayName, email, photoURL); //newPunch(uid); loadPunches(uid); document.getElementById('whoami').innerHTML = email; // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed in'; //document.getElementById('signout').disabled = false; //document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' '); // [END_EXCLUDE] } else { // User is signed out. // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed out'; //document.getElementById('signout').disabled = true; //document.getElementById('quickstart-account-details').textContent = 'null'; // [END_EXCLUDE] } }); // [END authstatelistener] //document.getElementById('signout').addEventListener('click', handleSignOut, true); } // AUTH // // [START googlecallback] function onSignIn(googleUser) { console.log('Google Auth Response', googleUser); // We need to register an Observer on Firebase Auth to make sure auth is initialized. var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) { unsubscribe(); // Check if we are already signed-in Firebase with the correct user. if (!isUserEqual(googleUser, firebaseUser)) { // Build Firebase credential with the Google ID token. // [START googlecredential] var credential = firebase.auth.GoogleAuthProvider.credential( googleUser.getAuthResponse().id_token); // [END googlecredential] // Sign in with credential from the Google user. // [START authwithcred] firebase.auth().signInWithCredential(credential).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // [START_EXCLUDE] if (errorCode === 'auth/account-exists-with-different-credential') { alert('You have already signed up with a different auth provider for that email.'); // If you are using multiple auth providers on your app you should handle linking // the user's accounts here. } else { console.error(error); } // [END_EXCLUDE] }); // [END authwithcred] } else { var user = googleUser; console.log('User already signed-in Firebase.'); var displayName = user.displayName; var email = user.email; var emailVerified = user.emailVerified; var photoURL = user.photoURL; var isAnonymous = user.isAnonymous; var uid = user.uid; var providerData = user.providerData; // [START_EXCLUDE] //document.getElementById('quickstart-sign-in-status').textContent = 'Signed in'; //document.getElementById('signout').disabled = false; //document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' '); // [END_EXCLUDE] } }); } // [END googlecallback] /** * Check that the given Google user is equals to the given Firebase user. */ // [START checksameuser] function isUserEqual(googleUser, firebaseUser) { if (firebaseUser) { var providerData = firebaseUser.providerData; for (var i = 0; i < providerData.length; i++) { if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID && providerData[i].uid === googleUser.getBasicProfile().getId()) { // We don't need to reauth the Firebase connection. return true; } } } return false; } // [END checksameuser] /** * Handle the sign out button press. */ function handleSignOut() { var googleAuth = gapi.auth2.getAuthInstance(); googleAuth.signOut().then(function() { firebase.auth().signOut(); }); } function writeUserData(userId, name, email, imageUrl) { firebase.database().ref('users/' + userId).update({ username: name, email: email, profile_picture : imageUrl }); } //var punchesRef = firebase.database().ref('users/' + userId + '/punches'); //punchesRef.on('value', function(snapshot) { // updateStarCount(postElement, snapshot.val()); //}); function newPunch(uid, subject, priority, progress, needBy, notes, tags) { var punchData = { uid: uid, subject: subject, priority: priority, progress: progress, needByDate: needBy, notes: notes, tags: tags }; //Get a key for a new post var newPunchKey = firebase.database().ref().child('users/' + window.uid + '/punches').push().key; // Write the new punch data var updates = {}; updates['users/' + uid + '/punches/' + newPunchKey] = punchData; return firebase.database().ref().update(updates); } // Read the punches via listener // standard functions function clearDefault(a){ if (a.defaultValue === a.value) { a.value=""; } } // some element functions... function enableElement(element) { console.log(`enabling ${element}`); document.getElementById(element).style.display = "block"; } function disableElement(element) { console.log(`disabling ${element}`); document.getElementById(element).style.display = "none"; } // menu function mainMenuDrop() { document.getElementById("mainMenuDropdown").classList.toggle("show"); } window.onclick = function(event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } } // load punches function genPunchListItem(elementData, element) { $( elementData ).appendTo( element ); } function loadPunches(uid) { var punchesRef = firebase.database().ref('users/' + uid + '/punches'); var itemStyle = "punches"; // list = '
    '; punchesRef.once('value', function(snapshot) { snapshot.forEach(function(childSnapshot) { var childKey = childSnapshot.key; var childData = childSnapshot.val(); console.log("Child Key: " + childKey); if (childData.progress.toLowerCase() === "in progress") { var style = "inProgress"; } else if (childData.progress.toLowerCase() === "waiting") { var style = "waiting"; } else if (childData.progress.toLowerCase() === "new") { var style = "punch-default"; } else { style = "punch-default" } var list = ''; if (childData.progress.toLowerCase() != "done") { genPunchListItem('
  1. ', '#sortable'); genPunchListItem('
    ', '#li' + childKey); genPunchListItem('
    ', '#div-portlet' + childKey); genPunchListItem('
    ' + childData.priority + '
    ', '#div-backlog-list-header' + childKey); genPunchListItem('
    ' + childData.subject + '
    ', '#div-backlog-list-header' + childKey); genPunchListItem('
    ' + childData.progress + '
    ', '#div-backlog-list-header' + childKey); // status dropdown genPunchListItem('', '#div-backlog-list-header' + childKey); genPunchListItem('', '#dropdown-wrapper' + childKey); genPunchListItem('', '#dropdown-wrapper' + childKey); genPunchListItem('New', '#progressDropdown-' + childKey); genPunchListItem('Start', '#progressDropdown-' + childKey); genPunchListItem('Waiting', '#progressDropdown-' + childKey); genPunchListItem('Finish', '#progressDropdown-' + childKey); if ( childData.needByDate != null && childData.needByDate != undefined && childData.needByDate != '' ) { genPunchListItem('
    ', '#div-backlog-list-header' + childKey); genPunchListItem('
    ', '#div-needby-wrapper' + childKey); genPunchListItem('
    ' + childData.needByDate + '
    ', '#neededBy' + childKey); genPunchListItem('
    ', '#div-needby-wrapper' + childKey); } else { genPunchListItem('
     
    ', '#div-backlog-list-header' + childKey); } if ( (new Date(childData.needByDate).getTime() - new Date().getTime()) <= 0 ) { genPunchListItem('
    OVER DUE
    ', '#div-backlog-list-header' + childKey); } else if ( ((new Date(childData.needByDate).getTime() - new Date().getTime()) / 1000) <= 259200 ) { genPunchListItem('
    DUE SOON
    ', '#div-backlog-list-header' + childKey); } else { genPunchListItem('
     
    ', '#div-backlog-list-header' + childKey); } if ( childData.tags != undefined ) { var tags = childData.tags; console.log("tags" + tags); genPunchListItem('
    ', '#div-backlog-list-header' + childKey); var i; for (i=0; i' + tagData + '', '#tags-wrapper' + childKey); if ( (i + 1) < tags.length ) { genPunchListItem(", ", '#tags-wrapper' + childKey);; } } } //genPunchListItem('
    '; //if ( childData.startTime != undefined ) { // genPunchListItem('
    '; // genPunchListItem(new Date(childData.startTime); // genPunchListItem('
    '; //} //if ( childData.notes != "" ) { // genPunchListItem('
    '; //} //genPunchListItem(''; //genPunchListItem('
    '; // genPunchListItem(list, '#sortable'); } }) }); // genPunchListItem('
'; // document.getElementById("punchListBacklog").innerHTML = list; } // create new punch function genEventForm() { document.getElementById("newSubject").value = "subject"; document.getElementById("newPriority").value = "priority"; document.getElementById("timepickerCreate").value = "date"; document.getElementById("newNotes").value = ''; document.getElementById("tagsCreate").value = 'tag1,tag2, tag3'; disableElement('punchListAll'); enableElement('newEvent'); } function createNewEvent() { disableElement("punchListAll"); enableElement("newEvent"); var subjectField = document.getElementById("newSubject").value; var priorityField = document.getElementById("newPriority").value; var progressField = document.getElementById("newProgress").value; var nDateField = document.getElementById("timepickerCreate").value; var notesField = document.getElementById("newNotes").value; var newTag = document.getElementById("tagsCreate").value.toLowerCase(); var stripLeadingSpace = newTag.replace(/, /g, ','); var noSpaces = stripLeadingSpace.replace(/ /g, '_'); var newTags = noSpaces.split(","); newPunch(window.uid, subjectField, priorityField, progressField, nDateField, notesField, newTags) disableElement("newEvent"); enableElement("punchListAll"); // document.getElementById("newEventList").innerHTML = jsonStr; } // load everything up! window.onload = function() { initApp(); }; function append() { $( "

Hello There!

" ).appendTo( ".punchListBacklog" ); } // old script