2022-11-18 07:13:53 +00:00
|
|
|
import { newReviewToStorage } from "./localStorage.js";
|
|
|
|
|
2022-11-18 02:45:53 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
/**
|
|
|
|
* Delegates the functionality for creating review cards.
|
|
|
|
*/
|
2022-11-18 02:45:53 +00:00
|
|
|
function init() {
|
2022-11-18 07:56:07 +00:00
|
|
|
initFormHandler();
|
2022-11-18 02:45:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
/**
|
|
|
|
* Creates a form and associates a new ID with the new review card.
|
|
|
|
*/
|
2022-11-18 02:45:53 +00:00
|
|
|
function initFormHandler() {
|
2022-11-29 23:39:43 +00:00
|
|
|
// Accesses form components
|
2022-11-18 02:45:53 +00:00
|
|
|
let tagContainer = document.getElementById("tag-container-form");
|
|
|
|
let form = document.querySelector("form");
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// Declaring variable storing image data url
|
|
|
|
let imgDataURL = "";
|
2022-12-03 02:02:52 +00:00
|
|
|
|
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");
|
2022-12-03 02:02:52 +00:00
|
|
|
let context = canvas.getContext("2d");
|
2022-11-30 23:59:43 +00:00
|
|
|
|
|
|
|
// Event listener for the photo taking/reset button
|
2022-12-03 02:02:52 +00:00
|
|
|
photoButton.addEventListener("click", () => {
|
2022-11-30 23:59:43 +00:00
|
|
|
// capturing the current video frame
|
|
|
|
if (videoMode) {
|
|
|
|
videoMode = false;
|
2022-12-03 02:02:52 +00:00
|
|
|
|
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-19 20:10:55 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// Event listener for reading image form different data
|
2022-11-19 20:10:55 +00:00
|
|
|
let select = document.getElementById("select");
|
2022-11-30 23:59:43 +00:00
|
|
|
const input = document.getElementById("mealImg");
|
2022-12-03 02:02:52 +00:00
|
|
|
select.addEventListener("change", function () {
|
2022-11-29 23:39:43 +00:00
|
|
|
// Select a photo with HTML file selector
|
2022-11-19 20:10:55 +00:00
|
|
|
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-19 20:10:55 +00:00
|
|
|
}
|
2022-11-29 23:39:43 +00:00
|
|
|
|
2022-11-30 23:59:43 +00:00
|
|
|
// Take a photo
|
2022-11-19 20:10:55 +00:00
|
|
|
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
|
2022-12-03 02:02:52 +00:00
|
|
|
navigator.mediaDevices.getUserMedia({ video: true }).then((stream) => {
|
2022-11-30 23:59:43 +00:00
|
|
|
player.srcObject = stream;
|
|
|
|
});
|
2022-11-19 20:10:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
// Addresses sourcing image from local file
|
2022-12-03 02:02:52 +00:00
|
|
|
document.getElementById("mealImg").addEventListener("change", function () {
|
2022-11-19 20:10:55 +00:00
|
|
|
const reader = new FileReader();
|
2022-12-03 02:02:52 +00:00
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
// Store image data URL after successful image load
|
2022-12-03 02:02:52 +00:00
|
|
|
reader.addEventListener(
|
|
|
|
"load",
|
|
|
|
() => {
|
|
|
|
imgDataURL = reader.result;
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
// Convert image file into data URL for local storage
|
2022-11-19 20:10:55 +00:00
|
|
|
reader.readAsDataURL(document.getElementById("mealImg").files[0]);
|
2022-11-20 23:23:22 +00:00
|
|
|
});
|
2022-11-30 23:59:43 +00:00
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
form.addEventListener("submit", function (e) {
|
2022-11-29 23:39:43 +00:00
|
|
|
// Create reviewObject and put in storage
|
2022-11-18 02:45:53 +00:00
|
|
|
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
|
2022-11-18 02:45:53 +00:00
|
|
|
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 !== "") {
|
2022-11-19 20:10:55 +00:00
|
|
|
reviewObject["mealImg"] = imgDataURL;
|
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
}
|
2022-11-29 23:39:43 +00:00
|
|
|
|
|
|
|
// Makes sure that ratings is filled
|
2022-12-03 02:02:52 +00:00
|
|
|
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-18 02:45:53 +00:00
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
// Grabs tags
|
2022-11-20 22:36:03 +00:00
|
|
|
let tags = document.querySelectorAll(".tag");
|
2022-12-03 02:02:52 +00:00
|
|
|
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-18 02:45:53 +00:00
|
|
|
|
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-12-03 02:02:52 +00:00
|
|
|
|
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-12-03 02:02:52 +00:00
|
|
|
}
|
2022-11-29 23:39:43 +00:00
|
|
|
// Does not let user proceed if rating is not complete
|
2022-12-03 02:02:52 +00:00
|
|
|
else {
|
2022-12-05 01:53:13 +00:00
|
|
|
window.alert("Please fill in rating by selecting the stars :)");
|
2022-11-20 22:36:03 +00:00
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
});
|
|
|
|
|
2022-11-29 23:39:43 +00:00
|
|
|
// Event listener for tag functionality
|
2022-11-19 19:42:30 +00:00
|
|
|
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();
|
2022-12-03 02:02:52 +00:00
|
|
|
tagAddBtn.addEventListener("click", () => {
|
2022-11-18 02:45:53 +00:00
|
|
|
let tagField = document.getElementById("tag-form");
|
2022-11-29 23:39:43 +00:00
|
|
|
|
|
|
|
// If there is a tag, it'll display the tag
|
2022-11-18 02:45:53 +00:00
|
|
|
if (tagField.value.length > 0) {
|
2022-12-03 02:31:26 +00:00
|
|
|
let tagSetVal = tagField.value.toLocaleLowerCase();
|
2022-12-03 02:02:52 +00:00
|
|
|
if (!tagSet.has(tagSetVal)) {
|
2022-11-29 02:48:55 +00:00
|
|
|
let tagLabel = document.createElement("label");
|
|
|
|
tagLabel.innerHTML = tagField.value;
|
2022-12-03 02:02:52 +00:00
|
|
|
tagLabel.setAttribute("class", "tag");
|
2022-12-01 00:35:45 +00:00
|
|
|
tagSet.add(tagSetVal);
|
2022-12-03 02:02:52 +00:00
|
|
|
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-12-03 02:02:52 +00:00
|
|
|
|
2022-11-29 02:48:55 +00:00
|
|
|
tagContainer.append(tagLabel);
|
|
|
|
} else {
|
|
|
|
window.alert("No duplicate tags allowed");
|
|
|
|
}
|
2022-11-18 02:45:53 +00:00
|
|
|
tagField.value = "";
|
|
|
|
}
|
2022-12-03 02:02:52 +00:00
|
|
|
});
|
2022-11-18 02:45:53 +00:00
|
|
|
}
|