From 3d4226d7118a118d0a4457f5d6d47dcd5af8df90 Mon Sep 17 00:00:00 2001 From: joetime Date: Wed, 7 Nov 2018 12:51:26 -0500 Subject: [PATCH 1/8] adding firebase integration for the ability to save and load puzzles. In progress, basics working, UI needs updating. --- cross.js | 38 +++++++------- firebaseConfig.js | 98 +++++++++++++++++++++++++++++++++++ index.html | 127 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 235 insertions(+), 28 deletions(-) create mode 100644 firebaseConfig.js diff --git a/cross.js b/cross.js index f03ae37..bb2e7c7 100644 --- a/cross.js +++ b/cross.js @@ -283,12 +283,14 @@ current.update(); // F U N C T I O N S function createNewPuzzle(rows, cols) { + xw = {}; // clear all fields, start from scratch xw["clues"] = {}; xw["title"] = DEFAULT_TITLE; xw["author"] = DEFAULT_AUTHOR; xw["rows"] = rows || DEFAULT_SIZE; xw["cols"] = cols || xw.rows; xw["fill"] = []; + for (let i = 0; i < xw.rows; i++) { xw.fill.push(""); for (let j = 0; j < xw.cols; j++) { @@ -439,7 +441,7 @@ function keyboardHandler(e) { // new entry holder let newFill = null; - + // stop default keypresses if (isSpace || isEnter || isBackspace || isDirection) e.preventDefault(); @@ -453,17 +455,17 @@ function keyboardHandler(e) { // get string newFill = String.fromCharCode(e.which); - + // move cursor after forceNextInput = current.direction == ACROSS ? keyboard.right : keyboard.down; - } + } // blacks if (isBlack && !wasBlack) newFill = BLACK; - + // blanks if (isBlack && wasBlack || isBackspace) newFill = BLANK; - + // move after backspace if (isBackspace) forceNextInput = current.direction == ACROSS ? keyboard.left : keyboard.up; @@ -486,10 +488,10 @@ function keyboardHandler(e) { // check for symmetry if (isSymmetrical && (wasBlack && newFill == BLANK || wasBlank && newFill == BLACK)) { - + // fill partner xw.fill[symRow] = xw.fill[symRow].slice(0, symCol) + newFill + xw.fill[symRow].slice(symCol + 1); - + // save sym history histObj.sym = { row: symRow, @@ -518,7 +520,7 @@ function keyboardHandler(e) { // check if real or fake input const isFakeEvent = e.key == undefined; - // let content = xw.fill[current.row][current.col]; + // let content = xw.fill[current.row][current.col]; let isNextBlack; switch (e.which) { case keyboard.left: @@ -528,11 +530,11 @@ function keyboardHandler(e) { break; case keyboard.up: // check if on board first (rows > cols) - let prevRow = xw.fill[current.row - 1]; + let prevRow = xw.fill[current.row - 1]; if (prevRow != undefined) { isNextBlack = prevRow[current.col] == BLACK; if (!isFakeEvent || (isFakeEvent && !isNextBlack)) current.row -= (current.row == 0) ? 0 : 1; - } + } changeDirection(DOWN); break; case keyboard.right: @@ -542,17 +544,17 @@ function keyboardHandler(e) { break; case keyboard.down: // check if on board first (rows > cols) - let nextRow = xw.fill[current.row + 1]; + let nextRow = xw.fill[current.row + 1]; if (nextRow != undefined) { isNextBlack = nextRow[current.col] == BLACK; if (!isFakeEvent || (isFakeEvent && !isNextBlack)) current.row += (current.row == xw.rows - 1) ? 0 : 1; - } + } changeDirection(DOWN); break; } } } - + // reactivate new current cell activeCell = grid.querySelector('[data-row="' + current.row + '"]').querySelector('[data-col="' + current.col + '"]'); activeCell.classList.add("active"); @@ -609,10 +611,10 @@ function updateGridUI() { let reflected_j = xw.cols - j - 1; let reflectedFill = xw.fill[reflected_i][reflected_j]; let warnActive = fill == BLANK && (reflectedFill != BLANK && reflectedFill != BLACK); - let warnOpposite = reflectedFill == BLANK && (fill != BLANK && fill != BLACK); + let warnOpposite = reflectedFill == BLANK && (fill != BLANK && fill != BLACK); // only warn pairs - if (warnOpposite || warnActive){ + if (warnOpposite || warnActive) { // pair contains a warn if (warnActive) { @@ -622,18 +624,18 @@ function updateGridUI() { const reflectedCell = grid.querySelector('[data-row="' + reflected_i + '"]').querySelector('[data-col="' + reflected_j + '"]'); reflectedCell.classList.add("warning"); } - + } else { // neither are warned activeCell.classList.remove("warning"); } } else { activeCell.classList.remove("warning"); - } + } // set letters activeCell.lastChild.innerHTML = fill; - + // show/hide black squares if (fill == BLACK) { activeCell.classList.add("black"); diff --git a/firebaseConfig.js b/firebaseConfig.js new file mode 100644 index 0000000..812c143 --- /dev/null +++ b/firebaseConfig.js @@ -0,0 +1,98 @@ +// Firebase config +var config = { + apiKey: "AIzaSyALPcu4vprNzxhwOrQHMM-Mmo24viUtclc", + authDomain: "kevin-xword.firebaseapp.com", + databaseURL: "https://kevin-xword.firebaseio.com", + projectId: "kevin-xword", + storageBucket: "kevin-xword.appspot.com", + messagingSenderId: "693272376803" +}; +firebase.initializeApp(config); + +var uiConfig = { + callbacks: { + signInSuccessWithAuthResult: function (authResult, redirectUrl) { + var user = authResult.user; + var credential = authResult.credential; + var isNewUser = authResult.additionalUserInfo.isNewUser; + var providerId = authResult.additionalUserInfo.providerId; + var operationType = authResult.operationType; + // Do something with the returned AuthResult. + // Return type determines whether we continue the redirect automatically + // or whether we leave that to developer to handle. + return true; + }, + signInFailure: function (error) { + // Some unrecoverable error occurred during sign-in. + // Return a promise when error handling is completed and FirebaseUI + // will reset, clearing any UI. This commonly occurs for error code + // 'firebaseui/anonymous-upgrade-merge-conflict' when merge conflict + // occurs. Check below for more details on this. + return handleUIError(error); + }, + uiShown: function () { + // The widget is rendered. + // Hide the loader. + document.getElementById('loader').style.display = 'none'; + } + }, + credentialHelper: firebaseui.auth.CredentialHelper.ACCOUNT_CHOOSER_COM, + // Query parameter name for mode. + queryParameterForWidgetMode: 'mode', + // Query parameter name for sign in success url. + queryParameterForSignInSuccessUrl: 'signInSuccessUrl', + // Will use popup for IDP Providers sign-in flow instead of the default, redirect. + signInFlow: 'popup', + signInSuccessUrl: '/', + signInOptions: [ + // Leave the lines as is for the providers you want to offer your users. + firebase.auth.GoogleAuthProvider.PROVIDER_ID, + firebase.auth.GithubAuthProvider.PROVIDER_ID, + firebase.auth.EmailAuthProvider.PROVIDER_ID + ], + // tosUrl and privacyPolicyUrl accept either url string or a callback + // function. + // Terms of service url/callback. + tosUrl: '', + // Privacy policy url/callback. + privacyPolicyUrl: function () { + window.location.assign(''); + } +}; + +// Initialize the FirebaseUI Widget using Firebase. +var ui = new firebaseui.auth.AuthUI(firebase.auth()); + +function initApp() { + console.log('app init'); + + database = firebase.database(); + + firebase.auth().onAuthStateChanged(function (user) { + if (user) { + console.log('user', user); + current_user = user; + // 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; + var providerData = user.providerData; + + document.getElementById("auth-status").innerText = displayName; + // ... + } else { + // User is signed out. + // ... + current_user = null; + document.getElementById("auth-status").innerText = "not signed in"; + } + }); +} + +// Triggers init on load +window.addEventListener('load', function () { + initApp() +}); \ No newline at end of file diff --git a/index.html b/index.html index 72193e2..2e5e132 100644 --- a/index.html +++ b/index.html @@ -15,9 +15,84 @@ return (false); } --> + + + + + + + + + + + + + +
+ + - +
- -
- +