autocomplete done with library
This commit is contained in:
parent
add70b9051
commit
a088642a30
156
main.js
156
main.js
@ -19,7 +19,7 @@ for (var i = 0; i < localStorage.length; i++){
|
|||||||
|
|
||||||
var movies = ["A Nightmare on Elm Street","Friday the 13th","Event Horizon","Hellraiser","The Cabin in the Woods","American Psycho","It Follows","Candyman","Child's Play","The Texas Chain Saw Massacre","Alien","Halloween"];
|
var movies = ["A Nightmare on Elm Street","Friday the 13th","Event Horizon","Hellraiser","The Cabin in the Woods","American Psycho","It Follows","Candyman","Child's Play","The Texas Chain Saw Massacre","Alien","Halloween"];
|
||||||
var pix = [];
|
var pix = [];
|
||||||
autocomplete(document.getElementById("guess"), movies);
|
// autocomplete(document.getElementById("guess"), movies);
|
||||||
var gameBeginning = new Date('September 18, 2022 00:00:00');
|
var gameBeginning = new Date('September 18, 2022 00:00:00');
|
||||||
var countDownTime = new Date();
|
var countDownTime = new Date();
|
||||||
var present_date = new Date();
|
var present_date = new Date();
|
||||||
@ -31,7 +31,7 @@ for (var i = 0; i < localStorage.length; i++){
|
|||||||
console.log("now: ",present_date);
|
console.log("now: ",present_date);
|
||||||
|
|
||||||
var textResult = "Horrordle #"+dayCount+"\n🔪";
|
var textResult = "Horrordle #"+dayCount+"\n🔪";
|
||||||
|
console.log(textResult);
|
||||||
function dayCounter(){
|
function dayCounter(){
|
||||||
dayCount = Math.floor((present_date - gameBeginning) / (1000 * 60 * 60 * 24));
|
dayCount = Math.floor((present_date - gameBeginning) / (1000 * 60 * 60 * 24));
|
||||||
return dayCount;
|
return dayCount;
|
||||||
@ -154,7 +154,7 @@ checkingGuess();
|
|||||||
function checkGuess() {
|
function checkGuess() {
|
||||||
|
|
||||||
console.log("day count:" + dayCount);
|
console.log("day count:" + dayCount);
|
||||||
let myGuess = guess.value
|
let myGuess = guess
|
||||||
if (myGuess === movieOfTheDay) {
|
if (myGuess === movieOfTheDay) {
|
||||||
if(guessNo==0){localStorage.setItem('firstGuessStat',parseInt(localStorage.getItem('firstGuessStat')) + 1);}
|
if(guessNo==0){localStorage.setItem('firstGuessStat',parseInt(localStorage.getItem('firstGuessStat')) + 1);}
|
||||||
if(guessNo==1){localStorage.setItem('secondGuessStat',parseInt(localStorage.getItem('secondGuessStat')) + 1);}
|
if(guessNo==1){localStorage.setItem('secondGuessStat',parseInt(localStorage.getItem('secondGuessStat')) + 1);}
|
||||||
@ -168,7 +168,7 @@ console.log("day count:" + dayCount);
|
|||||||
document.getElementById("movieName").innerHTML = movieOfTheDay + " ✅";
|
document.getElementById("movieName").innerHTML = movieOfTheDay + " ✅";
|
||||||
document.getElementById("countDown").style.display = "block";
|
document.getElementById("countDown").style.display = "block";
|
||||||
document.getElementById("shareResult").style.display = "block";
|
document.getElementById("shareResult").style.display = "block";
|
||||||
localStorage.setItem("winningGuess", guess.value);
|
localStorage.setItem("winningGuess", guess);
|
||||||
document.getElementById("firstGuess").style.display = "none";
|
document.getElementById("firstGuess").style.display = "none";
|
||||||
document.getElementById("secondGuess").style.display = "none";
|
document.getElementById("secondGuess").style.display = "none";
|
||||||
|
|
||||||
@ -187,8 +187,8 @@ console.log("day count:" + dayCount);
|
|||||||
localStorage.setItem('guessNo', guessNo);
|
localStorage.setItem('guessNo', guessNo);
|
||||||
feedback.textContent = "🩸 " + (3-guessNo) + " guesses remaining";
|
feedback.textContent = "🩸 " + (3-guessNo) + " guesses remaining";
|
||||||
document.getElementById("movieFrame").src = pix[guessNo];
|
document.getElementById("movieFrame").src = pix[guessNo];
|
||||||
firstGuess.textContent = "❌" + guess.value;
|
firstGuess.textContent = "❌" + guess;
|
||||||
localStorage.setItem('firstGuess', guess.value);
|
localStorage.setItem('firstGuess', guess);
|
||||||
document.getElementById("firstGuess").style.display = "block";
|
document.getElementById("firstGuess").style.display = "block";
|
||||||
textResult = textResult + "🟥";
|
textResult = textResult + "🟥";
|
||||||
localStorage.setItem('result', textResult);
|
localStorage.setItem('result', textResult);
|
||||||
@ -197,8 +197,8 @@ console.log("day count:" + dayCount);
|
|||||||
localStorage.setItem('guessNo', guessNo);
|
localStorage.setItem('guessNo', guessNo);
|
||||||
feedback.textContent = "🩸 " + (3-guessNo) + " guesses remaining";
|
feedback.textContent = "🩸 " + (3-guessNo) + " guesses remaining";
|
||||||
document.getElementById("movieFrame").src = pix[guessNo];
|
document.getElementById("movieFrame").src = pix[guessNo];
|
||||||
secondGuess.textContent = "❌" + guess.value;
|
secondGuess.textContent = "❌" + guess;
|
||||||
localStorage.setItem('secondGuess', guess.value);
|
localStorage.setItem('secondGuess', guess);
|
||||||
document.getElementById("secondGuess").style.display = "block";
|
document.getElementById("secondGuess").style.display = "block";
|
||||||
textResult = textResult + "🟥";
|
textResult = textResult + "🟥";
|
||||||
localStorage.setItem('result', textResult);
|
localStorage.setItem('result', textResult);
|
||||||
@ -217,110 +217,42 @@ console.log("day count:" + dayCount);
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function autocomplete(inp, arr) {
|
const autoCompleteJS = new autoComplete({
|
||||||
/*the autocomplete function takes two arguments,
|
placeHolder: "Search for Movies...",
|
||||||
the text field element and an array of possible autocompleted values:*/
|
data: {
|
||||||
var currentFocus;
|
src: movies
|
||||||
/*execute a function when someone writes in the text field:*/
|
},
|
||||||
inp.addEventListener("input", function(e) {
|
resultItem: {
|
||||||
var a, b, i, val = this.value;
|
highlight: true,
|
||||||
/*close any already open lists of autocompleted values*/
|
},
|
||||||
closeAllLists();
|
events: {
|
||||||
if (!val) { return false;}
|
input: {
|
||||||
currentFocus = -1;
|
selection: (event) => {
|
||||||
/*create a DIV element that will contain the items (values):*/
|
const selection = event.detail.selection.value;
|
||||||
a = document.createElement("DIV");
|
autoCompleteJS.input.value = selection;
|
||||||
a.setAttribute("id", this.id + "autocomplete-list");
|
},
|
||||||
a.setAttribute("class", "autocomplete-items");
|
open() {
|
||||||
/*append the DIV element as a child of the autocomplete container:*/
|
const position =
|
||||||
this.parentNode.appendChild(a);
|
autoCompleteJS.input.getBoundingClientRect().bottom + autoCompleteJS.list.getBoundingClientRect().height >
|
||||||
/*for each item in the array...*/
|
(window.innerHeight || document.documentElement.clientHeight);
|
||||||
|
|
||||||
for (i = 0; i < arr.length; i++) {
|
if (position) {
|
||||||
// newArray = arr[i].split(" ");
|
autoCompleteJS.list.style.bottom = autoCompleteJS.input.offsetHeight + 8 + "px";
|
||||||
newArray = arr[i].split(" ");
|
} else {
|
||||||
/*check if the item starts with the same letters as the text field value:*/
|
// autoCompleteJS.list.style.bottom = -autoCompleteJS.list.offsetHeight - 8 + "px";
|
||||||
for (var j = 0; j < newArray.length; j++) {
|
}
|
||||||
|
},
|
||||||
|
|
||||||
if (newArray[j].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
|
|
||||||
/*create a DIV element for each matching element:*/
|
|
||||||
b = document.createElement("DIV");
|
|
||||||
|
|
||||||
/*make the matching letters bold:*/
|
|
||||||
b.innerHTML = newArray.slice(0,j).join(' ') + " <strong>" + newArray[j].substr(0, val.length) + "</strong>";
|
|
||||||
b.innerHTML += newArray[j].substr(val.length) + " " + newArray.slice(j+1,newArray.length).join(' ');
|
|
||||||
/*insert a input field that will hold the current array item's value:*/
|
|
||||||
b.innerHTML += "<input type='hidden' value='" + newArray.join(' ') + "'>";
|
|
||||||
/*execute a function when someone clicks on the item value (DIV element):*/
|
|
||||||
b.addEventListener("click", function(e) {
|
|
||||||
/*insert the value for the autocomplete text field:*/
|
|
||||||
inp.value = this.getElementsByTagName("input")[0].value;
|
|
||||||
/*close the list of autocompleted values,
|
|
||||||
(or any other open lists of autocompleted values:*/
|
|
||||||
closeAllLists();
|
|
||||||
});
|
|
||||||
a.appendChild(b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
/*execute a function presses a key on the keyboard:*/
|
|
||||||
inp.addEventListener("keydown", function(e) {
|
|
||||||
var x = document.getElementById(this.id + "autocomplete-list");
|
|
||||||
if (x) x = x.getElementsByTagName("div");
|
|
||||||
if (e.keyCode == 40) {
|
|
||||||
/*If the arrow DOWN key is pressed,
|
|
||||||
increase the currentFocus variable:*/
|
|
||||||
currentFocus++;
|
|
||||||
/*and and make the current item more visible:*/
|
|
||||||
addActive(x);
|
|
||||||
} else if (e.keyCode == 38) { //up
|
|
||||||
/*If the arrow UP key is pressed,
|
|
||||||
decrease the currentFocus variable:*/
|
|
||||||
currentFocus--;
|
|
||||||
/*and and make the current item more visible:*/
|
|
||||||
addActive(x);
|
|
||||||
} else if (e.keyCode == 13) {
|
|
||||||
/*If the ENTER key is pressed, prevent the form from being submitted,*/
|
|
||||||
e.preventDefault();
|
|
||||||
if (currentFocus > -1) {
|
|
||||||
/*and simulate a click on the "active" item:*/
|
|
||||||
if (x) x[currentFocus].click();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
function addActive(x) {
|
|
||||||
/*a function to classify an item as "active":*/
|
|
||||||
if (!x) return false;
|
|
||||||
/*start by removing the "active" class on all items:*/
|
|
||||||
removeActive(x);
|
|
||||||
if (currentFocus >= x.length) currentFocus = 0;
|
|
||||||
if (currentFocus < 0) currentFocus = (x.length - 1);
|
|
||||||
/*add class "autocomplete-active":*/
|
|
||||||
x[currentFocus].classList.add("autocomplete-active");
|
|
||||||
}
|
|
||||||
function removeActive(x) {
|
|
||||||
/*a function to remove the "active" class from all autocomplete items:*/
|
|
||||||
for (var i = 0; i < x.length; i++) {
|
|
||||||
x[i].classList.remove("autocomplete-active");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function closeAllLists(elmnt) {
|
|
||||||
/*close all autocomplete lists in the document,
|
|
||||||
except the one passed as an argument:*/
|
|
||||||
var x = document.getElementsByClassName("autocomplete-items");
|
|
||||||
for (var i = 0; i < x.length; i++) {
|
|
||||||
if (elmnt != x[i] && elmnt != inp) {
|
|
||||||
x[i].parentNode.removeChild(x[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*execute a function when someone clicks in the document:*/
|
|
||||||
document.addEventListener("click", function (e) {
|
|
||||||
closeAllLists(e.target);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
autoCompleteJS.input.addEventListener("selection", function (event) {
|
||||||
|
const feedback = event.detail;
|
||||||
|
// Prepare User's Selected Value
|
||||||
|
guess = event.detail.selection.value
|
||||||
|
autoCompleteJS.input.value = guess;
|
||||||
|
// Console log autoComplete data feedback
|
||||||
|
console.log(event.detail.selection.value);
|
||||||
|
});
|
||||||
|
|
||||||
function copyToClipboard() {
|
function copyToClipboard() {
|
||||||
navigator.clipboard.writeText(textResult).then(() => {
|
navigator.clipboard.writeText(textResult).then(() => {
|
||||||
@ -393,7 +325,7 @@ var x = setInterval(function() {
|
|||||||
countDownTime.setMinutes(0);
|
countDownTime.setMinutes(0);
|
||||||
countDownTime.setSeconds(0);
|
countDownTime.setSeconds(0);
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
console.log("now: ",now);
|
// console.log("now: ",now);
|
||||||
var remainingTime = countDownTime - now;
|
var remainingTime = countDownTime - now;
|
||||||
const second = 1000;
|
const second = 1000;
|
||||||
const minute = second * 60;
|
const minute = second * 60;
|
||||||
@ -405,8 +337,8 @@ var x = setInterval(function() {
|
|||||||
document.getElementById("countDown").innerHTML = hoursLeft + "h "
|
document.getElementById("countDown").innerHTML = hoursLeft + "h "
|
||||||
+ minutesLeft + "m " + secondsLeft + "s ";
|
+ minutesLeft + "m " + secondsLeft + "s ";
|
||||||
//console.log(remainingTime);
|
//console.log(remainingTime);
|
||||||
console.log(localStorage.getItem('day'));
|
// console.log(localStorage.getItem('day'));
|
||||||
console.log("day count: " + dayCount);
|
// console.log("day count: " + dayCount);
|
||||||
dayCount = Math.floor((now - gameBeginning) / (1000 * 60 * 60 * 24));
|
dayCount = Math.floor((now - gameBeginning) / (1000 * 60 * 60 * 24));
|
||||||
if (localStorage.getItem('day')!=dayCount) {
|
if (localStorage.getItem('day')!=dayCount) {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
|
Loading…
Reference in New Issue
Block a user