cse110-fa22-group29/source/assets/scripts/CreatePage.js

174 lines
5.2 KiB
JavaScript
Raw Normal View History

import { newReviewToStorage } from "./localStorage.js";
window.addEventListener("DOMContentLoaded", init);
2022-11-29 23:39:43 +00:00
/**
* Delegates the functionality for creating review cards.
*/
function init() {
2022-11-18 07:56:07 +00:00
initFormHandler();
}
2022-11-29 23:39:43 +00:00
/**
* Creates a form and associates a new ID with the new review card.
*/
function initFormHandler() {
2022-11-29 23:39:43 +00:00
// Accesses form components
let tagContainer = document.getElementById("tag-container-form");
let form = document.querySelector("form");
2022-11-30 23:59:43 +00:00
// Declaring variable storing image data url
let imgDataURL = "";
2022-11-30 23:59:43 +00:00
// Accessing components related to taking photo
let videoMode = true;
let player = document.getElementById("player");
let canvas = document.getElementById("photoCanvas");
let photoButton = document.getElementById("photoButton");
let context = canvas.getContext("2d");
2022-11-30 23:59:43 +00:00
// Event listener for the photo taking/reset button
photoButton.addEventListener("click", () => {
2022-11-30 23:59:43 +00:00
// capturing the current video frame
if (videoMode) {
videoMode = false;
2022-11-30 23:59:43 +00:00
// setting up the appropriate components for displaying the photo preview
photoButton.innerText = "Retake";
player.setAttribute("hidden", "");
canvas.removeAttribute("hidden", "");
// displaying the captured snapshot on a canvas and saving it as a data url
context.drawImage(player, 0, 0, canvas.width, canvas.height);
imgDataURL = canvas.toDataURL();
}
// returning to displaying the video stream
else {
videoMode = true;
// setting up the appropriate components for the video stream
photoButton.innerText = "Take Photo";
canvas.setAttribute("hidden", "");
player.removeAttribute("hidden", "");
}
});
2022-11-30 23:59:43 +00:00
// Event listener for reading image form different data
let select = document.getElementById("select");
2022-11-30 23:59:43 +00:00
const input = document.getElementById("mealImg");
select.addEventListener("change", function () {
2022-11-29 23:39:43 +00:00
// Select a photo with HTML file selector
if (select.value == "file") {
2022-11-30 23:59:43 +00:00
// enabling file upload components and hiding photo taking components
input.removeAttribute("hidden", "");
player.setAttribute("hidden", "");
canvas.setAttribute("hidden", "");
photoButton.setAttribute("hidden", "");
// stopping the video stream
player.srcObject.getVideoTracks()[0].stop();
}
2022-11-29 23:39:43 +00:00
2022-11-30 23:59:43 +00:00
// Take a photo
else {
2022-11-30 23:59:43 +00:00
// enabling photo taking components and hiding file upload components
videoMode = true;
photoButton.innerText = "Take Photo";
input.setAttribute("hidden", "");
player.removeAttribute("hidden", "");
photoButton.removeAttribute("hidden", "");
// getting video stream from user's camera then displaying it on a video element
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
2022-11-30 23:59:43 +00:00
player.srcObject = stream;
});
}
});
2022-11-29 23:39:43 +00:00
// Addresses sourcing image from local file
document.getElementById("mealImg").addEventListener("change", function () {
const reader = new FileReader();
2022-11-29 23:39:43 +00:00
// Store image data URL after successful image load
reader.addEventListener(
"load",
() => {
imgDataURL = reader.result;
},
false
);
2022-11-29 23:39:43 +00:00
// Convert image file into data URL for local storage
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
});
2022-11-30 23:59:43 +00:00
form.addEventListener("submit", function (e) {
2022-11-29 23:39:43 +00:00
// Create reviewObject and put in storage
e.preventDefault();
let formData = new FormData(form);
let reviewObject = {};
2022-11-29 23:39:43 +00:00
// Adds data to the reviewObject from form data
for (let [key, value] of formData) {
if (`${key}` !== "tag-form") {
reviewObject[`${key}`] = `${value}`;
}
2022-11-30 23:59:43 +00:00
if (`${key}` === "mealImg" && imgDataURL !== "") {
reviewObject["mealImg"] = imgDataURL;
}
}
2022-11-29 23:39:43 +00:00
// Makes sure that ratings is filled
if (reviewObject["rating"] != null) {
2022-11-29 23:39:43 +00:00
//Adds rags separately as an array
2022-11-20 22:36:03 +00:00
reviewObject["tags"] = [];
2022-11-29 23:39:43 +00:00
// Grabs tags
2022-11-20 22:36:03 +00:00
let tags = document.querySelectorAll(".tag");
for (let i = 0; i < tags.length; i++) {
2022-11-20 22:36:03 +00:00
reviewObject["tags"].push(tags[i].innerHTML);
tagContainer.removeChild(tags[i]);
}
2022-11-29 23:39:43 +00:00
// Assigns the new review with a new ID
2022-11-20 22:36:03 +00:00
let nextReviewId = newReviewToStorage(reviewObject);
sessionStorage.setItem("currID", JSON.stringify(nextReviewId));
2022-11-29 23:39:43 +00:00
// Redirects to a page that shows the newly created review
2022-11-20 22:36:03 +00:00
window.location.assign("./ReviewDetails.html");
}
2022-11-29 23:39:43 +00:00
// Does not let user proceed if rating is not complete
else {
window.alert("Please fill in rating by selecting the stars :)");
2022-11-20 22:36:03 +00:00
}
});
2022-11-29 23:39:43 +00:00
// Event listener for tag functionality
let tagAddBtn = document.getElementById("tag-add-btn");
2022-11-29 02:48:55 +00:00
//Set used to track tags and ensure no duplicates
let tagSet = new Set();
tagAddBtn.addEventListener("click", () => {
let tagField = document.getElementById("tag-form");
2022-11-29 23:39:43 +00:00
// If there is a tag, it'll display the tag
if (tagField.value.length > 0) {
2022-12-03 02:31:26 +00:00
let tagSetVal = tagField.value.toLocaleLowerCase();
if (!tagSet.has(tagSetVal)) {
2022-11-29 02:48:55 +00:00
let tagLabel = document.createElement("label");
tagLabel.innerHTML = tagField.value;
tagLabel.setAttribute("class", "tag");
2022-12-01 00:35:45 +00:00
tagSet.add(tagSetVal);
tagLabel.addEventListener("click", () => {
2022-11-29 02:48:55 +00:00
tagContainer.removeChild(tagLabel);
2022-12-01 00:35:45 +00:00
tagSet.delete(tagSetVal);
2022-11-29 02:48:55 +00:00
});
2022-11-29 02:48:55 +00:00
tagContainer.append(tagLabel);
} else {
window.alert("No duplicate tags allowed");
}
tagField.value = "";
}
});
}