2022-11-08 23:57:02 +00:00
|
|
|
// ReviewCard.js
|
|
|
|
|
|
|
|
class ReviewCard extends HTMLElement {
|
2022-11-15 00:53:07 +00:00
|
|
|
// Called once when document.createElement('review-card') is called, or
|
|
|
|
// the element is written into the DOM directly as <review-card>
|
|
|
|
constructor() {
|
|
|
|
super();
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let shadowEl = this.attachShadow({mode:"open"});
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let articleEl = document.createElement("article");
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let styleEl = document.createElement("style");
|
|
|
|
styleEl.textContent = `
|
2022-11-22 01:20:12 +00:00
|
|
|
* {
|
|
|
|
font-family: Century Gothic;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
overflow-wrap: anywhere;
|
|
|
|
}
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
a:hover {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
|
|
|
|
|
|
|
article {
|
|
|
|
align-items: center;
|
|
|
|
border: 2px solid rgb(31, 41, 32);
|
|
|
|
border-radius: 8px;
|
|
|
|
display: grid;
|
|
|
|
grid-template-rows: 118px 56px 14px 18px 15px 36px;
|
|
|
|
height: auto;
|
|
|
|
row-gap: 5px;
|
|
|
|
padding: 0 16px 16px 16px;
|
|
|
|
width: 178px;
|
|
|
|
margin: 8px 8px 8px 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
div.rating {
|
|
|
|
align-items: center;
|
|
|
|
column-gap: 5px;
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
|
|
|
|
div.rating>img {
|
|
|
|
height: auto;
|
|
|
|
display: inline-block;
|
|
|
|
object-fit: scale-down;
|
|
|
|
width: 78px;
|
|
|
|
}
|
|
|
|
|
|
|
|
article>img {
|
|
|
|
border-top-left-radius: 6px;
|
|
|
|
border-top-right-radius: 6px;
|
|
|
|
height: 119px;
|
|
|
|
object-fit: cover;
|
|
|
|
margin-left: -16px;
|
|
|
|
width: calc(100% + 32px);
|
|
|
|
}
|
|
|
|
|
|
|
|
label.restaurant-name {
|
|
|
|
color: black !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
label.meal-name {
|
|
|
|
display: -webkit-box;
|
|
|
|
font-size: 16px;
|
|
|
|
height: 36px;
|
|
|
|
line-height: 18px;
|
|
|
|
overflow: hidden;
|
|
|
|
-webkit-line-clamp: 2;
|
|
|
|
-webkit-box-orient: vertical;
|
|
|
|
}
|
|
|
|
|
|
|
|
label:not(.meal-name),
|
|
|
|
span,
|
|
|
|
time {
|
|
|
|
color: #70757A;
|
|
|
|
font-size: 12px;
|
|
|
|
}
|
2022-11-21 21:52:44 +00:00
|
|
|
|
2022-11-22 01:20:12 +00:00
|
|
|
.tag-container {
|
|
|
|
margin-top: 20px;
|
|
|
|
display: flex;
|
|
|
|
flex-flow: row wrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
.a-tag {
|
|
|
|
background-color:#94da97;
|
|
|
|
border-radius: 7px;
|
|
|
|
color: #94da97;
|
|
|
|
padding-right: 7px;
|
|
|
|
padding-left: 7px;
|
|
|
|
margin: 3px;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
`;
|
2022-11-15 00:53:07 +00:00
|
|
|
articleEl.append(styleEl);
|
|
|
|
shadowEl.append(articleEl);
|
|
|
|
this.shadowEl = shadowEl;
|
2022-11-29 23:59:57 +00:00
|
|
|
|
|
|
|
// Attach event listener to each review-card
|
2022-11-15 00:53:07 +00:00
|
|
|
this.addEventListener("click", (event) => {
|
|
|
|
console.log(event.target);
|
2022-11-18 02:45:53 +00:00
|
|
|
console.log(event.target.reviewId);
|
2022-11-29 23:59:57 +00:00
|
|
|
// Saves the ID for corresponding review on new page (for data retrieval)
|
2022-11-18 02:45:53 +00:00
|
|
|
sessionStorage.setItem("currID", JSON.stringify(event.target.data.reviewID));
|
2022-11-29 23:59:57 +00:00
|
|
|
// Goes to the new page for the review
|
2022-11-15 00:53:07 +00:00
|
|
|
window.location.assign("./ReviewDetails.html");
|
|
|
|
});
|
|
|
|
}
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
/**
|
2022-11-29 23:59:57 +00:00
|
|
|
* Called when the .data property is set on this element.
|
|
|
|
*
|
|
|
|
* For Example:
|
|
|
|
* let reviewCard = document.createElement('review-card');
|
|
|
|
* reviewCard.data = { foo: 'bar' }
|
|
|
|
*
|
|
|
|
* @param {Object} data - The data to pass into the <review-card>, must be of the
|
|
|
|
* following format:
|
|
|
|
* {
|
|
|
|
* "mealImg": string,
|
|
|
|
* "mealName": string,
|
|
|
|
* "comments": string,
|
|
|
|
* "rating": number,
|
|
|
|
* "restaurant": string,
|
|
|
|
* "reviewID": number,
|
|
|
|
* "tags": string array
|
|
|
|
* }
|
|
|
|
*/
|
2022-11-15 00:53:07 +00:00
|
|
|
set data(data) {
|
|
|
|
// If nothing was passed in, return
|
|
|
|
if (!data) return;
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
// Select the <article> we added to the Shadow DOM in the constructor
|
|
|
|
let articleEl = this.shadowEl.querySelector("article");
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Setting the article elements for the review card
|
2022-11-18 07:56:07 +00:00
|
|
|
this.reviewID = data["reviewID"];
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Image setup
|
2022-11-15 00:53:07 +00:00
|
|
|
let mealImg = document.createElement("img");
|
|
|
|
mealImg.setAttribute("id", "a-mealImg");
|
2022-11-21 19:58:26 +00:00
|
|
|
mealImg.setAttribute("alt","Meal Photo Corrupted");
|
2022-11-21 20:29:06 +00:00
|
|
|
mealImg.setAttribute("src",data["mealImg"]);
|
|
|
|
mealImg.addEventListener("error", function(e) {
|
2022-11-22 00:12:17 +00:00
|
|
|
mealImg.setAttribute("src", "./assets/images/plate_with_cutlery.png");
|
2022-11-21 20:29:06 +00:00
|
|
|
e.onerror = null;
|
|
|
|
});
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Meal name setup
|
2022-11-15 00:53:07 +00:00
|
|
|
let mealLabel = document.createElement("label");
|
|
|
|
mealLabel.setAttribute("id", "a-mealName");
|
|
|
|
mealLabel.setAttribute("class","meal-name");
|
|
|
|
mealLabel.innerHTML = data["mealName"];
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Restaurant name setup
|
2022-11-15 00:53:07 +00:00
|
|
|
let restaurantLabel = document.createElement("label");
|
|
|
|
restaurantLabel.setAttribute("id", "a-restaurant");
|
|
|
|
restaurantLabel.setAttribute("class","restaurant-name");
|
|
|
|
restaurantLabel.innerHTML = data["restaurant"];
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Comment section setup (display set to none)
|
2022-11-15 00:53:07 +00:00
|
|
|
let comments = document.createElement("p");
|
|
|
|
comments.setAttribute("id", "a-comments");
|
|
|
|
comments.style.display = "none";
|
|
|
|
comments.innerText = data["comments"];
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Rating setup
|
2022-11-15 00:53:07 +00:00
|
|
|
let ratingDiv = document.createElement("div");
|
|
|
|
ratingDiv.setAttribute("class", "rating");
|
|
|
|
let starsImg = document.createElement("img");
|
|
|
|
starsImg.setAttribute("id", "a-rating");
|
2022-11-22 00:12:17 +00:00
|
|
|
starsImg.setAttribute("src", "./assets/images/"+data["rating"]+"-star.svg");
|
2022-11-15 00:53:07 +00:00
|
|
|
starsImg.setAttribute("alt", data["rating"] +" stars");
|
|
|
|
starsImg.setAttribute("num", data["rating"]);
|
|
|
|
ratingDiv.append(starsImg);
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Tags setup
|
2022-11-15 00:53:07 +00:00
|
|
|
let tagContainer = document.createElement("div");
|
|
|
|
tagContainer.setAttribute("class", "tag-container");
|
|
|
|
tagContainer.setAttribute("id", "a-tags");
|
|
|
|
tagContainer.setAttribute("list", data["tags"]);
|
2022-11-29 23:59:57 +00:00
|
|
|
|
|
|
|
// Checks if user gave tags, if so added to review card
|
2022-11-15 00:53:07 +00:00
|
|
|
if(data["tags"]){
|
|
|
|
for (let i = 0; i < data["tags"].length; i++) {
|
|
|
|
let newTag = document.createElement("label");
|
2022-11-21 23:27:45 +00:00
|
|
|
newTag.setAttribute("class","a-tag");
|
2022-11-18 02:45:53 +00:00
|
|
|
newTag.innerHTML = data["tags"][i];
|
2022-11-15 00:53:07 +00:00
|
|
|
tagContainer.append(newTag);
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 21:22:32 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Setting all the data to the review card
|
2022-11-15 00:53:07 +00:00
|
|
|
articleEl.append(mealImg);
|
|
|
|
articleEl.append(mealLabel);
|
|
|
|
articleEl.append(restaurantLabel);
|
|
|
|
articleEl.append(ratingDiv);
|
|
|
|
articleEl.append(tagContainer);
|
|
|
|
articleEl.append(comments);
|
2022-11-12 22:16:03 +00:00
|
|
|
|
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
}
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
/**
|
2022-11-29 23:59:57 +00:00
|
|
|
* Called when getting the .data property of this element.
|
|
|
|
*
|
|
|
|
* For Example:
|
|
|
|
* let reviewCard = document.createElement('review-card');
|
|
|
|
* reviewCard.data = { foo: 'bar' }
|
|
|
|
*
|
|
|
|
* @return {Object} data - The data from the <review-card>, of the
|
|
|
|
* following format:
|
|
|
|
* {
|
|
|
|
* "mealImg": string,
|
|
|
|
* "mealName": string,
|
|
|
|
* "comments": string,
|
|
|
|
* "rating": number,
|
|
|
|
* "restaurant": string,
|
|
|
|
* "reviewID": number,
|
|
|
|
* "tags": string array
|
|
|
|
* }
|
|
|
|
*/
|
2022-11-15 00:53:07 +00:00
|
|
|
get data() {
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
let dataContainer = {};
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Getting the article elements for the review card
|
2022-11-18 07:56:07 +00:00
|
|
|
dataContainer["reviewID"] = this.reviewID;
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Get image
|
2022-11-15 00:53:07 +00:00
|
|
|
let mealImg = this.shadowEl.getElementById("a-mealImg");
|
|
|
|
dataContainer["mealImg"] = mealImg.getAttribute("src");
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Get meal name
|
2022-11-15 00:53:07 +00:00
|
|
|
let mealLabel = this.shadowEl.getElementById("a-mealName");
|
|
|
|
dataContainer["mealName"] = mealLabel.innerHTML;
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Get comment section
|
2022-11-15 00:53:07 +00:00
|
|
|
let comments = this.shadowEl.getElementById("a-comments");
|
|
|
|
console.log(comments);
|
|
|
|
dataContainer["comments"] = comments.innerText;
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Get rating
|
2022-11-15 00:53:07 +00:00
|
|
|
let starsImg = this.shadowEl.getElementById("a-rating");
|
|
|
|
dataContainer["rating"] = starsImg.getAttribute("num");
|
2022-11-12 22:16:03 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
//Get restaurant name
|
2022-11-15 00:53:07 +00:00
|
|
|
let restaurantLabel = this.shadowEl.getElementById("a-restaurant");
|
|
|
|
dataContainer["restaurant"] = restaurantLabel.innerHTML;
|
2022-11-08 23:57:02 +00:00
|
|
|
|
2022-11-29 23:59:57 +00:00
|
|
|
// Get tags
|
2022-11-15 00:53:07 +00:00
|
|
|
let tagContainer = this.shadowEl.getElementById("a-tags");
|
|
|
|
dataContainer["tags"] = tagContainer.getAttribute("list").split(",");
|
2022-11-09 05:51:03 +00:00
|
|
|
|
2022-11-15 00:53:07 +00:00
|
|
|
return dataContainer;
|
|
|
|
}
|
2022-11-08 23:57:02 +00:00
|
|
|
}
|
2022-11-15 00:53:07 +00:00
|
|
|
customElements.define("review-card", ReviewCard);
|