From 416d26658d11ed7886eb4582a96dd81e4a96e870 Mon Sep 17 00:00:00 2001 From: Henry Feng Date: Thu, 17 Nov 2022 16:17:51 -0800 Subject: [PATCH 01/10] implementation of basic image local storage --- source/assets/scripts/main.js | 42 +++++++++++++++++++++++++++++++++++ source/index.html | 11 +++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/source/assets/scripts/main.js b/source/assets/scripts/main.js index 9d37c44..5fb44a4 100644 --- a/source/assets/scripts/main.js +++ b/source/assets/scripts/main.js @@ -43,6 +43,44 @@ function initFormHandler() { //accessing form components let tagContainer = document.getElementById("tag-container-form"); let form = document.querySelector("form"); + + /* + * change the input source of the image between local file and URL + * depending on user's selection + */ + let select = document.getElementById("select"); + select.addEventListener("change", function() { + const input = document.getElementById('source'); + + if (select.value == "file") { + input.innerHTML = ` + Source: + + ` + } + else { + input.innerHTML = ` + Source: + + ` + } + }); + + //addressing sourcing image from local file + let imgDataURL = ""; + document.getElementById("mealImg").addEventListener("change", function() { + const reader = new FileReader(); + + //store image data URL after successful image load + reader.addEventListener("load", ()=>{ + imgDataURL = reader.result; + }, false); + + //convert image file into data URL for local storage + reader.readAsDataURL(document.getElementById("mealImg").files[0]); + }) + + form.addEventListener("submit", function(){ /* @@ -51,12 +89,16 @@ function initFormHandler() { */ let formData = new FormData(form); let reviewObject = {}; + for (let [key, value] of formData) { console.log(`${key}`); console.log(`${value}`); if (`${key}` !== "tag-form") { reviewObject[`${key}`] = `${value}`; } + if (`${key}` === "mealImg" && select.value == "file") { + reviewObject["mealImg"] = imgDataURL; + } } reviewObject["tags"] = []; diff --git a/source/index.html b/source/index.html index 4418ea0..7708cb0 100644 --- a/source/index.html +++ b/source/index.html @@ -10,7 +10,6 @@ - @@ -26,9 +25,14 @@
Pic: -
- Meal: